Sandbox.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Twig_NodeVisitor_Sandbox implements sandboxing.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface
  17. {
  18. protected $inAModule = false;
  19. protected $tags;
  20. protected $filters;
  21. protected $functions;
  22. /**
  23. * Called before child nodes are visited.
  24. *
  25. * @param Twig_NodeInterface $node The node to visit
  26. * @param Twig_Environment $env The Twig environment instance
  27. *
  28. * @return Twig_NodeInterface The modified node
  29. */
  30. public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
  31. {
  32. // in a sandbox tag, only include tags are allowed
  33. if ($node instanceof Twig_Node_Sandbox && !$node->getNode('body') instanceof Twig_Node_Include) {
  34. foreach ($node->getNode('body') as $n) {
  35. if ($n instanceof Twig_Node_Text && ctype_space($n->getAttribute('data'))) {
  36. continue;
  37. }
  38. if (!$n instanceof Twig_Node_Include) {
  39. throw new Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section', $n->getLine());
  40. }
  41. }
  42. }
  43. if ($node instanceof Twig_Node_Module) {
  44. $this->inAModule = true;
  45. $this->tags = array();
  46. $this->filters = array();
  47. $this->functions = array();
  48. return $node;
  49. } elseif ($this->inAModule) {
  50. // look for tags
  51. if ($node->getNodeTag()) {
  52. $this->tags[] = $node->getNodeTag();
  53. }
  54. // look for filters
  55. if ($node instanceof Twig_Node_Expression_Filter) {
  56. $this->filters[] = $node->getNode('filter')->getAttribute('value');
  57. }
  58. // look for functions
  59. if ($node instanceof Twig_Node_Expression_Function) {
  60. $this->functions[] = $node->getAttribute('name');
  61. }
  62. // wrap print to check __toString() calls
  63. if ($node instanceof Twig_Node_Print) {
  64. return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
  65. }
  66. }
  67. return $node;
  68. }
  69. /**
  70. * Called after child nodes are visited.
  71. *
  72. * @param Twig_NodeInterface $node The node to visit
  73. * @param Twig_Environment $env The Twig environment instance
  74. *
  75. * @return Twig_NodeInterface The modified node
  76. */
  77. public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
  78. {
  79. if ($node instanceof Twig_Node_Module) {
  80. $this->inAModule = false;
  81. return new Twig_Node_SandboxedModule($node, array_unique($this->filters), array_unique($this->tags), array_unique($this->functions));
  82. }
  83. return $node;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getPriority()
  89. {
  90. return 0;
  91. }
  92. }