Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp\Cookie;
|
Chris@0
|
3
|
Chris@0
|
4 /**
|
Chris@0
|
5 * Set-Cookie object
|
Chris@0
|
6 */
|
Chris@0
|
7 class SetCookie
|
Chris@0
|
8 {
|
Chris@0
|
9 /** @var array */
|
Chris@0
|
10 private static $defaults = [
|
Chris@0
|
11 'Name' => null,
|
Chris@0
|
12 'Value' => null,
|
Chris@0
|
13 'Domain' => null,
|
Chris@0
|
14 'Path' => '/',
|
Chris@0
|
15 'Max-Age' => null,
|
Chris@0
|
16 'Expires' => null,
|
Chris@0
|
17 'Secure' => false,
|
Chris@0
|
18 'Discard' => false,
|
Chris@0
|
19 'HttpOnly' => false
|
Chris@0
|
20 ];
|
Chris@0
|
21
|
Chris@0
|
22 /** @var array Cookie data */
|
Chris@0
|
23 private $data;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Create a new SetCookie object from a string
|
Chris@0
|
27 *
|
Chris@0
|
28 * @param string $cookie Set-Cookie header string
|
Chris@0
|
29 *
|
Chris@0
|
30 * @return self
|
Chris@0
|
31 */
|
Chris@0
|
32 public static function fromString($cookie)
|
Chris@0
|
33 {
|
Chris@0
|
34 // Create the default return array
|
Chris@0
|
35 $data = self::$defaults;
|
Chris@0
|
36 // Explode the cookie string using a series of semicolons
|
Chris@0
|
37 $pieces = array_filter(array_map('trim', explode(';', $cookie)));
|
Chris@13
|
38 // The name of the cookie (first kvp) must exist and include an equal sign.
|
Chris@13
|
39 if (empty($pieces[0]) || !strpos($pieces[0], '=')) {
|
Chris@0
|
40 return new self($data);
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 // Add the cookie pieces into the parsed data array
|
Chris@0
|
44 foreach ($pieces as $part) {
|
Chris@0
|
45 $cookieParts = explode('=', $part, 2);
|
Chris@0
|
46 $key = trim($cookieParts[0]);
|
Chris@0
|
47 $value = isset($cookieParts[1])
|
Chris@0
|
48 ? trim($cookieParts[1], " \n\r\t\0\x0B")
|
Chris@0
|
49 : true;
|
Chris@0
|
50
|
Chris@0
|
51 // Only check for non-cookies when cookies have been found
|
Chris@0
|
52 if (empty($data['Name'])) {
|
Chris@0
|
53 $data['Name'] = $key;
|
Chris@0
|
54 $data['Value'] = $value;
|
Chris@0
|
55 } else {
|
Chris@0
|
56 foreach (array_keys(self::$defaults) as $search) {
|
Chris@0
|
57 if (!strcasecmp($search, $key)) {
|
Chris@0
|
58 $data[$search] = $value;
|
Chris@0
|
59 continue 2;
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|
Chris@0
|
62 $data[$key] = $value;
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 return new self($data);
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * @param array $data Array of cookie data provided by a Cookie parser
|
Chris@0
|
71 */
|
Chris@0
|
72 public function __construct(array $data = [])
|
Chris@0
|
73 {
|
Chris@0
|
74 $this->data = array_replace(self::$defaults, $data);
|
Chris@0
|
75 // Extract the Expires value and turn it into a UNIX timestamp if needed
|
Chris@0
|
76 if (!$this->getExpires() && $this->getMaxAge()) {
|
Chris@0
|
77 // Calculate the Expires date
|
Chris@0
|
78 $this->setExpires(time() + $this->getMaxAge());
|
Chris@0
|
79 } elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
|
Chris@0
|
80 $this->setExpires($this->getExpires());
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 public function __toString()
|
Chris@0
|
85 {
|
Chris@0
|
86 $str = $this->data['Name'] . '=' . $this->data['Value'] . '; ';
|
Chris@0
|
87 foreach ($this->data as $k => $v) {
|
Chris@0
|
88 if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) {
|
Chris@0
|
89 if ($k === 'Expires') {
|
Chris@0
|
90 $str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; ';
|
Chris@0
|
91 } else {
|
Chris@0
|
92 $str .= ($v === true ? $k : "{$k}={$v}") . '; ';
|
Chris@0
|
93 }
|
Chris@0
|
94 }
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 return rtrim($str, '; ');
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 public function toArray()
|
Chris@0
|
101 {
|
Chris@0
|
102 return $this->data;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Get the cookie name
|
Chris@0
|
107 *
|
Chris@0
|
108 * @return string
|
Chris@0
|
109 */
|
Chris@0
|
110 public function getName()
|
Chris@0
|
111 {
|
Chris@0
|
112 return $this->data['Name'];
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Set the cookie name
|
Chris@0
|
117 *
|
Chris@0
|
118 * @param string $name Cookie name
|
Chris@0
|
119 */
|
Chris@0
|
120 public function setName($name)
|
Chris@0
|
121 {
|
Chris@0
|
122 $this->data['Name'] = $name;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Get the cookie value
|
Chris@0
|
127 *
|
Chris@0
|
128 * @return string
|
Chris@0
|
129 */
|
Chris@0
|
130 public function getValue()
|
Chris@0
|
131 {
|
Chris@0
|
132 return $this->data['Value'];
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Set the cookie value
|
Chris@0
|
137 *
|
Chris@0
|
138 * @param string $value Cookie value
|
Chris@0
|
139 */
|
Chris@0
|
140 public function setValue($value)
|
Chris@0
|
141 {
|
Chris@0
|
142 $this->data['Value'] = $value;
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Get the domain
|
Chris@0
|
147 *
|
Chris@0
|
148 * @return string|null
|
Chris@0
|
149 */
|
Chris@0
|
150 public function getDomain()
|
Chris@0
|
151 {
|
Chris@0
|
152 return $this->data['Domain'];
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * Set the domain of the cookie
|
Chris@0
|
157 *
|
Chris@0
|
158 * @param string $domain
|
Chris@0
|
159 */
|
Chris@0
|
160 public function setDomain($domain)
|
Chris@0
|
161 {
|
Chris@0
|
162 $this->data['Domain'] = $domain;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /**
|
Chris@0
|
166 * Get the path
|
Chris@0
|
167 *
|
Chris@0
|
168 * @return string
|
Chris@0
|
169 */
|
Chris@0
|
170 public function getPath()
|
Chris@0
|
171 {
|
Chris@0
|
172 return $this->data['Path'];
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * Set the path of the cookie
|
Chris@0
|
177 *
|
Chris@0
|
178 * @param string $path Path of the cookie
|
Chris@0
|
179 */
|
Chris@0
|
180 public function setPath($path)
|
Chris@0
|
181 {
|
Chris@0
|
182 $this->data['Path'] = $path;
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 /**
|
Chris@0
|
186 * Maximum lifetime of the cookie in seconds
|
Chris@0
|
187 *
|
Chris@0
|
188 * @return int|null
|
Chris@0
|
189 */
|
Chris@0
|
190 public function getMaxAge()
|
Chris@0
|
191 {
|
Chris@0
|
192 return $this->data['Max-Age'];
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * Set the max-age of the cookie
|
Chris@0
|
197 *
|
Chris@0
|
198 * @param int $maxAge Max age of the cookie in seconds
|
Chris@0
|
199 */
|
Chris@0
|
200 public function setMaxAge($maxAge)
|
Chris@0
|
201 {
|
Chris@0
|
202 $this->data['Max-Age'] = $maxAge;
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * The UNIX timestamp when the cookie Expires
|
Chris@0
|
207 *
|
Chris@0
|
208 * @return mixed
|
Chris@0
|
209 */
|
Chris@0
|
210 public function getExpires()
|
Chris@0
|
211 {
|
Chris@0
|
212 return $this->data['Expires'];
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * Set the unix timestamp for which the cookie will expire
|
Chris@0
|
217 *
|
Chris@0
|
218 * @param int $timestamp Unix timestamp
|
Chris@0
|
219 */
|
Chris@0
|
220 public function setExpires($timestamp)
|
Chris@0
|
221 {
|
Chris@0
|
222 $this->data['Expires'] = is_numeric($timestamp)
|
Chris@0
|
223 ? (int) $timestamp
|
Chris@0
|
224 : strtotime($timestamp);
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Get whether or not this is a secure cookie
|
Chris@0
|
229 *
|
Chris@0
|
230 * @return null|bool
|
Chris@0
|
231 */
|
Chris@0
|
232 public function getSecure()
|
Chris@0
|
233 {
|
Chris@0
|
234 return $this->data['Secure'];
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * Set whether or not the cookie is secure
|
Chris@0
|
239 *
|
Chris@0
|
240 * @param bool $secure Set to true or false if secure
|
Chris@0
|
241 */
|
Chris@0
|
242 public function setSecure($secure)
|
Chris@0
|
243 {
|
Chris@0
|
244 $this->data['Secure'] = $secure;
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * Get whether or not this is a session cookie
|
Chris@0
|
249 *
|
Chris@0
|
250 * @return null|bool
|
Chris@0
|
251 */
|
Chris@0
|
252 public function getDiscard()
|
Chris@0
|
253 {
|
Chris@0
|
254 return $this->data['Discard'];
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Set whether or not this is a session cookie
|
Chris@0
|
259 *
|
Chris@0
|
260 * @param bool $discard Set to true or false if this is a session cookie
|
Chris@0
|
261 */
|
Chris@0
|
262 public function setDiscard($discard)
|
Chris@0
|
263 {
|
Chris@0
|
264 $this->data['Discard'] = $discard;
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 /**
|
Chris@0
|
268 * Get whether or not this is an HTTP only cookie
|
Chris@0
|
269 *
|
Chris@0
|
270 * @return bool
|
Chris@0
|
271 */
|
Chris@0
|
272 public function getHttpOnly()
|
Chris@0
|
273 {
|
Chris@0
|
274 return $this->data['HttpOnly'];
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 /**
|
Chris@0
|
278 * Set whether or not this is an HTTP only cookie
|
Chris@0
|
279 *
|
Chris@0
|
280 * @param bool $httpOnly Set to true or false if this is HTTP only
|
Chris@0
|
281 */
|
Chris@0
|
282 public function setHttpOnly($httpOnly)
|
Chris@0
|
283 {
|
Chris@0
|
284 $this->data['HttpOnly'] = $httpOnly;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * Check if the cookie matches a path value.
|
Chris@0
|
289 *
|
Chris@0
|
290 * A request-path path-matches a given cookie-path if at least one of
|
Chris@0
|
291 * the following conditions holds:
|
Chris@0
|
292 *
|
Chris@0
|
293 * - The cookie-path and the request-path are identical.
|
Chris@0
|
294 * - The cookie-path is a prefix of the request-path, and the last
|
Chris@0
|
295 * character of the cookie-path is %x2F ("/").
|
Chris@0
|
296 * - The cookie-path is a prefix of the request-path, and the first
|
Chris@0
|
297 * character of the request-path that is not included in the cookie-
|
Chris@0
|
298 * path is a %x2F ("/") character.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @param string $requestPath Path to check against
|
Chris@0
|
301 *
|
Chris@0
|
302 * @return bool
|
Chris@0
|
303 */
|
Chris@0
|
304 public function matchesPath($requestPath)
|
Chris@0
|
305 {
|
Chris@0
|
306 $cookiePath = $this->getPath();
|
Chris@0
|
307
|
Chris@0
|
308 // Match on exact matches or when path is the default empty "/"
|
Chris@0
|
309 if ($cookiePath === '/' || $cookiePath == $requestPath) {
|
Chris@0
|
310 return true;
|
Chris@0
|
311 }
|
Chris@0
|
312
|
Chris@0
|
313 // Ensure that the cookie-path is a prefix of the request path.
|
Chris@0
|
314 if (0 !== strpos($requestPath, $cookiePath)) {
|
Chris@0
|
315 return false;
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 // Match if the last character of the cookie-path is "/"
|
Chris@0
|
319 if (substr($cookiePath, -1, 1) === '/') {
|
Chris@0
|
320 return true;
|
Chris@0
|
321 }
|
Chris@0
|
322
|
Chris@0
|
323 // Match if the first character not included in cookie path is "/"
|
Chris@0
|
324 return substr($requestPath, strlen($cookiePath), 1) === '/';
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 /**
|
Chris@0
|
328 * Check if the cookie matches a domain value
|
Chris@0
|
329 *
|
Chris@0
|
330 * @param string $domain Domain to check against
|
Chris@0
|
331 *
|
Chris@0
|
332 * @return bool
|
Chris@0
|
333 */
|
Chris@0
|
334 public function matchesDomain($domain)
|
Chris@0
|
335 {
|
Chris@0
|
336 // Remove the leading '.' as per spec in RFC 6265.
|
Chris@0
|
337 // http://tools.ietf.org/html/rfc6265#section-5.2.3
|
Chris@0
|
338 $cookieDomain = ltrim($this->getDomain(), '.');
|
Chris@0
|
339
|
Chris@0
|
340 // Domain not set or exact match.
|
Chris@0
|
341 if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
|
Chris@0
|
342 return true;
|
Chris@0
|
343 }
|
Chris@0
|
344
|
Chris@0
|
345 // Matching the subdomain according to RFC 6265.
|
Chris@0
|
346 // http://tools.ietf.org/html/rfc6265#section-5.1.3
|
Chris@0
|
347 if (filter_var($domain, FILTER_VALIDATE_IP)) {
|
Chris@0
|
348 return false;
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@13
|
351 return (bool) preg_match('/\.' . preg_quote($cookieDomain, '/') . '$/', $domain);
|
Chris@0
|
352 }
|
Chris@0
|
353
|
Chris@0
|
354 /**
|
Chris@0
|
355 * Check if the cookie is expired
|
Chris@0
|
356 *
|
Chris@0
|
357 * @return bool
|
Chris@0
|
358 */
|
Chris@0
|
359 public function isExpired()
|
Chris@0
|
360 {
|
Chris@13
|
361 return $this->getExpires() !== null && time() > $this->getExpires();
|
Chris@0
|
362 }
|
Chris@0
|
363
|
Chris@0
|
364 /**
|
Chris@0
|
365 * Check if the cookie is valid according to RFC 6265
|
Chris@0
|
366 *
|
Chris@0
|
367 * @return bool|string Returns true if valid or an error message if invalid
|
Chris@0
|
368 */
|
Chris@0
|
369 public function validate()
|
Chris@0
|
370 {
|
Chris@0
|
371 // Names must not be empty, but can be 0
|
Chris@0
|
372 $name = $this->getName();
|
Chris@0
|
373 if (empty($name) && !is_numeric($name)) {
|
Chris@0
|
374 return 'The cookie name must not be empty';
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 // Check if any of the invalid characters are present in the cookie name
|
Chris@0
|
378 if (preg_match(
|
Chris@0
|
379 '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',
|
Chris@13
|
380 $name
|
Chris@13
|
381 )) {
|
Chris@0
|
382 return 'Cookie name must not contain invalid characters: ASCII '
|
Chris@0
|
383 . 'Control characters (0-31;127), space, tab and the '
|
Chris@0
|
384 . 'following characters: ()<>@,;:\"/?={}';
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 // Value must not be empty, but can be 0
|
Chris@0
|
388 $value = $this->getValue();
|
Chris@0
|
389 if (empty($value) && !is_numeric($value)) {
|
Chris@0
|
390 return 'The cookie value must not be empty';
|
Chris@0
|
391 }
|
Chris@0
|
392
|
Chris@0
|
393 // Domains must not be empty, but can be 0
|
Chris@0
|
394 // A "0" is not a valid internet domain, but may be used as server name
|
Chris@0
|
395 // in a private network.
|
Chris@0
|
396 $domain = $this->getDomain();
|
Chris@0
|
397 if (empty($domain) && !is_numeric($domain)) {
|
Chris@0
|
398 return 'The cookie domain must not be empty';
|
Chris@0
|
399 }
|
Chris@0
|
400
|
Chris@0
|
401 return true;
|
Chris@0
|
402 }
|
Chris@0
|
403 }
|