3 // vim: expandtab sw=4 ts=4 sts=4:
7 * This library grabs the names and values of the variables sent or posted to a
8 * script in the $_* arrays and sets simple globals variables from them. It does
9 * the same work for the $PHP_SELF, $HTTP_ACCEPT_LANGUAGE and
10 * $HTTP_AUTHORIZATION variables.
12 * loic1 - 2001/25/11: use the new globals arrays defined with php 4.1+
15 // just to be sure there was no import (registering) before here
16 $variables_whitelist = array (
27 foreach ( get_defined_vars() as $key => $value ) {
28 if ( ! in_array( $key, $variables_whitelist ) ) {
32 unset( $key, $value );
34 // protect against older PHP versions' bug about GLOBALS overwrite
35 // (no need to translate this one :) )
36 // but what if script.php?GLOABLS[admin]=1&GLOBALS[_REQUEST]=1 ???
37 if ( isset( $_REQUEST['GLOBALS'] ) ||
isset( $_FILES['GLOBALS'] )
38 ||
isset( $_SERVER['GLOBALS'] ) ||
isset( $_COOKIE['GLOBALS'] )
39 ||
isset( $_ENV['GLOBALS'] ) ) {
40 die( 'GLOBALS overwrite attempt' );
43 require_once './libraries/session.inc.php';
46 * @var array $import_blacklist variable names that should NEVER be imported
49 $import_blacklist = array(
50 '/^cfg$/i', // PMA configuration
51 '/^GLOBALS$/i', // the global scope
52 '/^str.*$/i', // PMA strings
53 '/^_.*$/i', // PMA does not use variables starting with _ from extern
54 '/^.*\s+.*$/i', // no whitespaces anywhere
55 '/^[0-9]+.*$/i', // numeric variable names
56 //'/^PMA_.*$/i', // other PMA variables
60 * copy values from one array to another, usally from a superglobal into $GLOBALS
62 * @uses $GLOBALS['import_blacklist']
63 * @uses preg_replace()
65 * @uses array_unique()
66 * @uses get_magic_quotes_gpc() to check wether stripslashes or not
67 * @uses stripslashes()
68 * @param array $array values from
69 * @param array $target values to
70 * @param boolean $sanitize prevent importing key names in $import_blacklist
72 function PMA_gpc_extract($array, &$target, $sanitize = TRUE) {
73 if (!is_array($array)) {
78 $valid_variables = preg_replace( $GLOBALS['import_blacklist'], '',
79 array_keys( $array ) );
80 $valid_variables = array_unique( $valid_variables );
82 $valid_variables = array_keys( $array );
85 $is_magic_quotes = get_magic_quotes_gpc();
87 foreach ( $valid_variables as $key ) {
89 if ( strlen( $key ) === 0 ) {
93 if ( is_array( $array[$key] ) ) {
94 // there could be a variable coming from a cookie of
95 // another application, with the same name as this array
98 PMA_gpc_extract($array[$key], $target[$key], FALSE);
99 } elseif ($is_magic_quotes) {
100 $target[$key] = stripslashes($array[$key]);
102 $target[$key] = $array[$key];
109 // check if a subform is submitted
111 if ( isset( $_POST['usesubform'] ) ) {
112 // if a subform is present and should be used
113 // the rest of the form is deprecated
114 $subform_id = key( $_POST['usesubform'] );
115 $subform = $_POST['subform'][$subform_id];
117 if ( isset( $_POST['redirect'] )
118 && $_POST['redirect'] != basename( $_SERVER['PHP_SELF'] ) ) {
119 $__redirect = $_POST['redirect'];
120 unset( $_POST['redirect'] );
121 } // end if ( isset( $_POST['redirect'] ) )
122 unset( $subform_id, $subform );
123 } // end if ( isset( $_POST['usesubform'] ) )
124 // end check if a subform is submitted
127 PMA_gpc_extract($_GET, $GLOBALS);
130 if (!empty($_POST)) {
131 PMA_gpc_extract($_POST, $GLOBALS);
132 } // end if (!empty($_POST))
134 if (!empty($_FILES)) {
135 foreach ($_FILES AS $name => $value) {
136 $
$name = $value['tmp_name'];
137 $
{$name . '_name'} = $value['name'];
140 unset( $name, $value );
142 if (!empty($_SERVER)) {
143 $server_vars = array('PHP_SELF', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_AUTHORIZATION');
144 foreach ( $server_vars as $current ) {
145 // its not important HOW we detect html tags
146 // its more important to prevent XSS
147 // so its not important if we result in an invalid string,
148 // its even better than a XSS capable string
149 if ( isset( $_SERVER[$current] ) && false === strpos( $_SERVER[$current], '<' ) ) {
150 $
$current = $_SERVER[$current];
151 // already importet by register_globals?
152 } elseif ( ! isset( $
$current ) ||
false !== strpos( $
$current, '<' ) ) {
156 unset( $server_vars, $current );
159 // Security fix: disallow accessing serious server files via "?goto="
160 if (isset($goto) && strpos(' ' . $goto, '/') > 0 && substr($goto, 0, 2) != './') {
164 unset( $import_blacklist );
166 if ( ! empty( $__redirect ) ) {
167 // TODO: ensure that PMA_securePath() is defined and available
168 // for this script. Meanwhile we duplicate what this function does:
169 require('./' . preg_replace('@\.\.*@','.',$__redirect));
171 } // end if ( ! empty( $__redirect ) )