No Description

Ssi.php 5.8KB

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