Source for file Policy.php

Documentation is available at Policy.php

  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.  * This file is released under the LGPL
  10.  * "GNU Lesser General Public License"
  11.  * More information can be found here:
  12.  * {@link http://www.gnu.org/copyleft/lesser.html}
  13.  *
  14.  * @author     Jordi Boggiano <[email protected]>
  15.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  16.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  17.  * @link       http://dwoo.org/
  18.  * @version    0.9.1
  19.  * @date       2008-05-30
  20.  * @package    Dwoo
  21.  */
  22. {
  23.     /**#@+
  24.      * php handling constants, defaults to PHP_REMOVE
  25.      *
  26.      * PHP_REMOVE : remove all <?php ?> (+ short tags if your short tags option is on) from the input template
  27.      * PHP_ALLOW : leave them as they are
  28.      * PHP_ENCODE : run htmlentities over them
  29.      *
  30.      * @var int
  31.      */
  32.     const PHP_ENCODE 1;
  33.     const PHP_REMOVE 2;
  34.     const PHP_ALLOW 3;
  35.     /**#@-*/
  36.  
  37.     /**#@+
  38.      * constant handling constants, defaults to CONST_DISALLOW
  39.      *
  40.      * CONST_DISALLOW : throw an error if {$dwoo.const.*} is used in the template
  41.      * CONST_ALLOW : allow {$dwoo.const.*} calls
  42.      */
  43.     const CONST_DISALLOW false;
  44.     const CONST_ALLOW true;
  45.     /**#@-*/
  46.  
  47.     /**
  48.      * php functions that are allowed to be used within the template
  49.      *
  50.      * @var array 
  51.      */
  52.     protected $allowedPhpFunctions = array
  53.     (
  54.         'str_repeat''number_format''htmlentities''htmlspecialchars',
  55.         'long2ip''strlen''list''empty''count''sizeof''in_array''is_array',
  56.     );
  57.  
  58.     /**
  59.      * paths that are safe to use with include or other file-access plugins
  60.      *
  61.      * @var array 
  62.      */
  63.     protected $allowedDirectories = array();
  64.  
  65.     /**
  66.      * stores the php handling level
  67.      *
  68.      * defaults to Dwoo_Security_Policy::PHP_REMOVE
  69.      *
  70.      * @var int 
  71.      */
  72.     protected $phpHandling = self::PHP_REMOVE;
  73.  
  74.     /**
  75.      * stores the constant handling level
  76.      *
  77.      * defaults to Dwoo_Security_Policy::CONST_DISALLOW
  78.      *
  79.      * @var bool 
  80.      */
  81.     protected $constHandling = self::CONST_DISALLOW;
  82.  
  83.     /**
  84.      * adds a php function to the allowed list
  85.      *
  86.      * @param mixed $func function name or array of function names
  87.      */
  88.     public function allowPhpFunction($func)
  89.     {
  90.         if (is_array($func))
  91.             foreach ($func as $fname)
  92.                 $this->allowedPhpFunctions[strtolower($fname)true;
  93.         else
  94.             $this->allowedPhpFunctions[strtolower($func)true;
  95.     }
  96.  
  97.     /**
  98.      * removes a php function from the allowed list
  99.      *
  100.      * @param mixed $func function name or array of function names
  101.      */
  102.     public function disallowPhpFunction($func)
  103.     {
  104.         if (is_array($func))
  105.             foreach ($func as $fname)
  106.                 unset($this->allowedPhpFunctions[strtolower($fname)]);
  107.         else
  108.             unset($this->allowedPhpFunctions[strtolower($func)]);
  109.     }
  110.  
  111.     /**
  112.      * returns the list of php functions allowed to run, note that the function names
  113.      * are stored in the array keys and not values
  114.      *
  115.      * @return array 
  116.      */
  117.     public function getAllowedPhpFunctions()
  118.     {
  119.         return $this->allowedPhpFunctions;
  120.     }
  121.  
  122.     /**
  123.      * adds a directory to the safelist for includes and other file-access plugins
  124.      *
  125.      * @param mixed $path a path name or an array of paths
  126.      */
  127.     public function allowDirectory($path)
  128.     {
  129.         if (is_array($path))
  130.             foreach ($path as $dir)
  131.                 $this->allowedDirectories[realpath($dir)true;
  132.         else
  133.             $this->allowedDirectories[realpath($path)true;
  134.     }
  135.  
  136.     /**
  137.      * removes a directory from the safelist
  138.      *
  139.      * @param mixed $path a path name or an array of paths
  140.      */
  141.     public function disallowDirectory($path)
  142.     {
  143.         if (is_array($path))
  144.             foreach ($path as $dir)
  145.                 unset($this->allowedDirectories[realpath($dir)]);
  146.         else
  147.             unset($this->allowedDirectories[realpath($path)]);
  148.     }
  149.  
  150.     /**
  151.      * returns the list of safe paths, note that the paths are stored in the array
  152.      * keys and not values
  153.      *
  154.      * @return array 
  155.      */
  156.     public function getAllowedDirectories()
  157.     {
  158.         return $this->allowedDirectories;
  159.     }
  160.  
  161.     /**
  162.      * sets the php handling level, defaults to REMOVE
  163.      *
  164.      * @param int $level one of the Dwoo_Security_Policy::PHP_* constants
  165.      */
  166.     public function setPhpHandling($level self::PHP_REMOVE)
  167.     {
  168.         $this->phpHandling = $level;
  169.     }
  170.  
  171.     /**
  172.      * returns the php handling level
  173.      *
  174.      * @return int the current level, one of the Dwoo_Security_Policy::PHP_* constants
  175.      */
  176.     public function getPhpHandling()
  177.     {
  178.         return $this->phpHandling;
  179.     }
  180.  
  181.     /**
  182.      * sets the constant handling level, defaults to CONST_DISALLOW
  183.      *
  184.      * @param bool $level one of the Dwoo_Security_Policy::CONST_* constants
  185.      */
  186.     public function setConstantHandling($level self::CONST_DISALLOW)
  187.     {
  188.         $this->constHandling = $level;
  189.     }
  190.  
  191.     /**
  192.      * returns the constant handling level
  193.      *
  194.      * @return bool the current level, one of the Dwoo_Security_Policy::CONST_* constants
  195.      */
  196.     public function getConstantHandling()
  197.     {
  198.         return $this->constHandling;
  199.     }
  200. }

Documentation generated on Sun, 03 Aug 2008 15:12:43 +0200 by phpDocumentor 1.4.0