modifier.escape.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  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/docs/en/language.modifier.escape
  15. * @author Monte Ohrt <monte at ohrt dot com>
  16. *
  17. * @param string $string input string
  18. * @param string $esc_type escape type
  19. * @param string $char_set character set, used for htmlspecialchars() or htmlentities()
  20. * @param boolean $double_encode encode already encoded entitites again, used for htmlspecialchars() or htmlentities()
  21. *
  22. * @return string escaped input string
  23. */
  24. function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true)
  25. {
  26. static $_double_encode = null;
  27. if ($_double_encode === null) {
  28. $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
  29. }
  30. if (!$char_set) {
  31. $char_set = Smarty::$_CHARSET;
  32. }
  33. switch ($esc_type) {
  34. case 'html':
  35. if ($_double_encode) {
  36. // php >=5.3.2 - go native
  37. return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
  38. } else {
  39. if ($double_encode) {
  40. // php <5.2.3 - only handle double encoding
  41. return htmlspecialchars($string, ENT_QUOTES, $char_set);
  42. } else {
  43. // php <5.2.3 - prevent double encoding
  44. $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
  45. $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
  46. $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
  47. return $string;
  48. }
  49. }
  50. case 'htmlall':
  51. if (Smarty::$_MBSTRING) {
  52. // mb_convert_encoding ignores htmlspecialchars()
  53. if ($_double_encode) {
  54. // php >=5.3.2 - go native
  55. $string = htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
  56. } else {
  57. if ($double_encode) {
  58. // php <5.2.3 - only handle double encoding
  59. $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
  60. } else {
  61. // php <5.2.3 - prevent double encoding
  62. $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
  63. $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
  64. $string =
  65. str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
  66. return $string;
  67. }
  68. }
  69. // htmlentities() won't convert everything, so use mb_convert_encoding
  70. return mb_convert_encoding($string, 'HTML-ENTITIES', $char_set);
  71. }
  72. // no MBString fallback
  73. if ($_double_encode) {
  74. return htmlentities($string, ENT_QUOTES, $char_set, $double_encode);
  75. } else {
  76. if ($double_encode) {
  77. return htmlentities($string, ENT_QUOTES, $char_set);
  78. } else {
  79. $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
  80. $string = htmlentities($string, ENT_QUOTES, $char_set);
  81. $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
  82. return $string;
  83. }
  84. }
  85. case 'url':
  86. return rawurlencode($string);
  87. case 'urlpathinfo':
  88. return str_replace('%2F', '/', rawurlencode($string));
  89. case 'quotes':
  90. // escape unescaped single quotes
  91. return preg_replace("%(?<!\\\\)'%", "\\'", $string);
  92. case 'hex':
  93. // escape every byte into hex
  94. // Note that the UTF-8 encoded character ä will be represented as %c3%a4
  95. $return = '';
  96. $_length = strlen($string);
  97. for ($x = 0; $x < $_length; $x ++) {
  98. $return .= '%' . bin2hex($string[ $x ]);
  99. }
  100. return $return;
  101. case 'hexentity':
  102. $return = '';
  103. if (Smarty::$_MBSTRING) {
  104. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
  105. $return = '';
  106. foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
  107. $return .= '&#x' . strtoupper(dechex($unicode)) . ';';
  108. }
  109. return $return;
  110. }
  111. // no MBString fallback
  112. $_length = strlen($string);
  113. for ($x = 0; $x < $_length; $x ++) {
  114. $return .= '&#x' . bin2hex($string[ $x ]) . ';';
  115. }
  116. return $return;
  117. case 'decentity':
  118. $return = '';
  119. if (Smarty::$_MBSTRING) {
  120. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
  121. $return = '';
  122. foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
  123. $return .= '&#' . $unicode . ';';
  124. }
  125. return $return;
  126. }
  127. // no MBString fallback
  128. $_length = strlen($string);
  129. for ($x = 0; $x < $_length; $x ++) {
  130. $return .= '&#' . ord($string[ $x ]) . ';';
  131. }
  132. return $return;
  133. case 'javascript':
  134. // escape quotes and backslashes, newlines, etc.
  135. return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n',
  136. '</' => '<\/'));
  137. case 'mail':
  138. if (Smarty::$_MBSTRING) {
  139. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
  140. return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  141. }
  142. // no MBString fallback
  143. return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  144. case 'nonstd':
  145. // escape non-standard chars, such as ms document quotes
  146. $return = '';
  147. if (Smarty::$_MBSTRING) {
  148. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
  149. foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
  150. if ($unicode >= 126) {
  151. $return .= '&#' . $unicode . ';';
  152. } else {
  153. $return .= chr($unicode);
  154. }
  155. }
  156. return $return;
  157. }
  158. $_length = strlen($string);
  159. for ($_i = 0; $_i < $_length; $_i ++) {
  160. $_ord = ord(substr($string, $_i, 1));
  161. // non-standard char, escape it
  162. if ($_ord >= 126) {
  163. $return .= '&#' . $_ord . ';';
  164. } else {
  165. $return .= substr($string, $_i, 1);
  166. }
  167. }
  168. return $return;
  169. default:
  170. return $string;
  171. }
  172. }