Sandbox.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. * Marks a section of a template as untrusted code that must be evaluated in the sandbox mode.
  12. *
  13. * <pre>
  14. * {% sandbox %}
  15. * {% include 'user.html' %}
  16. * {% endsandbox %}
  17. * </pre>
  18. *
  19. * @see http://www.twig-project.org/doc/api.html#sandbox-extension for details
  20. */
  21. class Twig_TokenParser_Sandbox extends Twig_TokenParser
  22. {
  23. /**
  24. * Parses a token and returns a node.
  25. *
  26. * @param Twig_Token $token A Twig_Token instance
  27. *
  28. * @return Twig_NodeInterface A Twig_NodeInterface instance
  29. */
  30. public function parse(Twig_Token $token)
  31. {
  32. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  33. $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
  34. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  35. return new Twig_Node_Sandbox($body, $token->getLine(), $this->getTag());
  36. }
  37. public function decideBlockEnd(Twig_Token $token)
  38. {
  39. return $token->test('endsandbox');
  40. }
  41. /**
  42. * Gets the tag name associated with this token parser.
  43. *
  44. * @return string The tag name
  45. */
  46. public function getTag()
  47. {
  48. return 'sandbox';
  49. }
  50. }