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
class Twig_Extension_Sandbox extends Twig_Extension
12
{
13
    protected $sandboxedGlobally;
14
    protected $sandboxed;
15
    protected $policy;
16

    
17
    public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
18
    {
19
        $this->policy = $policy;
20
        $this->sandboxedGlobally = $sandboxed;
21
    }
22

    
23
    public function getTokenParsers()
24
    {
25
        return array(new Twig_TokenParser_Sandbox());
26
    }
27

    
28
    public function getNodeVisitors()
29
    {
30
        return array(new Twig_NodeVisitor_Sandbox());
31
    }
32

    
33
    public function enableSandbox()
34
    {
35
        $this->sandboxed = true;
36
    }
37

    
38
    public function disableSandbox()
39
    {
40
        $this->sandboxed = false;
41
    }
42

    
43
    public function isSandboxed()
44
    {
45
        return $this->sandboxedGlobally || $this->sandboxed;
46
    }
47

    
48
    public function isSandboxedGlobally()
49
    {
50
        return $this->sandboxedGlobally;
51
    }
52

    
53
    public function setSecurityPolicy(Twig_Sandbox_SecurityPolicyInterface $policy)
54
    {
55
        $this->policy = $policy;
56
    }
57

    
58
    public function getSecurityPolicy()
59
    {
60
        return $this->policy;
61
    }
62

    
63
    public function checkSecurity($tags, $filters, $functions)
64
    {
65
        if ($this->isSandboxed()) {
66
            $this->policy->checkSecurity($tags, $filters, $functions);
67
        }
68
    }
69

    
70
    public function checkMethodAllowed($obj, $method)
71
    {
72
        if ($this->isSandboxed()) {
73
            $this->policy->checkMethodAllowed($obj, $method);
74
        }
75
    }
76

    
77
    public function checkPropertyAllowed($obj, $method)
78
    {
79
        if ($this->isSandboxed()) {
80
            $this->policy->checkPropertyAllowed($obj, $method);
81
        }
82
    }
83

    
84
    public function ensureToStringAllowed($obj)
85
    {
86
        if ($this->isSandboxed() && is_object($obj)) {
87
            $this->policy->checkMethodAllowed($obj, '__toString');
88
        }
89

    
90
        return $obj;
91
    }
92

    
93
    public function getName()
94
    {
95
        return 'sandbox';
96
    }
97
}
(8-8/10)