1: <?php namespace Laravel;
2:
3: class Cookie {
4:
5: /**
6: * How long is forever (in minutes)?
7: *
8: * @var int
9: */
10: const forever = 2628000;
11:
12: /**
13: * The cookies that have been set.
14: *
15: * @var array
16: */
17: public static $jar = array();
18:
19: /**
20: * Determine if a cookie exists.
21: *
22: * @param string $name
23: * @return bool
24: */
25: public static function has($name)
26: {
27: return ! is_null(static::get($name));
28: }
29:
30: /**
31: * Get the value of a cookie.
32: *
33: * <code>
34: * // Get the value of the "favorite" cookie
35: * $favorite = Cookie::get('favorite');
36: *
37: * // Get the value of a cookie or return a default value
38: * $favorite = Cookie::get('framework', 'Laravel');
39: * </code>
40: *
41: * @param string $name
42: * @param mixed $default
43: * @return string
44: */
45: public static function get($name, $default = null)
46: {
47: if (isset(static::$jar[$name])) return static::parse(static::$jar[$name]['value']);
48:
49: if ( ! is_null($value = Request::foundation()->cookies->get($name)))
50: {
51: return static::parse($value);
52: }
53:
54: return value($default);
55: }
56:
57: /**
58: * Set the value of a cookie.
59: *
60: * <code>
61: * // Set the value of the "favorite" cookie
62: * Cookie::put('favorite', 'Laravel');
63: *
64: * // Set the value of the "favorite" cookie for twenty minutes
65: * Cookie::put('favorite', 'Laravel', 20);
66: * </code>
67: *
68: * @param string $name
69: * @param string $value
70: * @param int $expiration
71: * @param string $path
72: * @param string $domain
73: * @param bool $secure
74: * @return void
75: */
76: public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
77: {
78: if ($expiration !== 0)
79: {
80: $expiration = time() + ($expiration * 60);
81: }
82:
83: $value = static::hash($value).'+'.$value;
84:
85: // If the developer has explicitly disabled SLL, then we shouldn't force
86: // this cookie over SSL.
87: $secure = $secure && Config::get('application.ssl');
88:
89: // If the secure option is set to true, yet the request is not over HTTPS
90: // we'll throw an exception to let the developer know that they are
91: // attempting to send a secure cookie over the insecure HTTP.
92: if ($secure and ! Request::secure())
93: {
94: throw new \Exception("Attempting to set secure cookie over HTTP.");
95: }
96:
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: * <code>
104: * // Set a cookie that should last one year
105: * Cookie::forever('favorite', 'Blue');
106: * </code>
107: *
108: * @param string $name
109: * @param string $value
110: * @param string $path
111: * @param string $domain
112: * @param bool $secure
113: * @return bool
114: */
115: public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
116: {
117: return static::put($name, $value, static::forever, $path, $domain, $secure);
118: }
119:
120: /**
121: * Delete a cookie.
122: *
123: * @param string $name
124: * @param string $path
125: * @param string $domain
126: * @param bool $secure
127: * @return bool
128: */
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: * Hash the given cookie value.
136: *
137: * @param string $value
138: * @return string
139: */
140: public static function hash($value)
141: {
142: return hash_hmac('sha1', $value, Config::get('application.key'));
143: }
144:
145: /**
146: * Parse a hash fingerprinted cookie value.
147: *
148: * @param string $value
149: * @return string
150: */
151: protected static function parse($value)
152: {
153: $segments = explode('+', $value);
154:
155: // First we will make sure the cookie actually has enough segments to even
156: // be valid as being set by the application. If it does not we will go
157: // ahead and throw exceptions now since there the cookie is invalid.
158: if ( ! (count($segments) >= 2))
159: {
160: return null;
161: }
162:
163: $value = implode('+', array_slice($segments, 1));
164:
165: // Now we will check if the SHA-1 hash present in the first segment matches
166: // the ShA-1 hash of the rest of the cookie value, since the hash should
167: // have been set when the cookie was first created by the application.
168: if ($segments[0] == static::hash($value))
169: {
170: return $value;
171: }
172:
173: return null;
174: }
175:
176: }
177: