<?php
/**
 * CtrlHttpRequest
 *
 * @date 15.03.2007
 * @name CtrlHttpRequest
 * @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 
 * 
 * Request class
 */
// To identify page request 
if(!defined('PAGE'))
{
	define('PAGE',			'rs');
}
// To separate class and method name
if(!defined('METHOD_SEPARATOR'))
{
	define('METHOD_SEPARATOR',		'.');
}
// To request default page
if(!defined('DEFAULT_PAGE'))
{
	define('DEFAULT_PAGE',	'MainPage');
}
// To request default page
if(!defined('DEFAULT_METHOD'))
{
	define('DEFAULT_METHOD',	'display');
}
class CtrlHttpRequest
{
	private $_path = false;
	private $_items = array();
	/*
	*
	*/
	public function __construct()
	{
		#parent::__construct();
		/*
		* If .htaccess is defined the parameter q
		* Then set to the path mode
		*/
		if(isset($_GET['q']))
		{
			$_GET = path_to_array($_GET['q']);// gets the url from the .htaccess, path uri override it
			$this->_path = true;
		}
	}
	/*
	* Is the connection secured
	* @returns boolean
	*/
	public function isSecureConnection()
	{
		return (isset($_SERVER['HTTPS']))?true:false;	
	}
	/*
	* Returns the requested page name
	* @return string page name
	*/
	public function getRequestedPage()
	{
		$page = $this->Get(PAGE,DEFAULT_PAGE);
		$part = explode(METHOD_SEPARATOR,$page);
		return $part[0];
	}	
	/*
	* Returns requested method
	* @returns string method name
	*/
	public function getRequestedMethod()
	{
		$page = $this->Get(PAGE,DEFAULT_PAGE);
		$part = explode(METHOD_SEPARATOR,$page);
		if(isset($part[1]))
		{
			return $part[1];
		}
		return DEFAULT_METHOD;
	}
	/*
	* Returns the base url
	* @returns string with base url
	*/
	public function getBaseUrl()
	{
		$basePath = implode('/',array_slice(explode('/',$_SERVER['SCRIPT_NAME']),0,-1));
		if ($this->IsSecureConnection()) 
		{
			$scheme = 'https';
		}
		else 
		{
			$scheme = 'http';
		}
		return $scheme.'://'.$_SERVER['SERVER_NAME'].$basePath;
	}	
	/**
	 * @name ConstructUrl
	 * @param array getParameters, string $format, merge the old parameters
	 * @returns string with constructed url
	 */
	public function constructUrl($getParameters=null,$merge=true)
	{
		$newUrl = array();	
		$url = $this->GetBaseUrl();
		/*
		* If set the get parameters
		*/
		if(is_array($this->_items) AND $merge == true)
		{
			$getParameters = (is_array($getParameters))? array_merge($_GET,$getParameters):$this->_items;// merge the url and parameters with the new getParameters		
		}
		if(is_array($getParameters))
		{
			if($this->_path==false)
			{
				foreach($getParameters as $name=>$value)
				{
					/*
					* If the value is empty then unset it from the url
					*/
					if(!!($value))
					{
						$new_url[$name] =urlencode($name).'='.urlencode($value);
					}
					else
					{
						if(isset($new_url[$name]))
						{
							unset($new_url[$name]);
						}
					}
				}
				$url .= '?'.implode('&',$new_url);
			}
			else
			{	
				foreach($getParameters as $name=>$value)
				{
					if(!!($value))
					{
						$new_url[$name] = $name.'/'.$value;
					}
					else
					{
						if(isset($new_url[$name]))
						{
							unset($new_url[$name]);
						}
					}					
				}
				$url .= '/'.implode('/',$new_url);		
			}
		}
		return $url;	
	}
	/*
	* Gets the requested parameters
	* @parameter string key to find
	* @parameter string alternative value
	* @returns the requested key value
	*/
	public function get($key=null,$alt=null)
	{
		// if request is not empty and key is empty then return complete request array
		if(!!($_REQUEST) AND !$key)
		{
			foreach($_REQUEST as $k=>$v)
			{
				if(is_array($v))
				{
					$tmp[$k] = array_add_slashes($v);
				}
				else
				{
					$tmp[$k] = addslashes(htmlentities($v,ENT_QUOTES,'utf-8'));				
				}
			}
			return $tmp;		
		}
		elseif(isset($_REQUEST[$key]) && !!($_REQUEST[$key]))
		{
			return $_REQUEST[$key];
		}
		else
		{
			return $alt;
		}	
	}
	/*
	* Is post back sent
	* @returns boolean
	*/
	public function isPostBack()
	{
		return !!($_POST)?true:false;
	}
	/*
	* Returns user agent
	* @returns string user agent
	*/
	public function getUserAgent()
	{
		return $_SERVER['HTTP_USER_AGENT'];
	}
	/*
	* Returns the IP address from which the user is viewing the current page.
	* @returns string ip address
	*/
	public function getRemoteAddr()
	{
		return $_SERVER['REMOTE_ADDR'];
	}
	/*
	* Returns IP address by host name
	* @parameter string host name
	* @returns string IP address
	*/
	public function getAddrByName($host_name)
	{
		return gethostbyname($host_name);
	}
}
/*
* Converts path to array
* @param string path
* @returns array result
*/
function path_to_array($path)
{
	$path_array = array();// array of path
	$temp_array = explode('/', $path);// path is allways defined with "/"
	$count = count($temp_array);
	
	for ($i = 0; $i <= $count; $i = $i+2)
	{
		if (array_key_exists($i,$temp_array) AND !!($temp_array[$i]))
		{
			$t = $i-1;
			if($t>0)
			{
				/*
				* If is array
				*/
				$is_array_position = stripos($temp_array[$t],'[]');
				if($is_array_position!==false)
				{
					$temp_array[$t] = substr($temp_array[$t],0,$is_array_position);
					$path_array[$temp_array[$t]][] = $temp_array[$i];
				}
				else
				{
					$path_array[$temp_array[$t]] = $temp_array[$i];
				}
				
			}
		}
	}
	if(!!($path_array))
	{
		return $path_array;
	}
}
/*
* @params array $array 
* @returns array with slashed data
*/
function array_add_slashes($array)
{
	if(is_array($array))
	{
		foreach($array as $k=>$v)
		{
			if(is_array($v))
			{
				$tmp[$k] = add_array_slashes($v);
			}
			else
			{
				$tmp[$k] = addslashes(htmlentities($v,ENT_QUOTES,'utf-8'));
			}
		}
		return $tmp;
	}
	return;
}
?>