<?php
/**
 * CtrlHttpCookie
 *
 * @date 15.03.2007
 * @name CtrlHttpCookie
 * @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 
 * 
 * Cookie class
 */
class CtrlHttpCookie
{
	private $_created;
	private $_version;
	private $_expire = 0;
	private $_path = null;
	private $_domain = null;
	private $_secure = false;
	private $_http_only = false;
	/**
	*
	*/	
	public function __construct()
	{
		$this->_expire = $this->get('expire',0);
	}
	/**
	*
	*/
	public function isExpired()
	{
		if($this->_expire > time())
		{
			return false;
		}
		return true;
	}
	/**
	* Sets expire time
	* @param integer in seconds
	*/
	public function setExpire($seconds)
	{
		if($this->_expire>0)
		{
			
		}
		else
		{
			$this->_expire = time()+$seconds;
			$this->set('expire',$this->_expire);
		}
	}
	/**
	* Removes one cookie
	* @param string cookie name
	* @return string cookie value
	*/	
	public function remove($name)
	{
		if(isset($_COOKIE[$name]))
		{
			$value = $_COOKIE[$name];
			setcookie($name, '', time()-1000);
			setcookie($name, '', time()-1000, '/');
			return $value;
		}
		return null;
	}
	/**
	* Gets the cookie searching by name
	* If not found then can use alternative parameter to return
	* @param string cookie name
	* @param string alternative return value
	*/	
	public function get($name,$alt='')
	{
		if(isset($_COOKIE[$name]))
		{
			$value = $_COOKIE[$name];
			return $value;
		}
		return $alt;
	}
	/**
	* Sets a value to a cookie
	* @param string cookie name
	* @param string cookie value
	*/	
	public function set($name,$value)
	{
		setcookie($name, $value, $this->_expire, $this->_path, $this->_domain,$this->_secure);
	}	
	/**
	* Counts the cookies
	* @return integer counted cookies
	*/	
	public function count()
	{
		return count($_COOKIE);
	}
	/**
	 * Destroys all data registered to a cookies.
	 */
	public function destroy()
	{
		if(isset($_SERVER['HTTP_COOKIE']))
		{
		   $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
		   foreach($cookies as $cookie) 
		   {
				$parts = explode('=', $cookie);
				$name = trim($parts[0]);
				$this->remove($name);
		   }			
		}
	}	
}
?>