SandboxedModule.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Represents a module node.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_Node_SandboxedModule extends Twig_Node_Module
  18. {
  19. protected $usedFilters;
  20. protected $usedTags;
  21. protected $usedFunctions;
  22. public function __construct(Twig_Node_Module $node, array $usedFilters, array $usedTags, array $usedFunctions)
  23. {
  24. parent::__construct($node->getNode('body'), $node->getNode('parent'), $node->getNode('blocks'), $node->getNode('macros'), $node->getNode('traits'), $node->getAttribute('filename'), $node->getLine(), $node->getNodeTag());
  25. $this->usedFilters = $usedFilters;
  26. $this->usedTags = $usedTags;
  27. $this->usedFunctions = $usedFunctions;
  28. }
  29. protected function compileDisplayBody(Twig_Compiler $compiler)
  30. {
  31. if (null === $this->getNode('parent')) {
  32. $compiler->write("\$this->checkSecurity();\n");
  33. }
  34. parent::compileDisplayBody($compiler);
  35. }
  36. protected function compileDisplayFooter(Twig_Compiler $compiler)
  37. {
  38. parent::compileDisplayFooter($compiler);
  39. $compiler
  40. ->write("protected function checkSecurity() {\n")
  41. ->indent()
  42. ->write("\$this->env->getExtension('sandbox')->checkSecurity(\n")
  43. ->indent()
  44. ->write(!$this->usedTags ? "array(),\n" : "array('".implode('\', \'', $this->usedTags)."'),\n")
  45. ->write(!$this->usedFilters ? "array(),\n" : "array('".implode('\', \'', $this->usedFilters)."'),\n")
  46. ->write(!$this->usedFunctions ? "array()\n" : "array('".implode('\', \'', $this->usedFunctions)."')\n")
  47. ->outdent()
  48. ->write(");\n")
  49. ;
  50. if (null !== $this->getNode('parent')) {
  51. $compiler
  52. ->raw("\n")
  53. ->write("\$this->parent->checkSecurity();\n")
  54. ;
  55. }
  56. $compiler
  57. ->outdent()
  58. ->write("}\n\n")
  59. ;
  60. }
  61. }