source: pro-violet-viettel/sourcecode/application/third_party/Twig/Sandbox/SecurityPolicy.php

Last change on this file was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 3.5 KB
Line 
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
12/**
13 * Represents a security policy which need to be enforced when sandbox mode is enabled.
14 *
15 * @package    twig
16 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17 */
18class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyInterface
19{
20    protected $allowedTags;
21    protected $allowedFilters;
22    protected $allowedMethods;
23    protected $allowedProperties;
24    protected $allowedFunctions;
25
26    public function __construct(array $allowedTags = array(), array $allowedFilters = array(), array $allowedMethods = array(), array $allowedProperties = array(), array $allowedFunctions = array())
27    {
28        $this->allowedTags = $allowedTags;
29        $this->allowedFilters = $allowedFilters;
30        $this->allowedMethods = $allowedMethods;
31        $this->allowedProperties = $allowedProperties;
32        $this->allowedFunctions = $allowedFunctions;
33    }
34
35    public function setAllowedTags(array $tags)
36    {
37        $this->allowedTags = $tags;
38    }
39
40    public function setAllowedFilters(array $filters)
41    {
42        $this->allowedFilters = $filters;
43    }
44
45    public function setAllowedMethods(array $methods)
46    {
47        $this->allowedMethods = $methods;
48    }
49
50    public function setAllowedProperties(array $properties)
51    {
52        $this->allowedProperties = $properties;
53    }
54
55    public function setAllowedFunctions(array $functions)
56    {
57        $this->allowedFunctions = $functions;
58    }
59
60    public function checkSecurity($tags, $filters, $functions)
61    {
62        foreach ($tags as $tag) {
63            if (!in_array($tag, $this->allowedTags)) {
64                throw new Twig_Sandbox_SecurityError(sprintf('Tag "%s" is not allowed.', $tag));
65            }
66        }
67
68        foreach ($filters as $filter) {
69            if (!in_array($filter, $this->allowedFilters)) {
70                throw new Twig_Sandbox_SecurityError(sprintf('Filter "%s" is not allowed.', $filter));
71            }
72        }
73
74        foreach ($functions as $function) {
75            if (!in_array($function, $this->allowedFunctions)) {
76                throw new Twig_Sandbox_SecurityError(sprintf('Function "%s" is not allowed.', $function));
77            }
78        }
79    }
80
81    public function checkMethodAllowed($obj, $method)
82    {
83        if ($obj instanceof Twig_TemplateInterface || $obj instanceof Twig_Markup) {
84            return true;
85        }
86
87        $allowed = false;
88        foreach ($this->allowedMethods as $class => $methods) {
89            if ($obj instanceof $class) {
90                $allowed = in_array($method, is_array($methods) ? $methods : array($methods));
91
92                break;
93            }
94        }
95
96        if (!$allowed) {
97            throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, get_class($obj)));
98        }
99    }
100
101    public function checkPropertyAllowed($obj, $property)
102    {
103        $allowed = false;
104        foreach ($this->allowedProperties as $class => $properties) {
105            if ($obj instanceof $class) {
106                $allowed = in_array($property, is_array($properties) ? $properties : array($properties));
107
108                break;
109            }
110        }
111
112        if (!$allowed) {
113            throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, get_class($obj)));
114        }
115    }
116}
Note: See TracBrowser for help on using the repository browser.