<?php
/* Methods for parsing user input.
*
* Written by: Chris Studholme
* Copyright: GPL (http://www.fsf.org/copyleft/gpl.html)
* $Id: input.php,v 1.1 2004/01/29 05:26:02 cstudhol Exp $
*/
// input methods
define("HTTP_POST",1);
define("HTTP_GET",2);
define("HTTP_ANY",3);
// boolean
function input_bool($name,$default=false,$method=HTTP_ANY) {
global $HTTP_GET_VARS, $HTTP_POST_VARS;
if ($method&HTTP_POST && isset($HTTP_POST_VARS[$name]))
return $HTTP_POST_VARS[$name] ? true : false;
else if ($method&HTTP_GET && isset($HTTP_GET_VARS[$name]))
return $HTTP_GET_VARS[$name] ? true : false;
return $default;
}
// integer
function input_int($name,$default=0,$method=HTTP_ANY) {
global $HTTP_GET_VARS, $HTTP_POST_VARS;
if ($method&HTTP_POST && isset($HTTP_POST_VARS[$name]))
return (int)$HTTP_POST_VARS[$name];
else if ($method&HTTP_GET && isset($HTTP_GET_VARS[$name]))
return (int)$HTTP_GET_VARS[$name];
return $default;
}
// float
function input_float($name,$default=0,$method=HTTP_ANY) {
global $HTTP_GET_VARS, $HTTP_POST_VARS;
if ($method&HTTP_POST && isset($HTTP_POST_VARS[$name]))
return (float)$HTTP_POST_VARS[$name];
else if ($method&HTTP_GET && isset($HTTP_GET_VARS[$name]))
return (float)$HTTP_GET_VARS[$name];
return $default;
}
// some sort of array (only works with POST)
function input_array($name,$default=false,$method=HTTP_ANY) {
global $HTTP_POST_VARS;
if ($method&HTTP_POST && is_array($HTTP_POST_VARS[$name]))
return $HTTP_POST_VARS[$name];
return $default;
}
// any string (returns false if string not found)
function input_string($name,$default=false,$method=HTTP_ANY) {
global $HTTP_GET_VARS, $HTTP_POST_VARS;
if ($method&HTTP_POST && isset($HTTP_POST_VARS[$name]))
$result = strval($HTTP_POST_VARS[$name]);
else if ($method&HTTP_GET && isset($HTTP_GET_VARS[$name]))
$result = strval($HTTP_GET_VARS[$name]);
else
return $default;
return get_magic_quotes_gpc() ? stripslashes($result) : $result;
}
// string without html
function input_string_nohtml($name,$default=false,$method=HTTP_ANY) {
$result = input_string($name,$default,$method);
return is_string($result) ? htmlspecialchars($result) : false;
}
// single word (can contain [a-zA-Z0-9_], [0-9] can't be at beginning)
function input_string_word($name,$default=false,$method=HTTP_ANY) {
$result = input_string($name,$default,$method);
return is_string($result) ?
ereg_replace("^[0-9]*","",ereg_replace("[^a-zA-Z0-9_]","",$result)) :
false;
}
// date (contains characters [-0-9])
function input_string_date($name) {
$result = input_string($name);
return is_string($result) ? ereg_replace("[^-0-9]","",$result) : false;
}
// relative filesystem path (relative to directory $base)
function input_path($name,$base) {
$p = input_string($name);
if (!is_string($p))
return false;
$result = false;
// check each subdirectory
while (ereg("^([^/]*)/(.*)$",$p,$regs)) {
$p = $regs[2];
if (ereg("^[^.]",$regs[1])) {
$base .= "/".$regs[1];
$result .= $regs[1]."/";
if (!is_dir($base))
return false;
}
}
if ($p) {
$base .= "/".$p;
$result .= $p;
if (!is_dir($base)&&!is_file($base))
return false;
}
return ereg_replace("[/]$","",$result);
}
// image type
function input_type($name="typeid",$usedefault=true) {
$image_types = get_image_types();
$typeid = input_int($name);
if ($typeid>0&&$image_types[$typeid])
return $typeid;
if (!$usedefault)
return false;
global $default_typeid;
return $default_typeid;
}
// search
function input_search($name="search") {
global $HTTP_GET_VARS,$HTTP_POST_VARS;
if ($HTTP_POST_VARS[$name])
return new SearchParameters($HTTP_POST_VARS[$name]);
if ($HTTP_GET_VARS[$name])
return new SearchParameters($HTTP_GET_VARS[$name]);
return new SearchParameters();
}
// cookie
function input_cookie($name="AlbumCookie") {
global $HTTP_COOKIE_VARS;
return ereg_replace("[^a-zA-Z0-9]","",$HTTP_COOKIE_VARS[$name]);
}
?>