[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/ -> ResponseHeaderBag.php (source)

   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  
  12  namespace Symfony\Component\HttpFoundation;
  13  
  14  /**
  15   * ResponseHeaderBag is a container for Response HTTP headers.
  16   *
  17   * @author Fabien Potencier <fabien@symfony.com>
  18   */
  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 = array();
  28      protected $cookies = array();
  29      protected $headerNames = array();
  30  
  31      public function __construct(array $headers = array())
  32      {
  33          parent::__construct($headers);
  34  
  35          if (!isset($this->headers['cache-control'])) {
  36              $this->set('Cache-Control', '');
  37          }
  38      }
  39  
  40      /**
  41       * {@inheritdoc}
  42       */
  43      public function __toString()
  44      {
  45          $cookies = '';
  46          foreach ($this->getCookies() as $cookie) {
  47              $cookies .= 'Set-Cookie: '.$cookie."\r\n";
  48          }
  49  
  50          ksort($this->headerNames);
  51  
  52          return parent::__toString().$cookies;
  53      }
  54  
  55      /**
  56       * Returns the headers, with original capitalizations.
  57       *
  58       * @return array An array of headers
  59       */
  60      public function allPreserveCase()
  61      {
  62          return array_combine($this->headerNames, $this->headers);
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68      public function replace(array $headers = array())
  69      {
  70          $this->headerNames = array();
  71  
  72          parent::replace($headers);
  73  
  74          if (!isset($this->headers['cache-control'])) {
  75              $this->set('Cache-Control', '');
  76          }
  77      }
  78  
  79      /**
  80       * {@inheritdoc}
  81       */
  82      public function set($key, $values, $replace = true)
  83      {
  84          parent::set($key, $values, $replace);
  85  
  86          $uniqueKey = str_replace('_', '-', strtolower($key));
  87          $this->headerNames[$uniqueKey] = $key;
  88  
  89          // ensure the cache-control header has sensible defaults
  90          if (\in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'))) {
  91              $computed = $this->computeCacheControlValue();
  92              $this->headers['cache-control'] = array($computed);
  93              $this->headerNames['cache-control'] = 'Cache-Control';
  94              $this->computedCacheControl = $this->parseCacheControl($computed);
  95          }
  96      }
  97  
  98      /**
  99       * {@inheritdoc}
 100       */
 101      public function remove($key)
 102      {
 103          parent::remove($key);
 104  
 105          $uniqueKey = str_replace('_', '-', strtolower($key));
 106          unset($this->headerNames[$uniqueKey]);
 107  
 108          if ('cache-control' === $uniqueKey) {
 109              $this->computedCacheControl = array();
 110          }
 111      }
 112  
 113      /**
 114       * {@inheritdoc}
 115       */
 116      public function hasCacheControlDirective($key)
 117      {
 118          return array_key_exists($key, $this->computedCacheControl);
 119      }
 120  
 121      /**
 122       * {@inheritdoc}
 123       */
 124      public function getCacheControlDirective($key)
 125      {
 126          return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
 127      }
 128  
 129      public function setCookie(Cookie $cookie)
 130      {
 131          $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
 132      }
 133  
 134      /**
 135       * Removes a cookie from the array, but does not unset it in the browser.
 136       *
 137       * @param string $name
 138       * @param string $path
 139       * @param string $domain
 140       */
 141      public function removeCookie($name, $path = '/', $domain = null)
 142      {
 143          if (null === $path) {
 144              $path = '/';
 145          }
 146  
 147          unset($this->cookies[$domain][$path][$name]);
 148  
 149          if (empty($this->cookies[$domain][$path])) {
 150              unset($this->cookies[$domain][$path]);
 151  
 152              if (empty($this->cookies[$domain])) {
 153                  unset($this->cookies[$domain]);
 154              }
 155          }
 156      }
 157  
 158      /**
 159       * Returns an array with all cookies.
 160       *
 161       * @param string $format
 162       *
 163       * @return Cookie[]
 164       *
 165       * @throws \InvalidArgumentException When the $format is invalid
 166       */
 167      public function getCookies($format = self::COOKIES_FLAT)
 168      {
 169          if (!\in_array($format, array(self::COOKIES_FLAT, self::COOKIES_ARRAY))) {
 170              throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', array(self::COOKIES_FLAT, self::COOKIES_ARRAY))));
 171          }
 172  
 173          if (self::COOKIES_ARRAY === $format) {
 174              return $this->cookies;
 175          }
 176  
 177          $flattenedCookies = array();
 178          foreach ($this->cookies as $path) {
 179              foreach ($path as $cookies) {
 180                  foreach ($cookies as $cookie) {
 181                      $flattenedCookies[] = $cookie;
 182                  }
 183              }
 184          }
 185  
 186          return $flattenedCookies;
 187      }
 188  
 189      /**
 190       * Clears a cookie in the browser.
 191       *
 192       * @param string $name
 193       * @param string $path
 194       * @param string $domain
 195       * @param bool   $secure
 196       * @param bool   $httpOnly
 197       */
 198      public function clearCookie($name, $path = '/', $domain = null, $secure = false, $httpOnly = true)
 199      {
 200          $this->setCookie(new Cookie($name, null, 1, $path, $domain, $secure, $httpOnly));
 201      }
 202  
 203      /**
 204       * Generates a HTTP Content-Disposition field-value.
 205       *
 206       * @param string $disposition      One of "inline" or "attachment"
 207       * @param string $filename         A unicode string
 208       * @param string $filenameFallback A string containing only ASCII characters that
 209       *                                 is semantically equivalent to $filename. If the filename is already ASCII,
 210       *                                 it can be omitted, or just copied from $filename
 211       *
 212       * @return string A string suitable for use as a Content-Disposition field-value
 213       *
 214       * @throws \InvalidArgumentException
 215       *
 216       * @see RFC 6266
 217       */
 218      public function makeDisposition($disposition, $filename, $filenameFallback = '')
 219      {
 220          if (!\in_array($disposition, array(self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE))) {
 221              throw new \InvalidArgumentException(sprintf('The disposition must be either "%s" or "%s".', self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE));
 222          }
 223  
 224          if ('' == $filenameFallback) {
 225              $filenameFallback = $filename;
 226          }
 227  
 228          // filenameFallback is not ASCII.
 229          if (!preg_match('/^[\x20-\x7e]*$/', $filenameFallback)) {
 230              throw new \InvalidArgumentException('The filename fallback must only contain ASCII characters.');
 231          }
 232  
 233          // percent characters aren't safe in fallback.
 234          if (false !== strpos($filenameFallback, '%')) {
 235              throw new \InvalidArgumentException('The filename fallback cannot contain the "%" character.');
 236          }
 237  
 238          // path separators aren't allowed in either.
 239          if (false !== strpos($filename, '/') || false !== strpos($filename, '\\') || false !== strpos($filenameFallback, '/') || false !== strpos($filenameFallback, '\\')) {
 240              throw new \InvalidArgumentException('The filename and the fallback cannot contain the "/" and "\\" characters.');
 241          }
 242  
 243          $output = sprintf('%s; filename="%s"', $disposition, str_replace('"', '\\"', $filenameFallback));
 244  
 245          if ($filename !== $filenameFallback) {
 246              $output .= sprintf("; filename*=utf-8''%s", rawurlencode($filename));
 247          }
 248  
 249          return $output;
 250      }
 251  
 252      /**
 253       * Returns the calculated value of the cache-control header.
 254       *
 255       * This considers several other headers and calculates or modifies the
 256       * cache-control header to a sensible, conservative value.
 257       *
 258       * @return string
 259       */
 260      protected function computeCacheControlValue()
 261      {
 262          if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
 263              return 'no-cache';
 264          }
 265  
 266          if (!$this->cacheControl) {
 267              // conservative by default
 268              return 'private, must-revalidate';
 269          }
 270  
 271          $header = $this->getCacheControlHeader();
 272          if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
 273              return $header;
 274          }
 275  
 276          // public if s-maxage is defined, private otherwise
 277          if (!isset($this->cacheControl['s-maxage'])) {
 278              return $header.', private';
 279          }
 280  
 281          return $header;
 282      }
 283  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1