Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
/**
13
 * Twig_NodeVisitor_Escaper implements output escaping.
14
 *
15
 * @author Fabien Potencier <fabien@symfony.com>
16
 */
17
class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor
18
{
19
    protected $statusStack = array();
20
    protected $blocks = array();
21
    protected $safeAnalysis;
22
    protected $traverser;
23
    protected $defaultStrategy = false;
24
    protected $safeVars = array();
25

    
26
    public function __construct()
27
    {
28
        $this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis();
29
    }
30

    
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
35
    {
36
        if ($node instanceof Twig_Node_Module) {
37
            if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename'))) {
38
                $this->defaultStrategy = $defaultStrategy;
39
            }
40
            $this->safeVars = array();
41
        } elseif ($node instanceof Twig_Node_AutoEscape) {
42
            $this->statusStack[] = $node->getAttribute('value');
43
        } elseif ($node instanceof Twig_Node_Block) {
44
            $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
45
        } elseif ($node instanceof Twig_Node_Import) {
46
            $this->safeVars[] = $node->getNode('var')->getAttribute('name');
47
        }
48

    
49
        return $node;
50
    }
51

    
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
56
    {
57
        if ($node instanceof Twig_Node_Module) {
58
            $this->defaultStrategy = false;
59
            $this->safeVars = array();
60
        } elseif ($node instanceof Twig_Node_Expression_Filter) {
61
            return $this->preEscapeFilterNode($node, $env);
62
        } elseif ($node instanceof Twig_Node_Print) {
63
            return $this->escapePrintNode($node, $env, $this->needEscaping($env));
64
        }
65

    
66
        if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) {
67
            array_pop($this->statusStack);
68
        } elseif ($node instanceof Twig_Node_BlockReference) {
69
            $this->blocks[$node->getAttribute('name')] = $this->needEscaping($env);
70
        }
71

    
72
        return $node;
73
    }
74

    
75
    protected function escapePrintNode(Twig_Node_Print $node, Twig_Environment $env, $type)
76
    {
77
        if (false === $type) {
78
            return $node;
79
        }
80

    
81
        $expression = $node->getNode('expr');
82

    
83
        if ($this->isSafeFor($type, $expression, $env)) {
84
            return $node;
85
        }
86

    
87
        $class = get_class($node);
88

    
89
        return new $class(
90
            $this->getEscaperFilter($type, $expression),
91
            $node->getLine()
92
        );
93
    }
94

    
95
    protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env)
96
    {
97
        $name = $filter->getNode('filter')->getAttribute('value');
98

    
99
        $type = $env->getFilter($name)->getPreEscape();
100
        if (null === $type) {
101
            return $filter;
102
        }
103

    
104
        $node = $filter->getNode('node');
105
        if ($this->isSafeFor($type, $node, $env)) {
106
            return $filter;
107
        }
108

    
109
        $filter->setNode('node', $this->getEscaperFilter($type, $node));
110

    
111
        return $filter;
112
    }
113

    
114
    protected function isSafeFor($type, Twig_NodeInterface $expression, $env)
115
    {
116
        $safe = $this->safeAnalysis->getSafe($expression);
117

    
118
        if (null === $safe) {
119
            if (null === $this->traverser) {
120
                $this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis));
121
            }
122

    
123
            $this->safeAnalysis->setSafeVars($this->safeVars);
124

    
125
            $this->traverser->traverse($expression);
126
            $safe = $this->safeAnalysis->getSafe($expression);
127
        }
128

    
129
        return in_array($type, $safe) || in_array('all', $safe);
130
    }
131

    
132
    protected function needEscaping(Twig_Environment $env)
133
    {
134
        if (count($this->statusStack)) {
135
            return $this->statusStack[count($this->statusStack) - 1];
136
        }
137

    
138
        return $this->defaultStrategy ? $this->defaultStrategy : false;
139
    }
140

    
141
    protected function getEscaperFilter($type, Twig_NodeInterface $node)
142
    {
143
        $line = $node->getLine();
144
        $name = new Twig_Node_Expression_Constant('escape', $line);
145
        $args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line), new Twig_Node_Expression_Constant(null, $line), new Twig_Node_Expression_Constant(true, $line)));
146

    
147
        return new Twig_Node_Expression_Filter($node, $name, $args, $line);
148
    }
149

    
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function getPriority()
154
    {
155
        return 0;
156
    }
157
}
(1-1/4)