/fuel/application/libraries/dwoo/Dwoo/Security/Policy.php

https://github.com/rodrigowebe/FUEL-CMS · PHP · 304 lines · 131 code · 22 blank · 151 comment · 12 complexity · dd8b3d0a637c45538bc1296fa36af687 MD5 · raw file

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