Open Journal Systems  3.3.0
lib/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 
20 {
21  const COOKIES_FLAT = 'flat';
22  const COOKIES_ARRAY = 'array';
23 
24  const DISPOSITION_ATTACHMENT = 'attachment';
25  const DISPOSITION_INLINE = 'inline';
26 
30  protected $computedCacheControl = array();
31 
35  protected $cookies = array();
36 
40  protected $headerNames = array();
41 
47  public function __construct(array $headers = array())
48  {
49  parent::__construct($headers);
50 
51  if (!isset($this->headers['cache-control'])) {
52  $this->set('Cache-Control', '');
53  }
54  }
55 
61  public function allPreserveCase()
62  {
63  $headers = array();
64  foreach ($this->all() as $name => $value) {
65  $headers[isset($this->headerNames[$name]) ? $this->headerNames[$name] : $name] = $value;
66  }
67 
68  return $headers;
69  }
70 
71  public function allPreserveCaseWithoutCookies()
72  {
73  $headers = $this->allPreserveCase();
74  if (isset($this->headerNames['set-cookie'])) {
75  unset($headers[$this->headerNames['set-cookie']]);
76  }
77 
78  return $headers;
79  }
80 
84  public function replace(array $headers = array())
85  {
86  $this->headerNames = array();
87 
88  parent::replace($headers);
89 
90  if (!isset($this->headers['cache-control'])) {
91  $this->set('Cache-Control', '');
92  }
93  }
94 
98  public function all()
99  {
100  $headers = parent::all();
101  foreach ($this->getCookies() as $cookie) {
102  $headers['set-cookie'][] = (string) $cookie;
103  }
104 
105  return $headers;
106  }
107 
111  public function set($key, $values, $replace = true)
112  {
113  $uniqueKey = str_replace('_', '-', strtolower($key));
114 
115  if ('set-cookie' === $uniqueKey) {
116  if ($replace) {
117  $this->cookies = array();
118  }
119  foreach ((array) $values as $cookie) {
120  $this->setCookie(Cookie::fromString($cookie));
121  }
122  $this->headerNames[$uniqueKey] = $key;
123 
124  return;
125  }
126 
127  $this->headerNames[$uniqueKey] = $key;
128 
129  parent::set($key, $values, $replace);
130 
131  // ensure the cache-control header has sensible defaults
132  if (in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'))) {
133  $computed = $this->computeCacheControlValue();
134  $this->headers['cache-control'] = array($computed);
135  $this->headerNames['cache-control'] = 'Cache-Control';
136  $this->computedCacheControl = $this->parseCacheControl($computed);
137  }
138  }
139 
143  public function remove($key)
144  {
145  $uniqueKey = str_replace('_', '-', strtolower($key));
146  unset($this->headerNames[$uniqueKey]);
147 
148  if ('set-cookie' === $uniqueKey) {
149  $this->cookies = array();
150 
151  return;
152  }
153 
154  parent::remove($key);
155 
156  if ('cache-control' === $uniqueKey) {
157  $this->computedCacheControl = array();
158  }
159  }
160 
164  public function hasCacheControlDirective($key)
165  {
166  return array_key_exists($key, $this->computedCacheControl);
167  }
168 
172  public function getCacheControlDirective($key)
173  {
174  return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
175  }
176 
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, array(self::COOKIES_FLAT, self::COOKIES_ARRAY))) {
228  throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', array(self::COOKIES_FLAT, self::COOKIES_ARRAY))));
229  }
230 
231  if (self::COOKIES_ARRAY === $format) {
232  return $this->cookies;
233  }
234 
235  $flattenedCookies = array();
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 
256  public function clearCookie($name, $path = '/', $domain = null, $secure = false, $httpOnly = true)
257  {
258  $this->setCookie(new Cookie($name, null, 1, $path, $domain, $secure, $httpOnly));
259  }
260 
276  public function makeDisposition($disposition, $filename, $filenameFallback = '')
277  {
278  if (!in_array($disposition, array(self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE))) {
279  throw new \InvalidArgumentException(sprintf('The disposition must be either "%s" or "%s".', self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE));
280  }
281 
282  if ('' == $filenameFallback) {
283  $filenameFallback = $filename;
284  }
285 
286  // filenameFallback is not ASCII.
287  if (!preg_match('/^[\x20-\x7e]*$/', $filenameFallback)) {
288  throw new \InvalidArgumentException('The filename fallback must only contain ASCII characters.');
289  }
290 
291  // percent characters aren't safe in fallback.
292  if (false !== strpos($filenameFallback, '%')) {
293  throw new \InvalidArgumentException('The filename fallback cannot contain the "%" character.');
294  }
295 
296  // path separators aren't allowed in either.
297  if (false !== strpos($filename, '/') || false !== strpos($filename, '\\') || false !== strpos($filenameFallback, '/') || false !== strpos($filenameFallback, '\\')) {
298  throw new \InvalidArgumentException('The filename and the fallback cannot contain the "/" and "\\" characters.');
299  }
300 
301  $output = sprintf('%s; filename="%s"', $disposition, str_replace('"', '\\"', $filenameFallback));
302 
303  if ($filename !== $filenameFallback) {
304  $output .= sprintf("; filename*=utf-8''%s", rawurlencode($filename));
305  }
306 
307  return $output;
308  }
309 
318  protected function computeCacheControlValue()
319  {
320  if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
321  return 'no-cache, private';
322  }
323 
324  if (!$this->cacheControl) {
325  // conservative by default
326  return 'private, must-revalidate';
327  }
328 
329  $header = $this->getCacheControlHeader();
330  if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
331  return $header;
332  }
333 
334  // public if s-maxage is defined, private otherwise
335  if (!isset($this->cacheControl['s-maxage'])) {
336  return $header.', private';
337  }
338 
339  return $header;
340  }
341 }
Symfony\Component\HttpFoundation\HeaderBag\getCacheControlHeader
getCacheControlHeader()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:289
Symfony\Component\HttpFoundation\ResponseHeaderBag\allPreserveCase
allPreserveCase()
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:70
Symfony\Component\HttpFoundation\ResponseHeaderBag\makeDisposition
makeDisposition($disposition, $filename, $filenameFallback='')
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:285
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: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:204
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: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:265
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\replace
replace(array $headers=array())
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:93
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: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:80
Symfony\Component\HttpFoundation\Cookie\getDomain
getDomain()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:207
Symfony\Component\HttpFoundation\ResponseHeaderBag
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:19
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: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:181
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: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:173
Symfony\Component\HttpFoundation\HeaderBag
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:19
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\ResponseHeaderBag\__construct
__construct(array $headers=array())
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:56