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 switch ($esc_type) {
27 case 'html':
28 return htmlspecialchars($string, ENT_QUOTES, $char_set);
30 case 'htmlall':
31 return htmlentities($string, ENT_QUOTES, $char_set);
33 case 'url':
34 return rawurlencode($string);
36 case 'urlpathinfo':
37 return str_replace('%2F', '/', rawurlencode($string));
39 case 'quotes':
40 // escape unescaped single quotes
41 return preg_replace("%(?<!\\\\)'%", "\\'", $string);
43 case 'hex':
44 // escape every character into hex
45 $return = '';
46 for ($x = 0; $x < strlen($string); $x++) {
47 $return .= '%' . bin2hex($string[$x]);
48 }
49 return $return;
51 case 'hexentity':
52 $return = '';
53 for ($x = 0; $x < strlen($string); $x++) {
54 $return .= '&#x' . bin2hex($string[$x]) . ';';
55 }
56 return $return;
58 case 'decentity':
59 $return = '';
60 for ($x = 0; $x < strlen($string); $x++) {
61 $return .= '&#' . ord($string[$x]) . ';';
62 }
63 return $return;
65 case 'javascript':
66 // escape quotes and backslashes, newlines, etc.
67 return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
69 case 'mail':
70 // safe way to display e-mail address on a web page
71 if ($smarty->has_mb) {
72 return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
73 } else {
74 return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
75 }
77 case 'nonstd':
78 // escape non-standard chars, such as ms document quotes
79 $_res = '';
80 for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
81 $_ord = ord(substr($string, $_i, 1));
82 // non-standard char, escape it
83 if ($_ord >= 126) {
84 $_res .= '&#' . $_ord . ';';
85 } else {
86 $_res .= substr($string, $_i, 1);
87 }
88 }
89 return $_res;
91 default:
92 return $string;
93 }
94 if (!function_exists("mb_str_replace")) {
95 // simulate the missing PHP mb_str_replace function
96 function mb_str_replace($needle, $replacement, $haystack)
97 {
98 $needle_len = mb_strlen($needle);
99 $replacement_len = mb_strlen($replacement);
100 $pos = mb_strpos($haystack, $needle, 0);
101 while ($pos !== false) {
102 $haystack = mb_substr($haystack, 0, $pos) . $replacement
103 . mb_substr($haystack, $pos + $needle_len);
104 $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
105 }
106 return $haystack;
107 }
108 }
109 }
111 ?>