1469a1e042bd2c2be6e70c84d3c5b15da7c579e2
[yaffs-website] / vendor / twig / twig / lib / Twig / Extension / Sandbox.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 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  * @final
14  */
15 class Twig_Extension_Sandbox extends Twig_Extension
16 {
17     protected $sandboxedGlobally;
18     protected $sandboxed;
19     protected $policy;
20
21     public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
22     {
23         $this->policy = $policy;
24         $this->sandboxedGlobally = $sandboxed;
25     }
26
27     public function getTokenParsers()
28     {
29         return array(new Twig_TokenParser_Sandbox());
30     }
31
32     public function getNodeVisitors()
33     {
34         return array(new Twig_NodeVisitor_Sandbox());
35     }
36
37     public function enableSandbox()
38     {
39         $this->sandboxed = true;
40     }
41
42     public function disableSandbox()
43     {
44         $this->sandboxed = false;
45     }
46
47     public function isSandboxed()
48     {
49         return $this->sandboxedGlobally || $this->sandboxed;
50     }
51
52     public function isSandboxedGlobally()
53     {
54         return $this->sandboxedGlobally;
55     }
56
57     public function setSecurityPolicy(Twig_Sandbox_SecurityPolicyInterface $policy)
58     {
59         $this->policy = $policy;
60     }
61
62     public function getSecurityPolicy()
63     {
64         return $this->policy;
65     }
66
67     public function checkSecurity($tags, $filters, $functions)
68     {
69         if ($this->isSandboxed()) {
70             $this->policy->checkSecurity($tags, $filters, $functions);
71         }
72     }
73
74     public function checkMethodAllowed($obj, $method)
75     {
76         if ($this->isSandboxed()) {
77             $this->policy->checkMethodAllowed($obj, $method);
78         }
79     }
80
81     public function checkPropertyAllowed($obj, $method)
82     {
83         if ($this->isSandboxed()) {
84             $this->policy->checkPropertyAllowed($obj, $method);
85         }
86     }
87
88     public function ensureToStringAllowed($obj)
89     {
90         if ($this->isSandboxed() && is_object($obj)) {
91             $this->policy->checkMethodAllowed($obj, '__toString');
92         }
93
94         return $obj;
95     }
96
97     public function getName()
98     {
99         return 'sandbox';
100     }
101 }