2009-04-04 22:15:04 +03:00
|
|
|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
|
|
/**
|
|
|
|
* Cookie helper.
|
|
|
|
*
|
|
|
|
* @package Kohana
|
|
|
|
* @author Kohana Team
|
|
|
|
* @copyright (c) 2008-2009 Kohana Team
|
|
|
|
* @license http://kohanaphp.com/license.html
|
|
|
|
*/
|
|
|
|
class cookie_Core {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Magic salt to add to the cookie
|
|
|
|
*/
|
|
|
|
public static $salt = 'kooky';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer Number of seconds before the cookie expires
|
|
|
|
*/
|
|
|
|
public static $expiration = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Restrict the path that the cookie is available to
|
|
|
|
*/
|
|
|
|
public static $path = '/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Restrict the domain that the cookie is available to
|
|
|
|
*/
|
|
|
|
public static $domain = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean Only transmit cookies over secure connections
|
|
|
|
*/
|
|
|
|
public static $secure = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean Only transmit cookies over HTTP, disabling Javascript access
|
|
|
|
*/
|
|
|
|
public static $httponly = FALSE;
|
|
|
|
|
|
|
|
/**
|
2009-05-20 20:25:28 +03:00
|
|
|
* Gets the value of a signed cookie. Cookies without signatures will not
|
|
|
|
* be returned. If the cookie signature is present, but invalid, the cookie
|
|
|
|
* will be deleted.
|
2009-04-04 22:15:04 +03:00
|
|
|
*
|
|
|
|
* @param string cookie name
|
|
|
|
* @param mixed default value to return
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get($key, $default = NULL)
|
|
|
|
{
|
|
|
|
if ( ! isset($_COOKIE[$key]))
|
|
|
|
{
|
|
|
|
// The cookie does not exist
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the cookie value
|
|
|
|
$cookie = $_COOKIE[$key];
|
|
|
|
|
2009-04-09 23:45:51 +03:00
|
|
|
// Find the position of the split between salt and contents
|
|
|
|
$split = strlen(cookie::salt($key, NULL));
|
|
|
|
|
2009-04-04 22:15:04 +03:00
|
|
|
if (isset($cookie[$split]) AND $cookie[$split] === '~')
|
|
|
|
{
|
|
|
|
// Separate the salt and the value
|
|
|
|
list ($hash, $value) = explode('~', $cookie, 2);
|
|
|
|
|
|
|
|
if (cookie::salt($key, $value) === $hash)
|
|
|
|
{
|
|
|
|
// Cookie signature is valid
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The cookie signature is invalid, delete it
|
|
|
|
cookie::delete($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-20 20:25:28 +03:00
|
|
|
* Sets a signed cookie. Note that all cookie values must be strings and no
|
|
|
|
* automatic serialization will be performed!
|
2009-04-04 22:15:04 +03:00
|
|
|
*
|
|
|
|
* @param string name of cookie
|
2009-05-20 20:25:28 +03:00
|
|
|
* @param string value of cookie
|
2009-04-04 22:15:04 +03:00
|
|
|
* @param integer lifetime in seconds
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-05-20 20:25:28 +03:00
|
|
|
public static function set($name, $value, $expiration = NULL)
|
2009-04-04 22:15:04 +03:00
|
|
|
{
|
|
|
|
if ($expiration === NULL)
|
|
|
|
{
|
|
|
|
// Use the default expiration
|
|
|
|
$expiration = cookie::$expiration;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expiration !== 0)
|
|
|
|
{
|
|
|
|
// The expiration is expected to be a UNIX timestamp
|
|
|
|
$expiration += time();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the salt to the cookie value
|
2009-05-20 20:25:28 +03:00
|
|
|
$value = cookie::salt($name, $value).'~'.$value;
|
2009-04-04 22:15:04 +03:00
|
|
|
|
2009-05-20 20:25:28 +03:00
|
|
|
return setcookie($name, $value, $expiration, cookie::$path, cookie::$domain, cookie::$secure, cookie::$httponly);
|
2009-04-04 22:15:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-20 20:25:28 +03:00
|
|
|
* Deletes a cookie by making the value NULL and expiring it.
|
2009-04-04 22:15:04 +03:00
|
|
|
*
|
|
|
|
* @param string cookie name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-05-20 20:25:28 +03:00
|
|
|
public static function delete($name)
|
2009-04-04 22:15:04 +03:00
|
|
|
{
|
|
|
|
// Remove the cookie
|
2009-05-20 20:25:28 +03:00
|
|
|
unset($_COOKIE[$name]);
|
2009-04-04 22:15:04 +03:00
|
|
|
|
2009-05-20 20:25:28 +03:00
|
|
|
// Nullify the cookie and make it expire
|
|
|
|
return cookie::set($name, NULL, -86400);
|
2009-04-04 22:15:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-20 20:25:28 +03:00
|
|
|
* Generates a salt string for a cookie based on the name and value.
|
2009-04-04 22:15:04 +03:00
|
|
|
*
|
|
|
|
* @param string name of cookie
|
2009-05-20 20:25:28 +03:00
|
|
|
* @param string value of cookie
|
2009-04-04 22:15:04 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2009-05-20 20:25:28 +03:00
|
|
|
public static function salt($name, $value)
|
2009-04-04 22:15:04 +03:00
|
|
|
{
|
2009-05-20 20:24:46 +03:00
|
|
|
// Determine the user agent
|
2009-05-20 20:25:28 +03:00
|
|
|
$agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown';
|
2009-05-20 20:24:46 +03:00
|
|
|
|
2009-05-20 20:25:28 +03:00
|
|
|
return sha1($agent.$name.$value.cookie::$salt);
|
2009-04-04 22:15:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End cookie
|