4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 class Twig_Extension_Sandbox
extends Twig_Extension
13 protected $sandboxedGlobally;
17 public function __construct(Twig_Sandbox_SecurityPolicyInterface
$policy, $sandboxed = false)
19 $this->policy
= $policy;
20 $this->sandboxedGlobally
= $sandboxed;
24 * Returns the token parser instances to add to the existing list.
26 * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
28 public function getTokenParsers()
30 return array(new Twig_TokenParser_Sandbox());
34 * Returns the node visitor instances to add to the existing list.
36 * @return array An array of Twig_NodeVisitorInterface instances
38 public function getNodeVisitors()
40 return array(new Twig_NodeVisitor_Sandbox());
43 public function enableSandbox()
45 $this->sandboxed
= true;
48 public function disableSandbox()
50 $this->sandboxed
= false;
53 public function isSandboxed()
55 return $this->sandboxedGlobally
|| $this->sandboxed
;
58 public function isSandboxedGlobally()
60 return $this->sandboxedGlobally
;
63 public function setSecurityPolicy(Twig_Sandbox_SecurityPolicyInterface
$policy)
65 $this->policy
= $policy;
68 public function getSecurityPolicy()
73 public function checkSecurity($tags, $filters, $functions)
75 if ($this->isSandboxed()) {
76 $this->policy
->checkSecurity($tags, $filters, $functions);
80 public function checkMethodAllowed($obj, $method)
82 if ($this->isSandboxed()) {
83 $this->policy
->checkMethodAllowed($obj, $method);
87 public function checkPropertyAllowed($obj, $method)
89 if ($this->isSandboxed()) {
90 $this->policy
->checkPropertyAllowed($obj, $method);
94 public function ensureToStringAllowed($obj)
96 if (is_object($obj)) {
97 $this->policy
->checkMethodAllowed($obj, '__toString');
104 * Returns the name of the extension.
106 * @return string The extension name
108 public function getName()