You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modifiercompiler.escape.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifierCompiler
  7. */
  8. /**
  9. * Smarty escape modifier plugin
  10. * Type: modifier
  11. * Name: escape
  12. * Purpose: escape string for output
  13. *
  14. * @link http://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual)
  15. * @author Rodney Rehm
  16. *
  17. * @param array $params parameters
  18. * @param Smarty_Internal_TemplateCompilerBase $compiler
  19. *
  20. * @return string with compiled code
  21. * @throws \SmartyException
  22. */
  23. function smarty_modifiercompiler_escape($params, Smarty_Internal_TemplateCompilerBase $compiler)
  24. {
  25. static $_double_encode = null;
  26. static $is_loaded = false;
  27. $compiler->template->_checkPlugins(array(array('function' => 'smarty_literal_compiler_param',
  28. 'file' => SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php')));
  29. if ($_double_encode === null) {
  30. $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
  31. }
  32. try {
  33. $esc_type = smarty_literal_compiler_param($params, 1, 'html');
  34. $char_set = smarty_literal_compiler_param($params, 2, Smarty::$_CHARSET);
  35. $double_encode = smarty_literal_compiler_param($params, 3, true);
  36. if (!$char_set) {
  37. $char_set = Smarty::$_CHARSET;
  38. }
  39. switch ($esc_type) {
  40. case 'html':
  41. if ($_double_encode) {
  42. return 'htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
  43. var_export($double_encode, true) . ')';
  44. } elseif ($double_encode) {
  45. return 'htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
  46. } else {
  47. // fall back to modifier.escape.php
  48. }
  49. case 'htmlall':
  50. if (Smarty::$_MBSTRING) {
  51. if ($_double_encode) {
  52. // php >=5.2.3 - go native
  53. return 'mb_convert_encoding(htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' .
  54. var_export($char_set, true) . ', ' . var_export($double_encode, true) .
  55. '), "HTML-ENTITIES", ' . var_export($char_set, true) . ')';
  56. } elseif ($double_encode) {
  57. // php <5.2.3 - only handle double encoding
  58. return 'mb_convert_encoding(htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' .
  59. var_export($char_set, true) . '), "HTML-ENTITIES", ' . var_export($char_set, true) . ')';
  60. } else {
  61. // fall back to modifier.escape.php
  62. }
  63. }
  64. // no MBString fallback
  65. if ($_double_encode) {
  66. // php >=5.2.3 - go native
  67. return 'htmlentities(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
  68. var_export($double_encode, true) . ')';
  69. } elseif ($double_encode) {
  70. // php <5.2.3 - only handle double encoding
  71. return 'htmlentities(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
  72. } else {
  73. // fall back to modifier.escape.php
  74. }
  75. case 'url':
  76. return 'rawurlencode(' . $params[ 0 ] . ')';
  77. case 'urlpathinfo':
  78. return 'str_replace("%2F", "/", rawurlencode(' . $params[ 0 ] . '))';
  79. case 'quotes':
  80. // escape unescaped single quotes
  81. return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[ 0 ] . ')';
  82. case 'javascript':
  83. // escape quotes and backslashes, newlines, etc.
  84. return 'strtr(' . $params[ 0 ] .
  85. ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/" ))';
  86. }
  87. }
  88. catch (SmartyException $e) {
  89. // pass through to regular plugin fallback
  90. }
  91. // could not optimize |escape call, so fallback to regular plugin
  92. if ($compiler->template->caching && ($compiler->tag_nocache | $compiler->nocache)) {
  93. $compiler->required_plugins[ 'nocache' ][ 'escape' ][ 'modifier' ][ 'file' ] =
  94. SMARTY_PLUGINS_DIR . 'modifier.escape.php';
  95. $compiler->required_plugins[ 'nocache' ][ 'escape' ][ 'modifier' ][ 'function' ] =
  96. 'smarty_modifier_escape';
  97. } else {
  98. $compiler->required_plugins[ 'compiled' ][ 'escape' ][ 'modifier' ][ 'file' ] =
  99. SMARTY_PLUGINS_DIR . 'modifier.escape.php';
  100. $compiler->required_plugins[ 'compiled' ][ 'escape' ][ 'modifier' ][ 'function' ] =
  101. 'smarty_modifier_escape';
  102. }
  103. return 'smarty_modifier_escape(' . join(', ', $params) . ')';
  104. }