Open Journal Systems  3.3.0
vendor/symfony/http-foundation/ResponseHeaderBag.php
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 
13 
19 class ResponseHeaderBag extends HeaderBag
20 {
21  const COOKIES_FLAT = 'flat';
22  const COOKIES_ARRAY = 'array';
23 
24  const DISPOSITION_ATTACHMENT = 'attachment';
25  const DISPOSITION_INLINE = 'inline';
26 
27  protected $computedCacheControl = [];
28  protected $cookies = [];
29  protected $headerNames = [];
30 
31  public function __construct(array $headers = [])
32  {
33  parent::__construct($headers);
34 
35  if (!isset($this->headers['cache-control'])) {
36  $this->set('Cache-Control', '');
37  }
38 
39  /* RFC2616 - 14.18 says all Responses need to have a Date */
40  if (!isset($this->headers['date'])) {
41  $this->initDate();
42  }
43  }
44 
50  public function allPreserveCase()
51  {
52  $headers = [];
53  foreach ($this->all() as $name => $value) {
54  $headers[$this->headerNames[$name] ?? $name] = $value;
55  }
56 
57  return $headers;
58  }
59 
61  {
62  $headers = $this->allPreserveCase();
63  if (isset($this->headerNames['set-cookie'])) {
64  unset($headers[$this->headerNames['set-cookie']]);
65  }
66 
67  return $headers;
68  }
69 
73  public function replace(array $headers = [])
74  {
75  $this->headerNames = [];
76 
77  parent::replace($headers);
78 
79  if (!isset($this->headers['cache-control'])) {
80  $this->set('Cache-Control', '');
81  }
82 
83  if (!isset($this->headers['date'])) {
84  $this->initDate();
85  }
86  }
87 
93  public function all(/*string $key = null*/)
94  {
95  $headers = parent::all();
96 
97  if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) {
98  $key = strtr($key, self::UPPER, self::LOWER);
99 
100  return 'set-cookie' !== $key ? $headers[$key] ?? [] : array_map('strval', $this->getCookies());
101  }
102 
103  foreach ($this->getCookies() as $cookie) {
104  $headers['set-cookie'][] = (string) $cookie;
105  }
106 
107  return $headers;
108  }
109 
113  public function set($key, $values, $replace = true)
114  {
115  $uniqueKey = strtr($key, self::UPPER, self::LOWER);
116 
117  if ('set-cookie' === $uniqueKey) {
118  if ($replace) {
119  $this->cookies = [];
120  }
121  foreach ((array) $values as $cookie) {
122  $this->setCookie(Cookie::fromString($cookie));
123  }
124  $this->headerNames[$uniqueKey] = $key;
125 
126  return;
127  }
128 
129  $this->headerNames[$uniqueKey] = $key;
130 
131  parent::set($key, $values, $replace);
132 
133  // ensure the cache-control header has sensible defaults
134  if (\in_array($uniqueKey, ['cache-control', 'etag', 'last-modified', 'expires'], true) && '' !== $computed = $this->computeCacheControlValue()) {
135  $this->headers['cache-control'] = [$computed];
136  $this->headerNames['cache-control'] = 'Cache-Control';
137  $this->computedCacheControl = $this->parseCacheControl($computed);
138  }
139  }
140 
144  public function remove($key)
145  {
146  $uniqueKey = strtr($key, self::UPPER, self::LOWER);
147  unset($this->headerNames[$uniqueKey]);
148 
149  if ('set-cookie' === $uniqueKey) {
150  $this->cookies = [];
151 
152  return;
153  }
154 
155  parent::remove($key);
156 
157  if ('cache-control' === $uniqueKey) {
158  $this->computedCacheControl = [];
159  }
160 
161  if ('date' === $uniqueKey) {
162  $this->initDate();
163  }
164  }
165 
169  public function hasCacheControlDirective($key)
170  {
171  return \array_key_exists($key, $this->computedCacheControl);
172  }
173 
177  public function getCacheControlDirective($key)
178  {
179  return \array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
180  }
181 
182  public function setCookie(Cookie $cookie)
183  {
184  $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
185  $this->headerNames['set-cookie'] = 'Set-Cookie';
186  }
187 
195  public function removeCookie($name, $path = '/', $domain = null)
196  {
197  if (null === $path) {
198  $path = '/';
199  }
200 
201  unset($this->cookies[$domain][$path][$name]);
202 
203  if (empty($this->cookies[$domain][$path])) {
204  unset($this->cookies[$domain][$path]);
205 
206  if (empty($this->cookies[$domain])) {
207  unset($this->cookies[$domain]);
208  }
209  }
210 
211  if (empty($this->cookies)) {
212  unset($this->headerNames['set-cookie']);
213  }
214  }
215 
225  public function getCookies($format = self::COOKIES_FLAT)
226  {
227  if (!\in_array($format, [self::COOKIES_FLAT, self::COOKIES_ARRAY])) {
228  throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', [self::COOKIES_FLAT, self::COOKIES_ARRAY])));
229  }
230 
231  if (self::COOKIES_ARRAY === $format) {
232  return $this->cookies;
233  }
234 
235  $flattenedCookies = [];
236  foreach ($this->cookies as $path) {
237  foreach ($path as $cookies) {
238  foreach ($cookies as $cookie) {
239  $flattenedCookies[] = $cookie;
240  }
241  }
242  }
243 
244  return $flattenedCookies;
245  }
246 
257  public function clearCookie($name, $path = '/', $domain = null, $secure = false, $httpOnly = true/*, $sameSite = null*/)
258  {
259  $sameSite = \func_num_args() > 5 ? func_get_arg(5) : null;
260 
261  $this->setCookie(new Cookie($name, null, 1, $path, $domain, $secure, $httpOnly, false, $sameSite));
262  }
263 
267  public function makeDisposition($disposition, $filename, $filenameFallback = '')
268  {
269  return HeaderUtils::makeDisposition((string) $disposition, (string) $filename, (string) $filenameFallback);
270  }
271 
280  protected function computeCacheControlValue()
281  {
282  if (!$this->cacheControl) {
283  if ($this->has('Last-Modified') || $this->has('Expires')) {
284  return 'private, must-revalidate'; // allows for heuristic expiration (RFC 7234 Section 4.2.2) in the case of "Last-Modified"
285  }
286 
287  // conservative by default
288  return 'no-cache, private';
289  }
290 
291  $header = $this->getCacheControlHeader();
292  if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
293  return $header;
294  }
295 
296  // public if s-maxage is defined, private otherwise
297  if (!isset($this->cacheControl['s-maxage'])) {
298  return $header.', private';
299  }
300 
301  return $header;
302  }
303 
304  private function initDate(): void
305  {
306  $now = \DateTime::createFromFormat('U', time());
307  $now->setTimezone(new \DateTimeZone('UTC'));
308  $this->set('Date', $now->format('D, d M Y H:i:s').' GMT');
309  }
310 }
Symfony\Component\HttpFoundation\HeaderBag\getCacheControlHeader
getCacheControlHeader()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:289
Symfony\Component\HttpFoundation\ResponseHeaderBag\allPreserveCase
allPreserveCase()
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:50
Symfony\Component\HttpFoundation\ResponseHeaderBag\makeDisposition
makeDisposition($disposition, $filename, $filenameFallback='')
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:267
Symfony\Component\HttpFoundation\ResponseHeaderBag\__construct
__construct(array $headers=[])
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:31
Symfony\Component\HttpFoundation\ResponseHeaderBag\replace
replace(array $headers=[])
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:73
Symfony\Component\HttpFoundation\Cookie\getPath
getPath()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:237
Symfony\Component\HttpFoundation\HeaderBag\$headers
$headers
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:21
Symfony\Component\HttpFoundation\ResponseHeaderBag\removeCookie
removeCookie($name, $path='/', $domain=null)
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:195
Symfony\Component\HttpFoundation\ResponseHeaderBag\getCookies
getCookies($format=self::COOKIES_FLAT)
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:234
Symfony\Component\HttpFoundation\ResponseHeaderBag\clearCookie
clearCookie($name, $path='/', $domain=null, $secure=false, $httpOnly=true)
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:257
Symfony\Component\HttpFoundation\ResponseHeaderBag\all
all()
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:107
Symfony\Component\HttpFoundation\ResponseHeaderBag\COOKIES_ARRAY
const COOKIES_ARRAY
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:22
Symfony\Component\HttpFoundation\ResponseHeaderBag\DISPOSITION_INLINE
const DISPOSITION_INLINE
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:25
Symfony\Component\HttpFoundation\ResponseHeaderBag\$computedCacheControl
$computedCacheControl
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:33
Symfony\Component\HttpFoundation\ResponseHeaderBag\$headerNames
$headerNames
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:49
Symfony\Component\HttpFoundation\ResponseHeaderBag\allPreserveCaseWithoutCookies
allPreserveCaseWithoutCookies()
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:60
Symfony\Component\HttpFoundation\Cookie\getDomain
getDomain()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:207
Symfony\Component\HttpFoundation\ResponseHeaderBag\DISPOSITION_ATTACHMENT
const DISPOSITION_ATTACHMENT
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:24
Symfony\Component\HttpFoundation\ResponseHeaderBag\$cookies
$cookies
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:41
Symfony\Component\HttpFoundation\Cookie\fromString
static fromString($cookie, $decode=false)
Definition: lib/vendor/symfony/http-foundation/Cookie.php:42
Symfony\Component\HttpFoundation\ResponseHeaderBag\getCacheControlDirective
getCacheControlDirective($key)
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:177
Symfony\Component\HttpFoundation\HeaderBag\parseCacheControl
parseCacheControl($header)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:315
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\Cookie
Definition: lib/vendor/symfony/http-foundation/Cookie.php:19
Symfony\Component\HttpFoundation\ResponseHeaderBag\hasCacheControlDirective
hasCacheControlDirective($key)
Definition: vendor/symfony/http-foundation/ResponseHeaderBag.php:169
Symfony\Component\HttpFoundation\ResponseHeaderBag\computeCacheControlValue
computeCacheControlValue()
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:327
Symfony\Component\HttpFoundation\ResponseHeaderBag\COOKIES_FLAT
const COOKIES_FLAT
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:21
Symfony\Component\HttpFoundation\ResponseHeaderBag\setCookie
setCookie(Cookie $cookie)
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:191
Symfony\Component\HttpFoundation\Cookie\getName
getName()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:187
Symfony\Component\HttpFoundation\HeaderBag\has
has($key)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:163
Symfony\Component\HttpFoundation\HeaderUtils\makeDisposition
static makeDisposition(string $disposition, string $filename, string $filenameFallback='')
Definition: HeaderUtils.php:163