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