Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

modifier.escape.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
  65. return $string;
  66. }
  67. }
  68. // htmlentities() won't convert everything, so use mb_convert_encoding
  69. return mb_convert_encoding($string, 'HTML-ENTITIES', $char_set);
  70. }
  71. // no MBString fallback
  72. if ($_double_encode) {
  73. return htmlentities($string, ENT_QUOTES, $char_set, $double_encode);
  74. } else {
  75. if ($double_encode) {
  76. return htmlentities($string, ENT_QUOTES, $char_set);
  77. } else {
  78. $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
  79. $string = htmlentities($string, ENT_QUOTES, $char_set);
  80. $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
  81. return $string;
  82. }
  83. }
  84. case 'url':
  85. return rawurlencode($string);
  86. case 'urlpathinfo':
  87. return str_replace('%2F', '/', rawurlencode($string));
  88. case 'quotes':
  89. // escape unescaped single quotes
  90. return preg_replace("%(?<!\\\\)'%", "\\'", $string);
  91. case 'hex':
  92. // escape every byte into hex
  93. // Note that the UTF-8 encoded character ä will be represented as %c3%a4
  94. $return = '';
  95. $_length = strlen($string);
  96. for ($x = 0; $x < $_length; $x ++) {
  97. $return .= '%' . bin2hex($string[$x]);
  98. }
  99. return $return;
  100. case 'hexentity':
  101. $return = '';
  102. if (Smarty::$_MBSTRING) {
  103. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
  104. $return = '';
  105. foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
  106. $return .= '&#x' . strtoupper(dechex($unicode)) . ';';
  107. }
  108. return $return;
  109. }
  110. // no MBString fallback
  111. $_length = strlen($string);
  112. for ($x = 0; $x < $_length; $x ++) {
  113. $return .= '&#x' . bin2hex($string[$x]) . ';';
  114. }
  115. return $return;
  116. case 'decentity':
  117. $return = '';
  118. if (Smarty::$_MBSTRING) {
  119. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
  120. $return = '';
  121. foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
  122. $return .= '&#' . $unicode . ';';
  123. }
  124. return $return;
  125. }
  126. // no MBString fallback
  127. $_length = strlen($string);
  128. for ($x = 0; $x < $_length; $x ++) {
  129. $return .= '&#' . ord($string[$x]) . ';';
  130. }
  131. return $return;
  132. case 'javascript':
  133. // escape quotes and backslashes, newlines, etc.
  134. return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
  135. case 'mail':
  136. if (Smarty::$_MBSTRING) {
  137. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
  138. return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  139. }
  140. // no MBString fallback
  141. return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  142. case 'nonstd':
  143. // escape non-standard chars, such as ms document quotes
  144. $return = '';
  145. if (Smarty::$_MBSTRING) {
  146. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
  147. foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
  148. if ($unicode >= 126) {
  149. $return .= '&#' . $unicode . ';';
  150. } else {
  151. $return .= chr($unicode);
  152. }
  153. }
  154. return $return;
  155. }
  156. $_length = strlen($string);
  157. for ($_i = 0; $_i < $_length; $_i ++) {
  158. $_ord = ord(substr($string, $_i, 1));
  159. // non-standard char, escape it
  160. if ($_ord >= 126) {
  161. $return .= '&#' . $_ord . ';';
  162. } else {
  163. $return .= substr($string, $_i, 1);
  164. }
  165. }
  166. return $return;
  167. default:
  168. return $string;
  169. }
  170. }