Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/environment/request.php

Documentation is available at request.php

  1. <?php
  2. /**
  3.  * @version        $Id: request.php 9944 2008-01-14 21:10:22Z eddieajau $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Environment
  6.  * @copyright    Copyright (C) 2005 - 2008 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. /**
  19.  * Create the request global object
  20.  */
  21. $GLOBALS['_JREQUEST'array();
  22.  
  23. /**
  24.  * Set the available masks for cleaning variables
  25.  */
  26. define'JREQUEST_NOTRIM'   );
  27. define'JREQUEST_ALLOWRAW' );
  28. define'JREQUEST_ALLOWHTML');
  29.  
  30. /**
  31.  * JRequest Class
  32.  *
  33.  * This class serves to provide the Joomla Framework with a common interface to access
  34.  * request variables.  This includes $_POST, $_GET, and naturally $_REQUEST.  Variables
  35.  * can be passed through an input filter to avoid injection or returned raw.
  36.  *
  37.  * @static
  38.  * @package        Joomla.Framework
  39.  * @subpackage    Environment
  40.  * @since        1.5
  41.  */
  42. class JRequest
  43. {
  44.     /**
  45.      * Gets the full request path
  46.      *
  47.      * @return string 
  48.      */
  49.     function getURI()
  50.     {
  51.         $uri &JFactory::getURI();
  52.         return $uri->toString(array('path''query'));
  53.     }
  54.  
  55.     /**
  56.      * Gets the request method
  57.      *
  58.      * @return string 
  59.      */
  60.     function getMethod()
  61.     {
  62.         $method strtoupper$_SERVER['REQUEST_METHOD');
  63.         return $method;
  64.     }
  65.  
  66.     /**
  67.      * Fetches and returns a given variable.
  68.      *
  69.      * The default behaviour is fetching variables depending on the
  70.      * current request method: GET and HEAD will result in returning
  71.      * an entry from $_GET, POST and PUT will result in returning an
  72.      * entry from $_POST.
  73.      *
  74.      * You can force the source by setting the $hash parameter:
  75.      *
  76.      *   post        $_POST
  77.      *   get        $_GET
  78.      *   files        $_FILES
  79.      *   cookie        $_COOKIE
  80.      *   env        $_ENV
  81.      *   server        $_SERVER
  82.      *   method        via current $_SERVER['REQUEST_METHOD']
  83.      *   default    $_REQUEST
  84.      *
  85.      * @static
  86.      * @param    string    $name        Variable name
  87.      * @param    string    $default    Default value if the variable does not exist
  88.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  89.      * @param    string    $type        Return type for the variable, for valid values see {@link JFilterInput::clean()}
  90.      * @param    int        $mask        Filter mask for the variable
  91.      * @return    mixed    Requested variable
  92.      * @since    1.5
  93.      */
  94.     function getVar($name$default null$hash 'default'$type 'none'$mask 0)
  95.     {
  96.         // Ensure hash and type are uppercase
  97.         $hash strtoupper$hash );
  98.         if ($hash === 'METHOD'{
  99.             $hash strtoupper$_SERVER['REQUEST_METHOD');
  100.         }
  101.         $type    strtoupper$type );
  102.         $sig    $hash.$type.$mask;
  103.  
  104.         // Get the input hash
  105.         switch ($hash)
  106.         {
  107.             case 'GET' :
  108.                 $input &$_GET;
  109.                 break;
  110.             case 'POST' :
  111.                 $input &$_POST;
  112.                 break;
  113.             case 'FILES' :
  114.                 $input &$_FILES;
  115.                 break;
  116.             case 'COOKIE' :
  117.                 $input &$_COOKIE;
  118.                 break;
  119.             case 'ENV'    :
  120.                 $input &$_ENV;
  121.                 break;
  122.             case 'SERVER'    :
  123.                 $input &$_SERVER;
  124.                 break;
  125.             default:
  126.                 $input &$_REQUEST;
  127.                 $hash 'REQUEST';
  128.                 break;
  129.         }
  130.  
  131.         if (isset($GLOBALS['_JREQUEST'][$name]['SET.'.$hash]&& ($GLOBALS['_JREQUEST'][$name]['SET.'.$hash=== true)) {
  132.             // Get the variable from the input hash
  133.             $var (isset($input[$name]&& $input[$name!== null$input[$name$default;
  134.         }
  135.         elseif (!isset($GLOBALS['_JREQUEST'][$name][$sig]))
  136.         {
  137.             if (isset($input[$name]&& $input[$name!== null{
  138.                 // Get the variable from the input hash and clean it
  139.                 $var JRequest::_cleanVar($input[$name]$mask$type);
  140.  
  141.                 // Handle magic quotes compatability
  142.                 if (get_magic_quotes_gpc(&& ($var != $default&& ($hash != 'FILES')) {
  143.                     $var JRequest::_stripSlashesRecursive$var );
  144.                 }
  145.  
  146.                 $GLOBALS['_JREQUEST'][$name][$sig$var;
  147.             }
  148.             elseif ($default !== null{
  149.                 // Clean the default value
  150.                 $var JRequest::_cleanVar($default$mask$type);
  151.             }
  152.             else {
  153.                 $var $default;
  154.             }
  155.         else {
  156.             $var $GLOBALS['_JREQUEST'][$name][$sig];
  157.         }
  158.  
  159.         return $var;
  160.     }
  161.  
  162.     /**
  163.      * Fetches and returns a given filtered variable. The integer
  164.      * filter will allow only digits to be returned. This is currently
  165.      * only a proxy function for getVar().
  166.      *
  167.      * See getVar() for more in-depth documentation on the parameters.
  168.      *
  169.      * @static
  170.      * @param    string    $name        Variable name
  171.      * @param    string    $default    Default value if the variable does not exist
  172.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  173.      * @return    integer    Requested variable
  174.      * @since    1.5
  175.      */
  176.     function getInt($name$default 0$hash 'default')
  177.     {
  178.         return JRequest::getVar($name$default$hash'int');
  179.     }
  180.  
  181.     /**
  182.      * Fetches and returns a given filtered variable.  The float
  183.      * filter only allows digits and periods.  This is currently
  184.      * only a proxy function for getVar().
  185.      *
  186.      * See getVar() for more in-depth documentation on the parameters.
  187.      *
  188.      * @static
  189.      * @param    string    $name        Variable name
  190.      * @param    string    $default    Default value if the variable does not exist
  191.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  192.      * @return    float    Requested variable
  193.      * @since    1.5
  194.      */
  195.     function getFloat($name$default 0.0$hash 'default')
  196.     {
  197.         return JRequest::getVar($name$default$hash'float');
  198.     }
  199.  
  200.     /**
  201.      * Fetches and returns a given filtered variable. The bool
  202.      * filter will only return true/false bool values. This is
  203.      * currently only a proxy function for getVar().
  204.      *
  205.      * See getVar() for more in-depth documentation on the parameters.
  206.      *
  207.      * @static
  208.      * @param    string    $name        Variable name
  209.      * @param    string    $default    Default value if the variable does not exist
  210.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  211.      * @return    bool        Requested variable
  212.      * @since    1.5
  213.      */
  214.     function getBool($name$default false$hash 'default')
  215.     {
  216.         return JRequest::getVar($name$default$hash'bool');
  217.     }
  218.  
  219.     /**
  220.      * Fetches and returns a given filtered variable. The word
  221.      * filter only allows the characters [A-Za-z_]. This is currently
  222.      * only a proxy function for getVar().
  223.      *
  224.      * See getVar() for more in-depth documentation on the parameters.
  225.      *
  226.      * @static
  227.      * @param    string    $name        Variable name
  228.      * @param    string    $default    Default value if the variable does not exist
  229.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  230.      * @return    string    Requested variable
  231.      * @since    1.5
  232.      */
  233.     function getWord($name$default ''$hash 'default')
  234.     {
  235.         return JRequest::getVar($name$default$hash'word');
  236.     }
  237.  
  238.     /**
  239.      * Fetches and returns a given filtered variable. The cmd
  240.      * filter only allows the characters [A-Za-z0-9.-_]. This is
  241.      * currently only a proxy function for getVar().
  242.      *
  243.      * See getVar() for more in-depth documentation on the parameters.
  244.      *
  245.      * @static
  246.      * @param    string    $name        Variable name
  247.      * @param    string    $default    Default value if the variable does not exist
  248.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  249.      * @return    string    Requested variable
  250.      * @since    1.5
  251.      */
  252.     function getCmd($name$default ''$hash 'default')
  253.     {
  254.         return JRequest::getVar($name$default$hash'cmd');
  255.     }
  256.  
  257.     /**
  258.      * Fetches and returns a given filtered variable. The string
  259.      * filter deletes 'bad' HTML code, if not overridden by the mask.
  260.      * This is currently only a proxy function for getVar().
  261.      *
  262.      * See getVar() for more in-depth documentation on the parameters.
  263.      *
  264.      * @static
  265.      * @param    string    $name        Variable name
  266.      * @param    string    $default    Default value if the variable does not exist
  267.      * @param    string    $hash        Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
  268.       * @param    int        $mask        Filter mask for the variable
  269.      * @return    string    Requested variable
  270.      * @since    1.5
  271.      */
  272.     function getString($name$default ''$hash 'default'$mask 0)
  273.     {
  274.         // Cast to string, in case JREQUEST_ALLOWRAW was specified for mask
  275.         return (string) JRequest::getVar($name$default$hash'string'$mask);
  276.     }
  277.  
  278.     /**
  279.      * Set a variabe in on of the request variables
  280.      *
  281.      * @access    public
  282.      * @param    string    $name        Name
  283.      * @param    string    $value        Value
  284.      * @param    string    $hash        Hash
  285.      * @param    boolean    $overwrite    Boolean
  286.      * @return    string    Previous value
  287.      * @since    1.5
  288.      */
  289.     function setVar($name$value null$hash 'method'$overwrite true)
  290.     {
  291.         //If overwrite is true, makes sure the variable hasn't been set yet
  292.         if(!$overwrite && array_key_exists($name$_REQUEST)) {
  293.             return $_REQUEST[$name];
  294.         }
  295.  
  296.         // Clean global request var
  297.         $GLOBALS['_JREQUEST'][$namearray();
  298.  
  299.         // Get the request hash value
  300.         $hash strtoupper($hash);
  301.         if ($hash === 'METHOD'{
  302.             $hash strtoupper($_SERVER['REQUEST_METHOD']);
  303.         }
  304.  
  305.         $previous    array_key_exists($name$_REQUEST$_REQUEST[$namenull;
  306.  
  307.         switch ($hash)
  308.         {
  309.             case 'GET' :
  310.                 $_GET[$name$value;
  311.                 $_REQUEST[$name$value;
  312.                 break;
  313.             case 'POST' :
  314.                 $_POST[$name$value;
  315.                 $_REQUEST[$name$value;
  316.                 break;
  317.             case 'COOKIE' :
  318.                 $_COOKIE[$name$value;
  319.                 $_REQUEST[$name$value;
  320.                 break;
  321.             case 'FILES' :
  322.                 $_FILES[$name$value;
  323.                 break;
  324.             case 'ENV'    :
  325.                 $_ENV['name'$value;
  326.                 break;
  327.             case 'SERVER'    :
  328.                 $_SERVER['name'$value;
  329.                 break;
  330.         }
  331.  
  332.         // Mark this variable as 'SET'
  333.         $GLOBALS['_JREQUEST'][$name]['SET.'.$hashtrue;
  334.         $GLOBALS['_JREQUEST'][$name]['SET.REQUEST'true;
  335.  
  336.         return $previous;
  337.     }
  338.  
  339.     /**
  340.      * Fetches and returns a request array.
  341.      *
  342.      * The default behaviour is fetching variables depending on the
  343.      * current request method: GET and HEAD will result in returning
  344.      * $_GET, POST and PUT will result in returning $_POST.
  345.      *
  346.      * You can force the source by setting the $hash parameter:
  347.      *
  348.      *   post        $_POST
  349.      *   get        $_GET
  350.      *   files        $_FILES
  351.      *   cookie        $_COOKIE
  352.      *   env        $_ENV
  353.      *   server        $_SERVER
  354.      *   method        via current $_SERVER['REQUEST_METHOD']
  355.      *   default    $_REQUEST
  356.      *
  357.      * @static
  358.      * @param    string    $hash    to get (POST, GET, FILES, METHOD)
  359.      * @param    int        $mask    Filter mask for the variable
  360.      * @return    mixed    Request hash
  361.      * @since    1.5
  362.      */
  363.     function get($hash 'default'$mask 0)
  364.     {
  365.         $hash strtoupper($hash);
  366.  
  367.         if ($hash === 'METHOD'{
  368.             $hash strtoupper$_SERVER['REQUEST_METHOD');
  369.         }
  370.  
  371.         switch ($hash)
  372.         {
  373.             case 'GET' :
  374.                 $input $_GET;
  375.                 break;
  376.  
  377.             case 'POST' :
  378.                 $input $_POST;
  379.                 break;
  380.  
  381.             case 'FILES' :
  382.                 $input $_FILES;
  383.                 break;
  384.  
  385.             case 'COOKIE' :
  386.                 $input $_COOKIE;
  387.                 break;
  388.  
  389.             case 'ENV'    :
  390.                 $input &$_ENV;
  391.                 break;
  392.  
  393.             case 'SERVER'    :
  394.                 $input &$_SERVER;
  395.                 break;
  396.  
  397.             default:
  398.                 $input $_REQUEST;
  399.                 break;
  400.         }
  401.  
  402.         $result JRequest::_cleanVar($input$mask);
  403.  
  404.         // Handle magic quotes compatability
  405.         if (get_magic_quotes_gpc(&& ($hash != 'FILES')) {
  406.             $result JRequest::_stripSlashesRecursive$result );
  407.         }
  408.  
  409.         return $result;
  410.     }
  411.  
  412.     /**
  413.      * Sets a request variable
  414.      *
  415.      * @param    array    An associative array of key-value pairs
  416.      * @param    string    The request variable to set (POST, GET, FILES, METHOD)
  417.      * @param    boolean    If true and an existing key is found, the value is overwritten, otherwise it is ingored
  418.      */
  419.     function set$array$hash 'default'$overwrite true )
  420.     {
  421.         foreach ($array as $key => $value{
  422.             JRequest::setVar($key$value$hash$overwrite);
  423.         }
  424.     }
  425.  
  426.     /**
  427.      * Checks for a form token in the request
  428.      *
  429.      * Use in conjuction with JHTML::_( 'form.token' )
  430.      *
  431.      * @param    string    The request method in which to look for the token key
  432.      * @return    boolean    True if found and valid, false otherwise
  433.      */
  434.     function checkToken$method 'post' )
  435.     {
  436.         $token    JUtility::getToken();
  437.         return JRequest::getVar$token''$method'alnum' );
  438.     }
  439.  
  440.     /**
  441.      * Cleans the request from script injection.
  442.      *
  443.      * @static
  444.      * @return    void 
  445.      * @since    1.5
  446.      */
  447.     function clean()
  448.     {
  449.         JRequest::_cleanArray$_FILES );
  450.         JRequest::_cleanArray$_ENV );
  451.         JRequest::_cleanArray$_GET );
  452.         JRequest::_cleanArray$_POST );
  453.         JRequest::_cleanArray$_COOKIE );
  454.         JRequest::_cleanArray$_SERVER );
  455.  
  456.         if (isset$_SESSION )) {
  457.             JRequest::_cleanArray$_SESSION );
  458.         }
  459.  
  460.         $REQUEST    $_REQUEST;
  461.         $GET        $_GET;
  462.         $POST        $_POST;
  463.         $COOKIE        $_COOKIE;
  464.