1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magister\Services\Cookie; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Magister\Services\Contracts\Cookie\Factory as JarContract; |
8
|
|
|
use Magister\Services\Encryption\Encrypter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CookieJar. |
12
|
|
|
*/ |
13
|
|
|
class CookieJar implements JarContract |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The encrypter instance. |
17
|
|
|
* |
18
|
|
|
* @var \Magister\Services\Encryption\Encrypter |
19
|
|
|
*/ |
20
|
|
|
protected $encrypter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The default path. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $path = '/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The default domain. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $domain = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The default secure setting (defaults to false). |
38
|
|
|
* |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected $secure = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new cookiejar instance. |
45
|
|
|
* |
46
|
|
|
* @param \Magister\Services\Encryption\Encrypter $encrypter |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Encrypter $encrypter) |
49
|
|
|
{ |
50
|
|
|
$this->encrypter = $encrypter; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Determine if a cookie exists and is not null. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function has($key) |
61
|
|
|
{ |
62
|
|
|
return !is_null($this->get($key)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the value of the given cookie. |
67
|
|
|
* |
68
|
|
|
* @param string $key |
69
|
|
|
* @param mixed $default |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function get($key, $default = null) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$value = isset($_COOKIE[$key]) ? $_COOKIE[$key] : null; |
76
|
|
|
|
77
|
|
|
if (!is_null($value)) { |
78
|
|
|
return $this->decrypt($value); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $default instanceof Closure ? $default() : $default; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Decrypt the given cookie value. |
86
|
|
|
* |
87
|
|
|
* @param string $value |
88
|
|
|
* |
89
|
|
|
* @return mixed|null |
90
|
|
|
*/ |
91
|
|
|
protected function decrypt($value) |
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
return $this->encrypter->decrypt($value); |
95
|
|
|
} catch (Exception $e) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create a new cookie. |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* @param string $value |
105
|
|
|
* @param int $expire |
106
|
|
|
* @param string $path |
107
|
|
|
* @param string $domain |
108
|
|
|
* @param bool $secure |
109
|
|
|
* @param bool $httpOnly |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function make($name, $value, $expire = 0, $path = null, $domain = null, $secure = false, $httpOnly = true) |
114
|
|
|
{ |
115
|
|
|
list($path, $domain, $secure) = $this->getPathAndDomain($path, $domain, $secure); |
116
|
|
|
|
117
|
|
|
$expire = ($expire == 0) ? 0 : time() + ($expire * 60); |
118
|
|
|
|
119
|
|
|
$value = $this->encrypter->encrypt($value); |
120
|
|
|
|
121
|
|
|
return setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Create a cookie that lasts forever. |
126
|
|
|
* |
127
|
|
|
* @param string $name |
128
|
|
|
* @param string $value |
129
|
|
|
* @param string $path |
130
|
|
|
* @param string $domain |
131
|
|
|
* @param bool $secure |
132
|
|
|
* @param bool $httpOnly |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true) |
137
|
|
|
{ |
138
|
|
|
return $this->make($name, $value, 525948, $path, $domain, $secure, $httpOnly); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Expire the given cookie. |
143
|
|
|
* |
144
|
|
|
* @param string $name |
145
|
|
|
* @param string $path |
146
|
|
|
* @param string $domain |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function forget($name, $path = null, $domain = null) |
151
|
|
|
{ |
152
|
|
|
return $this->make($name, null, -525948, $path, $domain); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the default path and domain. |
157
|
|
|
* |
158
|
|
|
* @param string $path |
159
|
|
|
* @param string $domain |
160
|
|
|
* @param bool $secure |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getPathAndDomain($path, $domain, $secure = false) |
165
|
|
|
{ |
166
|
|
|
return [$path ?: $this->path, $domain ?: $this->domain, $secure ?: $this->secure]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set the default path and domain. |
171
|
|
|
* |
172
|
|
|
* @param string $path |
173
|
|
|
* @param string $domain |
174
|
|
|
* @param bool $secure |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function setDefaultPathAndDomain($path, $domain, $secure = false) |
179
|
|
|
{ |
180
|
|
|
list($this->path, $this->domain, $this->secure) = [$path, $domain, $secure]; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the encrypter instance. |
187
|
|
|
* |
188
|
|
|
* @return \Magister\Services\Encryption\Encrypter |
189
|
|
|
*/ |
190
|
|
|
public function getEncrypter() |
191
|
|
|
{ |
192
|
|
|
return $this->encrypter; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: