Escaper.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class Twig_Extension_Escaper extends Twig_Extension
  11. {
  12. protected $defaultStrategy;
  13. public function __construct($defaultStrategy = 'html')
  14. {
  15. $this->setDefaultStrategy($defaultStrategy);
  16. }
  17. /**
  18. * Returns the token parser instances to add to the existing list.
  19. *
  20. * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
  21. */
  22. public function getTokenParsers()
  23. {
  24. return array(new Twig_TokenParser_AutoEscape());
  25. }
  26. /**
  27. * Returns the node visitor instances to add to the existing list.
  28. *
  29. * @return array An array of Twig_NodeVisitorInterface instances
  30. */
  31. public function getNodeVisitors()
  32. {
  33. return array(new Twig_NodeVisitor_Escaper());
  34. }
  35. /**
  36. * Returns a list of filters to add to the existing list.
  37. *
  38. * @return array An array of filters
  39. */
  40. public function getFilters()
  41. {
  42. return array(
  43. 'raw' => new Twig_Filter_Function('twig_raw_filter', array('is_safe' => array('all'))),
  44. );
  45. }
  46. /**
  47. * Sets the default strategy to use when not defined by the user.
  48. *
  49. * The strategy can be a valid PHP callback that takes the template
  50. * "filename" as an argument and returns the strategy to use.
  51. *
  52. * @param mixed $defaultStrategy An escaping strategy
  53. */
  54. public function setDefaultStrategy($defaultStrategy)
  55. {
  56. // for BC
  57. if (true === $defaultStrategy) {
  58. $defaultStrategy = 'html';
  59. }
  60. $this->defaultStrategy = $defaultStrategy;
  61. }
  62. /**
  63. * Gets the default strategy to use when not defined by the user.
  64. *
  65. * @param string $filename The template "filename"
  66. *
  67. * @return string The default strategy to use for the template
  68. */
  69. public function getDefaultStrategy($filename)
  70. {
  71. // disable string callables to avoid calling a function named html or js,
  72. // or any other upcoming escaping strategy
  73. if (!is_string($this->defaultStrategy) && is_callable($this->defaultStrategy)) {
  74. return call_user_func($this->defaultStrategy, $filename);
  75. }
  76. return $this->defaultStrategy;
  77. }
  78. /**
  79. * Returns the name of the extension.
  80. *
  81. * @return string The extension name
  82. */
  83. public function getName()
  84. {
  85. return 'escaper';
  86. }
  87. }
  88. /**
  89. * Marks a variable as being safe.
  90. *
  91. * @param string $string A PHP variable
  92. */
  93. function twig_raw_filter($string)
  94. {
  95. return $string;
  96. }