SandboxedPrint.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. * @package twig
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class Twig_Node_SandboxedPrint extends Twig_Node_Print
  22. {
  23. public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null)
  24. {
  25. parent::__construct($expr, $lineno, $tag);
  26. }
  27. /**
  28. * Compiles the node to PHP.
  29. *
  30. * @param Twig_Compiler A Twig_Compiler instance
  31. */
  32. public function compile(Twig_Compiler $compiler)
  33. {
  34. $compiler
  35. ->addDebugInfo($this)
  36. ->write('echo $this->env->getExtension(\'sandbox\')->ensureToStringAllowed(')
  37. ->subcompile($this->getNode('expr'))
  38. ->raw(");\n")
  39. ;
  40. }
  41. /**
  42. * Removes node filters.
  43. *
  44. * This is mostly needed when another visitor adds filters (like the escaper one).
  45. *
  46. * @param Twig_Node $node A Node
  47. */
  48. protected function removeNodeFilter($node)
  49. {
  50. if ($node instanceof Twig_Node_Expression_Filter) {
  51. return $this->removeNodeFilter($node->getNode('node'));
  52. }
  53. return $node;
  54. }
  55. }