Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

HeaderBag.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 8.71 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\HttpFoundation;
013   
014  /**
015   * HeaderBag is a container for HTTP headers.
016   *
017   * @author Fabien Potencier <fabien@symfony.com>
018   *
019   * @api
020   */
021  class HeaderBag implements \IteratorAggregate, \Countable
022  {
023      protected $headers;
024      protected $cacheControl;
025   
026      /**
027       * Constructor.
028       *
029       * @param array $headers An array of HTTP headers
030       *
031       * @api
032       */
033      public function __construct(array $headers = array())
034      {
035          $this->cacheControl = array();
036          $this->headers = array();
037          foreach ($headers as $key => $values) {
038              $this->set($key, $values);
039          }
040      }
041   
042      /**
043       * Returns the headers as a string.
044       *
045       * @return string The headers
046       */
047      public function __toString()
048      {
049          if (!$this->headers) {
050              return '';
051          }
052   
053          $max = max(array_map('strlen', array_keys($this->headers))) + 1;
054          $content = '';
055          ksort($this->headers);
056          foreach ($this->headers as $name => $values) {
057              $name = implode('-', array_map('ucfirst', explode('-', $name)));
058              foreach ($values as $value) {
059                  $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
060              }
061          }
062   
063          return $content;
064      }
065   
066      /**
067       * Returns the headers.
068       *
069       * @return array An array of headers
070       *
071       * @api
072       */
073      public function all()
074      {
075          return $this->headers;
076      }
077   
078      /**
079       * Returns the parameter keys.
080       *
081       * @return array An array of parameter keys
082       *
083       * @api
084       */
085      public function keys()
086      {
087          return array_keys($this->headers);
088      }
089   
090      /**
091       * Replaces the current HTTP headers by a new set.
092       *
093       * @param array $headers An array of HTTP headers
094       *
095       * @api
096       */
097      public function replace(array $headers = array())
098      {
099          $this->headers = array();
100          $this->add($headers);
101      }
102   
103      /**
104       * Adds new headers the current HTTP headers set.
105       *
106       * @param array $headers An array of HTTP headers
107       *
108       * @api
109       */
110      public function add(array $headers)
111      {
112          foreach ($headers as $key => $values) {
113              $this->set($key, $values);
114          }
115      }
116   
117      /**
118       * Returns a header value by name.
119       *
120       * @param string  $key     The header name
121       * @param mixed   $default The default value
122       * @param bool    $first   Whether to return the first value or all header values
123       *
124       * @return string|array The first header value if $first is true, an array of values otherwise
125       *
126       * @api
127       */
128      public function get($key, $default = null, $first = true)
129      {
130          $key = strtr(strtolower($key), '_', '-');
131   
132          if (!array_key_exists($key, $this->headers)) {
133              if (null === $default) {
134                  return $first ? null : array();
135              }
136   
137              return $first ? $default : array($default);
138          }
139   
140          if ($first) {
141              return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
142          }
143   
144          return $this->headers[$key];
145      }
146   
147      /**
148       * Sets a header by name.
149       *
150       * @param string       $key     The key
151       * @param string|array $values  The value or an array of values
152       * @param bool         $replace Whether to replace the actual value or not (true by default)
153       *
154       * @api
155       */
156      public function set($key, $values, $replace = true)
157      {
158          $key = strtr(strtolower($key), '_', '-');
159   
160          $values = array_values((array) $values);
161   
162          if (true === $replace || !isset($this->headers[$key])) {
163              $this->headers[$key] = $values;
164          } else {
165              $this->headers[$key] = array_merge($this->headers[$key], $values);
166          }
167   
168          if ('cache-control' === $key) {
169              $this->cacheControl = $this->parseCacheControl($values[0]);
170          }
171      }
172   
173      /**
174       * Returns true if the HTTP header is defined.
175       *
176       * @param string $key The HTTP header
177       *
178       * @return bool    true if the parameter exists, false otherwise
179       *
180       * @api
181       */
182      public function has($key)
183      {
184          return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
185      }
186   
187      /**
188       * Returns true if the given HTTP header contains the given value.
189       *
190       * @param string $key   The HTTP header name
191       * @param string $value The HTTP value
192       *
193       * @return bool    true if the value is contained in the header, false otherwise
194       *
195       * @api
196       */
197      public function contains($key, $value)
198      {
199          return in_array($value, $this->get($key, null, false));
200      }
201   
202      /**
203       * Removes a header.
204       *
205       * @param string $key The HTTP header name
206       *
207       * @api
208       */
209      public function remove($key)
210      {
211          $key = strtr(strtolower($key), '_', '-');
212   
213          unset($this->headers[$key]);
214   
215          if ('cache-control' === $key) {
216              $this->cacheControl = array();
217          }
218      }
219   
220      /**
221       * Returns the HTTP header value converted to a date.
222       *
223       * @param string    $key     The parameter key
224       * @param \DateTime $default The default value
225       *
226       * @return null|\DateTime The parsed DateTime or the default value if the header does not exist
227       *
228       * @throws \RuntimeException When the HTTP header is not parseable
229       *
230       * @api
231       */
232      public function getDate($key, \DateTime $default = null)
233      {
234          if (null === $value = $this->get($key)) {
235              return $default;
236          }
237   
238          if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
239              throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
240          }
241   
242          return $date;
243      }
244   
245      /**
246       * Adds a custom Cache-Control directive.
247       *
248       * @param string $key   The Cache-Control directive name
249       * @param mixed  $value The Cache-Control directive value
250       */
251      public function addCacheControlDirective($key, $value = true)
252      {
253          $this->cacheControl[$key] = $value;
254   
255          $this->set('Cache-Control', $this->getCacheControlHeader());
256      }
257   
258      /**
259       * Returns true if the Cache-Control directive is defined.
260       *
261       * @param string $key The Cache-Control directive
262       *
263       * @return bool true if the directive exists, false otherwise
264       */
265      public function hasCacheControlDirective($key)
266      {
267          return array_key_exists($key, $this->cacheControl);
268      }
269   
270      /**
271       * Returns a Cache-Control directive value by name.
272       *
273       * @param string $key The directive name
274       *
275       * @return mixed|null The directive value if defined, null otherwise
276       */
277      public function getCacheControlDirective($key)
278      {
279          return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
280      }
281   
282      /**
283       * Removes a Cache-Control directive.
284       *
285       * @param string $key The Cache-Control directive
286       */
287      public function removeCacheControlDirective($key)
288      {
289          unset($this->cacheControl[$key]);
290   
291          $this->set('Cache-Control', $this->getCacheControlHeader());
292      }
293   
294      /**
295       * Returns an iterator for headers.
296       *
297       * @return \ArrayIterator An \ArrayIterator instance
298       */
299      public function getIterator()
300      {
301          return new \ArrayIterator($this->headers);
302      }
303   
304      /**
305       * Returns the number of headers.
306       *
307       * @return int The number of headers
308       */
309      public function count()
310      {
311          return count($this->headers);
312      }
313   
314      protected function getCacheControlHeader()
315      {
316          $parts = array();
317          ksort($this->cacheControl);
318          foreach ($this->cacheControl as $key => $value) {
319              if (true === $value) {
320                  $parts[] = $key;
321              } else {
322                  if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
323                      $value = '"'.$value.'"';
324                  }
325   
326                  $parts[] = "$key=$value";
327              }
328          }
329   
330          return implode(', ', $parts);
331      }
332   
333      /**
334       * Parses a Cache-Control HTTP header.
335       *
336       * @param string $header The value of the Cache-Control HTTP header
337       *
338       * @return array An array representing the attribute values
339       */
340      protected function parseCacheControl($header)
341      {
342          $cacheControl = array();
343          preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
344          foreach ($matches as $match) {
345              $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
346          }
347   
348          return $cacheControl;
349      }
350  }
351