Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\HttpCache;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Ssi implements the SSI capabilities to Request and Response instances.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Sebastian Krebs <krebs.seb@gmail.com>
|
Chris@0
|
21 */
|
Chris@0
|
22 class Ssi extends AbstractSurrogate
|
Chris@0
|
23 {
|
Chris@0
|
24 /**
|
Chris@0
|
25 * {@inheritdoc}
|
Chris@0
|
26 */
|
Chris@0
|
27 public function getName()
|
Chris@0
|
28 {
|
Chris@0
|
29 return 'ssi';
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * {@inheritdoc}
|
Chris@0
|
34 */
|
Chris@0
|
35 public function addSurrogateControl(Response $response)
|
Chris@0
|
36 {
|
Chris@0
|
37 if (false !== strpos($response->getContent(), '<!--#include')) {
|
Chris@0
|
38 $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
|
Chris@0
|
39 }
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
|
Chris@0
|
46 {
|
Chris@0
|
47 return sprintf('<!--#include virtual="%s" -->', $uri);
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * {@inheritdoc}
|
Chris@0
|
52 */
|
Chris@0
|
53 public function process(Request $request, Response $response)
|
Chris@0
|
54 {
|
Chris@0
|
55 $type = $response->headers->get('Content-Type');
|
Chris@0
|
56 if (empty($type)) {
|
Chris@0
|
57 $type = 'text/html';
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 $parts = explode(';', $type);
|
Chris@17
|
61 if (!\in_array($parts[0], $this->contentTypes)) {
|
Chris@0
|
62 return $response;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 // we don't use a proper XML parser here as we can have SSI tags in a plain text response
|
Chris@0
|
66 $content = $response->getContent();
|
Chris@0
|
67
|
Chris@0
|
68 $chunks = preg_split('#<!--\#include\s+(.*?)\s*-->#', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
|
Chris@0
|
69 $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
|
Chris@0
|
70
|
Chris@0
|
71 $i = 1;
|
Chris@0
|
72 while (isset($chunks[$i])) {
|
Chris@17
|
73 $options = [];
|
Chris@0
|
74 preg_match_all('/(virtual)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER);
|
Chris@0
|
75 foreach ($matches as $set) {
|
Chris@0
|
76 $options[$set[1]] = $set[2];
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 if (!isset($options['virtual'])) {
|
Chris@0
|
80 throw new \RuntimeException('Unable to process an SSI tag without a "virtual" attribute.');
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, \'\', false) ?>'."\n",
|
Chris@0
|
84 var_export($options['virtual'], true)
|
Chris@0
|
85 );
|
Chris@0
|
86 ++$i;
|
Chris@0
|
87 $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
|
Chris@0
|
88 ++$i;
|
Chris@0
|
89 }
|
Chris@0
|
90 $content = implode('', $chunks);
|
Chris@0
|
91
|
Chris@0
|
92 $response->setContent($content);
|
Chris@0
|
93 $response->headers->set('X-Body-Eval', 'SSI');
|
Chris@0
|
94
|
Chris@0
|
95 // remove SSI/1.0 from the Surrogate-Control header
|
Chris@0
|
96 $this->removeFromControl($response);
|
Chris@0
|
97 }
|
Chris@0
|
98 }
|