Source for file input.php

Documentation is available at input.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  input
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. /**
  12.  * FrameworkOnFramework input handling class. Extends upon the JInput class.
  13.  *
  14.  * @package  FrameworkOnFramework
  15.  * @since    2.0
  16.  */
  17. class FOFInput extends JInput
  18. {
  19.     /**
  20.      * Public constructor. Overriden to allow specifying the global input array
  21.      * to use as a string and instantiate from an objetc holding variables.
  22.      *
  23.      * @param   array|string|object|null $source   Source data; set null to use $_REQUEST
  24.      * @param   array                     $options  Filter options
  25.      */
  26.     public function __construct($source nullarray $options array())
  27.     {
  28.         $hash null;
  29.  
  30.         if (is_string($source))
  31.         {
  32.             $hash strtoupper($source);
  33.  
  34.             switch ($hash)
  35.             {
  36.                 case 'GET':
  37.                     $source $_GET;
  38.                     break;
  39.                 case 'POST':
  40.                     $source $_POST;
  41.                     break;
  42.                 case 'FILES':
  43.                     $source $_FILES;
  44.                     break;
  45.                 case 'COOKIE':
  46.                     $source $_COOKIE;
  47.                     break;
  48.                 case 'ENV':
  49.                     $source $_ENV;
  50.                     break;
  51.                 case 'SERVER':
  52.                     $source $_SERVER;
  53.                     break;
  54.                 default:
  55.                     $source $_REQUEST;
  56.                     $hash 'REQUEST';
  57.                     break;
  58.             }
  59.         }
  60.         elseif (is_object($source))
  61.         {
  62.             try
  63.             {
  64.                 $source = (array) $source;
  65.             }
  66.             catch (Exception $exc)
  67.             {
  68.                 $source null;
  69.             }
  70.         }
  71.         elseif (is_array($source))
  72.         {
  73.             // Nothing, it's already an array
  74.         }
  75.         else
  76.         {
  77.             // Any other case
  78.             $source $_REQUEST;
  79.             $hash 'REQUEST';
  80.         }
  81.  
  82.         // Magic quotes GPC handling (something JInput simply can't handle at all)
  83.  
  84.         if (($hash == 'REQUEST'&& get_magic_quotes_gpc(&& class_exists('JRequest'true))
  85.         {
  86.             $source JRequest::get('REQUEST'2);
  87.         }
  88.  
  89.         parent::__construct($source$options);
  90.     }
  91.  
  92.     /**
  93.      * Gets a value from the input data. Overriden to allow specifying a filter
  94.      * mask.
  95.      *
  96.      * @param   string  $name     Name of the value to get.
  97.      * @param   mixed   $default  Default value to return if variable does not exist.
  98.      * @param   string  $filter   Filter to apply to the value.
  99.      * @param   int     $mask     The filter mask
  100.      *
  101.      * @return  mixed  The filtered input value.
  102.      */
  103.     public function get($name$default null$filter 'cmd'$mask 0)
  104.     {
  105.         if (isset($this->data[$name]))
  106.         {
  107.             return $this->_cleanVar($this->data[$name]$mask$filter);
  108.         }
  109.  
  110.         return $default;
  111.     }
  112.  
  113.     /**
  114.      * Returns a copy of the raw data stored in the class
  115.      *
  116.      * @return  array 
  117.      */
  118.     public function getData()
  119.     {
  120.         return $this->data;
  121.     }
  122.  
  123.     /**
  124.      * Old static methods are now deprecated. This magic method makes sure there
  125.      * is a continuity in our approach. The downside is that it's only compatible
  126.      * with PHP 5.3.0. Sorry!
  127.      *
  128.      * @param   string  $name       Name of the method we're calling
  129.      * @param   array   $arguments  The arguments passed to the method
  130.      *
  131.      * @return  mixed 
  132.      */
  133.     public static function __callStatic($name$arguments)
  134.     {
  135.         JLog::add('FOFInput: static getXXX() methods are deprecated. Use the input object\'s methods instead.'JLog::WARNING'deprecated');
  136.  
  137.         if (substr($name03== 'get')
  138.         {
  139.             // Initialise arguments
  140.             $key array_shift($arguments);
  141.             $default array_shift($arguments);
  142.             $input array_shift($arguments);
  143.             $type 'none';
  144.             $mask 0;
  145.  
  146.             $type strtolower(substr($name3));
  147.  
  148.             if ($type == 'var')
  149.             {
  150.                 $type array_shift($arguments);
  151.                 $mask array_shift($arguments);
  152.             }
  153.  
  154.             if (is_null($type))
  155.             {
  156.                 $type 'none';
  157.             }
  158.  
  159.             if (is_null($mask))
  160.             {
  161.                 $mask 0;
  162.             }
  163.  
  164.             if (!($input instanceof FOFInput&& !($input instanceof JInput))
  165.             {
  166.                 $input new FOFInput($input);
  167.             }
  168.  
  169.             return $input->get($key$default$type$mask);
  170.         }
  171.  
  172.         return false;
  173.     }
  174.  
  175.     /**
  176.      * Magic method to get filtered input data.
  177.      *
  178.      * @param   mixed   $name       Name of the value to get.
  179.      * @param   string  $arguments  Default value to return if variable does not exist.
  180.      *
  181.      * @return  boolean  The filtered boolean input value.
  182.      */
  183.     public function __call($name$arguments)
  184.     {
  185.         if (substr($name03== 'get')
  186.         {
  187.             $filter substr($name3);
  188.  
  189.             $default null;
  190.             $mask 0;
  191.  
  192.             if (isset($arguments[1]))
  193.             {
  194.                 $default $arguments[1];
  195.             }
  196.  
  197.             if (isset($arguments[2]))
  198.             {
  199.                 $mask $arguments[2];
  200.             }
  201.  
  202.             return $this->get($arguments[0]$default$filter$mask);
  203.         }
  204.     }
  205.  
  206.     /**
  207.      * Sets an input variable. WARNING: IT SHOULD NO LONGER BE USED!
  208.      *
  209.      * @param   string   $name       The name of the variable to set
  210.      * @param   mixed    $value      The value to set it to
  211.      * @param   array    &$input     The input array or FOFInput object
  212.      * @param   boolean  $overwrite  Should I overwrite existing values (default: true)
  213.      *
  214.      * @return  string   Previous value
  215.      *
  216.      * @deprecated
  217.      */
  218.     public static function setVar($name$value null&$input array()$overwrite true)
  219.     {
  220.         JLog::add('FOFInput::setVar() is deprecated. Use set() instead.'JLog::WARNING'deprecated');
  221.  
  222.         if (empty($input))
  223.         {
  224.             return JRequest::setVar($name$value'default'$overwrite);
  225.         }
  226.         elseif (is_string($input))
  227.         {
  228.             return JRequest::setVar($name$value$input$overwrite);
  229.         }
  230.         else
  231.         {
  232.             if (!$overwrite && array_key_exists($name$input))
  233.             {
  234.                 return $input[$name];
  235.             }
  236.  
  237.             $previous array_key_exists($name$input$input[$namenull;
  238.  
  239.             if (is_array($input))
  240.             {
  241.                 $input[$name$value;
  242.             }
  243.             elseif ($input instanceof FOFInput)
  244.             {
  245.                 $input->set($name$value);
  246.             }
  247.  
  248.             return $previous;
  249.         }
  250.     }
  251.  
  252.     /**
  253.      * Custom filter implementation. Works better with arrays and allows the use
  254.      * of a filter mask.
  255.      *
  256.      * @param   mixed    $var   The variable (value) to clean
  257.      * @param   integer  $mask  The clean mask
  258.      * @param   string   $type  The variable type
  259.      *
  260.      * @return   mixed 
  261.      */
  262.     protected function _cleanVar($var$mask 0$type null)
  263.     {
  264.         if (is_array($var))
  265.         {
  266.             $temp array();
  267.  
  268.             foreach ($var as $k => $v)
  269.             {
  270.                 $temp[$kself::_cleanVar($v$mask);
  271.             }
  272.  
  273.             return $temp;
  274.         }
  275.  
  276.         // If the no trim flag is not set, trim the variable
  277.         if (!($mask 1&& is_string($var))
  278.         {
  279.             $var trim($var);
  280.         }
  281.  
  282.         // Now we handle input filtering
  283.         if ($mask 2)
  284.         {
  285.             // If the allow raw flag is set, do not modify the variable
  286.             $var $var;
  287.         }
  288.         elseif ($mask 4)
  289.         {
  290.             // If the allow HTML flag is set, apply a safe HTML filter to the variable
  291.             $safeHtmlFilter JFilterInput::getInstance(nullnull11);
  292.             $var $safeHtmlFilter->clean($var$type);
  293.         }
  294.         else
  295.         {
  296.             $var $this->filter->clean($var$type);
  297.         }
  298.  
  299.         return $var;
  300.     }
  301. }

Documentation generated on Tue, 19 Nov 2013 15:05:37 +0100 by phpDocumentor 1.4.3