You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.9 KiB

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