<?php
/**
 * CtrlHttpResponse
 *
 * @date 15.03.2007
 * @name CtrlHttpResponse
 * @author Margo Poolak <margo 'dot' poolak 'at' mail 'dot' ee>
 * @copyright Copyright &copy;2006 Margo Poolak. All Rights Reserved.
 * @version 2.0
 * @package System.Web
 * @category 
 * <code>
 * <?php $this->Response->header('Content-Type: text/html; charset=utf-8'); ?> 
 * </code>
 * Response class
 */
class CtrlHttpResponse
{
	private $_start_time;// when response started
	/*
	* Starts the time
	* Starts the response when the class in initailized
	*/
	public function __construct()
	{
		$this->_start_time = microtime();
		$this->start(true);
	}
	/*
	* Starts response
	* @parameter  boolean to use zipped content
	* @returns
	*/
	public function start($gzip=false)
	{
		if ($gzip == true AND (!headers_sent() && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')))
		{
			ob_start("ob_gzhandler");
		}
		else
		{
			ob_start();
		}
	}
	/*
	* Retruns content length in bytes
	* returns content size in bytes
	*/	
	public function getContentLength()
	{
		return ob_get_length();
	}
	/*
	* Returns the time rendered from the start
	* @returns the time in seconds
	*/
	public function getTimeLeft()
	{
		return microtime()-$this->_start_time;
	}
	/*
	*
	*/
	public function header($value)
	{
		header($value);
	}
	/*
	* Returns the content 
	* @parameter boolean cleans the content
	* @returns string of the content
	*/	
	public function getContent($clean=false) 
	{
		$content = '';
		if($clean==false)
		{
			$content = ob_get_contents();
		}
		else
		{
			$content = ob_get_clean();
		}
		/*
		* If you want to clean the whitespaces
		*/
		/*
		if($this->_compact == true)
		{
			$content = preg_replace("/\n|\s\s+/", "", $content);// remove all the whitespaces	
		}*/
		return $content;
	}
	/*
	* redirects to the specified url
	* @parameter string full url
	* @returns redirection with header
	*/
	public function redirect ($url)
	{
		$this->end();
		$this->header ('Location: '.$url);
		exit();
	}
	/*
	* Ends the response
	*/
	public function end()
	{
		if($this->getContent()>0)
		{	
			ob_end_clean();
		}
		return;
	}
	/**
	 * Sends a file back to user.
	 * Make sure not to output anything else after calling this method.
	 * @param string file name
	 * @param string content to be set. If null, the content will be read from the server file pointed to by $file_fame.
	 * @param string mime type of the content.
	 * @param array list of headers to be sent
	 */
	public function output($file_name,$content=null,$mime_type=null,$headers=null)
	{
		static $defaultMimeTypes=array(
			'css'=>'text/css',
			'gif'=>'image/gif',
			'jpg'=>'image/jpeg',
			'jpeg'=>'image/jpeg',
			'htm'=>'text/html',
			'html'=>'text/html',
			'js'=>'javascript/js',
			'xls'=>'application/x-msdownload'
		);
		if(is_readable($file_name) == true)
		{
			if($mime_type===null)
			{
				$mime_type='text/plain';
				if(function_exists('mime_content_type'))
				{
					$mime_type=mime_content_type($file_name);
				}
				elseif(($ext=strrchr($file_name,'.'))!==false)
				{
					$ext=substr($ext,1);
					if(isset($defaultMimeTypes[$ext]))
					{
						$mime_type=$defaultMimeTypes[$ext];
					}
				}
			}
			$fn=basename($file_name);
			if(is_array($headers))
			{
				foreach($headers as $h)
				{
					$this->header($h);
				}
			}
			else
			{
				$this->header('Pragma: public');
				$this->header('Expires: 0');
				$this->header('Cache-Component: must-revalidate, post-check=0, pre-check=0');
			}
			$this->header("Content-type: $mime_type");
			$this->header('Content-Length: '.($content===null?filesize($file_name):strlen($content)));
			$this->header("Content-Disposition: attachment; filename=\"$fn\"");
			$this->header('Content-Transfer-Encoding: binary');
			if($content===null)
			{
				readfile($file_name);
				exit();
			}
			else
			{
				exit($content);
			}
		}
		else
		{
			exit('Cant\'t download this file');
		}
	}	
	/*
	*
	*/
	public function __destruct()
	{
	
	}
}
?>