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