[345] | 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 | * |
---|
| 17 | * Type: modifier<br> |
---|
| 18 | * Name: escape<br> |
---|
| 19 | * Purpose: escape string for output |
---|
| 20 | * |
---|
| 21 | * @link http://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual) |
---|
| 22 | * @author Rodney Rehm |
---|
| 23 | * @param array $params parameters |
---|
| 24 | * @return string with compiled code |
---|
| 25 | */ |
---|
| 26 | function smarty_modifiercompiler_escape($params, $compiler) |
---|
| 27 | { |
---|
| 28 | try { |
---|
| 29 | $esc_type = smarty_literal_compiler_param($params, 1, 'html'); |
---|
| 30 | $char_set = smarty_literal_compiler_param($params, 2, Smarty::$_CHARSET); |
---|
| 31 | $double_encode = smarty_literal_compiler_param($params, 3, true); |
---|
| 32 | |
---|
| 33 | if (!$char_set) { |
---|
| 34 | $char_set = Smarty::$_CHARSET; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | switch ($esc_type) { |
---|
| 38 | case 'html': |
---|
| 39 | return 'htmlspecialchars(' |
---|
| 40 | . $params[0] .', ENT_QUOTES, ' |
---|
| 41 | . var_export($char_set, true) . ', ' |
---|
| 42 | . var_export($double_encode, true) . ')'; |
---|
| 43 | |
---|
| 44 | case 'htmlall': |
---|
| 45 | if (Smarty::$_MBSTRING) { |
---|
| 46 | return 'mb_convert_encoding(htmlspecialchars(' |
---|
| 47 | . $params[0] .', ENT_QUOTES, ' |
---|
| 48 | . var_export($char_set, true) . ', ' |
---|
| 49 | . var_export($double_encode, true) |
---|
| 50 | . '), "HTML-ENTITIES", ' |
---|
| 51 | . var_export($char_set, true) . ')'; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // no MBString fallback |
---|
| 55 | return 'htmlentities(' |
---|
| 56 | . $params[0] .', ENT_QUOTES, ' |
---|
| 57 | . var_export($char_set, true) . ', ' |
---|
| 58 | . var_export($double_encode, true) . ')'; |
---|
| 59 | |
---|
| 60 | case 'url': |
---|
| 61 | return 'rawurlencode(' . $params[0] . ')'; |
---|
| 62 | |
---|
| 63 | case 'urlpathinfo': |
---|
| 64 | return 'str_replace("%2F", "/", rawurlencode(' . $params[0] . '))'; |
---|
| 65 | |
---|
| 66 | case 'quotes': |
---|
| 67 | // escape unescaped single quotes |
---|
| 68 | return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[0] . ')'; |
---|
| 69 | |
---|
| 70 | case 'javascript': |
---|
| 71 | // escape quotes and backslashes, newlines, etc. |
---|
| 72 | return 'strtr(' . $params[0] . ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/" ))'; |
---|
| 73 | |
---|
| 74 | } |
---|
| 75 | } catch(SmartyException $e) { |
---|
| 76 | // pass through to regular plugin fallback |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | // could not optimize |escape call, so fallback to regular plugin |
---|
| 80 | if ($compiler->tag_nocache | $compiler->nocache) { |
---|
| 81 | $compiler->template->required_plugins['nocache']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php'; |
---|
| 82 | $compiler->template->required_plugins['nocache']['escape']['modifier']['function'] = 'smarty_modifier_escape'; |
---|
| 83 | } else { |
---|
| 84 | $compiler->template->required_plugins['compiled']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php'; |
---|
| 85 | $compiler->template->required_plugins['compiled']['escape']['modifier']['function'] = 'smarty_modifier_escape'; |
---|
| 86 | } |
---|
| 87 | return 'smarty_modifier_escape(' . join( ', ', $params ) . ')'; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | ?> |
---|