1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\CookiesBundle\Cookie\Model; |
13
|
|
|
|
14
|
|
|
use ONGR\CookiesBundle\Exception\CookieNotLoadedException; |
15
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Generic cookie class model to deal with cookies. |
19
|
|
|
*/ |
20
|
|
|
class GenericCookie implements CookieInterface |
21
|
|
|
{ |
22
|
|
|
use CookieFieldsTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string Raw cookie value. |
26
|
|
|
*/ |
27
|
|
|
protected $rawValue; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var mixed Parsed value of the cookie. |
31
|
|
|
*/ |
32
|
|
|
protected $value; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $isLoaded = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $shouldClear = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Hash of the raw value generated once on load. |
46
|
|
|
*/ |
47
|
|
|
protected $valueHash; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $name |
53
|
|
|
*/ |
54
|
|
|
public function __construct($name) |
55
|
|
|
{ |
56
|
|
|
$this->name = $name; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function setValue($value) |
63
|
|
|
{ |
64
|
|
|
$this->ensureLoaded(); |
65
|
|
|
$this->value = $value; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getValue() |
74
|
|
|
{ |
75
|
|
|
$this->ensureLoaded(); |
76
|
|
|
|
77
|
|
|
return $this->value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function load($rawValue) |
84
|
|
|
{ |
85
|
|
|
if ($this->hasRawValue()) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->isLoaded = true; |
90
|
|
|
$this->rawValue = $rawValue; |
91
|
|
|
|
92
|
|
|
$defaults = [ |
93
|
|
|
'domain' => null, |
94
|
|
|
'path' => '/', |
95
|
|
|
'http_only' => true, |
96
|
|
|
'secure' => false, |
97
|
|
|
'expires_time' => 0, |
98
|
|
|
]; |
99
|
|
|
$defaults = array_merge($defaults, $this->defaults); |
100
|
|
|
$this->setDomain($defaults['domain']); |
101
|
|
|
|
102
|
|
|
$expiresTime = $defaults['expires_time']; |
103
|
|
|
if (isset($this->defaults['expires_interval'])) { |
104
|
|
|
$expiresTime = (new \DateTime()) |
105
|
|
|
->add(new \DateInterval($this->defaults['expires_interval'])) |
106
|
|
|
->getTimestamp(); |
107
|
|
|
} |
108
|
|
|
$this->setExpiresTime($expiresTime); |
109
|
|
|
|
110
|
|
|
$this->setIsHttpOnly($defaults['http_only']); |
111
|
|
|
$this->setIsSecure($defaults['secure']); |
112
|
|
|
$this->setPath($defaults['path']); |
113
|
|
|
|
114
|
|
|
$this->setValue($this->decode($this->rawValue)); |
115
|
|
|
$this->loadHash(); |
116
|
|
|
|
117
|
|
|
$this->afterLoad(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Override this for the ability to modify value after loading. |
122
|
|
|
* |
123
|
|
|
* E.g. set expiration time dynamically. |
124
|
|
|
*/ |
125
|
|
|
protected function afterLoad() |
126
|
|
|
{ |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function isLoaded() |
133
|
|
|
{ |
134
|
|
|
return $this->isLoaded; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check if model already has loaded cookie. |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function hasRawValue() |
143
|
|
|
{ |
144
|
|
|
return (null !== $this->rawValue); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return value of $rawValue. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getRawValue() |
153
|
|
|
{ |
154
|
|
|
$this->ensureLoaded(); |
155
|
|
|
|
156
|
|
|
return $this->rawValue; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function toCookie() |
163
|
|
|
{ |
164
|
|
|
$this->rawValue = $this->encode($this->getValue()); |
165
|
|
|
|
166
|
|
|
return new Cookie( |
167
|
|
|
$this->getName(), |
168
|
|
|
$this->getRawValue(), |
169
|
|
|
$this->getExpiresTime(), |
170
|
|
|
$this->getPath(), |
171
|
|
|
$this->getDomain(), |
172
|
|
|
$this->isSecure(), |
173
|
|
|
$this->isHttpOnly() |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Build $rawValue from data in $value. |
179
|
|
|
* |
180
|
|
|
* @param mixed $value |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function encode($value) |
185
|
|
|
{ |
186
|
|
|
return $value; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Decode $value from $rawValue. |
191
|
|
|
* |
192
|
|
|
* @param string $rawValue |
193
|
|
|
* |
194
|
|
|
* @return mixed |
195
|
|
|
*/ |
196
|
|
|
protected function decode($rawValue) |
197
|
|
|
{ |
198
|
|
|
return $rawValue; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function isDirty() |
205
|
|
|
{ |
206
|
|
|
return ($this->calculateHash() !== $this->valueHash); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Calculate hash of cookie data. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
protected function calculateHash() |
215
|
|
|
{ |
216
|
|
|
return md5((string)$this->toCookie()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
|
|
public function getClear() |
223
|
|
|
{ |
224
|
|
|
$this->ensureLoaded(); |
225
|
|
|
|
226
|
|
|
return $this->shouldClear; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function setClear($clear) |
233
|
|
|
{ |
234
|
|
|
$this->ensureLoaded(); |
235
|
|
|
$this->shouldClear = $clear; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Store hash of the cookie value for dirty detection. |
240
|
|
|
*/ |
241
|
|
|
protected function loadHash() |
242
|
|
|
{ |
243
|
|
|
$this->valueHash = $this->calculateHash(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Throw exception, if cookie has not been loaded yet. |
248
|
|
|
* |
249
|
|
|
* @throws CookieNotLoadedException |
250
|
|
|
*/ |
251
|
|
|
protected function ensureLoaded() |
252
|
|
|
{ |
253
|
|
|
if (!$this->isLoaded) { |
254
|
|
|
throw new CookieNotLoadedException(); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|