2012-05-11 12:28:52 +03:00
|
|
|
<?php namespace Laravel;
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
class Cookie {
|
|
|
|
|
|
2012-05-10 16:32:07 -05:00
|
|
|
/**
|
2012-06-13 11:19:20 -04:00
|
|
|
* How long is forever (in minutes)?
|
2012-05-10 16:32:07 -05:00
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
const forever = 525600;
|
|
|
|
|
|
2012-01-31 15:19:23 -06:00
|
|
|
/**
|
|
|
|
|
* The cookies that have been set.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public static $jar = array();
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Determine if a cookie exists.
|
|
|
|
|
*
|
2011-06-15 10:23:38 -07:00
|
|
|
* @param string $name
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function has($name)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return ! is_null(static::get($name));
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the value of a cookie.
|
|
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* <code>
|
|
|
|
|
* // Get the value of the "favorite" cookie
|
|
|
|
|
* $favorite = Cookie::get('favorite');
|
|
|
|
|
*
|
2012-05-10 16:32:07 -05:00
|
|
|
* // Get the value of a cookie or return a default value
|
2012-01-16 13:59:24 -06:00
|
|
|
* $favorite = Cookie::get('framework', 'Laravel');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-06-15 10:23:38 -07:00
|
|
|
* @param string $name
|
2011-06-08 23:45:08 -05:00
|
|
|
* @param mixed $default
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function get($name, $default = null)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-09-25 08:40:29 -05:00
|
|
|
if (isset(static::$jar[$name])) return static::parse(static::$jar[$name]['value']);
|
2012-01-31 15:58:00 -06:00
|
|
|
|
2012-09-25 08:40:29 -05:00
|
|
|
if ( ! is_null($value = Request::foundation()->cookies->get($name)))
|
|
|
|
|
{
|
|
|
|
|
return static::parse($value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value($default);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Set the value of a cookie.
|
|
|
|
|
*
|
|
|
|
|
* <code>
|
|
|
|
|
* // Set the value of the "favorite" cookie
|
|
|
|
|
* Cookie::put('favorite', 'Laravel');
|
|
|
|
|
*
|
|
|
|
|
* // Set the value of the "favorite" cookie for twenty minutes
|
|
|
|
|
* Cookie::put('favorite', 'Laravel', 20);
|
|
|
|
|
* </code>
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
2012-03-28 22:43:58 -05:00
|
|
|
* @param int $expiration
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
2012-02-04 21:30:52 +00:00
|
|
|
* @return void
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2012-03-28 22:43:58 -05:00
|
|
|
public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-03-28 22:43:58 -05:00
|
|
|
if ($expiration !== 0)
|
|
|
|
|
{
|
|
|
|
|
$expiration = time() + ($expiration * 60);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-25 16:43:58 -04:00
|
|
|
$value = static::hash($value).'+'.$value;
|
2012-09-25 08:40:29 -05:00
|
|
|
|
2012-04-04 10:28:06 -05:00
|
|
|
// If the secure option is set to true, yet the request is not over HTTPS
|
|
|
|
|
// we'll throw an exception to let the developer know that they are
|
2012-07-24 09:31:27 +00:00
|
|
|
// attempting to send a secure cookie over the insecure HTTP.
|
2012-04-04 10:28:06 -05:00
|
|
|
if ($secure and ! Request::secure())
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("Attempting to set secure cookie over HTTP.");
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-28 22:43:58 -05:00
|
|
|
static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Set a "permanent" cookie. The cookie will last for one year.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* <code>
|
|
|
|
|
* // Set a cookie that should last one year
|
|
|
|
|
* Cookie::forever('favorite', 'Blue');
|
|
|
|
|
* </code>
|
2011-09-09 20:55:24 -05:00
|
|
|
*
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-05-10 16:32:07 -05:00
|
|
|
return static::put($name, $value, static::forever, $path, $domain, $secure);
|
2011-10-10 21:34:15 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Delete a cookie.
|
|
|
|
|
*
|
2011-06-15 10:23:38 -07:00
|
|
|
* @param string $name
|
2012-01-05 11:52:42 -06:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static function forget($name, $path = '/', $domain = null, $secure = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return static::put($name, null, -2000, $path, $domain, $secure);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2012-09-25 16:43:58 -04:00
|
|
|
/**
|
|
|
|
|
* Hash the given cookie value.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function hash($value)
|
|
|
|
|
{
|
|
|
|
|
return hash_hmac('sha1', $value, Config::get('application.key'));
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-25 08:40:29 -05:00
|
|
|
/**
|
|
|
|
|
* Parse a hash fingerprinted cookie value.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function parse($value)
|
|
|
|
|
{
|
|
|
|
|
$segments = explode('+', $value);
|
|
|
|
|
|
|
|
|
|
// First we will make sure the cookie actually has enough segments to even
|
|
|
|
|
// be valid as being set by the application. If it does not we will go
|
|
|
|
|
// ahead and throw exceptions now since there the cookie is invalid.
|
|
|
|
|
if ( ! (count($segments) >= 2))
|
|
|
|
|
{
|
2012-09-25 16:43:58 -04:00
|
|
|
return null;
|
2012-09-25 08:40:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$value = implode('+', array_slice($segments, 1));
|
|
|
|
|
|
|
|
|
|
// Now we will check if the SHA-1 hash present in the first segment matches
|
|
|
|
|
// the ShA-1 hash of the rest of the cookie value, since the hash should
|
|
|
|
|
// have been set when the cookie was first created by the application.
|
2012-09-25 16:43:58 -04:00
|
|
|
if ($segments[0] == static::hash($value))
|
2012-09-25 08:40:29 -05:00
|
|
|
{
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-25 16:43:58 -04:00
|
|
|
return null;
|
2012-09-25 08:40:29 -05:00
|
|
|
}
|
|
|
|
|
|
2012-07-31 16:55:02 +01:00
|
|
|
}
|