| 1 | <?php | <?php | 
| 2 |  | /** | 
|  | /* |  | 
| 3 | * Smarty plugin | * Smarty plugin | 
| 4 | * ------------------------------------------------------------ | * @package Smarty | 
| 5 | * Type:     modifier | * @subpackage plugins | 
| 6 | * Name:     escape | */ | 
| 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 | * 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') | function smarty_modifier_escape($string, $esc_type = 'html') | 
| 22 | { | { | 
| 34 | // escape unescaped single quotes | // escape unescaped single quotes | 
| 35 | return preg_replace("%(?<!\\\\)'%", "\\'", $string); | return preg_replace("%(?<!\\\\)'%", "\\'", $string); | 
| 36 |  |  | 
| 37 | case 'hex': | case 'hex': | 
| 38 | // escape every character into hex | // escape every character into hex | 
| 39 | for ($x=0; $x < strlen($string); $x++) { | $return = ''; | 
| 40 | $return .= '%' . bin2hex($string[$x]); | for ($x=0; $x < strlen($string); $x++) { | 
| 41 | } | $return .= '%' . bin2hex($string[$x]); | 
| 42 | return $return; | } | 
| 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 'decentity': | 
| 53 |  | $return = ''; | 
| 54 |  | for ($x=0; $x < strlen($string); $x++) { | 
| 55 |  | $return .= '&#' . ord($string[$x]) . ';'; | 
| 56 |  | } | 
| 57 |  | return $return; | 
| 58 |  |  | 
| 59 |  | case 'javascript': | 
| 60 |  | // escape quotes and backslashes, newlines, etc. | 
| 61 |  | return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/')); | 
| 62 |  |  | 
| 63 |  | case 'mail': | 
| 64 |  | // safe way to display e-mail address on a web page | 
| 65 |  | return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string); | 
| 66 |  |  | 
| 67 | case 'hexentity': | case 'nonstd': | 
| 68 | for ($x=0; $x < strlen($string); $x++) { | // escape non-standard chars, such as ms document quotes | 
| 69 | $return .= '&#x' . bin2hex($string[$x]) . ';'; | $_res = ''; | 
| 70 | } | for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) { | 
| 71 | return $return; | $_ord = ord($string{$_i}); | 
| 72 |  | // non-standard char, escape it | 
| 73 |  | if($_ord >= 126){ | 
| 74 |  | $_res .= '&#' . $_ord . ';'; | 
| 75 |  | } | 
| 76 |  | else { | 
| 77 |  | $_res .= $string{$_i}; | 
| 78 |  | } | 
| 79 |  | } | 
| 80 |  | return $_res; | 
| 81 |  |  | 
| 82 | default: | default: | 
| 83 | return $string; | return $string; |