1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig\TokenParser; 13 14use Twig\Error\SyntaxError; 15use Twig\Node\IncludeNode; 16use Twig\Node\SandboxNode; 17use Twig\Node\TextNode; 18use Twig\Token; 19 20/** 21 * Marks a section of a template as untrusted code that must be evaluated in the sandbox mode. 22 * 23 * {% sandbox %} 24 * {% include 'user.html' %} 25 * {% endsandbox %} 26 * 27 * @see https://twig.symfony.com/doc/api.html#sandbox-extension for details 28 */ 29final class SandboxTokenParser extends AbstractTokenParser 30{ 31 public function parse(Token $token) 32 { 33 $stream = $this->parser->getStream(); 34 $stream->expect(/* Token::BLOCK_END_TYPE */ 3); 35 $body = $this->parser->subparse([$this, 'decideBlockEnd'], true); 36 $stream->expect(/* Token::BLOCK_END_TYPE */ 3); 37 38 // in a sandbox tag, only include tags are allowed 39 if (!$body instanceof IncludeNode) { 40 foreach ($body as $node) { 41 if ($node instanceof TextNode && ctype_space($node->getAttribute('data'))) { 42 continue; 43 } 44 45 if (!$node instanceof IncludeNode) { 46 throw new SyntaxError('Only "include" tags are allowed within a "sandbox" section.', $node->getTemplateLine(), $stream->getSourceContext()); 47 } 48 } 49 } 50 51 return new SandboxNode($body, $token->getLine(), $this->getTag()); 52 } 53 54 public function decideBlockEnd(Token $token) 55 { 56 return $token->test('endsandbox'); 57 } 58 59 public function getTag() 60 { 61 return 'sandbox'; 62 } 63} 64 65class_alias('Twig\TokenParser\SandboxTokenParser', 'Twig_TokenParser_Sandbox'); 66