1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-19 10:55:56 +01:00
vichan/inc/lib/Twig/Extension/Sandbox.php

104 lines
2.2 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
/*
* This file is part of Twig.
*
2018-05-10 12:24:53 +02:00
* (c) Fabien Potencier
2011-10-05 06:22:53 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2018-05-10 12:24:53 +02:00
/**
* @final
*/
2011-10-05 06:22:53 +02:00
class Twig_Extension_Sandbox extends Twig_Extension
{
protected $sandboxedGlobally;
protected $sandboxed;
protected $policy;
public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
{
2018-05-10 12:24:53 +02:00
$this->policy = $policy;
2011-10-05 06:22:53 +02:00
$this->sandboxedGlobally = $sandboxed;
}
public function getTokenParsers()
{
return array(new Twig_TokenParser_Sandbox());
}
public function getNodeVisitors()
{
return array(new Twig_NodeVisitor_Sandbox());
}
public function enableSandbox()
{
$this->sandboxed = true;
}
public function disableSandbox()
{
$this->sandboxed = false;
}
public function isSandboxed()
{
return $this->sandboxedGlobally || $this->sandboxed;
}
public function isSandboxedGlobally()
{
return $this->sandboxedGlobally;
}
public function setSecurityPolicy(Twig_Sandbox_SecurityPolicyInterface $policy)
{
$this->policy = $policy;
}
public function getSecurityPolicy()
{
return $this->policy;
}
public function checkSecurity($tags, $filters, $functions)
{
if ($this->isSandboxed()) {
$this->policy->checkSecurity($tags, $filters, $functions);
}
}
public function checkMethodAllowed($obj, $method)
{
if ($this->isSandboxed()) {
$this->policy->checkMethodAllowed($obj, $method);
}
}
public function checkPropertyAllowed($obj, $method)
{
if ($this->isSandboxed()) {
$this->policy->checkPropertyAllowed($obj, $method);
}
}
public function ensureToStringAllowed($obj)
{
2018-05-10 12:24:53 +02:00
if ($this->isSandboxed() && is_object($obj)) {
2011-10-05 06:22:53 +02:00
$this->policy->checkMethodAllowed($obj, '__toString');
}
return $obj;
}
public function getName()
{
return 'sandbox';
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_Extension_Sandbox', 'Twig\Extension\SandboxExtension', false);