Drupal investigation

Ssi.php 5.9KB

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