Ssi.php 5.9 KB

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