TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-29-2008, 09:41 PM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Help What is happening in this code?

Hi.. I have this code and I'm not quite understand what exacly is happening here.
So could someone explain it to me? ;)

Code:
if (!isset($HTTP_POST_VARS) && isset($_POST))
{
	$HTTP_POST_VARS = $_POST;
	$HTTP_GET_VARS = $_GET;
	$HTTP_SERVER_VARS = $_SERVER;
	$HTTP_COOKIE_VARS = $_COOKIE;
	$HTTP_ENV_VARS = $_ENV;
	$HTTP_POST_FILES = $_FILES;
}

if (get_magic_quotes_gpc())
{
	if (!empty($_GET))    { $_GET    = strip_magic_quotes($_GET);    }
	if (!empty($_POST))   { $_POST   = strip_magic_quotes($_POST);   }
	if (!empty($_COOKIE)) { $_COOKIE = strip_magic_quotes($_COOKIE); }
}

if( !get_magic_quotes_gpc() )
{
	if( is_array($HTTP_GET_VARS) )
	{
		while( list($k, $v) = each($HTTP_GET_VARS) )
		{
			if( is_array($HTTP_GET_VARS[$k]) )
			{
				while( list($k2, $v2) = each($HTTP_GET_VARS[$k]) )
				{
					$HTTP_GET_VARS[$k][$k2] = addslashes($v2);
				}
				@reset($HTTP_GET_VARS[$k]);
			}
			else
			{
				$HTTP_GET_VARS[$k] = addslashes($v);
			}
		}
		@reset($HTTP_GET_VARS);
	}

	if( is_array($HTTP_POST_VARS) )
	{
		while( list($k, $v) = each($HTTP_POST_VARS) )
		{
			if( is_array($HTTP_POST_VARS[$k]) )
			{
				while( list($k2, $v2) = each($HTTP_POST_VARS[$k]) )
				{
					$HTTP_POST_VARS[$k][$k2] = addslashes($v2);
				}
				@reset($HTTP_POST_VARS[$k]);
			}
			else
			{
				$HTTP_POST_VARS[$k] = addslashes($v);
			}
		}
		@reset($HTTP_POST_VARS);
	}

	if( is_array($HTTP_COOKIE_VARS) )
	{
		while( list($k, $v) = each($HTTP_COOKIE_VARS) )
		{
			if( is_array($HTTP_COOKIE_VARS[$k]) )
			{
				while( list($k2, $v2) = each($HTTP_COOKIE_VARS[$k]) )
				{
					$HTTP_COOKIE_VARS[$k][$k2] = addslashes($v2);
				}
				@reset($HTTP_COOKIE_VARS[$k]);
			}
			else
			{
				$HTTP_COOKIE_VARS[$k] = addslashes($v);
			}
		}
		@reset($HTTP_COOKIE_VARS);
	}
}


Thanks..
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 02-29-2008, 09:55 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Looks to me as though it's a script which tries to make PHP backward-compatible by mimicking the old $HTTP_POST_VARS, $HTTP_COOKIE_VARS, et cetera.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-29-2008, 09:56 PM   #3 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Hi Marxx,

See below, I have added inline comments to your code:

PHP Code:
<?php

// The $HTTP_x_VARS superglobal array was replaced by $_POST, $_GET, etc a while
// ago.  This block of code just checks to see if the $HTTP_x_VARS are set and if
// they aren't, it sets them using the new $_POST/GET/etc superglobals.
if (!isset($HTTP_POST_VARS) && isset($_POST))
{
    
$HTTP_POST_VARS $_POST;
    
$HTTP_GET_VARS $_GET;
    
$HTTP_SERVER_VARS $_SERVER;
    
$HTTP_COOKIE_VARS $_COOKIE;
    
$HTTP_ENV_VARS $_ENV;
    
$HTTP_POST_FILES $_FILES;
}

// Magic Quotes is a PHP settings that will automaticly escape quotes in input.
// For example, it will turn ' into \' which (in theory) makes it safe to put
// into a database (it doesn't).
//
// get_magic_quotes_gpc() checks if Magic Quotes are turned on or off.  This
// block will only run if Magic Quotes is turned on (in php.ini)
if (get_magic_quotes_gpc())
{
    
// This block un-escapes the values - ie, \' would become '    
    
if (!empty($_GET))    { $_GET    strip_magic_quotes($_GET);    }
    if (!empty(
$_POST))   { $_POST   strip_magic_quotes($_POST);   }
    if (!empty(
$_COOKIE)) { $_COOKIE strip_magic_quotes($_COOKIE); }
}

// This block will only run if Magic Quotes are turned off
if( !get_magic_quotes_gpc() )
{
    
// Check that $HTTP_GET_VARS (aka, $_GET) contains some values
    
if( is_array($HTTP_GET_VARS) )
    {
        
// ... Then loop through it
        
while( list($k$v) = each($HTTP_GET_VARS) )
        {
            
// ... If it find another array ...
            
if( is_array($HTTP_GET_VARS[$k]) )
            {
                
// ... Then loop through it
                
while( list($k2$v2) = each($HTTP_GET_VARS[$k]) )
                {
                    
// Use the addslashes() function to escape quotes (ie, ' becomes \')
                    
$HTTP_GET_VARS[$k][$k2] = addslashes($v2);
                }
                @
reset($HTTP_GET_VARS[$k]);
            }
            else
            {
                
// Use the addslashes() function to escape quotes (ie, ' becomes \')
                
$HTTP_GET_VARS[$k] = addslashes($v);
            }
        }
        
// Arrays contain an internal 'pointer' which tells PHP what element
        // in the array we are currnetly using - reset() just resets that back
        // to 0
        
@reset($HTTP_GET_VARS);
    }

    
// See above - it now does exactly the same for the $HTTP_POST_VARS / $_POST array
    
if( is_array($HTTP_POST_VARS) )
    {
        while( list(
$k$v) = each($HTTP_POST_VARS) )
        {
            if( 
is_array($HTTP_POST_VARS[$k]) )
            {
                while( list(
$k2$v2) = each($HTTP_POST_VARS[$k]) )
                {
                    
$HTTP_POST_VARS[$k][$k2] = addslashes($v2);
                }
                @
reset($HTTP_POST_VARS[$k]);
            }
            else
            {
                
$HTTP_POST_VARS[$k] = addslashes($v);
            }
        }
        @
reset($HTTP_POST_VARS);
    }

    
// And now for the $HTTP_COOKIE_VARS / $_COOKIE array
    
if( is_array($HTTP_COOKIE_VARS) )
    {
        while( list(
$k$v) = each($HTTP_COOKIE_VARS) )
        {
            if( 
is_array($HTTP_COOKIE_VARS[$k]) )
            {
                while( list(
$k2$v2) = each($HTTP_COOKIE_VARS[$k]) )
                {
                    
$HTTP_COOKIE_VARS[$k][$k2] = addslashes($v2);
                }
                @
reset($HTTP_COOKIE_VARS[$k]);
            }
            else
            {
                
$HTTP_COOKIE_VARS[$k] = addslashes($v);
            }
        }
        @
reset($HTTP_COOKIE_VARS);
    }
}
Essentially, it just runs addslashes() on every post/get/cookie variable. I'm guessing that they where trying to do a catch-all security system to escape user input but this really isn't a good idea. You should generally filter/escape input on individual rules, not rely on a catch-all system like this

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:36 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design