Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| routing | |
0.00% |
0 / 14 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFunctions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| isUrlGenerationSafe | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\template\twig\extension; |
| 15 | |
| 16 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 17 | use Twig\Extension\AbstractExtension; |
| 18 | use Twig\Node\Expression\ArrayExpression; |
| 19 | use Twig\Node\Expression\ConstantExpression; |
| 20 | use Twig\Node\Node; |
| 21 | use Twig\TwigFunction; |
| 22 | |
| 23 | class routing extends AbstractExtension |
| 24 | { |
| 25 | /** @var \phpbb\routing\helper */ |
| 26 | protected $helper; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @param \phpbb\routing\helper $helper |
| 32 | */ |
| 33 | public function __construct(\phpbb\routing\helper $helper) |
| 34 | { |
| 35 | $this->helper = $helper; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * {@inheritdoc} |
| 40 | */ |
| 41 | public function getFunctions(): array |
| 42 | { |
| 43 | return [ |
| 44 | new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), |
| 45 | new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), |
| 46 | ]; |
| 47 | } |
| 48 | |
| 49 | public function getPath($name, $parameters = array(), $relative = false) |
| 50 | { |
| 51 | return $this->helper->route($name, $parameters, true, false, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH); |
| 52 | } |
| 53 | |
| 54 | public function getUrl($name, $parameters = array(), $schemeRelative = false) |
| 55 | { |
| 56 | return $this->helper->route($name, $parameters, true, false, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Borrowed from the Symfony Twig bridge routing extension |
| 61 | * |
| 62 | * @author Fabien Potencier <fabien@symfony.com> |
| 63 | * |
| 64 | * Determines at compile time whether the generated URL will be safe and thus |
| 65 | * saving the unneeded automatic escaping for performance reasons. |
| 66 | * |
| 67 | * The URL generation process percent encodes non-alphanumeric characters. So there is no risk |
| 68 | * that malicious/invalid characters are part of the URL. The only character within an URL that |
| 69 | * must be escaped in html is the ampersand ("&") which separates query params. So we cannot mark |
| 70 | * the URL generation as always safe, but only when we are sure there won't be multiple query |
| 71 | * params. This is the case when there are none or only one constant parameter given. |
| 72 | * E.g. we know beforehand this will be safe: |
| 73 | * - path('route') |
| 74 | * - path('route', {'param': 'value'}) |
| 75 | * But the following may not: |
| 76 | * - path('route', var) |
| 77 | * - path('route', {'param': ['val1', 'val2'] }) // a sub-array |
| 78 | * - path('route', {'param1': 'value1', 'param2': 'value2'}) |
| 79 | * If param1 and param2 reference placeholder in the route, it would still be safe. But we don't know. |
| 80 | * |
| 81 | * @param Node $argsNode The arguments of the path/url function |
| 82 | * |
| 83 | * @return array An array with the contexts the URL is safe |
| 84 | */ |
| 85 | public function isUrlGenerationSafe(Node $argsNode): array |
| 86 | { |
| 87 | // support named arguments |
| 88 | $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : ( |
| 89 | $argsNode->hasNode(1) ? $argsNode->getNode(1) : null |
| 90 | ); |
| 91 | |
| 92 | if (null === $paramsNode || $paramsNode instanceof ArrayExpression && \count($paramsNode) <= 2 && |
| 93 | (!$paramsNode->hasNode(1) || $paramsNode->getNode(1) instanceof ConstantExpression) |
| 94 | ) |
| 95 | { |
| 96 | return ['html']; |
| 97 | } |
| 98 | |
| 99 | return []; |
| 100 | } |
| 101 | } |