modifier.escape.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  7. */
  8. /**
  9. * Smarty escape modifier plugin
  10. *
  11. * Type: modifier<br>
  12. * Name: escape<br>
  13. * Purpose: escape string for output
  14. *
  15. * @link http://smarty.php.net/manual/en/language.modifier.count.characters.php count_characters (Smarty online manual)
  16. * @author Monte Ohrt <monte at ohrt dot com>
  17. * @param string $string input string
  18. * @param string $esc_type escape type
  19. * @param string $char_set character set
  20. * @return string escaped input string
  21. */
  22. function smarty_modifier_escape($string, $esc_type = 'html', $char_set = SMARTY_RESOURCE_CHAR_SET)
  23. {
  24. if (!function_exists('mb_str_replace')) {
  25. // simulate the missing PHP mb_str_replace function
  26. function mb_str_replace($needles, $replacements, $haystack)
  27. {
  28. $rep = (array)$replacements;
  29. foreach ((array)$needles as $key => $needle) {
  30. $replacement = $rep[$key];
  31. $needle_len = mb_strlen($needle);
  32. $replacement_len = mb_strlen($replacement);
  33. $pos = mb_strpos($haystack, $needle, 0);
  34. while ($pos !== false) {
  35. $haystack = mb_substr($haystack, 0, $pos) . $replacement
  36. . mb_substr($haystack, $pos + $needle_len);
  37. $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
  38. }
  39. }
  40. return $haystack;
  41. }
  42. }
  43. switch ($esc_type) {
  44. case 'html':
  45. return htmlspecialchars($string, ENT_QUOTES, $char_set);
  46. case 'htmlall':
  47. return htmlentities($string, ENT_QUOTES, $char_set);
  48. case 'url':
  49. return rawurlencode($string);
  50. case 'urlpathinfo':
  51. return str_replace('%2F', '/', rawurlencode($string));
  52. case 'quotes':
  53. // escape unescaped single quotes
  54. return preg_replace("%(?<!\\\\)'%", "\\'", $string);
  55. case 'hex':
  56. // escape every character into hex
  57. $return = '';
  58. for ($x = 0; $x < strlen($string); $x++) {
  59. $return .= '%' . bin2hex($string[$x]);
  60. }
  61. return $return;
  62. case 'hexentity':
  63. $return = '';
  64. for ($x = 0; $x < strlen($string); $x++) {
  65. $return .= '&#x' . bin2hex($string[$x]) . ';';
  66. }
  67. return $return;
  68. case 'decentity':
  69. $return = '';
  70. for ($x = 0; $x < strlen($string); $x++) {
  71. $return .= '&#' . ord($string[$x]) . ';';
  72. }
  73. return $return;
  74. case 'javascript':
  75. // escape quotes and backslashes, newlines, etc.
  76. return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
  77. case 'mail':
  78. // safe way to display e-mail address on a web page
  79. if (function_exists('mb_substr')) {
  80. return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  81. } else {
  82. return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  83. }
  84. case 'nonstd':
  85. // escape non-standard chars, such as ms document quotes
  86. $_res = '';
  87. for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
  88. $_ord = ord(substr($string, $_i, 1));
  89. // non-standard char, escape it
  90. if ($_ord >= 126) {
  91. $_res .= '&#' . $_ord . ';';
  92. } else {
  93. $_res .= substr($string, $_i, 1);
  94. }
  95. }
  96. return $_res;
  97. default:
  98. return $string;
  99. }
  100. }
  101. ?>