]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/plugins/modifiercompiler.escape.php
add smarty engine, remove some obsolete zot1 stuff
[friendica.git] / library / Smarty / libs / plugins / modifiercompiler.escape.php
1 <?php\r
2 /**\r
3  * Smarty plugin\r
4  *\r
5  * @package Smarty\r
6  * @subpackage PluginsModifierCompiler\r
7  */\r
8 \r
9 /**\r
10  * @ignore\r
11  */\r
12 require_once( SMARTY_PLUGINS_DIR .'shared.literal_compiler_param.php' );\r
13 \r
14 /**\r
15  * Smarty escape modifier plugin\r
16  *\r
17  * Type:     modifier<br>\r
18  * Name:     escape<br>\r
19  * Purpose:  escape string for output\r
20  *\r
21  * @link http://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual)\r
22  * @author Rodney Rehm\r
23  * @param array $params parameters\r
24  * @return string with compiled code\r
25  */\r
26 function smarty_modifiercompiler_escape($params, $compiler)\r
27 {\r
28     static $_double_encode = null;\r
29     if ($_double_encode === null) {\r
30         $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');\r
31     }\r
32     \r
33     try {\r
34         $esc_type = smarty_literal_compiler_param($params, 1, 'html');\r
35         $char_set = smarty_literal_compiler_param($params, 2, Smarty::$_CHARSET);\r
36         $double_encode = smarty_literal_compiler_param($params, 3, true);\r
37 \r
38         if (!$char_set) {\r
39             $char_set = Smarty::$_CHARSET;\r
40         }\r
41 \r
42         switch ($esc_type) {\r
43             case 'html':\r
44                 if ($_double_encode) {\r
45                     return 'htmlspecialchars('\r
46                         . $params[0] .', ENT_QUOTES, '\r
47                         . var_export($char_set, true) . ', '\r
48                         . var_export($double_encode, true) . ')';\r
49                 } else if ($double_encode) {\r
50                     return 'htmlspecialchars('\r
51                         . $params[0] .', ENT_QUOTES, '\r
52                         . var_export($char_set, true) . ')';\r
53                 } else {\r
54                     // fall back to modifier.escape.php\r
55                 }\r
56 \r
57             case 'htmlall':\r
58                 if (Smarty::$_MBSTRING) {\r
59                     if ($_double_encode) {\r
60                         // php >=5.3.2 - go native\r
61                         return 'mb_convert_encoding(htmlspecialchars('\r
62                             . $params[0] .', ENT_QUOTES, '\r
63                             . var_export($char_set, true) . ', '\r
64                             . var_export($double_encode, true)\r
65                             . '), "HTML-ENTITIES", '\r
66                             . var_export($char_set, true) . ')';\r
67                     } else if ($double_encode) {\r
68                         // php <5.3.2 - only handle double encoding\r
69                         return 'mb_convert_encoding(htmlspecialchars('\r
70                             . $params[0] .', ENT_QUOTES, '\r
71                             . var_export($char_set, true)\r
72                             . '), "HTML-ENTITIES", '\r
73                             . var_export($char_set, true) . ')';\r
74                     } else {\r
75                         // fall back to modifier.escape.php\r
76                     }\r
77                 }\r
78 \r
79                 // no MBString fallback\r
80                 if ($_double_encode) {\r
81                     // php >=5.3.2 - go native\r
82                     return 'htmlentities('\r
83                         . $params[0] .', ENT_QUOTES, '\r
84                         . var_export($char_set, true) . ', '\r
85                         . var_export($double_encode, true) . ')';\r
86                 } else if ($double_encode) {\r
87                     // php <5.3.2 - only handle double encoding\r
88                     return 'htmlentities('\r
89                         . $params[0] .', ENT_QUOTES, '\r
90                         . var_export($char_set, true) . ')';\r
91                 } else {\r
92                     // fall back to modifier.escape.php\r
93                 }\r
94 \r
95             case 'url':\r
96                 return 'rawurlencode(' . $params[0] . ')';\r
97 \r
98             case 'urlpathinfo':\r
99                 return 'str_replace("%2F", "/", rawurlencode(' . $params[0] . '))';\r
100 \r
101             case 'quotes':\r
102                 // escape unescaped single quotes\r
103                 return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[0] . ')';\r
104 \r
105             case 'javascript':\r
106                 // escape quotes and backslashes, newlines, etc.\r
107                 return 'strtr(' . $params[0] . ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/" ))';\r
108 \r
109         }\r
110     } catch(SmartyException $e) {\r
111         // pass through to regular plugin fallback\r
112     }\r
113 \r
114     // could not optimize |escape call, so fallback to regular plugin\r
115     if ($compiler->tag_nocache | $compiler->nocache) {\r
116         $compiler->template->required_plugins['nocache']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php';\r
117         $compiler->template->required_plugins['nocache']['escape']['modifier']['function'] = 'smarty_modifier_escape';\r
118     } else {\r
119         $compiler->template->required_plugins['compiled']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php';\r
120         $compiler->template->required_plugins['compiled']['escape']['modifier']['function'] = 'smarty_modifier_escape';\r
121     }\r
122     return 'smarty_modifier_escape(' . join( ', ', $params ) . ')';\r
123 }\r
124 \r
125 ?>