[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
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 use Symfony\Component\HttpKernel\HttpKernelInterface; 17 18 /** 19 * Esi implements the ESI capabilities to Request and Response instances. 20 * 21 * For more information, read the following W3C notes: 22 * 23 * * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang) 24 * 25 * * Edge Architecture Specification (http://www.w3.org/TR/edge-arch) 26 * 27 * @author Fabien Potencier <fabien@symfony.com> 28 */ 29 class Esi implements SurrogateInterface 30 { 31 private $contentTypes; 32 private $phpEscapeMap = array( 33 array('<?', '<%', '<s', '<S'), 34 array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'), 35 ); 36 37 /** 38 * @param array $contentTypes An array of content-type that should be parsed for ESI information 39 * (default: text/html, text/xml, application/xhtml+xml, and application/xml) 40 */ 41 public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml')) 42 { 43 $this->contentTypes = $contentTypes; 44 } 45 46 public function getName() 47 { 48 return 'esi'; 49 } 50 51 /** 52 * Returns a new cache strategy instance. 53 * 54 * @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance 55 */ 56 public function createCacheStrategy() 57 { 58 return new ResponseCacheStrategy(); 59 } 60 61 /** 62 * Checks that at least one surrogate has ESI/1.0 capability. 63 * 64 * @return bool true if one surrogate has ESI/1.0 capability, false otherwise 65 */ 66 public function hasSurrogateCapability(Request $request) 67 { 68 if (null === $value = $request->headers->get('Surrogate-Capability')) { 69 return false; 70 } 71 72 return false !== strpos($value, 'ESI/1.0'); 73 } 74 75 /** 76 * Checks that at least one surrogate has ESI/1.0 capability. 77 * 78 * @param Request $request A Request instance 79 * 80 * @return bool true if one surrogate has ESI/1.0 capability, false otherwise 81 * 82 * @deprecated since version 2.6, to be removed in 3.0. Use hasSurrogateCapability() instead 83 */ 84 public function hasSurrogateEsiCapability(Request $request) 85 { 86 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0. Use the hasSurrogateCapability() method instead.', E_USER_DEPRECATED); 87 88 return $this->hasSurrogateCapability($request); 89 } 90 91 /** 92 * Adds ESI/1.0 capability to the given Request. 93 */ 94 public function addSurrogateCapability(Request $request) 95 { 96 $current = $request->headers->get('Surrogate-Capability'); 97 $new = 'symfony2="ESI/1.0"'; 98 99 $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new); 100 } 101 102 /** 103 * Adds ESI/1.0 capability to the given Request. 104 * 105 * @param Request $request A Request instance 106 * 107 * @deprecated since version 2.6, to be removed in 3.0. Use addSurrogateCapability() instead 108 */ 109 public function addSurrogateEsiCapability(Request $request) 110 { 111 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0. Use the addSurrogateCapability() method instead.', E_USER_DEPRECATED); 112 113 $this->addSurrogateCapability($request); 114 } 115 116 /** 117 * Adds HTTP headers to specify that the Response needs to be parsed for ESI. 118 * 119 * This method only adds an ESI HTTP header if the Response has some ESI tags. 120 */ 121 public function addSurrogateControl(Response $response) 122 { 123 if (false !== strpos($response->getContent(), '<esi:include')) { 124 $response->headers->set('Surrogate-Control', 'content="ESI/1.0"'); 125 } 126 } 127 128 /** 129 * Checks that the Response needs to be parsed for ESI tags. 130 * 131 * @return bool true if the Response needs to be parsed, false otherwise 132 */ 133 public function needsParsing(Response $response) 134 { 135 if (!$control = $response->headers->get('Surrogate-Control')) { 136 return false; 137 } 138 139 return (bool) preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control); 140 } 141 142 /** 143 * Checks that the Response needs to be parsed for ESI tags. 144 * 145 * @param Response $response A Response instance 146 * 147 * @return bool true if the Response needs to be parsed, false otherwise 148 * 149 * @deprecated since version 2.6, to be removed in 3.0. Use needsParsing() instead 150 */ 151 public function needsEsiParsing(Response $response) 152 { 153 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0. Use the needsParsing() method instead.', E_USER_DEPRECATED); 154 155 return $this->needsParsing($response); 156 } 157 158 /** 159 * Renders an ESI tag. 160 * 161 * @param string $uri A URI 162 * @param string $alt An alternate URI 163 * @param bool $ignoreErrors Whether to ignore errors or not 164 * @param string $comment A comment to add as an esi:include tag 165 * 166 * @return string 167 */ 168 public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '') 169 { 170 $html = sprintf('<esi:include src="%s"%s%s />', 171 $uri, 172 $ignoreErrors ? ' onerror="continue"' : '', 173 $alt ? sprintf(' alt="%s"', $alt) : '' 174 ); 175 176 if (!empty($comment)) { 177 return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html); 178 } 179 180 return $html; 181 } 182 183 /** 184 * Replaces a Response ESI tags with the included resource content. 185 * 186 * @return Response 187 */ 188 public function process(Request $request, Response $response) 189 { 190 $type = $response->headers->get('Content-Type'); 191 if (empty($type)) { 192 $type = 'text/html'; 193 } 194 195 $parts = explode(';', $type); 196 if (!\in_array($parts[0], $this->contentTypes)) { 197 return $response; 198 } 199 200 // we don't use a proper XML parser here as we can have ESI tags in a plain text response 201 $content = $response->getContent(); 202 $content = preg_replace('#<esi\:remove>.*?</esi\:remove>#s', '', $content); 203 $content = preg_replace('#<esi\:comment[^>]+>#s', '', $content); 204 205 $chunks = preg_split('#<esi\:include\s+(.*?)\s*(?:/|</esi\:include)>#', $content, -1, PREG_SPLIT_DELIM_CAPTURE); 206 $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]); 207 208 $i = 1; 209 while (isset($chunks[$i])) { 210 $options = array(); 211 preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER); 212 foreach ($matches as $set) { 213 $options[$set[1]] = $set[2]; 214 } 215 216 if (!isset($options['src'])) { 217 throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.'); 218 } 219 220 $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, %s, %s) ?>'."\n", 221 var_export($options['src'], true), 222 var_export(isset($options['alt']) ? $options['alt'] : '', true), 223 isset($options['onerror']) && 'continue' === $options['onerror'] ? 'true' : 'false' 224 ); 225 ++$i; 226 $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]); 227 ++$i; 228 } 229 $content = implode('', $chunks); 230 231 $response->setContent($content); 232 $response->headers->set('X-Body-Eval', 'ESI'); 233 234 // remove ESI/1.0 from the Surrogate-Control header 235 if ($response->headers->has('Surrogate-Control')) { 236 $value = $response->headers->get('Surrogate-Control'); 237 if ('content="ESI/1.0"' == $value) { 238 $response->headers->remove('Surrogate-Control'); 239 } elseif (preg_match('#,\s*content="ESI/1.0"#', $value)) { 240 $response->headers->set('Surrogate-Control', preg_replace('#,\s*content="ESI/1.0"#', '', $value)); 241 } elseif (preg_match('#content="ESI/1.0",\s*#', $value)) { 242 $response->headers->set('Surrogate-Control', preg_replace('#content="ESI/1.0",\s*#', '', $value)); 243 } 244 } 245 } 246 247 /** 248 * Handles an ESI from the cache. 249 * 250 * @param HttpCache $cache An HttpCache instance 251 * @param string $uri The main URI 252 * @param string $alt An alternative URI 253 * @param bool $ignoreErrors Whether to ignore errors or not 254 * 255 * @return string 256 * 257 * @throws \RuntimeException 258 * @throws \Exception 259 */ 260 public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors) 261 { 262 $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all()); 263 264 try { 265 $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); 266 267 if (!$response->isSuccessful()) { 268 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode())); 269 } 270 271 return $response->getContent(); 272 } catch (\Exception $e) { 273 if ($alt) { 274 return $this->handle($cache, $alt, '', $ignoreErrors); 275 } 276 277 if (!$ignoreErrors) { 278 throw $e; 279 } 280 } 281 } 282 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |