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