Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
SetCookie.php
001 <?php
002 namespace GuzzleHttp\Cookie;
003
004 /**
005 * Set-Cookie object
006 */
007 class SetCookie
008 {
009 /** @var array */
010 private static $defaults = [
011 'Name' => null,
012 'Value' => null,
013 'Domain' => null,
014 'Path' => '/',
015 'Max-Age' => null,
016 'Expires' => null,
017 'Secure' => false,
018 'Discard' => false,
019 'HttpOnly' => false
020 ];
021
022 /** @var array Cookie data */
023 private $data;
024
025 /**
026 * Create a new SetCookie object from a string
027 *
028 * @param string $cookie Set-Cookie header string
029 *
030 * @return self
031 */
032 public static function fromString($cookie)
033 {
034 // Create the default return array
035 $data = self::$defaults;
036 // Explode the cookie string using a series of semicolons
037 $pieces = array_filter(array_map('trim', explode(';', $cookie)));
038 // The name of the cookie (first kvp) must exist and include an equal sign.
039 if (empty($pieces[0]) || !strpos($pieces[0], '=')) {
040 return new self($data);
041 }
042
043 // Add the cookie pieces into the parsed data array
044 foreach ($pieces as $part) {
045 $cookieParts = explode('=', $part, 2);
046 $key = trim($cookieParts[0]);
047 $value = isset($cookieParts[1])
048 ? trim($cookieParts[1], " \n\r\t\0\x0B")
049 : true;
050
051 // Only check for non-cookies when cookies have been found
052 if (empty($data['Name'])) {
053 $data['Name'] = $key;
054 $data['Value'] = $value;
055 } else {
056 foreach (array_keys(self::$defaults) as $search) {
057 if (!strcasecmp($search, $key)) {
058 $data[$search] = $value;
059 continue 2;
060 }
061 }
062 $data[$key] = $value;
063 }
064 }
065
066 return new self($data);
067 }
068
069 /**
070 * @param array $data Array of cookie data provided by a Cookie parser
071 */
072 public function __construct(array $data = [])
073 {
074 $this->data = array_replace(self::$defaults, $data);
075 // Extract the Expires value and turn it into a UNIX timestamp if needed
076 if (!$this->getExpires() && $this->getMaxAge()) {
077 // Calculate the Expires date
078 $this->setExpires(time() + $this->getMaxAge());
079 } elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
080 $this->setExpires($this->getExpires());
081 }
082 }
083
084 public function __toString()
085 {
086 $str = $this->data['Name'] . '=' . $this->data['Value'] . '; ';
087 foreach ($this->data as $k => $v) {
088 if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) {
089 if ($k === 'Expires') {
090 $str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; ';
091 } else {
092 $str .= ($v === true ? $k : "{$k}={$v}") . '; ';
093 }
094 }
095 }
096
097 return rtrim($str, '; ');
098 }
099
100 public function toArray()
101 {
102 return $this->data;
103 }
104
105 /**
106 * Get the cookie name
107 *
108 * @return string
109 */
110 public function getName()
111 {
112 return $this->data['Name'];
113 }
114
115 /**
116 * Set the cookie name
117 *
118 * @param string $name Cookie name
119 */
120 public function setName($name)
121 {
122 $this->data['Name'] = $name;
123 }
124
125 /**
126 * Get the cookie value
127 *
128 * @return string
129 */
130 public function getValue()
131 {
132 return $this->data['Value'];
133 }
134
135 /**
136 * Set the cookie value
137 *
138 * @param string $value Cookie value
139 */
140 public function setValue($value)
141 {
142 $this->data['Value'] = $value;
143 }
144
145 /**
146 * Get the domain
147 *
148 * @return string|null
149 */
150 public function getDomain()
151 {
152 return $this->data['Domain'];
153 }
154
155 /**
156 * Set the domain of the cookie
157 *
158 * @param string $domain
159 */
160 public function setDomain($domain)
161 {
162 $this->data['Domain'] = $domain;
163 }
164
165 /**
166 * Get the path
167 *
168 * @return string
169 */
170 public function getPath()
171 {
172 return $this->data['Path'];
173 }
174
175 /**
176 * Set the path of the cookie
177 *
178 * @param string $path Path of the cookie
179 */
180 public function setPath($path)
181 {
182 $this->data['Path'] = $path;
183 }
184
185 /**
186 * Maximum lifetime of the cookie in seconds
187 *
188 * @return int|null
189 */
190 public function getMaxAge()
191 {
192 return $this->data['Max-Age'];
193 }
194
195 /**
196 * Set the max-age of the cookie
197 *
198 * @param int $maxAge Max age of the cookie in seconds
199 */
200 public function setMaxAge($maxAge)
201 {
202 $this->data['Max-Age'] = $maxAge;
203 }
204
205 /**
206 * The UNIX timestamp when the cookie Expires
207 *
208 * @return mixed
209 */
210 public function getExpires()
211 {
212 return $this->data['Expires'];
213 }
214
215 /**
216 * Set the unix timestamp for which the cookie will expire
217 *
218 * @param int $timestamp Unix timestamp
219 */
220 public function setExpires($timestamp)
221 {
222 $this->data['Expires'] = is_numeric($timestamp)
223 ? (int) $timestamp
224 : strtotime($timestamp);
225 }
226
227 /**
228 * Get whether or not this is a secure cookie
229 *
230 * @return bool|null
231 */
232 public function getSecure()
233 {
234 return $this->data['Secure'];
235 }
236
237 /**
238 * Set whether or not the cookie is secure
239 *
240 * @param bool $secure Set to true or false if secure
241 */
242 public function setSecure($secure)
243 {
244 $this->data['Secure'] = $secure;
245 }
246
247 /**
248 * Get whether or not this is a session cookie
249 *
250 * @return bool|null
251 */
252 public function getDiscard()
253 {
254 return $this->data['Discard'];
255 }
256
257 /**
258 * Set whether or not this is a session cookie
259 *
260 * @param bool $discard Set to true or false if this is a session cookie
261 */
262 public function setDiscard($discard)
263 {
264 $this->data['Discard'] = $discard;
265 }
266
267 /**
268 * Get whether or not this is an HTTP only cookie
269 *
270 * @return bool
271 */
272 public function getHttpOnly()
273 {
274 return $this->data['HttpOnly'];
275 }
276
277 /**
278 * Set whether or not this is an HTTP only cookie
279 *
280 * @param bool $httpOnly Set to true or false if this is HTTP only
281 */
282 public function setHttpOnly($httpOnly)
283 {
284 $this->data['HttpOnly'] = $httpOnly;
285 }
286
287 /**
288 * Check if the cookie matches a path value.
289 *
290 * A request-path path-matches a given cookie-path if at least one of
291 * the following conditions holds:
292 *
293 * - The cookie-path and the request-path are identical.
294 * - The cookie-path is a prefix of the request-path, and the last
295 * character of the cookie-path is %x2F ("/").
296 * - The cookie-path is a prefix of the request-path, and the first
297 * character of the request-path that is not included in the cookie-
298 * path is a %x2F ("/") character.
299 *
300 * @param string $requestPath Path to check against
301 *
302 * @return bool
303 */
304 public function matchesPath($requestPath)
305 {
306 $cookiePath = $this->getPath();
307
308 // Match on exact matches or when path is the default empty "/"
309 if ($cookiePath === '/' || $cookiePath == $requestPath) {
310 return true;
311 }
312
313 // Ensure that the cookie-path is a prefix of the request path.
314 if (0 !== strpos($requestPath, $cookiePath)) {
315 return false;
316 }
317
318 // Match if the last character of the cookie-path is "/"
319 if (substr($cookiePath, -1, 1) === '/') {
320 return true;
321 }
322
323 // Match if the first character not included in cookie path is "/"
324 return substr($requestPath, strlen($cookiePath), 1) === '/';
325 }
326
327 /**
328 * Check if the cookie matches a domain value
329 *
330 * @param string $domain Domain to check against
331 *
332 * @return bool
333 */
334 public function matchesDomain($domain)
335 {
336 $cookieDomain = $this->getDomain();
337 if (null === $cookieDomain) {
338 return true;
339 }
340
341 // Remove the leading '.' as per spec in RFC 6265.
342 // http://tools.ietf.org/html/rfc6265#section-5.2.3
343 $cookieDomain = ltrim(strtolower($cookieDomain), '.');
344
345 $domain = strtolower($domain);
346
347 // Domain not set or exact match.
348 if ('' === $cookieDomain || $domain === $cookieDomain) {
349 return true;
350 }
351
352 // Matching the subdomain according to RFC 6265.
353 // http://tools.ietf.org/html/rfc6265#section-5.1.3
354 if (filter_var($domain, FILTER_VALIDATE_IP)) {
355 return false;
356 }
357
358 return (bool) preg_match('/\.' . preg_quote($cookieDomain, '/') . '$/', $domain);
359 }
360
361 /**
362 * Check if the cookie is expired
363 *
364 * @return bool
365 */
366 public function isExpired()
367 {
368 return $this->getExpires() !== null && time() > $this->getExpires();
369 }
370
371 /**
372 * Check if the cookie is valid according to RFC 6265
373 *
374 * @return bool|string Returns true if valid or an error message if invalid
375 */
376 public function validate()
377 {
378 // Names must not be empty, but can be 0
379 $name = $this->getName();
380 if (empty($name) && !is_numeric($name)) {
381 return 'The cookie name must not be empty';
382 }
383
384 // Check if any of the invalid characters are present in the cookie name
385 if (preg_match(
386 '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',
387 $name
388 )) {
389 return 'Cookie name must not contain invalid characters: ASCII '
390 . 'Control characters (0-31;127), space, tab and the '
391 . 'following characters: ()<>@,;:\"/?={}';
392 }
393
394 // Value must not be empty, but can be 0
395 $value = $this->getValue();
396 if (empty($value) && !is_numeric($value)) {
397 return 'The cookie value must not be empty';
398 }
399
400 // Domains must not be empty, but can be 0
401 // A "0" is not a valid internet domain, but may be used as server name
402 // in a private network.
403 $domain = $this->getDomain();
404 if (empty($domain) && !is_numeric($domain)) {
405 return 'The cookie domain must not be empty';
406 }
407
408 return true;
409 }
410 }
411