| 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\Node; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Attribute\YieldReady; |
| 14 |
use ElementorDeps\Twig\Compiler; |
| 15 |
/** |
| 16 |
* @author Fabien Potencier <fabien@symfony.com> |
| 17 |
*/ |
| 18 |
#[\Twig\Attribute\YieldReady] |
| 19 |
class CheckSecurityNode extends Node |
| 20 |
{ |
| 21 |
private $usedFilters; |
| 22 |
private $usedTags; |
| 23 |
private $usedFunctions; |
| 24 |
/** |
| 25 |
* @param array<string, int> $usedFilters |
| 26 |
* @param array<string, int> $usedTags |
| 27 |
* @param array<string, int> $usedFunctions |
| 28 |
*/ |
| 29 |
public function __construct(array $usedFilters, array $usedTags, array $usedFunctions) |
| 30 |
{ |
| 31 |
$this->usedFilters = $usedFilters; |
| 32 |
$this->usedTags = $usedTags; |
| 33 |
$this->usedFunctions = $usedFunctions; |
| 34 |
parent::__construct(); |
| 35 |
} |
| 36 |
public function compile(Compiler $compiler) : void |
| 37 |
{ |
| 38 |
$compiler->write("\n")->write("public function checkSecurity()\n")->write("{\n")->indent()->write('static $tags = ')->repr(\array_filter($this->usedTags))->raw(";\n")->write('static $filters = ')->repr(\array_filter($this->usedFilters))->raw(";\n")->write('static $functions = ')->repr(\array_filter($this->usedFunctions))->raw(";\n\n")->write("try {\n")->indent()->write("\$this->sandbox->checkSecurity(\n")->indent()->write(!$this->usedTags ? "[],\n" : "['" . \implode("', '", \array_keys($this->usedTags)) . "'],\n")->write(!$this->usedFilters ? "[],\n" : "['" . \implode("', '", \array_keys($this->usedFilters)) . "'],\n")->write(!$this->usedFunctions ? "[],\n" : "['" . \implode("', '", \array_keys($this->usedFunctions)) . "'],\n")->write("\$this->source\n")->outdent()->write(");\n")->outdent()->write("} catch (SecurityError \$e) {\n")->indent()->write("\$e->setSourceContext(\$this->source);\n\n")->write("if (\$e instanceof SecurityNotAllowedTagError && isset(\$tags[\$e->getTagName()])) {\n")->indent()->write("\$e->setTemplateLine(\$tags[\$e->getTagName()]);\n")->outdent()->write("} elseif (\$e instanceof SecurityNotAllowedFilterError && isset(\$filters[\$e->getFilterName()])) {\n")->indent()->write("\$e->setTemplateLine(\$filters[\$e->getFilterName()]);\n")->outdent()->write("} elseif (\$e instanceof SecurityNotAllowedFunctionError && isset(\$functions[\$e->getFunctionName()])) {\n")->indent()->write("\$e->setTemplateLine(\$functions[\$e->getFunctionName()]);\n")->outdent()->write("}\n\n")->write("throw \$e;\n")->outdent()->write("}\n\n")->outdent()->write("}\n"); |
| 39 |
} |
| 40 |
} |
| 41 |
|