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

Esi.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 8.13 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\HttpKernel\HttpCache;
013   
014  use Symfony\Component\HttpFoundation\Request;
015  use Symfony\Component\HttpFoundation\Response;
016  use Symfony\Component\HttpKernel\HttpKernelInterface;
017   
018  /**
019   * Esi implements the ESI capabilities to Request and Response instances.
020   *
021   * For more information, read the following W3C notes:
022   *
023   *  * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang)
024   *
025   *  * Edge Architecture Specification (http://www.w3.org/TR/edge-arch)
026   *
027   * @author Fabien Potencier <fabien@symfony.com>
028   */
029  class Esi
030  {
031      private $contentTypes;
032   
033      /**
034       * Constructor.
035       *
036       * @param array $contentTypes An array of content-type that should be parsed for ESI information.
037       *                           (default: text/html, text/xml, application/xhtml+xml, and application/xml)
038       */
039      public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
040      {
041          $this->contentTypes = $contentTypes;
042      }
043   
044      /**
045       * Returns a new cache strategy instance.
046       *
047       * @return EsiResponseCacheStrategyInterface A EsiResponseCacheStrategyInterface instance
048       */
049      public function createCacheStrategy()
050      {
051          return new EsiResponseCacheStrategy();
052      }
053   
054      /**
055       * Checks that at least one surrogate has ESI/1.0 capability.
056       *
057       * @param Request $request A Request instance
058       *
059       * @return bool    true if one surrogate has ESI/1.0 capability, false otherwise
060       */
061      public function hasSurrogateEsiCapability(Request $request)
062      {
063          if (null === $value = $request->headers->get('Surrogate-Capability')) {
064              return false;
065          }
066   
067          return false !== strpos($value, 'ESI/1.0');
068      }
069   
070      /**
071       * Adds ESI/1.0 capability to the given Request.
072       *
073       * @param Request $request A Request instance
074       */
075      public function addSurrogateEsiCapability(Request $request)
076      {
077          $current = $request->headers->get('Surrogate-Capability');
078          $new = 'symfony2="ESI/1.0"';
079   
080          $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
081      }
082   
083      /**
084       * Adds HTTP headers to specify that the Response needs to be parsed for ESI.
085       *
086       * This method only adds an ESI HTTP header if the Response has some ESI tags.
087       *
088       * @param Response $response A Response instance
089       */
090      public function addSurrogateControl(Response $response)
091      {
092          if (false !== strpos($response->getContent(), '<esi:include')) {
093              $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
094          }
095      }
096   
097      /**
098       * Checks that the Response needs to be parsed for ESI tags.
099       *
100       * @param Response $response A Response instance
101       *
102       * @return bool    true if the Response needs to be parsed, false otherwise
103       */
104      public function needsEsiParsing(Response $response)
105      {
106          if (!$control = $response->headers->get('Surrogate-Control')) {
107              return false;
108          }
109   
110          return (bool) preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control);
111      }
112   
113      /**
114       * Renders an ESI tag.
115       *
116       * @param string  $uri          A URI
117       * @param string  $alt          An alternate URI
118       * @param bool    $ignoreErrors Whether to ignore errors or not
119       * @param string  $comment      A comment to add as an esi:include tag
120       *
121       * @return string
122       */
123      public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
124      {
125          $html = sprintf('<esi:include src="%s"%s%s />',
126              $uri,
127              $ignoreErrors ? ' onerror="continue"' : '',
128              $alt ? sprintf(' alt="%s"', $alt) : ''
129          );
130   
131          if (!empty($comment)) {
132              return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
133          }
134   
135          return $html;
136      }
137   
138      /**
139       * Replaces a Response ESI tags with the included resource content.
140       *
141       * @param Request  $request  A Request instance
142       * @param Response $response A Response instance
143       *
144       * @return Response
145       */
146      public function process(Request $request, Response $response)
147      {
148          $this->request = $request;
149          $type = $response->headers->get('Content-Type');
150          if (empty($type)) {
151              $type = 'text/html';
152          }
153   
154          $parts = explode(';', $type);
155          if (!in_array($parts[0], $this->contentTypes)) {
156              return $response;
157          }
158   
159          // we don't use a proper XML parser here as we can have ESI tags in a plain text response
160          $content = $response->getContent();
161          $content = str_replace(array('<?', '<%'), array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>'), $content);
162          $content = preg_replace_callback('#<esi\:include\s+(.*?)\s*(?:/|</esi\:include)>#', array($this, 'handleEsiIncludeTag'), $content);
163          $content = preg_replace('#<esi\:comment[^>]*(?:/|</esi\:comment)>#', '', $content);
164          $content = preg_replace('#<esi\:remove>.*?</esi\:remove>#', '', $content);
165   
166          $response->setContent($content);
167          $response->headers->set('X-Body-Eval', 'ESI');
168   
169          // remove ESI/1.0 from the Surrogate-Control header
170          if ($response->headers->has('Surrogate-Control')) {
171              $value = $response->headers->get('Surrogate-Control');
172              if ('content="ESI/1.0"' == $value) {
173                  $response->headers->remove('Surrogate-Control');
174              } elseif (preg_match('#,\s*content="ESI/1.0"#', $value)) {
175                  $response->headers->set('Surrogate-Control', preg_replace('#,\s*content="ESI/1.0"#', '', $value));
176              } elseif (preg_match('#content="ESI/1.0",\s*#', $value)) {
177                  $response->headers->set('Surrogate-Control', preg_replace('#content="ESI/1.0",\s*#', '', $value));
178              }
179          }
180      }
181   
182      /**
183       * Handles an ESI from the cache.
184       *
185       * @param HttpCache $cache        An HttpCache instance
186       * @param string    $uri          The main URI
187       * @param string    $alt          An alternative URI
188       * @param bool      $ignoreErrors Whether to ignore errors or not
189       *
190       * @return string
191       *
192       * @throws \RuntimeException
193       * @throws \Exception
194       */
195      public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
196      {
197          $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
198   
199          try {
200              $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
201   
202              if (!$response->isSuccessful()) {
203                  throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
204              }
205   
206              return $response->getContent();
207          } catch (\Exception $e) {
208              if ($alt) {
209                  return $this->handle($cache, $alt, '', $ignoreErrors);
210              }
211   
212              if (!$ignoreErrors) {
213                  throw $e;
214              }
215          }
216      }
217   
218      /**
219       * Handles an ESI include tag (called internally).
220       *
221       * @param array $attributes An array containing the attributes.
222       *
223       * @return string The response content for the include.
224       *
225       * @throws \RuntimeException
226       */
227      private function handleEsiIncludeTag($attributes)
228      {
229          $options = array();
230          preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $attributes[1], $matches, PREG_SET_ORDER);
231          foreach ($matches as $set) {
232              $options[$set[1]] = $set[2];
233          }
234   
235          if (!isset($options['src'])) {
236              throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.');
237          }
238   
239          return sprintf('<?php echo $this->esi->handle($this, %s, %s, %s) ?>'."\n",
240              var_export($options['src'], true),
241              var_export(isset($options['alt']) ? $options['alt'] : '', true),
242              isset($options['onerror']) && 'continue' == $options['onerror'] ? 'true' : 'false'
243          );
244      }
245  }
246