‪TYPO3CMS  9.5
CookieHeaderTrait.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 namespace ‪TYPO3\CMS\Core\Http;
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 use Symfony\Component\HttpFoundation\Cookie;
19 
21 {
22  private function ‪hasSameSiteCookieSupport(): bool
23  {
24  return version_compare(PHP_VERSION, '7.3.0', '>=');
25  }
26 
34  private function ‪resendCookieHeader(array $cookieNames = []): void
35  {
36  $cookies = array_filter(headers_list(), function (string $header) {
37  return stripos($header, 'Set-Cookie:') === 0;
38  });
39  $cookies = array_map(function (string $cookieHeader) use ($cookieNames) {
40  $payload = ltrim(substr($cookieHeader, 11));
41  $cookie = Cookie::fromString($payload);
42  $sameSite = $cookie->getSameSite();
43  // adjust SameSite flag only for given cookie names (applied to all if not declared)
44  if (empty($cookieNames) || in_array($cookie->getName(), $cookieNames, true)) {
45  $sameSite = $sameSite ?? Cookie::SAMESITE_STRICT;
46  }
47  return (string)Cookie::create(
48  $cookie->getName(),
49  $cookie->getValue(),
50  $cookie->getExpiresTime(),
51  $cookie->getPath(),
52  $cookie->getDomain(),
53  $cookie->isSecure(),
54  $cookie->isHttpOnly(),
55  $cookie->isRaw(),
56  $sameSite
57  );
58  }, $cookies);
59  if (!empty($cookies)) {
60  header_remove('Set-Cookie');
61  foreach ($cookies as $cookie) {
62  header('Set-Cookie: ' . $cookie, false);
63  }
64  }
65  }
66 
67  private function ‪sanitizeSameSiteCookieValue(string $cookieSameSite): string
68  {
69  if (!in_array($cookieSameSite, [Cookie::SAMESITE_STRICT, Cookie::SAMESITE_LAX, Cookie::SAMESITE_NONE], true)) {
70  $cookieSameSite = Cookie::SAMESITE_STRICT;
71  }
72  return $cookieSameSite;
73  }
74 }
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:3