Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsModifierCompiler
7
 */
8
/**
9
 * Smarty escape modifier plugin
10
 * Type:     modifier
11
 * Name:     escape
12
 * Purpose:  escape string for output
13
 *
14
 * @link   https://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual)
15
 * @author Rodney Rehm
16
 *
17
 * @param array                                $params parameters
18
 * @param Smarty_Internal_TemplateCompilerBase $compiler
19
 *
20
 * @return string with compiled code
21
 * @throws SmartyException
22
 */
23
function smarty_modifiercompiler_escape($params, Smarty_Internal_TemplateCompilerBase $compiler)
24
{
25
    $compiler->template->_checkPlugins(
26
        array(
27
            array(
28
                'function' => 'smarty_literal_compiler_param',
29
                'file'     => SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php'
30
            )
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
        if (!$char_set) {
38
            $char_set = Smarty::$_CHARSET;
39
        }
40
        switch ($esc_type) {
41
            case 'html':
42
                return 'htmlspecialchars((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
43
                    var_export($double_encode, true) . ')';
44
            // no break
45
            case 'htmlall':
46
                if (Smarty::$_MBSTRING) {
47
                    return 'htmlentities(mb_convert_encoding((string)' . $params[ 0 ] . ', \'UTF-8\', ' .
48
                        var_export($char_set, true) . '), ENT_QUOTES, \'UTF-8\', ' .
49
                        var_export($double_encode, true) . ')';
50
                }
51
                // no MBString fallback
52
                return 'htmlentities((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
53
                    var_export($double_encode, true) . ')';
54
            // no break
55
            case 'url':
56
                return 'rawurlencode((string)' . $params[ 0 ] . ')';
57
            case 'urlpathinfo':
58
                return 'str_replace("%2F", "/", rawurlencode((string)' . $params[ 0 ] . '))';
59
            case 'quotes':
60
                // escape unescaped single quotes
61
                return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'", (string)' . $params[ 0 ] . ')';
62
            case 'javascript':
63
                // escape quotes and backslashes, newlines, etc.
64
                // see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
65
                return 'strtr((string)' .
66
                       $params[ 0 ] .
991 lars 67
                       ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r",
68
                       "\\n" => "\\\n", "</" => "<\/", "<!--" => "<\!--", "<s" => "<\s", "<S" => "<\S",
69
                       "`" => "\\\\`", "\${" => "\\\\\\$\\{"))';
148 lars 70
        }
71
    } catch (SmartyException $e) {
72
        // pass through to regular plugin fallback
73
    }
74
    // could not optimize |escape call, so fallback to regular plugin
75
    if ($compiler->template->caching && ($compiler->tag_nocache | $compiler->nocache)) {
76
        $compiler->required_plugins[ 'nocache' ][ 'escape' ][ 'modifier' ][ 'file' ] =
77
            SMARTY_PLUGINS_DIR . 'modifier.escape.php';
78
        $compiler->required_plugins[ 'nocache' ][ 'escape' ][ 'modifier' ][ 'function' ] =
79
            'smarty_modifier_escape';
80
    } else {
81
        $compiler->required_plugins[ 'compiled' ][ 'escape' ][ 'modifier' ][ 'file' ] =
82
            SMARTY_PLUGINS_DIR . 'modifier.escape.php';
83
        $compiler->required_plugins[ 'compiled' ][ 'escape' ][ 'modifier' ][ 'function' ] =
84
            'smarty_modifier_escape';
85
    }
86
    return 'smarty_modifier_escape(' . join(', ', $params) . ')';
87
}