source: branches/0.4/web/addons/job_monarch/dwoo/Dwoo/Security/Policy.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 7.7 KB
Line 
1<?php
2
3/**
4 * represents the security settings of a dwoo instance, it can be passed around to different dwoo instances
5 *
6 * This software is provided 'as-is', without any express or implied warranty.
7 * In no event will the authors be held liable for any damages arising from the use of this software.
8 *
9 * @author     Jordi Boggiano <j.boggiano@seld.be>
10 * @copyright  Copyright (c) 2008, Jordi Boggiano
11 * @license    http://dwoo.org/LICENSE   Modified BSD License
12 * @link       http://dwoo.org/
13 * @version    1.0.0
14 * @date       2008-10-23
15 * @package    Dwoo
16 */
17class Dwoo_Security_Policy
18{
19        /**#@+
20         * php handling constants, defaults to PHP_REMOVE
21         *
22         * PHP_REMOVE : remove all <?php ?> (+ short tags if your short tags option is on) from the input template
23         * PHP_ALLOW : leave them as they are
24         * PHP_ENCODE : run htmlentities over them
25         *
26         * @var int
27         */
28        const PHP_ENCODE = 1;
29        const PHP_REMOVE = 2;
30        const PHP_ALLOW = 3;
31        /**#@-*/
32
33        /**#@+
34         * constant handling constants, defaults to CONST_DISALLOW
35         *
36         * CONST_DISALLOW : throw an error if {$dwoo.const.*} is used in the template
37         * CONST_ALLOW : allow {$dwoo.const.*} calls
38         */
39        const CONST_DISALLOW = false;
40        const CONST_ALLOW = true;
41        /**#@-*/
42
43        /**
44         * php functions that are allowed to be used within the template
45         *
46         * @var array
47         */
48        protected $allowedPhpFunctions = array
49        (
50                'str_repeat' => true,
51                'number_format' => true,
52                'htmlentities' => true,
53                'htmlspecialchars' => true,
54                'long2ip' => true,
55                'strlen' => true,
56                'list' => true,
57                'empty' => true,
58                'count' => true,
59                'sizeof' => true,
60                'in_array' => true,
61                'is_array' => true,
62        );
63
64        /**
65         * methods that are allowed to be used within the template
66         *
67         * @var array
68         */
69        protected $allowedMethods = array();
70
71        /**
72         * paths that are safe to use with include or other file-access plugins
73         *
74         * @var array
75         */
76        protected $allowedDirectories = array();
77
78        /**
79         * stores the php handling level
80         *
81         * defaults to Dwoo_Security_Policy::PHP_REMOVE
82         *
83         * @var int
84         */
85        protected $phpHandling = self::PHP_REMOVE;
86
87        /**
88         * stores the constant handling level
89         *
90         * defaults to Dwoo_Security_Policy::CONST_DISALLOW
91         *
92         * @var bool
93         */
94        protected $constHandling = self::CONST_DISALLOW;
95
96        /**
97         * adds a php function to the allowed list
98         *
99         * @param mixed $func function name or array of function names
100         */
101        public function allowPhpFunction($func)
102        {
103                if (is_array($func))
104                        foreach ($func as $fname)
105                                $this->allowedPhpFunctions[strtolower($fname)] = true;
106                else
107                        $this->allowedPhpFunctions[strtolower($func)] = true;
108        }
109
110        /**
111         * removes a php function from the allowed list
112         *
113         * @param mixed $func function name or array of function names
114         */
115        public function disallowPhpFunction($func)
116        {
117                if (is_array($func))
118                        foreach ($func as $fname)
119                                unset($this->allowedPhpFunctions[strtolower($fname)]);
120                else
121                        unset($this->allowedPhpFunctions[strtolower($func)]);
122        }
123
124        /**
125         * returns the list of php functions allowed to run, note that the function names
126         * are stored in the array keys and not values
127         *
128         * @return array
129         */
130        public function getAllowedPhpFunctions()
131        {
132                return $this->allowedPhpFunctions;
133        }
134
135        /**
136         * adds a class method to the allowed list, this must be used for
137         * both static and non static method by providing the class name
138         * and method name to use
139         *
140         * @param mixed $class class name or array of array('class', 'method') couples
141         * @param string $method method name
142         */
143        public function allowMethod($class, $method = null)
144        {
145                if (is_array($class))
146                        foreach ($class as $elem)
147                                $this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])] = true;
148                else
149                        $this->allowedMethods[strtolower($class)][strtolower($method)] = true;
150        }
151
152        /**
153         * removes a class method from the allowed list
154         *
155         * @param mixed $class class name or array of array('class', 'method') couples
156         * @param string $method method name
157         */
158        public function disallowMethod($class, $method = null)
159        {
160                if (is_array($class))
161                        foreach ($class as $elem)
162                                unset($this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])]);
163                else
164                        unset($this->allowedMethods[strtolower($class)][strtolower($method)]);
165        }
166
167        /**
168         * returns the list of class methods allowed to run, note that the class names
169         * and method names are stored in the array keys and not values
170         *
171         * @return array
172         */
173        public function getAllowedMethods()
174        {
175                return $this->allowedMethods;
176        }
177
178        /**
179         * adds a directory to the safelist for includes and other file-access plugins
180         *
181         * note that all the includePath directories you provide to the Dwoo_Template_File class
182         * are automatically marked as safe
183         *
184         * @param mixed $path a path name or an array of paths
185         */
186        public function allowDirectory($path)
187        {
188                if (is_array($path))
189                        foreach ($path as $dir)
190                                $this->allowedDirectories[realpath($dir)] = true;
191                else
192                        $this->allowedDirectories[realpath($path)] = true;
193        }
194
195        /**
196         * removes a directory from the safelist
197         *
198         * @param mixed $path a path name or an array of paths
199         */
200        public function disallowDirectory($path)
201        {
202                if (is_array($path))
203                        foreach ($path as $dir)
204                                unset($this->allowedDirectories[realpath($dir)]);
205                else
206                        unset($this->allowedDirectories[realpath($path)]);
207        }
208
209        /**
210         * returns the list of safe paths, note that the paths are stored in the array
211         * keys and not values
212         *
213         * @return array
214         */
215        public function getAllowedDirectories()
216        {
217                return $this->allowedDirectories;
218        }
219
220        /**
221         * sets the php handling level, defaults to REMOVE
222         *
223         * @param int $level one of the Dwoo_Security_Policy::PHP_* constants
224         */
225        public function setPhpHandling($level = self::PHP_REMOVE)
226        {
227                $this->phpHandling = $level;
228        }
229
230        /**
231         * returns the php handling level
232         *
233         * @return int the current level, one of the Dwoo_Security_Policy::PHP_* constants
234         */
235        public function getPhpHandling()
236        {
237                return $this->phpHandling;
238        }
239
240        /**
241         * sets the constant handling level, defaults to CONST_DISALLOW
242         *
243         * @param bool $level one of the Dwoo_Security_Policy::CONST_* constants
244         */
245        public function setConstantHandling($level = self::CONST_DISALLOW)
246        {
247                $this->constHandling = $level;
248        }
249
250        /**
251         * returns the constant handling level
252         *
253         * @return bool the current level, one of the Dwoo_Security_Policy::CONST_* constants
254         */
255        public function getConstantHandling()
256        {
257                return $this->constHandling;
258        }
259
260        /**
261         * this is used at run time to check whether method calls are allowed or not
262         *
263         * @param Dwoo_Core $dwoo dwoo instance that calls this
264         * @param object $obj any object on which the method must be called
265         * @param string $method lowercased method name
266         * @param array $args arguments array
267         * @return mixed result of method call or unll + E_USER_NOTICE if not allowed
268         */
269        public function callMethod(Dwoo_Core $dwoo, $obj, $method, $args)
270        {
271                foreach ($this->allowedMethods as $class => $methods) {
272                        if (!isset($methods[$method])) {
273                                continue;
274                        }
275                        if ($obj instanceof $class) {
276                                return call_user_func_array(array($obj, $method), $args);
277                        }
278                }
279                $dwoo->triggerError('The current security policy prevents you from calling '.get_class($obj).'::'.$method.'()');
280                return null;
281        }
282
283        /**
284         * this is used at compile time to check whether static method calls are allowed or not
285         *
286         * @param mixed $class lowercased class name or array('class', 'method') couple
287         * @param string $method lowercased method name
288         * @return bool
289         */
290        public function isMethodAllowed($class, $method = null) {
291                if (is_array($class)) {
292                        list($class, $method) = $class;
293                }
294                foreach ($this->allowedMethods as $allowedClass => $methods) {
295                        if (!isset($methods[$method])) {
296                                continue;
297                        }
298                        if ($class === $allowedClass || is_subclass_of($class, $allowedClass)) {
299                                return true;
300                        }
301                }
302                return false;
303        }
304}
Note: See TracBrowser for help on using the repository browser.