[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpCache/ -> Esi.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\HttpKernel\HttpCache;
  13  
  14  use Symfony\Component\HttpFoundation\Request;
  15  use Symfony\Component\HttpFoundation\Response;
  16  use Symfony\Component\HttpKernel\HttpKernelInterface;
  17  
  18  /**
  19   * Esi implements the ESI capabilities to Request and Response instances.
  20   *
  21   * For more information, read the following W3C notes:
  22   *
  23   *  * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang)
  24   *
  25   *  * Edge Architecture Specification (http://www.w3.org/TR/edge-arch)
  26   *
  27   * @author Fabien Potencier <fabien@symfony.com>
  28   */
  29  class Esi
  30  {
  31      private $contentTypes;
  32      private $phpEscapeMap = array(
  33          array('<?', '<%', '<s', '<S'),
  34          array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
  35      );
  36  
  37      /**
  38       * Constructor.
  39       *
  40       * @param array $contentTypes An array of content-type that should be parsed for ESI information.
  41       *                            (default: text/html, text/xml, application/xhtml+xml, and application/xml)
  42       */
  43      public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
  44      {
  45          $this->contentTypes = $contentTypes;
  46      }
  47  
  48      /**
  49       * Returns a new cache strategy instance.
  50       *
  51       * @return EsiResponseCacheStrategyInterface A EsiResponseCacheStrategyInterface instance
  52       */
  53      public function createCacheStrategy()
  54      {
  55          return new EsiResponseCacheStrategy();
  56      }
  57  
  58      /**
  59       * Checks that at least one surrogate has ESI/1.0 capability.
  60       *
  61       * @param Request $request A Request instance
  62       *
  63       * @return bool true if one surrogate has ESI/1.0 capability, false otherwise
  64       */
  65      public function hasSurrogateEsiCapability(Request $request)
  66      {
  67          if (null === $value = $request->headers->get('Surrogate-Capability')) {
  68              return false;
  69          }
  70  
  71          return false !== strpos($value, 'ESI/1.0');
  72      }
  73  
  74      /**
  75       * Adds ESI/1.0 capability to the given Request.
  76       *
  77       * @param Request $request A Request instance
  78       */
  79      public function addSurrogateEsiCapability(Request $request)
  80      {
  81          $current = $request->headers->get('Surrogate-Capability');
  82          $new = 'symfony2="ESI/1.0"';
  83  
  84          $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
  85      }
  86  
  87      /**
  88       * Adds HTTP headers to specify that the Response needs to be parsed for ESI.
  89       *
  90       * This method only adds an ESI HTTP header if the Response has some ESI tags.
  91       *
  92       * @param Response $response A Response instance
  93       */
  94      public function addSurrogateControl(Response $response)
  95      {
  96          if (false !== strpos($response->getContent(), '<esi:include')) {
  97              $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
  98          }
  99      }
 100  
 101      /**
 102       * Checks that the Response needs to be parsed for ESI tags.
 103       *
 104       * @param Response $response A Response instance
 105       *
 106       * @return bool true if the Response needs to be parsed, false otherwise
 107       */
 108      public function needsEsiParsing(Response $response)
 109      {
 110          if (!$control = $response->headers->get('Surrogate-Control')) {
 111              return false;
 112          }
 113  
 114          return (bool) preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control);
 115      }
 116  
 117      /**
 118       * Renders an ESI tag.
 119       *
 120       * @param string $uri          A URI
 121       * @param string $alt          An alternate URI
 122       * @param bool   $ignoreErrors Whether to ignore errors or not
 123       * @param string $comment      A comment to add as an esi:include tag
 124       *
 125       * @return string
 126       */
 127      public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
 128      {
 129          $html = sprintf('<esi:include src="%s"%s%s />',
 130              $uri,
 131              $ignoreErrors ? ' onerror="continue"' : '',
 132              $alt ? sprintf(' alt="%s"', $alt) : ''
 133          );
 134  
 135          if (!empty($comment)) {
 136              return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
 137          }
 138  
 139          return $html;
 140      }
 141  
 142      /**
 143       * Replaces a Response ESI tags with the included resource content.
 144       *
 145       * @param Request  $request  A Request instance
 146       * @param Response $response A Response instance
 147       *
 148       * @return Response
 149       */
 150      public function process(Request $request, Response $response)
 151      {
 152          $this->request = $request;
 153          $type = $response->headers->get('Content-Type');
 154          if (empty($type)) {
 155              $type = 'text/html';
 156          }
 157  
 158          $parts = explode(';', $type);
 159          if (!in_array($parts[0], $this->contentTypes)) {
 160              return $response;
 161          }
 162  
 163          // we don't use a proper XML parser here as we can have ESI tags in a plain text response
 164          $content = $response->getContent();
 165          $content = preg_replace('#<esi\:remove>.*?</esi\:remove>#s', '', $content);
 166          $content = preg_replace('#<esi\:comment[^>]+>#s', '', $content);
 167  
 168          $chunks = preg_split('#<esi\:include\s+(.*?)\s*(?:/|</esi\:include)>#', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
 169          $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
 170  
 171          $i = 1;
 172          while (isset($chunks[$i])) {
 173              $options = array();
 174              preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER);
 175              foreach ($matches as $set) {
 176                  $options[$set[1]] = $set[2];
 177              }
 178  
 179              if (!isset($options['src'])) {
 180                  throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.');
 181              }
 182  
 183              $chunks[$i] = sprintf('<?php echo $this->esi->handle($this, %s, %s, %s) ?>'."\n",
 184                  var_export($options['src'], true),
 185                  var_export(isset($options['alt']) ? $options['alt'] : '', true),
 186                  isset($options['onerror']) && 'continue' === $options['onerror'] ? 'true' : 'false'
 187              );
 188              ++$i;
 189              $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
 190              ++$i;
 191          }
 192          $content = implode('', $chunks);
 193  
 194          $response->setContent($content);
 195          $response->headers->set('X-Body-Eval', 'ESI');
 196  
 197          // remove ESI/1.0 from the Surrogate-Control header
 198          if ($response->headers->has('Surrogate-Control')) {
 199              $value = $response->headers->get('Surrogate-Control');
 200              if ('content="ESI/1.0"' == $value) {
 201                  $response->headers->remove('Surrogate-Control');
 202              } elseif (preg_match('#,\s*content="ESI/1.0"#', $value)) {
 203                  $response->headers->set('Surrogate-Control', preg_replace('#,\s*content="ESI/1.0"#', '', $value));
 204              } elseif (preg_match('#content="ESI/1.0",\s*#', $value)) {
 205                  $response->headers->set('Surrogate-Control', preg_replace('#content="ESI/1.0",\s*#', '', $value));
 206              }
 207          }
 208      }
 209  
 210      /**
 211       * Handles an ESI from the cache.
 212       *
 213       * @param HttpCache $cache        An HttpCache instance
 214       * @param string    $uri          The main URI
 215       * @param string    $alt          An alternative URI
 216       * @param bool      $ignoreErrors Whether to ignore errors or not
 217       *
 218       * @return string
 219       *
 220       * @throws \RuntimeException
 221       * @throws \Exception
 222       */
 223      public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
 224      {
 225          $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
 226  
 227          try {
 228              $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
 229  
 230              if (!$response->isSuccessful()) {
 231                  throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
 232              }
 233  
 234              return $response->getContent();
 235          } catch (\Exception $e) {
 236              if ($alt) {
 237                  return $this->handle($cache, $alt, '', $ignoreErrors);
 238              }
 239  
 240              if (!$ignoreErrors) {
 241                  throw $e;
 242              }
 243          }
 244      }
 245  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1