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