PageRenderTime 20ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/modifier.escape.php

http://zoop.googlecode.com/
PHP | 111 lines | 78 code | 10 blank | 23 comment | 6 complexity | 4126123f6b6023e9d0b0dd233eaacde9 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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') && function_exists('mb_strlen')) {
  25. // simulate the missing PHP mb_str_replace function
  26. function mb_str_replace($needle, $replacement, $haystack)
  27. {
  28. $needle_len = mb_strlen($needle);
  29. $replacement_len = mb_strlen($replacement);
  30. $pos = mb_strpos($haystack, $needle, 0);
  31. while ($pos !== false) {
  32. $haystack = mb_substr($haystack, 0, $pos) . $replacement
  33. . mb_substr($haystack, $pos + $needle_len);
  34. $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
  35. }
  36. return $haystack;
  37. }
  38. }
  39. switch ($esc_type) {
  40. case 'html':
  41. return htmlspecialchars($string, ENT_QUOTES, $char_set);
  42. case 'htmlall':
  43. return htmlentities($string, ENT_QUOTES, $char_set);
  44. case 'url':
  45. return rawurlencode($string);
  46. case 'urlpathinfo':
  47. return str_replace('%2F', '/', rawurlencode($string));
  48. case 'quotes':
  49. // escape unescaped single quotes
  50. return preg_replace("%(?<!\\\\)'%", "\\'", $string);
  51. case 'hex':
  52. // escape every character into hex
  53. $return = '';
  54. for ($x = 0; $x < strlen($string); $x++) {
  55. $return .= '%' . bin2hex($string[$x]);
  56. }
  57. return $return;
  58. case 'hexentity':
  59. $return = '';
  60. for ($x = 0; $x < strlen($string); $x++) {
  61. $return .= '&#x' . bin2hex($string[$x]) . ';';
  62. }
  63. return $return;
  64. case 'decentity':
  65. $return = '';
  66. for ($x = 0; $x < strlen($string); $x++) {
  67. $return .= '&#' . ord($string[$x]) . ';';
  68. }
  69. return $return;
  70. case 'javascript':
  71. // escape quotes and backslashes, newlines, etc.
  72. return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
  73. case 'mail':
  74. // safe way to display e-mail address on a web page
  75. if (function_exists('mb_str_replace')) {
  76. return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  77. } else {
  78. return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
  79. }
  80. case 'nonstd':
  81. // escape non-standard chars, such as ms document quotes
  82. $_res = '';
  83. for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
  84. $_ord = ord(substr($string, $_i, 1));
  85. // non-standard char, escape it
  86. if ($_ord >= 126) {
  87. $_res .= '&#' . $_ord . ';';
  88. } else {
  89. $_res .= substr($string, $_i, 1);
  90. }
  91. }
  92. return $_res;
  93. default:
  94. return $string;
  95. }
  96. }
  97. ?>