-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcookie.php
172 lines (152 loc) · 3.95 KB
/
cookie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php namespace Laravel;
class Cookie {
/**
* How long is forever (in minutes)?
*
* @var int
*/
const forever = 2628000;
/**
* The cookies that have been set.
*
* @var array
*/
public static $jar = array();
/**
* Determine if a cookie exists.
*
* @param string $name
* @return bool
*/
public static function has($name)
{
return ! is_null(static::get($name));
}
/**
* Get the value of a cookie.
*
* <code>
* // Get the value of the "favorite" cookie
* $favorite = Cookie::get('favorite');
*
* // Get the value of a cookie or return a default value
* $favorite = Cookie::get('framework', 'Laravel');
* </code>
*
* @param string $name
* @param mixed $default
* @return string
*/
public static function get($name, $default = null)
{
if (isset(static::$jar[$name])) return static::parse(static::$jar[$name]['value']);
if ( ! is_null($value = Request::foundation()->cookies->get($name)))
{
return static::parse($value);
}
return value($default);
}
/**
* 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>
*
* @param string $name
* @param string $value
* @param int $expiration
* @param string $path
* @param string $domain
* @param bool $secure
* @return void
*/
public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
{
if ($expiration !== 0)
{
$expiration = time() + ($expiration * 60);
}
$value = static::hash($value).'+'.$value;
// 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
// attempting to send a secure cookie over the insecure HTTP.
if ($secure and ! Request::secure())
{
throw new \Exception("Attempting to set secure cookie over HTTP.");
}
static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
}
/**
* Set a "permanent" cookie. The cookie will last for one year.
*
* <code>
* // Set a cookie that should last one year
* Cookie::forever('favorite', 'Blue');
* </code>
*
* @param string $name
* @param string $value
* @param string $path
* @param string $domain
* @param bool $secure
* @return bool
*/
public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
{
return static::put($name, $value, static::forever, $path, $domain, $secure);
}
/**
* Delete a cookie.
*
* @param string $name
* @param string $path
* @param string $domain
* @param bool $secure
* @return bool
*/
public static function forget($name, $path = '/', $domain = null, $secure = false)
{
return static::put($name, null, -2000, $path, $domain, $secure);
}
/**
* Hash the given cookie value.
*
* @param string $value
* @return string
*/
public static function hash($value)
{
return hash_hmac('sha1', $value, Config::get('application.key'));
}
/**
* 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))
{
return null;
}
$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.
if ($segments[0] == static::hash($value))
{
return $value;
}
return null;
}
}