Source for file input.php

Documentation is available at input.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Input
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Joomla! Input Base Class
  14.  *
  15.  * This is an abstracted input class used to manage retrieving data from the application environment.
  16.  *
  17.  * @package     Joomla.Platform
  18.  * @subpackage  Input
  19.  * @since       11.1
  20.  *
  21.  * @property-read    JInput        $get 
  22.  * @property-read    JInput        $post 
  23.  * @property-read    JInput        $request 
  24.  * @property-read    JInput        $server 
  25.  * @property-read    JInputFiles   $files 
  26.  * @property-read    JInputCookie  $cookie 
  27.  *
  28.  * @method      integer  getInt()       getInt($name, $default = null)    Get a signed integer.
  29.  * @method      integer  getUint()      getUint($name, $default = null)   Get an unsigned integer.
  30.  * @method      float    getFloat()     getFloat($name, $default = null)  Get a floating-point number.
  31.  * @method      boolean  getBool()      getBool($name, $default = null)   Get a boolean.
  32.  * @method      string   getWord()      getWord($name, $default = null)
  33.  * @method      string   getAlnum()     getAlnum($name, $default = null)
  34.  * @method      string   getCmd()       getCmd($name, $default = null)
  35.  * @method      string   getBase64()    getBase64($name, $default = null)
  36.  * @method      string   getString()    getString($name, $default = null)
  37.  * @method      string   getHtml()      getHtml($name, $default = null)
  38.  * @method      string   getPath()      getPath($name, $default = null)
  39.  * @method      string   getUsername()  getUsername($name, $default = null)
  40.  */
  41. class JInput implements SerializableCountable
  42. {
  43.     /**
  44.      * Options array for the JInput instance.
  45.      *
  46.      * @var    array 
  47.      * @since  11.1
  48.      */
  49.     protected $options = array();
  50.  
  51.     /**
  52.      * Filter object to use.
  53.      *
  54.      * @var    JFilterInput 
  55.      * @since  11.1
  56.      */
  57.     protected $filter = null;
  58.  
  59.     /**
  60.      * Input data.
  61.      *
  62.      * @var    array 
  63.      * @since  11.1
  64.      */
  65.     protected $data = array();
  66.  
  67.     /**
  68.      * Input objects
  69.      *
  70.      * @var    array 
  71.      * @since  11.1
  72.      */
  73.     protected $inputs = array();
  74.  
  75.     /**
  76.      * Constructor.
  77.      *
  78.      * @param   array  $source   Source data (Optional, default is $_REQUEST)
  79.      * @param   array  $options  Array of configuration parameters (Optional)
  80.      *
  81.      * @since   11.1
  82.      */
  83.     public function __construct($source nullarray $options array())
  84.     {
  85.         if (isset($options['filter']))
  86.         {
  87.             $this->filter = $options['filter'];
  88.         }
  89.         else
  90.         {
  91.             $this->filter = JFilterInput::getInstance();
  92.         }
  93.  
  94.         if (is_null($source))
  95.         {
  96.             $this->data = &$_REQUEST;
  97.         }
  98.         else
  99.         {
  100.             $this->data = $source;
  101.         }
  102.  
  103.         // Set the options for the class.
  104.         $this->options = $options;
  105.     }
  106.  
  107.     /**
  108.      * Magic method to get an input object
  109.      *
  110.      * @param   mixed  $name  Name of the input object to retrieve.
  111.      *
  112.      * @return  JInput  The request input object
  113.      *
  114.      * @since   11.1
  115.      */
  116.     public function __get($name)
  117.     {
  118.         if (isset($this->inputs[$name]))
  119.         {
  120.             return $this->inputs[$name];
  121.         }
  122.  
  123.         $className 'JInput' ucfirst($name);
  124.  
  125.         if (class_exists($className))
  126.         {
  127.             $this->inputs[$namenew $className(null$this->options);
  128.  
  129.             return $this->inputs[$name];
  130.         }
  131.  
  132.         $superGlobal '_' strtoupper($name);
  133.  
  134.         if (isset($GLOBALS[$superGlobal]))
  135.         {
  136.             $this->inputs[$namenew JInput($GLOBALS[$superGlobal]$this->options);
  137.  
  138.             return $this->inputs[$name];
  139.         }
  140.  
  141.         // TODO throw an exception
  142.     }
  143.  
  144.     /**
  145.      * Get the number of variables.
  146.      *
  147.      * @return  integer  The number of variables in the input.
  148.      *
  149.      * @since   12.2
  150.      * @see     Countable::count()
  151.      */
  152.     public function count()
  153.     {
  154.         return count($this->data);
  155.     }
  156.  
  157.     /**
  158.      * Gets a value from the input data.
  159.      *
  160.      * @param   string  $name     Name of the value to get.
  161.      * @param   mixed   $default  Default value to return if variable does not exist.
  162.      * @param   string  $filter   Filter to apply to the value.
  163.      *
  164.      * @return  mixed  The filtered input value.
  165.      *
  166.      * @since   11.1
  167.      */
  168.     public function get($name$default null$filter 'cmd')
  169.     {
  170.         if (isset($this->data[$name]))
  171.         {
  172.             return $this->filter->clean($this->data[$name]$filter);
  173.         }
  174.  
  175.         return $default;
  176.     }
  177.  
  178.     /**
  179.      * Gets an array of values from the request.
  180.      *
  181.      * @param   array  $vars        Associative array of keys and filter types to apply.
  182.      *                               If empty and datasource is null, all the input data will be returned
  183.      *                               but filtered using the default case in JFilterInput::clean.
  184.      * @param   mixed  $datasource  Array to retrieve data from, or null
  185.      *
  186.      * @return  mixed  The filtered input data.
  187.      *
  188.      * @since   11.1
  189.      */
  190.     public function getArray(array $vars array()$datasource null)
  191.     {
  192.         if (empty($vars&& is_null($datasource))
  193.         {
  194.             $vars $this->data;
  195.         }
  196.  
  197.         $results array();
  198.  
  199.         foreach ($vars as $k => $v)
  200.         {
  201.             if (is_array($v))
  202.             {
  203.                 if (is_null($datasource))
  204.                 {
  205.                     $results[$k$this->getArray($v$this->get($knull'array'));
  206.                 }
  207.                 else
  208.                 {
  209.                     $results[$k$this->getArray($v$datasource[$k]);
  210.                 }
  211.             }
  212.             else
  213.             {
  214.                 if (is_null($datasource))
  215.                 {
  216.                     $results[$k$this->get($knull$v);
  217.                 }
  218.                 elseif (isset($datasource[$k]))
  219.                 {
  220.                     $results[$k$this->filter->clean($datasource[$k]$v);
  221.                 }
  222.                 else
  223.                 {
  224.                     $results[$k$this->filter->clean(null$v);
  225.                 }
  226.             }
  227.         }
  228.  
  229.         return $results;
  230.     }
  231.  
  232.     /**
  233.      * Sets a value
  234.      *
  235.      * @param   string  $name   Name of the value to set.
  236.      * @param   mixed   $value  Value to assign to the input.
  237.      *
  238.      * @return  void 
  239.      *
  240.      * @since   11.1
  241.      */
  242.     public function set($name$value)
  243.     {
  244.         $this->data[$name$value;
  245.     }
  246.  
  247.     /**
  248.      * Define a value. The value will only be set if there's no value for the name or if it is null.
  249.      *
  250.      * @param   string  $name   Name of the value to define.
  251.      * @param   mixed   $value  Value to assign to the input.
  252.      *
  253.      * @return  void 
  254.      *
  255.      * @since   12.1
  256.      */
  257.     public function def($name$value)
  258.     {
  259.         if (isset($this->data[$name]))
  260.         {
  261.             return;
  262.         }
  263.  
  264.         $this->data[$name$value;
  265.     }
  266.  
  267.     /**
  268.      * Magic method to get filtered input data.
  269.      *
  270.      * @param   string  $name       Name of the filter type prefixed with 'get'.
  271.      * @param   array   $arguments  [0] The name of the variable [1] The default value.
  272.      *
  273.      * @return  mixed   The filtered input value.
  274.      *
  275.      * @since   11.1
  276.      */
  277.     public function __call($name$arguments)
  278.     {
  279.         if (substr($name03== 'get')
  280.         {
  281.             $filter substr($name3);
  282.  
  283.             $default null;
  284.  
  285.             if (isset($arguments[1]))
  286.             {
  287.                 $default $arguments[1];
  288.             }
  289.  
  290.             return $this->get($arguments[0]$default$filter);
  291.         }
  292.     }
  293.  
  294.     /**
  295.      * Gets the request method.
  296.      *
  297.      * @return  string   The request method.
  298.      *
  299.      * @since   11.1
  300.      */
  301.     public function getMethod()
  302.     {
  303.         $method strtoupper($_SERVER['REQUEST_METHOD']);
  304.  
  305.         return $method;
  306.     }
  307.  
  308.     /**
  309.      * Method to serialize the input.
  310.      *
  311.      * @return  string  The serialized input.
  312.      *
  313.      * @since   12.1
  314.      */
  315.     public function serialize()
  316.     {
  317.         // Load all of the inputs.
  318.         $this->loadAllInputs();
  319.  
  320.         // Remove $_ENV and $_SERVER from the inputs.
  321.         $inputs $this->inputs;
  322.         unset($inputs['env']);
  323.         unset($inputs['server']);
  324.  
  325.         // Serialize the options, data, and inputs.
  326.         return serialize(array($this->options$this->data$inputs));
  327.     }
  328.  
  329.     /**
  330.      * Method to unserialize the input.
  331.      *
  332.      * @param   string  $input  The serialized input.
  333.      *
  334.      * @return  JInput  The input object.
  335.      *
  336.      * @since   12.1
  337.      */
  338.     public function unserialize($input)
  339.     {
  340.         // Unserialize the options, data, and inputs.
  341.         list($this->options$this->data$this->inputsunserialize($input);
  342.  
  343.         // Load the filter.
  344.         if (isset($this->options['filter']))
  345.         {
  346.             $this->filter = $this->options['filter'];
  347.         }
  348.         else
  349.         {
  350.             $this->filter = JFilterInput::getInstance();
  351.         }
  352.     }
  353.  
  354.     /**
  355.      * Method to load all of the global inputs.
  356.      *
  357.      * @return  void 
  358.      *
  359.      * @since   12.1
  360.      */
  361.     protected function loadAllInputs()
  362.     {
  363.         static $loaded false;
  364.  
  365.         if (!$loaded)
  366.         {
  367.             // Load up all the globals.
  368.             foreach ($GLOBALS as $global => $data)
  369.             {
  370.                 // Check if the global starts with an underscore.
  371.                 if (strpos($global'_'=== 0)
  372.                 {
  373.                     // Convert global name to input name.
  374.                     $global strtolower($global);
  375.                     $global substr($global1);
  376.  
  377.                     // Get the input.
  378.                     $this->$global;
  379.                 }
  380.             }
  381.  
  382.             $loaded true;
  383.         }
  384.     }
  385. }

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