comparison vendor/symfony/browser-kit/Cookie.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\BrowserKit;
13
14 /**
15 * Cookie represents an HTTP cookie.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19 class Cookie
20 {
21 /**
22 * Handles dates as defined by RFC 2616 section 3.3.1, and also some other
23 * non-standard, but common formats.
24 *
25 * @var array
26 */
27 private static $dateFormats = array(
28 'D, d M Y H:i:s T',
29 'D, d-M-y H:i:s T',
30 'D, d-M-Y H:i:s T',
31 'D, d-m-y H:i:s T',
32 'D, d-m-Y H:i:s T',
33 'D M j G:i:s Y',
34 'D M d H:i:s Y T',
35 );
36
37 protected $name;
38 protected $value;
39 protected $expires;
40 protected $path;
41 protected $domain;
42 protected $secure;
43 protected $httponly;
44 protected $rawValue;
45
46 /**
47 * Sets a cookie.
48 *
49 * @param string $name The cookie name
50 * @param string $value The value of the cookie
51 * @param string $expires The time the cookie expires
52 * @param string $path The path on the server in which the cookie will be available on
53 * @param string $domain The domain that the cookie is available
54 * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
55 * @param bool $httponly The cookie httponly flag
56 * @param bool $encodedValue Whether the value is encoded or not
57 */
58 public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false)
59 {
60 if ($encodedValue) {
61 $this->value = urldecode($value);
62 $this->rawValue = $value;
63 } else {
64 $this->value = $value;
65 $this->rawValue = urlencode($value);
66 }
67 $this->name = $name;
68 $this->path = empty($path) ? '/' : $path;
69 $this->domain = $domain;
70 $this->secure = (bool) $secure;
71 $this->httponly = (bool) $httponly;
72
73 if (null !== $expires) {
74 $timestampAsDateTime = \DateTime::createFromFormat('U', $expires);
75 if (false === $timestampAsDateTime) {
76 throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.', $expires));
77 }
78
79 $this->expires = $timestampAsDateTime->format('U');
80 }
81 }
82
83 /**
84 * Returns the HTTP representation of the Cookie.
85 *
86 * @return string The HTTP representation of the Cookie
87 *
88 * @throws \UnexpectedValueException
89 */
90 public function __toString()
91 {
92 $cookie = sprintf('%s=%s', $this->name, $this->rawValue);
93
94 if (null !== $this->expires) {
95 $dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT'));
96 $cookie .= '; expires='.str_replace('+0000', '', $dateTime->format(self::$dateFormats[0]));
97 }
98
99 if ('' !== $this->domain) {
100 $cookie .= '; domain='.$this->domain;
101 }
102
103 if ($this->path) {
104 $cookie .= '; path='.$this->path;
105 }
106
107 if ($this->secure) {
108 $cookie .= '; secure';
109 }
110
111 if ($this->httponly) {
112 $cookie .= '; httponly';
113 }
114
115 return $cookie;
116 }
117
118 /**
119 * Creates a Cookie instance from a Set-Cookie header value.
120 *
121 * @param string $cookie A Set-Cookie header value
122 * @param string $url The base URL
123 *
124 * @return static
125 *
126 * @throws \InvalidArgumentException
127 */
128 public static function fromString($cookie, $url = null)
129 {
130 $parts = explode(';', $cookie);
131
132 if (false === strpos($parts[0], '=')) {
133 throw new \InvalidArgumentException(sprintf('The cookie string "%s" is not valid.', $parts[0]));
134 }
135
136 list($name, $value) = explode('=', array_shift($parts), 2);
137
138 $values = array(
139 'name' => trim($name),
140 'value' => trim($value),
141 'expires' => null,
142 'path' => '/',
143 'domain' => '',
144 'secure' => false,
145 'httponly' => false,
146 'passedRawValue' => true,
147 );
148
149 if (null !== $url) {
150 if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host'])) {
151 throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
152 }
153
154 $values['domain'] = $urlParts['host'];
155 $values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
156 }
157
158 foreach ($parts as $part) {
159 $part = trim($part);
160
161 if ('secure' === strtolower($part)) {
162 // Ignore the secure flag if the original URI is not given or is not HTTPS
163 if (!$url || !isset($urlParts['scheme']) || 'https' != $urlParts['scheme']) {
164 continue;
165 }
166
167 $values['secure'] = true;
168
169 continue;
170 }
171
172 if ('httponly' === strtolower($part)) {
173 $values['httponly'] = true;
174
175 continue;
176 }
177
178 if (2 === count($elements = explode('=', $part, 2))) {
179 if ('expires' === strtolower($elements[0])) {
180 $elements[1] = self::parseDate($elements[1]);
181 }
182
183 $values[strtolower($elements[0])] = $elements[1];
184 }
185 }
186
187 return new static(
188 $values['name'],
189 $values['value'],
190 $values['expires'],
191 $values['path'],
192 $values['domain'],
193 $values['secure'],
194 $values['httponly'],
195 $values['passedRawValue']
196 );
197 }
198
199 private static function parseDate($dateValue)
200 {
201 // trim single quotes around date if present
202 if (($length = strlen($dateValue)) > 1 && "'" === $dateValue[0] && "'" === $dateValue[$length - 1]) {
203 $dateValue = substr($dateValue, 1, -1);
204 }
205
206 foreach (self::$dateFormats as $dateFormat) {
207 if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) {
208 return $date->format('U');
209 }
210 }
211
212 // attempt a fallback for unusual formatting
213 if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) {
214 return $date->format('U');
215 }
216 }
217
218 /**
219 * Gets the name of the cookie.
220 *
221 * @return string The cookie name
222 */
223 public function getName()
224 {
225 return $this->name;
226 }
227
228 /**
229 * Gets the value of the cookie.
230 *
231 * @return string The cookie value
232 */
233 public function getValue()
234 {
235 return $this->value;
236 }
237
238 /**
239 * Gets the raw value of the cookie.
240 *
241 * @return string The cookie value
242 */
243 public function getRawValue()
244 {
245 return $this->rawValue;
246 }
247
248 /**
249 * Gets the expires time of the cookie.
250 *
251 * @return string The cookie expires time
252 */
253 public function getExpiresTime()
254 {
255 return $this->expires;
256 }
257
258 /**
259 * Gets the path of the cookie.
260 *
261 * @return string The cookie path
262 */
263 public function getPath()
264 {
265 return $this->path;
266 }
267
268 /**
269 * Gets the domain of the cookie.
270 *
271 * @return string The cookie domain
272 */
273 public function getDomain()
274 {
275 return $this->domain;
276 }
277
278 /**
279 * Returns the secure flag of the cookie.
280 *
281 * @return bool The cookie secure flag
282 */
283 public function isSecure()
284 {
285 return $this->secure;
286 }
287
288 /**
289 * Returns the httponly flag of the cookie.
290 *
291 * @return bool The cookie httponly flag
292 */
293 public function isHttpOnly()
294 {
295 return $this->httponly;
296 }
297
298 /**
299 * Returns true if the cookie has expired.
300 *
301 * @return bool true if the cookie has expired, false otherwise
302 */
303 public function isExpired()
304 {
305 return null !== $this->expires && 0 != $this->expires && $this->expires < time();
306 }
307 }