<?php

class vB5_Cookie
{
	protected static $enabled = null;
	protected static $cookiePrefix = null;
	protected static $path = null;
	protected static $domain = null;
	protected static $secure = null;

	const TYPE_UINT = 1;
	const TYPE_STRING = 2;

	public static function set($name, $value, $expireDays = 0, $httpOnly = true)
	{
		self::loadConfig();

		if (!self::$enabled)
		{
			return;
		}

		if ($expireDays == 0)
		{
			$expire = 0;
		}
		else
		{
			$expire = time() + ($expireDays * 86400);
		}

		$name = self::$cookiePrefix . $name;

		if (!setcookie($name, $value, $expire, self::$path, self::$domain, self::$secure, $httpOnly))
		{
			throw new Exception('Unable to set cookies');
		}
	}

	public static function get($name, $type)
	{
		self::loadConfig();

		if (!self::$enabled)
		{
			return;
		}

		$name = self::$cookiePrefix . $name;

		$value = isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;

		switch($type)
		{
			case self::TYPE_UINT:
				$value = intval($value);
				$value = $value < 0 ? 0 : $value;
				break;

			case self::TYPE_STRING:
				$value = strval($value);
				break;

			default:
				throw new Exception('Invalid cookie clean type');
				break;
		}

		return $value;
	}

	public static function delete($name)
	{
		self::set($name, '', -1);
	}

	/**
	 * Deletes all cookies starting with cookiePrefix
	 */
	public static function deleteAll()
	{
		$prefix_length = strlen(self::$cookiePrefix);
		foreach ($_COOKIE AS $key => $val)
		{
			$index = strpos($key, self::$cookiePrefix);
			if ($index == 0 AND $index !== false)
			{
				$key = substr($key, $prefix_length);
				if (trim($key) == '')
				{
					continue;
				}
				// self::set will add the cookie prefix
				self::delete($key);
			}
		}
	}

	public static function isEnabled()
	{
		self::loadConfig();

		return self::$enabled;
	}

	protected static function loadConfig()
	{
		if (self::$cookiePrefix !== null)
		{
			return;
		}

		$config = vB5_Config::instance();

		// these could potentially all be config options
		self::$enabled = ($config->cookie_enabled !== false);
		self::$cookiePrefix = $config->cookie_prefix;

		//0 is vB_Cache::CACHE_STD when we make these more friendly we need to change this as well
		//as the config file  
		$cache = Api_InterfaceAbstract::instance()->cacheInstance(0);

		$cacheKey = 'vB5Presentation_cookieInfo';
		$cookieInfo = $cache->read($cacheKey);
		if (empty($cookieInfo))
		{
			$options = vB5_Template_Options::instance();
			self::$path = $options->get('options.cookiepath');
			self::$domain = $options->get('options.cookiedomain');
			$cache->write($cacheKey, array('cookiepath' => self::$path, 'cookiedomain' => self::$domain, 86400, 'vB_CookieChange'));
		}
		else
		{
			self::$path = $cookieInfo['cookiepath'];
			self::$domain = $cookieInfo['cookiedomain'];

		}

		self::$secure = (
			(
				(443 === intval($_SERVER['SERVER_PORT']))
				OR
				(isset($_SERVER['HTTPS']) AND $_SERVER['HTTPS'] AND ($_SERVER['HTTPS'] != 'off'))
			)
			AND
			(isset($url['scheme']) AND $url['scheme'] == 'https')
		);
	}
}
