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.  * @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.  */
  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.     /**#@+
  33.      * constant handling constants, defaults to CONST_DISALLOW
  34.      *
  35.      * CONST_DISALLOW : throw an error if {$dwoo.const.*} is used in the template
  36.      * CONST_ALLOW : allow {$dwoo.const.*} calls
  37.      */
  38.     const CONST_DISALLOW false;
  39.     const CONST_ALLOW true;
  40.     /**#@-*/
  41.  
  42.     /**
  43.      * php functions that are allowed to be used within the template
  44.      *
  45.      * @var array 
  46.      */
  47.     protected $allowedPhpFunctions = array
  48.     (
  49.         'str_repeat''number_format''htmlentities''htmlspecialchars',
  50.         'long2ip''strlen''list''empty''count''sizeof''in_array''is_array',
  51.     );
  52.  
  53.     /**
  54.      * paths that are safe to use with include or other file-access plugins
  55.      *
  56.      * @var array 
  57.      */
  58.     protected $allowedDirectories = array();
  59.  
  60.     /**
  61.      * stores the php handling level
  62.      *
  63.      * defaults to Dwoo_Security_Policy::PHP_REMOVE
  64.      *
  65.      * @var int 
  66.      */
  67.     protected $phpHandling = self::PHP_REMOVE;
  68.  
  69.     /**
  70.      * stores the constant handling level
  71.      *
  72.      * defaults to Dwoo_Security_Policy::CONST_DISALLOW
  73.      *
  74.      * @var bool 
  75.      */
  76.     protected $constHandling = self::CONST_DISALLOW;
  77.  
  78.     /**
  79.      * adds a php function to the allowed list
  80.      *
  81.      * @param mixed $func function name or array of function names
  82.      */
  83.     public function allowPhpFunction($func)
  84.     {
  85.         if (is_array($func))
  86.             foreach ($func as $fname)
  87.                 $this->allowedPhpFunctions[strtolower($fname)true;
  88.         else
  89.             $this->allowedPhpFunctions[strtolower($func)true;
  90.     }
  91.  
  92.     /**
  93.      * removes a php function from the allowed list
  94.      *
  95.      * @param mixed $func function name or array of function names
  96.      */
  97.     public function disallowPhpFunction($func)
  98.     {
  99.         if (is_array($func))
  100.             foreach ($func as $fname)
  101.                 unset($this->allowedPhpFunctions[strtolower($fname)]);
  102.         else
  103.             unset($this->allowedPhpFunctions[strtolower($func)]);
  104.     }
  105.  
  106.     /**
  107.      * returns the list of php functions allowed to run, note that the function names
  108.      * are stored in the array keys and not values
  109.      *
  110.      * @return array 
  111.      */
  112.     public function getAllowedPhpFunctions()
  113.     {
  114.         return $this->allowedPhpFunctions;
  115.     }
  116.  
  117.     /**
  118.      * adds a directory to the safelist for includes and other file-access plugins
  119.      *
  120.      * note that all the includePath directories you provide to the Dwoo_Template_File class
  121.      * are automatically marked as safe
  122.      *
  123.      * @param mixed $path a path name or an array of paths
  124.      */
  125.     public function allowDirectory($path)
  126.     {
  127.         if (is_array($path))
  128.             foreach ($path as $dir)
  129.                 $this->allowedDirectories[realpath($dir)true;
  130.         else
  131.             $this->allowedDirectories[realpath($path)true;
  132.     }
  133.  
  134.     /**
  135.      * removes a directory from the safelist
  136.      *
  137.      * @param mixed $path a path name or an array of paths
  138.      */
  139.     public function disallowDirectory($path)
  140.     {
  141.         if (is_array($path))
  142.             foreach ($path as $dir)
  143.                 unset($this->allowedDirectories[realpath($dir)]);
  144.         else
  145.             unset($this->allowedDirectories[realpath($path)]);
  146.     }
  147.  
  148.     /**
  149.      * returns the list of safe paths, note that the paths are stored in the array
  150.      * keys and not values
  151.      *
  152.      * @return array 
  153.      */
  154.     public function getAllowedDirectories()
  155.     {
  156.         return $this->allowedDirectories;
  157.     }
  158.  
  159.     /**
  160.      * sets the php handling level, defaults to REMOVE
  161.      *
  162.      * @param int $level one of the Dwoo_Security_Policy::PHP_* constants
  163.      */
  164.     public function setPhpHandling($level self::PHP_REMOVE)
  165.     {
  166.         $this->phpHandling = $level;
  167.     }
  168.  
  169.     /**
  170.      * returns the php handling level
  171.      *
  172.      * @return int the current level, one of the Dwoo_Security_Policy::PHP_* constants
  173.      */
  174.     public function getPhpHandling()
  175.     {
  176.         return $this->phpHandling;
  177.     }
  178.  
  179.     /**
  180.      * sets the constant handling level, defaults to CONST_DISALLOW
  181.      *
  182.      * @param bool $level one of the Dwoo_Security_Policy::CONST_* constants
  183.      */
  184.     public function setConstantHandling($level self::CONST_DISALLOW)
  185.     {
  186.         $this->constHandling = $level;
  187.     }
  188.  
  189.     /**
  190.      * returns the constant handling level
  191.      *
  192.      * @return bool the current level, one of the Dwoo_Security_Policy::CONST_* constants
  193.      */
  194.     public function getConstantHandling()
  195.     {
  196.         return $this->constHandling;
  197.     }
  198. }

Documentation generated on Sun, 07 Feb 2010 17:53:54 +0000 by phpDocumentor 1.4.0