‪TYPO3CMS  10.4
CookieHeaderTrait.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use Symfony\Component\HttpFoundation\Cookie;
21 
23 {
24  private function ‪hasSameSiteCookieSupport(): bool
25  {
26  return version_compare(PHP_VERSION, '7.3.0', '>=');
27  }
28 
36  private function ‪resendCookieHeader(array $cookieNames = []): void
37  {
38  $cookies = array_filter(headers_list(), function (string $header) {
39  return stripos($header, 'Set-Cookie:') === 0;
40  });
41  $cookies = array_map(function (string $cookieHeader) use ($cookieNames) {
42  $payload = ltrim(substr($cookieHeader, 11));
43  $cookie = Cookie::fromString($payload);
44  $sameSite = $cookie->getSameSite();
45  // adjust SameSite flag only for given cookie names (applied to all if not declared)
46  if (empty($cookieNames) || in_array($cookie->getName(), $cookieNames, true)) {
47  $sameSite = $sameSite ?? Cookie::SAMESITE_STRICT;
48  }
49  return (string)Cookie::create(
50  $cookie->getName(),
51  $cookie->getValue(),
52  $cookie->getExpiresTime(),
53  $cookie->getPath(),
54  $cookie->getDomain(),
55  $cookie->isSecure(),
56  $cookie->isHttpOnly(),
57  $cookie->isRaw(),
58  $sameSite
59  );
60  }, $cookies);
61  if (!empty($cookies)) {
62  header_remove('Set-Cookie');
63  foreach ($cookies as $cookie) {
64  header('Set-Cookie: ' . $cookie, false);
65  }
66  }
67  }
68 
69  private function ‪sanitizeSameSiteCookieValue(string $cookieSameSite): string
70  {
71  if (!in_array($cookieSameSite, [Cookie::SAMESITE_STRICT, Cookie::SAMESITE_LAX, Cookie::SAMESITE_NONE], true)) {
72  $cookieSameSite = Cookie::SAMESITE_STRICT;
73  }
74  return $cookieSameSite;
75  }
76 }
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18