cookie.php 4.11 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
	 *
	 * @var int
	 */
Taylor Otwell committed
10
	const forever = 2628000;
11 12

	/**
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

85 86 87 88
		// If the developer has explicitly disabled SLL, then we shouldn't force
		// this cookie over SSL.
		$secure = $secure && Config::get('application.ssl');

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

97
		static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
98 99 100
	}

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

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

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

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

		$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.
168
		if ($segments[0] == static::hash($value))
169 170 171 172
		{
			return $value;
		}

173
		return null;
174 175
	}

176
}