SandboxedPrint.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 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_Node_SandboxedPrint adds a check for the __toString() method
  12. * when the variable is an object and the sandbox is activated.
  13. *
  14. * When there is a simple Print statement, like {{ article }},
  15. * and if the sandbox is enabled, we need to check that the __toString()
  16. * method is allowed if 'article' is an object.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class Twig_Node_SandboxedPrint extends Twig_Node_Print
  21. {
  22. public function compile(Twig_Compiler $compiler)
  23. {
  24. $compiler
  25. ->addDebugInfo($this)
  26. ->write('echo $this->env->getExtension(\'sandbox\')->ensureToStringAllowed(')
  27. ->subcompile($this->getNode('expr'))
  28. ->raw(");\n")
  29. ;
  30. }
  31. /**
  32. * Removes node filters.
  33. *
  34. * This is mostly needed when another visitor adds filters (like the escaper one).
  35. *
  36. * @param Twig_Node $node A Node
  37. *
  38. * @return Twig_Node
  39. */
  40. protected function removeNodeFilter($node)
  41. {
  42. if ($node instanceof Twig_Node_Expression_Filter) {
  43. return $this->removeNodeFilter($node->getNode('node'));
  44. }
  45. return $node;
  46. }
  47. }