4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
15 class Twig_Extension_Sandbox extends Twig_Extension
17 protected $sandboxedGlobally;
21 public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
23 $this->policy = $policy;
24 $this->sandboxedGlobally = $sandboxed;
27 public function getTokenParsers()
29 return array(new Twig_TokenParser_Sandbox());
32 public function getNodeVisitors()
34 return array(new Twig_NodeVisitor_Sandbox());
37 public function enableSandbox()
39 $this->sandboxed = true;
42 public function disableSandbox()
44 $this->sandboxed = false;
47 public function isSandboxed()
49 return $this->sandboxedGlobally || $this->sandboxed;
52 public function isSandboxedGlobally()
54 return $this->sandboxedGlobally;
57 public function setSecurityPolicy(Twig_Sandbox_SecurityPolicyInterface $policy)
59 $this->policy = $policy;
62 public function getSecurityPolicy()
67 public function checkSecurity($tags, $filters, $functions)
69 if ($this->isSandboxed()) {
70 $this->policy->checkSecurity($tags, $filters, $functions);
74 public function checkMethodAllowed($obj, $method)
76 if ($this->isSandboxed()) {
77 $this->policy->checkMethodAllowed($obj, $method);
81 public function checkPropertyAllowed($obj, $method)
83 if ($this->isSandboxed()) {
84 $this->policy->checkPropertyAllowed($obj, $method);
88 public function ensureToStringAllowed($obj)
90 if ($this->isSandboxed() && is_object($obj)) {
91 $this->policy->checkMethodAllowed($obj, '__toString');
97 public function getName()
103 class_alias('Twig_Extension_Sandbox', 'Twig\Extension\SandboxExtension', false);