modifiercompiler.escape.php 4.9 KB

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