home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / grab_globals.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  3.8 KB  |  122 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * This library grabs the names and values of the variables sent or posted to a
  5.  * script in the $_* arrays and sets simple globals variables from them. It does
  6.  * the same work for the $PHP_SELF, $HTTP_ACCEPT_LANGUAGE and
  7.  * $HTTP_AUTHORIZATION variables.
  8.  *
  9.  * loic1 - 2001/25/11: use the new globals arrays defined with php 4.1+
  10.  *
  11.  * @version $Id: grab_globals.lib.php 10796 2007-10-16 07:09:50Z cybot_tm $
  12.  */
  13.  
  14. /**
  15.  * copy values from one array to another, usally from a superglobal into $GLOBALS
  16.  *
  17.  * @uses    $GLOBALS['_import_blacklist']
  18.  * @uses    preg_replace()
  19.  * @uses    array_keys()
  20.  * @uses    array_unique()
  21.  * @uses    stripslashes()
  22.  * @param   array   $array      values from
  23.  * @param   array   $target     values to
  24.  * @param   boolean $sanitize   prevent importing key names in $_import_blacklist
  25.  */
  26. function PMA_gpc_extract($array, &$target, $sanitize = true)
  27. {
  28.     if (! is_array($array)) {
  29.         return false;
  30.     }
  31.  
  32.     if ($sanitize) {
  33.         $valid_variables = preg_replace($GLOBALS['_import_blacklist'], '',
  34.             array_keys($array));
  35.         $valid_variables = array_unique($valid_variables);
  36.     } else {
  37.         $valid_variables = array_keys($array);
  38.     }
  39.  
  40.     foreach ($valid_variables as $key) {
  41.  
  42.         if (strlen($key) === 0) {
  43.             continue;
  44.         }
  45.  
  46.         if (is_array($array[$key])) {
  47.             // there could be a variable coming from a cookie of
  48.             // another application, with the same name as this array
  49.             unset($target[$key]);
  50.  
  51.             PMA_gpc_extract($array[$key], $target[$key], false);
  52.         } else {
  53.             $target[$key] = $array[$key];
  54.         }
  55.     }
  56.     return true;
  57. }
  58.  
  59.  
  60. /**
  61.  * @var array $_import_blacklist variable names that should NEVER be imported
  62.  *                              from superglobals
  63.  */
  64. $_import_blacklist = array(
  65.     '/^cfg$/i',         // PMA configuration
  66.     '/^server$/i',      // selected server
  67.     '/^db$/i',          // page to display
  68.     '/^table$/i',       // page to display
  69.     '/^goto$/i',        // page to display
  70.     '/^back$/i',        // the page go back
  71.     '/^lang$/i',        // selected language
  72.     '/^convcharset$/i', // PMA convert charset
  73.     '/^collation_connection$/i', //
  74.     '/^set_theme$/i',   //
  75.     '/^sql_query$/i',   // the query to be executed
  76.     '/^GLOBALS$/i',     // the global scope
  77.     '/^str.*$/i',       // PMA localized strings
  78.     '/^_.*$/i',         // PMA does not use variables starting with _ from extern
  79.     '/^.*\s+.*$/i',     // no whitespaces anywhere
  80.     '/^[0-9]+.*$/i',    // numeric variable names
  81.     //'/^PMA_.*$/i',      // other PMA variables
  82. );
  83.  
  84. if (! empty($_GET)) {
  85.     PMA_gpc_extract($_GET, $GLOBALS);
  86. }
  87.  
  88. if (! empty($_POST)) {
  89.     PMA_gpc_extract($_POST, $GLOBALS);
  90. }
  91.  
  92. if (! empty($_FILES)) {
  93.     $_valid_variables = preg_replace($GLOBALS['_import_blacklist'], '', array_keys($_FILES));
  94.     foreach ($_valid_variables as $name) {
  95.         if (strlen($name) != 0) {
  96.             $$name = $_FILES[$name]['tmp_name'];
  97.             ${$name . '_name'} = $_FILES[$name]['name'];
  98.         }
  99.     }
  100.     unset($name, $value);
  101. }
  102.  
  103. /**
  104.  * globalize some environment variables
  105.  */
  106. $server_vars = array('HTTP_ACCEPT_LANGUAGE', 'HTTP_AUTHORIZATION');
  107. foreach ($server_vars as $current) {
  108.     // its not important HOW we detect html tags
  109.     // its more important to prevent XSS
  110.     // so its not important if we result in an invalid string,
  111.     // its even better than a XSS capable string
  112.     if (PMA_getenv($current) && false === strpos(PMA_getenv($current), '<')) {
  113.         $$current = PMA_getenv($current);
  114.     // already importet by register_globals?
  115.     } elseif (! isset($$current) || false !== strpos($$current, '<')) {
  116.         $$current = '';
  117.     }
  118. }
  119. unset($server_vars, $current, $_import_blacklist);
  120.  
  121. ?>
  122.