cookie.php 3.95 KB
Newer Older
1
<?php namespace Laravel;
2

3 4 5
class Cookie {

	/**
Chris Berthe committed
6
	 * How long is forever (in minutes)?
7 8 9 10 11 12
	 *
	 * @var int
	 */
	const forever = 525600;

	/**
13 14 15 16 17 18 19
	 * The cookies that have been set.
	 *
	 * @var array
	 */
	public static $jar = array();

	/**
20 21
	 * Determine if a cookie exists.
	 *
22
	 * @param  string  $name
23 24
	 * @return bool
	 */
25
	public static function has($name)
26
	{
27
		return ! is_null(static::get($name));
28 29 30 31 32
	}

	/**
	 * Get the value of a cookie.
	 *
33 34 35 36
	 * <code>
	 *		// Get the value of the "favorite" cookie
	 *		$favorite = Cookie::get('favorite');
	 *
37
	 *		// Get the value of a cookie or return a default value
38 39 40
	 *		$favorite = Cookie::get('framework', 'Laravel');
	 * </code>
	 *
41
	 * @param  string  $name
42 43 44
	 * @param  mixed   $default
	 * @return string
	 */
45
	public static function get($name, $default = null)
46
	{
47
		if (isset(static::$jar[$name])) return static::parse(static::$jar[$name]['value']);
48

49 50 51 52 53 54
		if ( ! is_null($value = Request::foundation()->cookies->get($name)))
		{
			return static::parse($value);
		}

		return value($default);
55 56 57
	}

	/**
58 59 60 61 62 63 64 65 66
	 * 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>
67
	 *
68 69
	 * @param  string  $name
	 * @param  string  $value
70
	 * @param  int     $expiration
71 72 73
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
Phill Sparks committed
74
	 * @return void
75
	 */
76
	public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
77
	{
78 79 80 81 82
		if ($expiration !== 0)
		{
			$expiration = time() + ($expiration * 60);
		}

83
		$value = static::hash($value).'+'.$value;
84

Taylor Otwell committed
85 86
		// 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
Pascal Borreli committed
87
		// attempting to send a secure cookie over the insecure HTTP.
Taylor Otwell committed
88 89 90 91 92
		if ($secure and ! Request::secure())
		{
			throw new \Exception("Attempting to set secure cookie over HTTP.");
		}

93
		static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
94 95 96
	}

	/**
97
	 * Set a "permanent" cookie. The cookie will last for one year.
98
	 *
99 100 101 102
	 * <code>
	 *		// Set a cookie that should last one year
	 *		Cookie::forever('favorite', 'Blue');
	 * </code>
103
	 *
104 105 106 107 108
	 * @param  string  $name
	 * @param  string  $value
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
109 110
	 * @return bool
	 */
111
	public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
112
	{
113
		return static::put($name, $value, static::forever, $path, $domain, $secure);
Taylor Otwell committed
114 115 116
	}

	/**
117 118
	 * Delete a cookie.
	 *
119
	 * @param  string  $name
120 121 122
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
123 124
	 * @return bool
	 */
125
	public static function forget($name, $path = '/', $domain = null, $secure = false)
126
	{
127
		return static::put($name, null, -2000, $path, $domain, $secure);
128 129
	}

130
	/**
131 132 133 134 135 136 137 138 139 140 141
	 * Hash the given cookie value.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public static function hash($value)
	{
		return hash_hmac('sha1', $value, Config::get('application.key'));
	}

	/**
142 143 144 145 146 147 148 149 150 151 152 153 154 155
	 * 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))
		{
156
			return null;
157 158 159 160 161 162 163
		}

		$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.
164
		if ($segments[0] == static::hash($value))
165 166 167 168
		{
			return $value;
		}

169
		return null;
170 171
	}

172
}