Location: PHPKode > scripts > Cookie preferences > cookie-preferences/cookie_preferences.inc
<?
/*
 * Copyright 2004 Ken Stanley
 *
 * cookie_preferences is free software.
 * You can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 */
class cookie_preferences
{
	/* Constructor: */
	function cookie_preferences()
	{
		/* Function Init: */
		$argc					= func_num_args();
		$argv					= func_get_args();
		
		/* Get the values pass in to us: */
		$this->cookie_name		= (!empty($argv[0])) ? $argv[0] : 'preferences';	// Optional
		$this->cookie_expiry	= (!empty($argv[1])) ? $argv[1] : '0';				// Optional
		$this->cookie_secure	= (!empty($argv[2])) ? $argv[2] : '0';				// Optional
		$this->use_rot13		= (!empty($argv[3])) ? $argv[3] : false;			// Optional
	}

	/* Public Collection: */
	function preference_new()
	{
		/* Variable Init: */
		$preferences_temp		= '';

		/* Function Init: */
		$argc					= func_num_args();
		$argv					= func_get_args();

		/* Get the values pass in to us: */
		$preference_name		= (!empty($argv[0])) ? $argv[0] : NULL;	// Required
		$preference_value		= (!empty($argv[1])) ? $argv[1] : NULL;	// Required

		if ($preference_name == NULL || $preference_value == NULL)
		{
			trigger_error(__FUNCTION__ . " expects two arguements", E_USER_NOTICE);
			return false;
		}

		$preferences			 = $this->_get_preferences();

		/* Check to see if a preference is set with the same name: */
		for ($i = 0; $i < count($preferences); $i++)
		{
			if ($preferences[$i]['name'] == $preference_name)
			{
				/* Preference setting already exists, so we return false: */
				return false;
			}
			else
			{
				/* Let's start packing a new preference: */
				$preferences_temp .= $preferences[$i]['name'] . ":" . $preferences[$i]['value'] . ";";
			}
		}

		/* If we got this far then it's ok to set the preference: */
		$preferences_temp .= $preference_name . ":" . $preference_value . ";";

		/* Apply ROT13 to the new preference before setting the new cookie: */
		if ($this->use_rot13)
		{
			$preferences_temp = $this->_str_rot13($preferences_temp);
		}

		if ($this->_set_preferences($preferences_temp))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	function preference_update()
	{
		/* Variable Init: */
		$preferences_temp		= '';

		/* Function Init: */
		$argc					= func_num_args();
		$argv					= func_get_args();

		/* Get the values passed in to us: */
		$preference_name		= (!empty($argv[0])) ? $argv[0] : NULL;	// Required
		$preference_value		= (!empty($argv[1])) ? $argv[1] : NULL;	// Required

		if ($preference_name == NULL || $preference_value == NULL)
		{
			trigger_error(__FUNCTION__ . " expects two arguements", E_USER_NOTICE);
			return false;
		}

		/* Get the preferences: */
		$preferences			= $this->_get_preferences();

		for ($i = 0; $i < count($preferences); $i++)
		{
			if ($preferences[$i]['name'] == $preference_name)
			{
				$preferences_temp .= $preference_name . ":" . $preference_value . ";";
			}
			else
			{
				$preferences_temp .= $preferences[$i]['name'] . ":" . $preferences[$i]['value'] . ";";
			}
		}

		/* Apply ROT13 to the new preference before setting the new cookie: */
		if ($this->use_rot13)
		{
			$preferences_temp = $this->_str_rot13($preferences_temp);
		}

		if ($this->_set_preferences($preferences_temp))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	function preference_get()
	{
		/* Function Init: */
		$argc					= func_num_args();
		$argv					= func_get_args();

		/* Get the values passed in to us: */
		$preference_name		= (!empty($argv[0])) ? $argv[0] : NULL; // Required

		if ($preference_name == NULL)
		{
			trigger_error(__FUNCTION__ . " expects one arguement", E_USER_NOTICE);
			return false;
		}

		/* Retrieve the cookie data: */
		$preferences			= $this->_get_preferences();

		/* Check for the cookie existence: */
		for ($i = 0; $i < count($preferences); $i++)
		{
			if ($preferences[$i]['name'] == $preference_name)
			{
				return $preferences[$i]['value'];
			}
		}

		/* Preference didn't exist, so we return false: */
		return false;
	}

	/* Private Collection: */
	function _get_preferences()
	{
		/* Initialize the cookie array: */
		$cookie_array			= array();

		/* Retrieve the cookie, if it exists: */
		if (!empty($_COOKIE[$this->cookie_name]))
		{
			$cookie				= $_COOKIE[$this->cookie_name];

			if ($this->use_rot13)
			{
				$cookie			= $this->_str_rot13($cookie);
			}
			$keys				= split(";", $cookie);

			for ($i = 0; $i < count($keys); $i++)
			{
				if ($keys[$i] != "")
				{
					list($cookie_array[$i]['name'], $cookie_array[$i]['value']) = split(":", $keys[$i]);
				}
			}
		}
		return $cookie_array;
	}

	function _str_rot13()
	{
		/* Function Init: */
		$argc					= func_num_args();
		$argv					= func_get_args();

		/* Get the values passed in to us: */
		$rot_string				= (!empty($argv[0])) ? $argv[0] : NULL;	// Required

		if ($rot_string == NULL)
		{
			trigger_error(__FUNCTION__ . " expects one arguement", E_USER_NOTICE);
			return false;
		}

		if (!function_exists("str_rot13"))
		{
			$decoded = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$encoded = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
			return strtr($rot_string, $decoded, $encoded);
		}
		else
		{
			return str_rot13($rot_string);
		}
	}

	function _set_preferences()
	{
		/* Function Init: */
		$argc					= func_num_args();
		$argv					= func_get_args();

		/* Get the values passed in to us: */
		$preferences			= (!empty($argv[0]) ? $argv[0] : NULL);	// Required

		if ($preferences == NULL)
		{
			trigger_error(__FUNCTION__ . " expects one arguement", E_USER_NOTICE);
			return false;
		}

		/* Get the path of the cookie: */
		$cookie_path			= dirname($_SERVER['PHP_SELF']);

		/* Set the cookie: */
		$result = setcookie(
					$this->cookie_name,
					$preferences,
					$this->cookie_expiry,
					$cookie_path,
					'',
					$this->cookie_secure);

		/* $result is boolean, so return that to our calling function: */
		return $result;
	}
}
?>
Return current item: Cookie preferences