[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/HttpCache/ -> Ssi.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   * Ssi implements the SSI capabilities to Request and Response instances.
  20   *
  21   * @author Sebastian Krebs <krebs.seb@gmail.com>
  22   */
  23  class Ssi implements SurrogateInterface
  24  {
  25      private $contentTypes;
  26      private $phpEscapeMap = array(
  27          array('<?', '<%', '<s', '<S'),
  28          array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
  29      );
  30  
  31      /**
  32       * @param array $contentTypes An array of content-type that should be parsed for SSI information
  33       *                            (default: text/html, text/xml, application/xhtml+xml, and application/xml)
  34       */
  35      public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
  36      {
  37          $this->contentTypes = $contentTypes;
  38      }
  39  
  40      /**
  41       * {@inheritdoc}
  42       */
  43      public function getName()
  44      {
  45          return 'ssi';
  46      }
  47  
  48      /**
  49       * {@inheritdoc}
  50       */
  51      public function createCacheStrategy()
  52      {
  53          return new ResponseCacheStrategy();
  54      }
  55  
  56      /**
  57       * {@inheritdoc}
  58       */
  59      public function hasSurrogateCapability(Request $request)
  60      {
  61          if (null === $value = $request->headers->get('Surrogate-Capability')) {
  62              return false;
  63          }
  64  
  65          return false !== strpos($value, 'SSI/1.0');
  66      }
  67  
  68      /**
  69       * {@inheritdoc}
  70       */
  71      public function addSurrogateCapability(Request $request)
  72      {
  73          $current = $request->headers->get('Surrogate-Capability');
  74          $new = 'symfony2="SSI/1.0"';
  75  
  76          $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
  77      }
  78  
  79      /**
  80       * {@inheritdoc}
  81       */
  82      public function addSurrogateControl(Response $response)
  83      {
  84          if (false !== strpos($response->getContent(), '<!--#include')) {
  85              $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
  86          }
  87      }
  88  
  89      /**
  90       * {@inheritdoc}
  91       */
  92      public function needsParsing(Response $response)
  93      {
  94          if (!$control = $response->headers->get('Surrogate-Control')) {
  95              return false;
  96          }
  97  
  98          return (bool) preg_match('#content="[^"]*SSI/1.0[^"]*"#', $control);
  99      }
 100  
 101      /**
 102       * {@inheritdoc}
 103       */
 104      public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
 105      {
 106          return sprintf('<!--#include virtual="%s" -->', $uri);
 107      }
 108  
 109      /**
 110       * {@inheritdoc}
 111       */
 112      public function process(Request $request, Response $response)
 113      {
 114          $type = $response->headers->get('Content-Type');
 115          if (empty($type)) {
 116              $type = 'text/html';
 117          }
 118  
 119          $parts = explode(';', $type);
 120          if (!\in_array($parts[0], $this->contentTypes)) {
 121              return $response;
 122          }
 123  
 124          // we don't use a proper XML parser here as we can have SSI tags in a plain text response
 125          $content = $response->getContent();
 126  
 127          $chunks = preg_split('#<!--\#include\s+(.*?)\s*-->#', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
 128          $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
 129  
 130          $i = 1;
 131          while (isset($chunks[$i])) {
 132              $options = array();
 133              preg_match_all('/(virtual)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER);
 134              foreach ($matches as $set) {
 135                  $options[$set[1]] = $set[2];
 136              }
 137  
 138              if (!isset($options['virtual'])) {
 139                  throw new \RuntimeException('Unable to process an SSI tag without a "virtual" attribute.');
 140              }
 141  
 142              $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, \'\', false) ?>'."\n",
 143                  var_export($options['virtual'], true)
 144              );
 145              ++$i;
 146              $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
 147              ++$i;
 148          }
 149          $content = implode('', $chunks);
 150  
 151          $response->setContent($content);
 152          $response->headers->set('X-Body-Eval', 'SSI');
 153  
 154          // remove SSI/1.0 from the Surrogate-Control header
 155          if ($response->headers->has('Surrogate-Control')) {
 156              $value = $response->headers->get('Surrogate-Control');
 157              if ('content="SSI/1.0"' == $value) {
 158                  $response->headers->remove('Surrogate-Control');
 159              } elseif (preg_match('#,\s*content="SSI/1.0"#', $value)) {
 160                  $response->headers->set('Surrogate-Control', preg_replace('#,\s*content="SSI/1.0"#', '', $value));
 161              } elseif (preg_match('#content="SSI/1.0",\s*#', $value)) {
 162                  $response->headers->set('Surrogate-Control', preg_replace('#content="SSI/1.0",\s*#', '', $value));
 163              }
 164          }
 165      }
 166  
 167      /**
 168       * {@inheritdoc}
 169       */
 170      public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
 171      {
 172          $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
 173  
 174          try {
 175              $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
 176  
 177              if (!$response->isSuccessful()) {
 178                  throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
 179              }
 180  
 181              return $response->getContent();
 182          } catch (\Exception $e) {
 183              if ($alt) {
 184                  return $this->handle($cache, $alt, '', $ignoreErrors);
 185              }
 186  
 187              if (!$ignoreErrors) {
 188                  throw $e;
 189              }
 190          }
 191      }
 192  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1