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 <j.boggiano@seld.be>
  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.      * note that all the includePath directories you provide to the Dwoo_Template_File class
  126.      * are automatically marked as safe
  127.      *
  128.      * @param mixed $path a path name or an array of paths
  129.      */
  130.     public function allowDirectory($path)
  131.     {
  132.         if (is_array($path))
  133.             foreach ($path as $dir)
  134.                 $this->allowedDirectories[realpath($dir)true;
  135.         else
  136.             $this->allowedDirectories[realpath($path)true;
  137.     }
  138.  
  139.     /**
  140.      * removes a directory from the safelist
  141.      *
  142.      * @param mixed $path a path name or an array of paths
  143.      */
  144.     public function disallowDirectory($path)
  145.     {
  146.         if (is_array($path))
  147.             foreach ($path as $dir)
  148.                 unset($this->allowedDirectories[realpath($dir)]);
  149.         else
  150.             unset($this->allowedDirectories[realpath($path)]);
  151.     }
  152.  
  153.     /**
  154.      * returns the list of safe paths, note that the paths are stored in the array
  155.      * keys and not values
  156.      *
  157.      * @return array 
  158.      */
  159.     public function getAllowedDirectories()
  160.     {
  161.         return $this->allowedDirectories;
  162.     }
  163.  
  164.     /**
  165.      * sets the php handling level, defaults to REMOVE
  166.      *
  167.      * @param int $level one of the Dwoo_Security_Policy::PHP_* constants
  168.      */
  169.     public function setPhpHandling($level self::PHP_REMOVE)
  170.     {
  171.         $this->phpHandling = $level;
  172.     }
  173.  
  174.     /**
  175.      * returns the php handling level
  176.      *
  177.      * @return int the current level, one of the Dwoo_Security_Policy::PHP_* constants
  178.      */
  179.     public function getPhpHandling()
  180.     {
  181.         return $this->phpHandling;
  182.     }
  183.  
  184.     /**
  185.      * sets the constant handling level, defaults to CONST_DISALLOW
  186.      *
  187.      * @param bool $level one of the Dwoo_Security_Policy::CONST_* constants
  188.      */
  189.     public function setConstantHandling($level self::CONST_DISALLOW)
  190.     {
  191.         $this->constHandling = $level;
  192.     }
  193.  
  194.     /**
  195.      * returns the constant handling level
  196.      *
  197.      * @return bool the current level, one of the Dwoo_Security_Policy::CONST_* constants
  198.      */
  199.     public function getConstantHandling()
  200.     {
  201.         return $this->constHandling;
  202.     }
  203. }

Documentation generated on Sun, 07 Sep 2008 23:57:55 +0200 by phpDocumentor 1.4.0