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