Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/environment/request.php

Documentation is available at request.php

  1. <?php
  2. /**
  3.  * @version        $Id: request.php 6634 2007-02-15 18:27:18Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Environment
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. jimport('joomla.utilities.array');
  19. jimport('joomla.utilities.functions');
  20.  
  21. /**
  22.  * Create the request global object
  23.  */
  24. $GLOBALS['_JREQUEST'array();
  25.  
  26. /**
  27.  * Set the available masks for cleaning variables
  28.  */
  29. define("JREQUEST_NOTRIM"   1);
  30. define("JREQUEST_ALLOWRAW" 2);
  31. define("JREQUEST_ALLOWHTML"4);
  32.  
  33. /**
  34.  * JRequest Class
  35.  *
  36.  * This class serves to provide the Joomla Framework with a common interface to access
  37.  * request variables.  This includes $_POST, $_GET, and naturally $_REQUEST.  Variables
  38.  * can be passed through an input filter to avoid injection or returned raw.
  39.  *
  40.  * @static
  41.  * @author        Louis Landry <[email protected]>
  42.  * @package        Joomla.Framework
  43.  * @subpackage    Environment
  44.  * @since        1.5
  45.  */
  46. class JRequest
  47. {
  48.     /**
  49.      * Gets the full request path
  50.      * @return string 
  51.      */
  52.     function getURI()
  53.     {
  54.         $uri &JFactory::getURI();
  55.         return $uri->toString();
  56.     }
  57.  
  58.     /**
  59.      * Fetches and returns a given variable.
  60.      *
  61.      * The default behaviour is fetching variables depending on the
  62.      * current request method: GET and HEAD will result in returning
  63.      * an entry from $_GET, POST and PUT will result in returning an
  64.      * entry from $_POST.
  65.      *
  66.      * You can force the source by setting the $hash parameter:
  67.      *
  68.      *   post        $_POST
  69.      *   get            $_GET
  70.      *   files        $_FILES
  71.      *   cookie        $_COOKIE
  72.      *   method        via current $_SERVER['REQUEST_METHOD']
  73.      *   default    $_REQUEST
  74.      *
  75.      * @static
  76.      * @param    string    $name        Variable name
  77.      * @param    string    $default        Default value if the variable does not exist
  78.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  79.      * @param    string    $type        Return type for the variable (INT, FLOAT, STRING, BOOLEAN, ARRAY)
  80.      * @param    int        $mask        Filter mask for the variable
  81.      * @return    mixed    Requested variable
  82.      * @since    1.5
  83.      */
  84.     function getVar($name$default null$hash 'default'$type 'none'$mask 0)
  85.     {
  86.         // Ensure hash and type are uppercase
  87.         $hash strtoupper$hash );
  88.         if ($hash === 'METHOD'{
  89.             $hash strtoupper$_SERVER['REQUEST_METHOD');
  90.         }
  91.         $type    strtoupper$type );
  92.         $sig    $hash.$mask;
  93.  
  94.         // Get the input hash
  95.         switch ($hash)
  96.         {
  97.             case 'GET' :
  98.                 $input &$_GET;
  99.                 break;
  100.             case 'POST' :
  101.                 $input &$_POST;
  102.                 break;
  103.             case 'FILES' :
  104.                 $input &$_FILES;
  105.                 break;
  106.             case 'COOKIE' :
  107.                 $input &$_COOKIE;
  108.                 break;
  109.             default:
  110.                 $input &$_REQUEST;
  111.                 break;
  112.         }
  113.  
  114.         if (isset($GLOBALS['_JREQUEST'][$name]&& ($GLOBALS['_JREQUEST'][$name== 'SET')) {
  115.             // Get the variable from the input hash
  116.             $var (isset($input[$name]&& $input[$name!== null$input[$name$default;
  117.         }
  118.         elseif (!isset($GLOBALS['_JREQUEST'][$name][$sig]))
  119.         {    
  120.             $var (isset($input[$name]&& $input[$name!== null$input[$name$default;
  121.             // Get the variable from the input hash
  122.             $var JRequest::_cleanVar($var$mask$type);
  123.             
  124.             // Handle magic quotes compatability
  125.             if (get_magic_quotes_gpc(&& ($var != $default))
  126.             {
  127.                 if (!is_array($var&& is_string($var)) {
  128.                     $var stripslashes($var);
  129.                 }
  130.             }
  131.             if ($var !== null{
  132.                 $GLOBALS['_JREQUEST'][$name][$sig$var;
  133.             else {
  134.                 $var $default;
  135.             }
  136.         else {
  137.             $var $GLOBALS['_JREQUEST'][$name][$sig];
  138.         }
  139.  
  140.         return $var;
  141.     }
  142.  
  143.     function setVar($name$value null$hash 'default'$overwrite true)
  144.     {
  145.         //If overwrite is true, makes sure the variable hasn't been set yet
  146.         if(!$overwrite && isset($_REQUEST[$name])) {
  147.             return $_REQUEST[$name];
  148.         }
  149.  
  150.         // Clean global request var
  151.         $GLOBALS['_JREQUEST'][$name'SET';
  152.  
  153.         // Get the request hash value
  154.         $hash strtoupper($hash);
  155.         if ($hash === 'METHOD'{
  156.             $hash strtoupper($_SERVER['REQUEST_METHOD']);
  157.         }
  158.         switch ($hash)
  159.         {
  160.             case 'GET' :
  161.                 $_GET[$name$value;
  162.                 $_REQUEST[$name$value;
  163.                 break;
  164.             case 'POST' :
  165.                 $_POST[$name$value;
  166.                 $_REQUEST[$name$value;
  167.                 break;
  168.             case 'FILES' :
  169.                 $_FILES[$name$value;
  170.                 $_REQUEST[$name$value;
  171.                 break;
  172.             case 'COOKIE' :
  173.                 $_COOKIE[$name$value;
  174.                 $_REQUEST[$name$value;
  175.                 break;
  176.             default:
  177.                 $_GET[$name$value;
  178.                 $_POST[$name$value;
  179.                 $_REQUEST[$name$value;
  180.                 break;
  181.         }
  182.  
  183.         return $value;
  184.     }
  185.  
  186.     /**
  187.      * Fetches and returns a request array.
  188.      *
  189.      * The default behaviour is fetching variables depending on the
  190.      * current request method: GET and HEAD will result in returning
  191.      * $_GET, POST and PUT will result in returning $_POST.
  192.      *
  193.      * You can force the source by setting the $hash parameter:
  194.      *
  195.      *   post        $_POST
  196.      *   get            $_GET
  197.      *   files        $_FILES
  198.      *   cookie        $_COOKIE
  199.      *   method        via current $_SERVER['REQUEST_METHOD']
  200.      *   default    $_REQUEST
  201.      *
  202.      * @static
  203.      * @param    string    $hash    to get (POST, GET, FILES, METHOD)
  204.      * @param    int        $mask    Filter mask for the variable
  205.      * @return    mixed    Request hash
  206.      * @since    1.5
  207.      */
  208.     function get($hash 'default'$mask 0)
  209.     {
  210.         static $hashes;
  211.  
  212.         if (!isset($hashes)) {
  213.             $hashes array();
  214.         }
  215.  
  216.         $hash        strtoupper$hash );
  217.         $signature    $hash.$mask;
  218.         if (!isset($hashes[$signature]))
  219.         {
  220.             $result        null;
  221.             $matches    array();
  222.  
  223.             if ($hash === 'METHOD'{
  224.                 $hash strtoupper$_SERVER['REQUEST_METHOD');
  225.             }
  226.  
  227.             switch ($hash)
  228.             {
  229.                 case 'GET' :
  230.                     $input $_GET;
  231.                     break;
  232.  
  233.                 case 'POST' :
  234.                     $input $_POST;
  235.                     break;
  236.  
  237.                 case 'FILES' :
  238.                     $input $_FILES;
  239.                     break;
  240.  
  241.                 case 'COOKIE' :
  242.                     $input $_COOKIE;
  243.                     break;
  244.  
  245.                 default:
  246.                     $input $_REQUEST;
  247.                     break;
  248.             }
  249.  
  250.             $result JRequest::_cleanVar($input$mask);
  251.  
  252.             // Handle magic quotes compatability
  253.             if (get_magic_quotes_gpc()) {
  254.                 $result JRequest::_stripSlashesRecursive$result );
  255.             }
  256.             $hashes[$signature&$result;
  257.         }
  258.         return $hashes[$signature];
  259.     }
  260.  
  261.     function set($array$hash 'default'$overwrite true)
  262.     {
  263.         foreach($array as $key => $value{
  264.             JRequest::setVar($key$value$hash$overwrite);
  265.         }
  266.     }
  267.  
  268.     /**
  269.      * Cleans the request from script injection.
  270.      *
  271.      * @static
  272.      * @return    void 
  273.      * @since    1.5
  274.      */
  275.     function clean()
  276.     {
  277.         JRequest::_cleanArray$_FILES );
  278.         JRequest::_cleanArray$_ENV );
  279.         JRequest::_cleanArray$_GET );
  280.         JRequest::_cleanArray$_POST );
  281.         JRequest::_cleanArray$_COOKIE );
  282.         JRequest::_cleanArray$_SERVER );
  283.  
  284.         if (isset$_SESSION )) {
  285.             JRequest::_cleanArray$_SESSION );
  286.         }
  287.  
  288.         $REQUEST    $_REQUEST;
  289.         $GET        $_GET;
  290.         $POST        $_POST;
  291.         $COOKIE        $_COOKIE;
  292.         $FILES        $_FILES;
  293.         $ENV        $_ENV;
  294.         $SERVER        $_SERVER;
  295.  
  296.         if (isset $_SESSION )) {
  297.             $SESSION $_SESSION;
  298.         }
  299.  
  300.         foreach ($GLOBALS as $key => $value{
  301.             if $key != 'GLOBALS' {
  302.                 unset $GLOBALS $key );
  303.             }
  304.         }
  305.         $_REQUEST    $REQUEST;
  306.         $_GET        $GET;
  307.         $_POST        $POST;
  308.         $_COOKIE    $COOKIE;
  309.         $_FILES        $FILES;
  310.         $_ENV         $ENV;
  311.         $_SERVER     $SERVER;
  312.  
  313.         if (isset $SESSION )) {
  314.             $_SESSION $SESSION;
  315.         }
  316.  
  317.         // Make sure the request hash is clean on file inclusion
  318.         $GLOBALS['_JREQUEST'array();
  319.     }
  320.  
  321.     /**
  322.      * Adds an array to the GLOBALS array and checks that the GLOBALS variable is not being attacked
  323.      *
  324.      * @access    protected
  325.      * @param    array    $array    Array to clean
  326.      * @param    boolean    True if the array is to be added to the GLOBALS
  327.      * @since    1.5
  328.      */
  329.     function _cleanArray&$array$globalise=false )
  330.     {
  331.         static $banned array'_files''_env''_get''_post''_cookie''_server''_session''globals' );
  332.  
  333.         foreach ($array as $key => $value)
  334.         {
  335.             // PHP GLOBALS injection bug
  336.                         $failed in_arraystrtolower$key )$banned );
  337.  
  338.             // PHP Zend_Hash_Del_Key_Or_Index bug
  339.             $failed |= is_numeric$key );
  340.             if ($failed{
  341.                 die'Illegal variable <b>' implode'</b> or <b>'$banned '</b> passed to script.' );
  342.             }
  343.             if ($globalise{
  344.                 $GLOBALS[$key$value;
  345.             }
  346.         }
  347.     }
  348.  
  349.     function _cleanVar($var$mask=0$type=null)
  350.     {
  351.         // Static input filters for specific settings
  352.         static $noHtmlFilter    null;
  353.         static $safeHtmlFilter    null;
  354.             
  355.         // If the no trim flag is not set, trim the variable
  356.                 if (!($mask 1&& is_string($var)) {
  357.             $var trim($var);
  358.         }
  359.  
  360.         // Now we handle input filtering
  361.                 if ($mask 2)
  362.         {
  363.             // If the allow raw flag is set, do not modify the variable
  364.                         $var $var;
  365.         }
  366.         elseif ($mask 4)
  367.         {
  368.             // If the allow html flag is set, apply a safe html filter to the variable
  369.                         if (is_null($safeHtmlFilter)) {
  370.                 $safeHtmlFilter JInputFilter::getInstance(nullnull11);
  371.             }
  372.             $var $safeHtmlFilter->clean($var$type);
  373.         }
  374.         else
  375.         {
  376.         
  377.             
  378.             // Since no allow flags were set, we will apply the most strict filter to the variable
  379.                         if (is_null($noHtmlFilter)) {
  380.                 $noHtmlFilter JInputFilter::getInstance(/* $tags, $attr, $tag_method, $attr_method, $xss_auto */);
  381.             }
  382.             $var $noHtmlFilter->clean($var$type);
  383.         }
  384.         return $var;
  385.     }
  386.  
  387.     /**
  388.      * Strips slashes recursively on an array
  389.      *
  390.      * @access    protected
  391.      * @param    array    $array        Array of (nested arrays of) strings
  392.      * @return    array    The input array with stripshlashes applied to it
  393.      */
  394.     function _stripSlashesRecursive$value )
  395.     {
  396.         $value is_array$value array_maparray'JRequest''_stripSlashesRecursive' )$value stripslashes$value );
  397.         return $value;
  398.     }
  399. }
  400. ?>

Documentation generated on Mon, 05 Mar 2007 21:20:12 +0000 by phpDocumentor 1.3.1