]> git.sur5r.net Git - contagged/blob - inc/smarty/plugins/modifier.escape.php
7d3230ba73e429cb688e5e5b4906c9413df27486
[contagged] / inc / smarty / plugins / modifier.escape.php
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  * @author   Monte Ohrt <monte at ohrt dot com>
18  * @param string
19  * @param html|htmlall|url|quotes|hex|hexentity|javascript
20  * @return string
21  */
22 function smarty_modifier_escape($string, $esc_type = 'html', $char_set = 'ISO-8859-1')
23 {
24     switch ($esc_type) {
25         case 'html':
26             return htmlspecialchars($string, ENT_QUOTES, $char_set);
27
28         case 'htmlall':
29             return htmlentities($string, ENT_QUOTES, $char_set);
30
31         case 'url':
32             return rawurlencode($string);
33
34         case 'urlpathinfo':
35             return str_replace('%2F','/',rawurlencode($string));
36
37         case 'quotes':
38             // escape unescaped single quotes
39             return preg_replace("%(?<!\\\\)'%", "\\'", $string);
40
41         case 'hex':
42             // escape every character into hex
43             $return = '';
44             for ($x=0; $x < strlen($string); $x++) {
45                 $return .= '%' . bin2hex($string[$x]);
46             }
47             return $return;
48
49         case 'phone':
50             $return = str_replace('+','00',$string);
51             $return = preg_replace('/[^0-9]+/','',$return);
52             return $return;
53
54         case 'hexentity':
55             $return = '';
56             for ($x=0; $x < strlen($string); $x++) {
57                 $return .= '&#x' . bin2hex($string[$x]) . ';';
58             }
59             return $return;
60
61         case 'decentity':
62             $return = '';
63             for ($x=0; $x < strlen($string); $x++) {
64                 $return .= '&#' . ord($string[$x]) . ';';
65             }
66             return $return;
67
68         case 'javascript':
69             // escape quotes and backslashes, newlines, etc.
70             return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
71
72         case 'mail':
73             // safe way to display e-mail address on a web page
74             return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string);
75
76         case 'nonstd':
77            // escape non-standard chars, such as ms document quotes
78            $_res = '';
79            for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
80                $_ord = ord(substr($string, $_i, 1));
81                // non-standard char, escape it
82                if($_ord >= 126){
83                    $_res .= '&#' . $_ord . ';';
84                }
85                else {
86                    $_res .= substr($string, $_i, 1);
87                }
88            }
89            return $_res;
90
91         default:
92             return $string;
93     }
94 }
95
96 /* vim: set expandtab: */
97
98 ?>