Escaper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_Escaper implements output escaping.
  12. *
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor
  16. {
  17. protected $statusStack = array();
  18. protected $blocks = array();
  19. protected $safeAnalysis;
  20. protected $traverser;
  21. protected $defaultStrategy = false;
  22. protected $safeVars = array();
  23. public function __construct()
  24. {
  25. $this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis();
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
  31. {
  32. if ($node instanceof Twig_Node_Module) {
  33. if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename'))) {
  34. $this->defaultStrategy = $defaultStrategy;
  35. }
  36. $this->safeVars = array();
  37. } elseif ($node instanceof Twig_Node_AutoEscape) {
  38. $this->statusStack[] = $node->getAttribute('value');
  39. } elseif ($node instanceof Twig_Node_Block) {
  40. $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
  41. } elseif ($node instanceof Twig_Node_Import) {
  42. $this->safeVars[] = $node->getNode('var')->getAttribute('name');
  43. }
  44. return $node;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
  50. {
  51. if ($node instanceof Twig_Node_Module) {
  52. $this->defaultStrategy = false;
  53. $this->safeVars = array();
  54. } elseif ($node instanceof Twig_Node_Expression_Filter) {
  55. return $this->preEscapeFilterNode($node, $env);
  56. } elseif ($node instanceof Twig_Node_Print) {
  57. return $this->escapePrintNode($node, $env, $this->needEscaping($env));
  58. }
  59. if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) {
  60. array_pop($this->statusStack);
  61. } elseif ($node instanceof Twig_Node_BlockReference) {
  62. $this->blocks[$node->getAttribute('name')] = $this->needEscaping($env);
  63. }
  64. return $node;
  65. }
  66. protected function escapePrintNode(Twig_Node_Print $node, Twig_Environment $env, $type)
  67. {
  68. if (false === $type) {
  69. return $node;
  70. }
  71. $expression = $node->getNode('expr');
  72. if ($this->isSafeFor($type, $expression, $env)) {
  73. return $node;
  74. }
  75. $class = get_class($node);
  76. return new $class(
  77. $this->getEscaperFilter($type, $expression),
  78. $node->getLine()
  79. );
  80. }
  81. protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env)
  82. {
  83. $name = $filter->getNode('filter')->getAttribute('value');
  84. $type = $env->getFilter($name)->getPreEscape();
  85. if (null === $type) {
  86. return $filter;
  87. }
  88. $node = $filter->getNode('node');
  89. if ($this->isSafeFor($type, $node, $env)) {
  90. return $filter;
  91. }
  92. $filter->setNode('node', $this->getEscaperFilter($type, $node));
  93. return $filter;
  94. }
  95. protected function isSafeFor($type, Twig_NodeInterface $expression, $env)
  96. {
  97. $safe = $this->safeAnalysis->getSafe($expression);
  98. if (null === $safe) {
  99. if (null === $this->traverser) {
  100. $this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis));
  101. }
  102. $this->safeAnalysis->setSafeVars($this->safeVars);
  103. $this->traverser->traverse($expression);
  104. $safe = $this->safeAnalysis->getSafe($expression);
  105. }
  106. return in_array($type, $safe) || in_array('all', $safe);
  107. }
  108. protected function needEscaping(Twig_Environment $env)
  109. {
  110. if (count($this->statusStack)) {
  111. return $this->statusStack[count($this->statusStack) - 1];
  112. }
  113. return $this->defaultStrategy ? $this->defaultStrategy : false;
  114. }
  115. protected function getEscaperFilter($type, Twig_NodeInterface $node)
  116. {
  117. $line = $node->getLine();
  118. $name = new Twig_Node_Expression_Constant('escape', $line);
  119. $args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line), new Twig_Node_Expression_Constant(null, $line), new Twig_Node_Expression_Constant(true, $line)));
  120. return new Twig_Node_Expression_Filter($node, $name, $args, $line);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getPriority()
  126. {
  127. return 0;
  128. }
  129. }