| 148 |
lars |
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 |
public function getName(): string
|
|
|
25 |
{
|
|
|
26 |
return 'ssi';
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public function addSurrogateControl(Response $response)
|
|
|
30 |
{
|
|
|
31 |
if (str_contains($response->getContent(), '<!--#include')) {
|
|
|
32 |
$response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function renderIncludeTag(string $uri, string $alt = null, bool $ignoreErrors = true, string $comment = ''): string
|
|
|
37 |
{
|
|
|
38 |
return sprintf('<!--#include virtual="%s" -->', $uri);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public function process(Request $request, Response $response): Response
|
|
|
42 |
{
|
|
|
43 |
$type = $response->headers->get('Content-Type');
|
|
|
44 |
if (empty($type)) {
|
|
|
45 |
$type = 'text/html';
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$parts = explode(';', $type);
|
|
|
49 |
if (!\in_array($parts[0], $this->contentTypes)) {
|
|
|
50 |
return $response;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
// we don't use a proper XML parser here as we can have SSI tags in a plain text response
|
|
|
54 |
$content = $response->getContent();
|
|
|
55 |
|
|
|
56 |
$chunks = preg_split('#<!--\#include\s+(.*?)\s*-->#', $content, -1, \PREG_SPLIT_DELIM_CAPTURE);
|
|
|
57 |
$chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
|
|
|
58 |
|
|
|
59 |
$i = 1;
|
|
|
60 |
while (isset($chunks[$i])) {
|
|
|
61 |
$options = [];
|
|
|
62 |
preg_match_all('/(virtual)="([^"]*?)"/', $chunks[$i], $matches, \PREG_SET_ORDER);
|
|
|
63 |
foreach ($matches as $set) {
|
|
|
64 |
$options[$set[1]] = $set[2];
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if (!isset($options['virtual'])) {
|
|
|
68 |
throw new \RuntimeException('Unable to process an SSI tag without a "virtual" attribute.');
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, \'\', false) ?>'."\n",
|
|
|
72 |
var_export($options['virtual'], true)
|
|
|
73 |
);
|
|
|
74 |
++$i;
|
|
|
75 |
$chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
|
|
|
76 |
++$i;
|
|
|
77 |
}
|
|
|
78 |
$content = implode('', $chunks);
|
|
|
79 |
|
|
|
80 |
$response->setContent($content);
|
|
|
81 |
$response->headers->set('X-Body-Eval', 'SSI');
|
|
|
82 |
|
|
|
83 |
// remove SSI/1.0 from the Surrogate-Control header
|
|
|
84 |
$this->removeFromControl($response);
|
|
|
85 |
|
|
|
86 |
return $response;
|
|
|
87 |
}
|
|
|
88 |
}
|