VCL
[ class tree: VCL ] [ index: VCL ] [ all elements ]

Source for file system.inc.php

Documentation is available at system.inc.php

  1. <?php
  2. /**
  3. *  This file is part of the VCL for PHP project
  4. *
  5. *  Copyright (c) 2004-2007 qadram software <support@qadram.com>
  6. *
  7. *  Checkout AUTHORS file for more information on the developers
  8. *
  9. *  This library is free software; you can redistribute it and/or
  10. *  modify it under the terms of the GNU Lesser General Public
  11. *  License as published by the Free Software Foundation; either
  12. *  version 2.1 of the License, or (at your option) any later version.
  13. *
  14. *  This library is distributed in the hope that it will be useful,
  15. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. *  Lesser General Public License for more details.
  18. *
  19. *  You should have received a copy of the GNU Lesser General Public
  20. *  License along with this library; if not, write to the Free Software
  21. *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  22. *  USA
  23. *
  24. */
  25.  
  26. /**
  27.  * EPropertyNotFound exception
  28.  *
  29.  * Exception thrown when trying to access a property not defined
  30.  */
  31. class EPropertyNotFound extends Exception
  32. {
  33.        /**
  34.         * Constructor
  35.         *
  36.         * @param string $message 
  37.         * @param integer $code 
  38.         */
  39.         function __construct($message null$code 0)
  40.         {
  41.                 $message=sprintf("Error using a property, not defined as a member [%s]"$message);
  42.  
  43.                 // make sure everything is assigned properly
  44.                 parent::__construct($message$code);
  45.         }
  46. }
  47.  
  48. /**
  49.  * Object class
  50.  *
  51.  * All objects must inherit from this class
  52.  */
  53. class Object
  54. {
  55.         /**
  56.         * Global input object, easily accessible without declaring global
  57.         */
  58.         public $input=null;
  59.  
  60.         /**
  61.         * Constructor
  62.         * @return object 
  63.         */
  64.         function __construct()
  65.         {
  66.                 global $input;
  67.  
  68.                 //Assign the global input object so it can be used from inside
  69.                 $this->input=$input;
  70.         }
  71.  
  72.         /**
  73.          * Returns the name of the object class
  74.          * @return string 
  75.          */
  76.         function className()
  77.         {
  78.                 return(get_class($this));
  79.         }
  80.  
  81.  
  82.         /**
  83.          * Check if the object is from a specific class
  84.          * @param string $name Name to compare
  85.          * @return boolean 
  86.          */
  87.         function classNameIs($name)
  88.         {
  89.                 return(strtolower($this->ClassName())==strtolower($name));
  90.         }
  91.  
  92.  
  93.         /**
  94.          * Check if a method exists
  95.          * @param string $method Method name to check
  96.          * @return boolean 
  97.          */
  98.         function methodExists($method)
  99.         {
  100.                 return(method_exists($this,$method));
  101.         }
  102.  
  103.         /**
  104.          * Returns the parent class of the object
  105.          * @return class 
  106.          */
  107.         function classParent()
  108.         {
  109.                 return(get_parent_class($this));
  110.         }
  111.  
  112.         /**
  113.          * Check if the object inherits from a specific class
  114.          * @param string $class Class name to check
  115.          * @return boolean 
  116.          */
  117.         function inheritsFrom($class)
  118.         {
  119.                 return(is_subclass_of($this,$classor $this->classNameIs($class));
  120.         }
  121.  
  122.         /**
  123.          * Reads a property from the streams
  124.          * @param string $propertyname Name of the property to read
  125.          * @param string $valuename Value name to read
  126.          * @param string $stream Stream to read from
  127.          */
  128.         function readProperty($propertyname,$valuename,$stream='post')
  129.         {
  130.                 //TODO: Use also get array
  131.                 //TODO: Use the input object
  132.                 if (isset($_POST[$valuename]))
  133.                 {
  134.                         $value=$_POST[$valuename];
  135.                         $this->$propertyname=$value;
  136.                 }
  137.         }
  138.  
  139.  
  140.          /**
  141.          * To virtualize properties
  142.          * @param string $nm Property name
  143.          * @return mixed 
  144.          */
  145.         function __get($nm)
  146.         {
  147.                 $method='get'.$nm;
  148.  
  149.                 //Search first for get$nm
  150.                 if (method_exists($this,$method))
  151.                 {
  152.                         return ($this->$method());
  153.                 }
  154.                 else
  155.                 {
  156.                         $method='read'.$nm;
  157.  
  158.                         //Search for read$nm
  159.                         if (method_exists($this,$method))
  160.                         {
  161.                                 return ($this->$method());
  162.                         }
  163.                         else
  164.                         {
  165.                                 //If not, search a component owned by it, with that name
  166.                                 if ($this->inheritsFrom('Component'))
  167.                                 {
  168.                                         reset($this->components->items);
  169.                                         while (list($k,$v)=each($this->components->items))
  170.                                         {
  171.                                                 if (strtolower($v->Name)==strtolower($nm)) return($v);
  172.                                         }
  173.                                 }
  174.                                 throw new EPropertyNotFound($this->ClassName().".".$nm);
  175.                         }
  176.                 }
  177.         }
  178.  
  179.   /**
  180.   * To virtualize properties
  181.   * @param string $nm Property name
  182.   * @param mixed $val Property value
  183.   */
  184.   function __set($nm$val)
  185.   {
  186.         $method='set'.$nm;
  187.  
  188.         if (method_exists($this,$method))
  189.         {
  190.                 $this->$method($val);
  191.         }
  192.         else
  193.         {
  194.                 $method='write'.$nm;
  195.  
  196.                 if (method_exists($this,$method))
  197.                 {
  198.                         $this->$method($val);
  199.                 }
  200.                 else
  201.                 {
  202.                         throw new EPropertyNotFound($this->ClassName().".".$nm);
  203.                 }
  204.         }
  205.   }
  206.  
  207.  
  208. }
  209.  
  210.  
  211. define('sGET',0);
  212. define('sPOST',1);
  213. define('sREQUEST',2);
  214. define('sCOOKIES',3);
  215. define('sSERVER',4);
  216.  
  217. /**
  218. * Native Input Filter, not working yet
  219. */
  220. {
  221.         /**
  222.         * Process user input to be clean
  223.         * @param string $input Input to be cleaned
  224.         * @return string 
  225.         */
  226.         function process($input)
  227.         {
  228.                 //TODO: Our own input filtering class in native PHP code
  229.                 //NOTE: Comment this line to don't raise the exception an get the unfiltered input
  230.                 throw new Exception("The Input Filter PHP extension is not setup on this PHP installation, so the contents returned by Input is *not* filtered");
  231.                 return($input);
  232.         }
  233. }
  234.  
  235. /**
  236. * Represents an input parameter from the user, it doesn't inherit from Object to
  237. * be faster and smaller. Objects of this type are returned from the Input object.
  238. */
  239. class InputParam
  240. {
  241.         public $name='';
  242.         public $stream;
  243.         public $filter_extension=false;
  244.         public $filter=null;
  245.  
  246.         /**
  247.         * Create the object
  248.         * @param $name   Key of the stream to look form
  249.         * @param $stream Stream to look for
  250.         */
  251.         function __construct($name$stream=SGET)
  252.         {
  253.                 //Checkout if filter extension has been installer or not
  254.                 $this->filter_extension=function_exists('filter_data');
  255.  
  256.                 //If not, creates the native filter
  257.                 if (!$this->filter_extension)
  258.                 {
  259.                         //TODO: Use a global native filter to reduce overhead
  260.                         $this->createNativeFilter();
  261.                 }
  262.  
  263.                 $this->name=$name;
  264.  
  265.                 //Set the stream to look for
  266.                 switch($stream)
  267.                 {
  268.                         case sGET$this->stream=&$_GETbreak;
  269.                         case sPOST$this->stream=&$_POSTbreak;
  270.                         case sREQUEST$this->stream=&$_REQUESTbreak;
  271.                         case sCOOKIES$this->stream=&$_COOKIESbreak;
  272.                         case sSERVER$this->stream=&$_SERVERbreak;
  273.                 }
  274.  
  275.         }
  276.  
  277.         /**
  278.         * Creates the native Input Filter to be used when there is no available extension
  279.         */
  280.         function createNativeFilter()
  281.         {
  282.                 $this->filter = new InputFilter();
  283.         }
  284.  
  285.         //TODO: Add filtering without the filtering extension installed
  286.  
  287.         
  288.  
  289.         /**
  290.         * Returns the input filtered as a string
  291.         */
  292.         function asString()
  293.         {
  294.                 //Filter this out
  295.                 if ($this->filter_extension)
  296.                 {
  297.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_STRING));
  298.                 }
  299.                 else
  300.                 {
  301.                         return $this->filter->process($this->stream[$this->name]);
  302.                 }
  303.         }
  304.  
  305.         /**
  306.         * Returns the input filtered as a string array
  307.         */
  308.         function asStringArray()
  309.         {
  310.                 //Filter this out
  311.                 if ($this->filter_extension)
  312.                 {
  313.                         $data=$this->stream[$this->name];
  314.                         reset($data);
  315.                         $result=array();
  316.                         while (list($k,$v)=each($data))
  317.                         {
  318.                                 $result[filter_data($k,FILTER_SANITIZE_STRING)]=filter_data($v,FILTER_SANITIZE_STRING);
  319.                         }
  320.                         return($result);
  321.                 }
  322.                 else
  323.                 {
  324.                         //TODO: Filter using a native library
  325.                         return $this->filter->process($this->stream[$this->name]);
  326.                 }
  327.         }
  328.  
  329.         /**
  330.         * Returns the input filtered as a integer
  331.         */
  332.         function asInteger()
  333.         {
  334.                 if ($this->filter_extension)
  335.                 {
  336.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_NUMBER_INT));
  337.                 }
  338.                 else
  339.                 {
  340.                         return $this->filter->process($this->stream[$this->name]);
  341.                 }
  342.         }
  343.  
  344.         /**
  345.         * Returns the input filtered as a boolean
  346.         */
  347.         function asBoolean()
  348.         {
  349.                 if ($this->filter_extension)
  350.                 {
  351.                         return(filter_data($this->stream[$this->name],FILTER_VALIDATE_BOOLEAN));
  352.                 }
  353.                 else
  354.                 {
  355.                         return $this->filter->process($this->stream[$this->name]);
  356.                 }
  357.         }
  358.  
  359.         /**
  360.         * Returns the input filtered as a float
  361.         */
  362.         function asFloat($flags=0)
  363.         {
  364.                 if ($this->filter_extension)
  365.                 {
  366.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_NUMBER_FLOAT$flags));
  367.                 }
  368.                 else
  369.                 {
  370.                         return $this->filter->process($this->stream[$this->name]);
  371.                 }
  372.         }
  373.  
  374.         /**
  375.         * Returns the input filtered as a regular expression
  376.         */
  377.         function asRegExp()
  378.         {
  379.                 //Filter this out
  380.                 if ($this->filter_extension)
  381.                 {
  382.                         return(filter_data($this->stream[$this->name],FILTER_VALIDATE_REGEXP));
  383.                 }
  384.                 else
  385.                 {
  386.                         return $this->filter->process($this->stream[$this->name]);
  387.                 }
  388.         }
  389.  
  390.         /**
  391.         * Returns the input filtered as an URL
  392.         */
  393.         function asURL()
  394.         {
  395.                 //Filter this out
  396.                 if ($this->filter_extension)
  397.                 {
  398.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_URL));
  399.                 }
  400.                 else
  401.                 {
  402.                         return $this->filter->process($this->stream[$this->name]);
  403.                 }
  404.         }
  405.  
  406.         /**
  407.         * Returns the input filtered as an email address
  408.         */
  409.         function asEmail()
  410.         {
  411.                 //Filter this out
  412.                 if ($this->filter_extension)
  413.                 {
  414.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_EMAIL));
  415.                 }
  416.                 else
  417.                 {
  418.                         return $this->filter->process($this->stream[$this->name]);
  419.                 }
  420.         }
  421.  
  422.         /**
  423.         * Returns the input filtered as an IP address
  424.         */
  425.         function asIP()
  426.         {
  427.                 //Filter this out
  428.                 if ($this->filter_extension)
  429.                 {
  430.                         return(filter_data($this->stream[$this->name],FILTER_VALIDATE_IP));
  431.                 }
  432.                 else
  433.                 {
  434.                         return $this->filter->process($this->stream[$this->name]);
  435.                 }
  436.         }
  437.  
  438.         /**
  439.         * Returns the input filtered as an string
  440.         */
  441.         function asStripped()
  442.         {
  443.                 //Filter this out
  444.                 if ($this->filter_extension)
  445.                 {
  446.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_STRIPPED));
  447.                 }
  448.                 else
  449.                 {
  450.                         return $this->filter->process($this->stream[$this->name]);
  451.                 }
  452.         }
  453.  
  454.         /**
  455.         * URL-encode string, optionally strip or encode special characters.
  456.         */
  457.         function asEncoded()
  458.         {
  459.                 //Filter this out
  460.                 if ($this->filter_extension)
  461.                 {
  462.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_ENCODED));
  463.                 }
  464.                 else
  465.                 {
  466.                         return $this->filter->process($this->stream[$this->name]);
  467.                 }
  468.         }
  469.  
  470.         /**
  471.         * HTML-escape '"<>& and characters with ASCII value less than 32, optionally strip or encode other special characters.
  472.         */
  473.         function asSpecialChars()
  474.         {
  475.                 //Filter this out
  476.                 if ($this->filter_extension)
  477.                 {
  478.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_SPECIAL_CHARS));
  479.                 }
  480.                 else
  481.                 {
  482.                         return $this->filter->process($this->stream[$this->name]);
  483.                 }
  484.         }
  485.  
  486.         /**
  487.         * Do nothing, optionally strip or encode special characters.
  488.         */
  489.         function asUnsafeRaw()
  490.         {
  491.                 //Filter this out
  492.                 if ($this->filter_extension)
  493.                 {
  494.                         return(filter_data($this->stream[$this->name],FILTER_SANITIZE_UNSAFE_RAW));
  495.                 }
  496.                 else
  497.                 {
  498.                         return $this->filter->process($this->stream[$this->name]);
  499.                 }
  500.         }
  501. }
  502.  
  503. /**
  504.  * Input class, offers an easy way to get filtered input from the user
  505.  * Usage:
  506.  * global $input;
  507.  * $action=$input->action;
  508.  * if (is_object($action))
  509.  * {
  510.  *     $toperform=$action->asString();
  511.  * }
  512.  */
  513. class Input
  514. {
  515.         /**
  516.          * Magic method to search for the input from the user,
  517.          * checkout the order in which the variable is searched for:
  518.          * $_GET, $_POST, $_REQUEST, $_COOKIES, $_SERVER
  519.          *
  520.          * @return InputParam object or null if it's not found
  521.          *
  522.          */
  523.         function __get($nm)
  524.         {
  525.                 if (isset($_GET[$nm]))
  526.                 {
  527.                         return(new InputParam($nmsGET));
  528.                 }
  529.                 else
  530.                 if (isset($_POST[$nm]))
  531.                 {
  532.                         return(new InputParam($nmsPOST));
  533.                 }
  534.                 else
  535.                 if (isset($_REQUEST[$nm]))
  536.                 {
  537.                         return(new InputParam($nmsREQUEST));
  538.                 }
  539.                 else
  540.                 if (isset($_COOKIES[$nm]))
  541.                 {
  542.                         return(new InputParam($nmsCOOKIES));
  543.                 }
  544.                 else
  545.                 if (isset($_SERVER[$nm]))
  546.                 {
  547.                         return(new InputParam($nmsSERVER));
  548.                 }
  549.                 else
  550.                 {
  551.                         return(null);
  552.                 }
  553.           }
  554. }
  555.  
  556. /**
  557.  * Global $input variable, use it to get filtered/sanitized input from the user
  558.  */
  559. global $input;
  560.  
  561. $input=new Input();
  562.  
  563. ?>

Documentation generated on Tue, 27 Mar 2007 13:35:58 +0200 by phpDocumentor 1.3.1