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