Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / http-kernel / HttpCache / Ssi.php
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
17 /**
18  * Ssi implements the SSI capabilities to Request and Response instances.
19  *
20  * @author Sebastian Krebs <krebs.seb@gmail.com>
21  */
22 class Ssi extends AbstractSurrogate
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function getName()
28     {
29         return 'ssi';
30     }
31
32     /**
33      * {@inheritdoc}
34      */
35     public function addSurrogateControl(Response $response)
36     {
37         if (false !== strpos($response->getContent(), '<!--#include')) {
38             $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
39         }
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
46     {
47         return sprintf('<!--#include virtual="%s" -->', $uri);
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     public function process(Request $request, Response $response)
54     {
55         $type = $response->headers->get('Content-Type');
56         if (empty($type)) {
57             $type = 'text/html';
58         }
59
60         $parts = explode(';', $type);
61         if (!in_array($parts[0], $this->contentTypes)) {
62             return $response;
63         }
64
65         // we don't use a proper XML parser here as we can have SSI tags in a plain text response
66         $content = $response->getContent();
67
68         $chunks = preg_split('#<!--\#include\s+(.*?)\s*-->#', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
69         $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
70
71         $i = 1;
72         while (isset($chunks[$i])) {
73             $options = array();
74             preg_match_all('/(virtual)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER);
75             foreach ($matches as $set) {
76                 $options[$set[1]] = $set[2];
77             }
78
79             if (!isset($options['virtual'])) {
80                 throw new \RuntimeException('Unable to process an SSI tag without a "virtual" attribute.');
81             }
82
83             $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, \'\', false) ?>'."\n",
84                 var_export($options['virtual'], true)
85             );
86             ++$i;
87             $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
88             ++$i;
89         }
90         $content = implode('', $chunks);
91
92         $response->setContent($content);
93         $response->headers->set('X-Body-Eval', 'SSI');
94
95         // remove SSI/1.0 from the Surrogate-Control header
96         $this->removeFromControl($response);
97     }
98 }