<?php
class request
{
/*
request class, a PHP class for web request data
Copyright (C) 2007 Joshua Townsend
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Contact GamingG at <http://www.gamingg.net> and <http://www.pwnedgalaxy.net>
*/
// Variables
var $fixed_magic_quotes = false; // set to true if magic quotes was fixed by request::fix_magic_quotes()
var $http_headers = array(); // array that will contain all HTTP headers provided by server array
var $mode_cache = array(); // array containing the return values of parse_mode()
var $custom_modes = array(); // array containing custom modes and their data
var $default_mode = "r"; // default mode to use if mode is not specified in a function
var $mode_override = false; // whether or not to allow overriding of default modes
// Constructor
function request()
{
$this->fixed_magic_quotes = $this->magic_quotes();
$this->parse_http_headers();
}
// Parsing methods
function parse_mode($mode)
{
if($mode === null)
{
$mode = $this->default_mode;
}
$mode = strtolower($mode);
if(array_key_exists($mode, $this->mode_cache))
{
return $this->mode_cache[$mode];
}
$reta = array();
for($x = 0; $x < strlen($mode); $x++)
{
if(array_key_exists($mode{$x}, $this->custom_modes))
{
$reta[] = &$this->custom_modes[$mode{$x}];
continue;
}
switch($mode{$x})
{
case "c": $reta[] = &$_COOKIE; break;
case "e": $reta[] = &$_ENV; break;
case "g": $reta[] = &$_GET; break;
case "p": $reta[] = &$_POST; break;
case "r": $reta[] = &$_REQUEST; break;
case "s": $reta[] = &$_SERVER; break;
default: trigger_error(__CLASS__."::".__FUNCTION__."(): Unknown mode \"{$mode{$x}}\"", E_USER_WARNING);
}
}
$reta = array_reverse($reta); // reverse to give the last arrays more precedence, like in php.ini variables.order
$this->mode_cache[$mode] = $reta;
return $reta;
}
function parse_http_headers()
{
foreach(array_keys($_SERVER) as $key)
{
if(substr($key, 0, 5) != "HTTP_")
{
continue;
}
$header = implode("-", array_map("ucfirst", explode("_", strtolower(substr($key, 5))))); // fix header case and word delimiter (i.e. HTTP_FOO_BAR -> Foo-Bar)
$this->http_headers[$header] = $_SERVER[$key];
}
}
// Magic Quotes methods
function magic_quotes()
{
if(get_magic_quotes_gpc() && !$this->fixed_magic_quotes)
{
foreach(array("_COOKIE", "_ENV", "_GET", "_POST", "_REQUEST", "_SERVER") as $var)
{
$this->fix_magic_quotes(${$var});
}
return true;
}
return false;
}
function &fix_magic_quotes(&$arr)
{
if(is_array($arr))
{
$this->fix_magic_quotes($arr);
}
else
{
$arr = stripslashes($arr);
}
return $arr;
}
// Basic method for getting values from the request
function get_value($name, $mode = null, $default = null)
{
foreach($this->parse_mode($mode) as $arr)
{
if(array_key_exists($name, $arr))
{
return $arr[$name];
}
}
return $default;
}
// Methods for getting specific types of values from the request
function get_bool($name, $mode = null, $default = null)
{
return (bool)$this->get_value($name, $mode, $default);
}
function get_int($name, $mode = null, $default = null)
{
return (int)$this->get_value($name, $mode, $default);
}
function get_float($name, $mode = null, $default = null)
{
return (float)$this->get_value($name, $mode, $default);
}
function get_string($name, $mode = null, $default = null)
{
return (string)$this->get_value($name, $mode, $default);
}
function get_array($name, $mode = null, $default = null)
{
return (array)$this->get_value($name, $mode, $default);
}
function get_typed_array($name, $mode = null, $type = null, $default = null)
{
$arr = $this->get_array($name, $mode, $default);
foreach(array_keys($arr) as $key)
{
switch($type)
{
case "bool": $arr[$key] = (bool)$arr[$key]; break;
case "int": $arr[$key] = (int)$arr[$key]; break;
case "float": $arr[$key] = (float)$arr[$key]; break;
case "string": $arr[$key] = (string)$arr[$key]; break;
case "array": $arr[$key] = (array)$arr[$key]; break;
}
}
return $arr;
}
// Methods for uploaded files
function file_tempname($name)
{
return (string)(array_key_exists($name, $_FILES) ? $_FILES[$name]['tmp_name'] : null);
}
function file_size($name)
{
return (int)(array_key_exists($name, $_FILES) ? $_FILES[$name]['size'] : 0);
}
function file_error($name)
{
return (int)((array_key_exists($name, $_FILES) && array_key_exists("error", $_FILES[$name])) ? $_FILES[$name]['error'] : UPLOAD_ERR_OK);
}
function file_name($name)
{
return (string)(array_key_exists($name, $_FILES) ? $_FILES[$name]['name'] : null);
}
// Custom mode methods
function add_mode($mode, $data)
{
$mode = strtolower($mode);
$data = (array)$data;
if(strlen($mode) != 1)
{
trigger_error(__CLASS__."::".__FUNCTION__."(): mode \"{$mode}\" is not a single character, ignoring attempt to add", E_USER_WARNING);
return false;
}
if(!$this->mode_override && in_array($mode, array("e", "g", "p", "c", "s", "r")))
{
trigger_error(__CLASS__."::".__FUNCTION__."(): Cannot override default mode \"{$mode}\", ignoring attempt to add", E_USER_WARNING);
return false;
}
$this->custom_modes[$mode] = $data;
$this->remove_mode_cache($mode);
return true;
}
function remove_mode($mode)
{
if(array_key_exists($mode, $this->custom_modes))
{
unset($this->custom_modes[$mode]);
$this->remove_mode_cache($mode);
return true;
}
trigger_error(__CLASS__."::".__FUNCTION__."(): mode \"{$mode}\" has not been added yet, ignoring attempt to remove", E_USER_WARNING);
return false;
}
function set_default_mode($mode)
{
$this->default_mode = $mode;
return true;
}
function allow_mode_override($value = null)
{
if(func_num_args() > 0)
{
$this->mode_override = (bool)$value;
}
return $this->mode_override;
}
function remove_mode_cache($mode)
{
foreach(array_keys($this->mode_cache) as $key)
{
if(strpos($key, $mode) !== false)
{
unset($this->mode_cache[$key]);
}
}
}
// Special-purpose methods
function get_html_string($name, $mode = null, $default = null)
{
return htmlspecialchars(str_replace(array("\r\n", "\r"), "\n", $this->get_string($name, $mode, $default))); // fixes line endings and html characters
}
function get_clean_string($name, $mode = null, $default = null)
{
return str_replace(array("\r\n", "\r"), "\n", trim($this->get_string($name, $mode, $default)));
}
function is_checked($name, $mode = null)
{
foreach($this->parse_mode($mode) as $arr)
{
if(array_key_exists($name, $arr))
{
return true;
}
}
return false;
}
function check_session_cookie()
{
return array_key_exists(ini_get("session.name"), $_COOKIE);
}
function get_header($name, $default = null)
{
return (string)(array_key_exists($name, $this->http_headers) ? $this->http_headers[$name] : $default);
}
}
?>