Commit | Line | Data |
---|---|---|
e586807d H |
1 | <?php |
2 | /** | |
3 | * Smarty plugin | |
4 | * @package Smarty | |
5 | * @subpackage plugins | |
6 | */ | |
7 | ||
8 | ||
9 | /** | |
10 | * Smarty escape modifier plugin | |
11 | * | |
12 | * Type: modifier<br> | |
13 | * Name: escape<br> | |
14 | * Purpose: Escape the string according to escapement type | |
15 | * @link http://smarty.php.net/manual/en/language.modifier.escape.php | |
16 | * escape (Smarty online manual) | |
17 | * @param string | |
18 | * @param html|htmlall|url|quotes|hex|hexentity|javascript | |
19 | * @return string | |
20 | */ | |
21 | function smarty_modifier_escape($string, $esc_type = 'html') | |
22 | { | |
23 | switch ($esc_type) { | |
24 | case 'html': | |
25 | return htmlspecialchars($string, ENT_QUOTES); | |
26 | ||
27 | case 'htmlall': | |
28 | return htmlentities($string, ENT_QUOTES); | |
29 | ||
30 | case 'url': | |
31 | return urlencode($string); | |
32 | ||
33 | case 'quotes': | |
34 | // escape unescaped single quotes | |
35 | return preg_replace("%(?<!\\\\)'%", "\\'", $string); | |
36 | ||
37 | case 'hex': | |
38 | // escape every character into hex | |
39 | $return = ''; | |
40 | for ($x=0; $x < strlen($string); $x++) { | |
41 | $return .= '%' . bin2hex($string[$x]); | |
42 | } | |
43 | return $return; | |
44 | ||
45 | case 'hexentity': | |
46 | $return = ''; | |
47 | for ($x=0; $x < strlen($string); $x++) { | |
48 | $return .= '&#x' . bin2hex($string[$x]) . ';'; | |
49 | } | |
50 | return $return; | |
51 | ||
52 | case 'javascript': | |
53 | // escape quotes and backslashes and newlines | |
54 | return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n')); | |
55 | ||
56 | default: | |
57 | return $string; | |
58 | } | |
59 | } | |
60 | ||
61 | /* vim: set expandtab: */ | |
62 | ||
63 | ?> |