inc/post.php -
index
<?php
/* POST.PHP - POST data parser/validator
There are many functions that parse $_POST data, all of which do similar
things - isset(), $_POST['index'], length checks, character filters, etc.
Well, let's see if we can do all that stuff in one place, here.
*/
// first things first!
foreach ($_POST as $name => $value) {
$_POST[$name] = preg_replace("/\r/","",$value); // WINDOWS/MAC
$_POST[$name] = trim($_POST[$name]);
if (get_magic_quotes_gpc())
$_POST[$name] = stripslashes($_POST[$name]);
}
// the POST macro
$POST = 'foreach(explode(",",$post)as$_p){$$_p=($_t=trim(@$_POST[$_p]))?$_t:"";}';
// usage:
// $post = 'name,button';
// eval($GLOBALS['POST']);
// echo "name=$name, button=$button";
/* postvar - read a $_POST variable */
function postvar($var) {
return ($t = @$_POST[$var]) ? $_POST[$var] : '';
}
/* postvars - turn POST data into an array (see test code EOF) */
function postvars($a = array(), $assoc = FALSE) {
// postvars("one","two","three");
if (!is_array(func_get_arg(0)))
foreach (func_get_args() as $var)
if ($assoc) $p[$var] = postvar($var);
else $p[] = postvar($var);
else
// postvars(array("one","two","three"));
if (isset($a[0]))
foreach (func_get_arg(0) as $var)
if ($assoc) $p[$var] = postvar($var);
else $p[] = postvar($var);
else
// postvars(array("one"=>"10","two"=>"","three"=>"crazy stuff");
// this one is used by the comment form
foreach (func_get_arg(0) as $var => $val) {
$t = postvar($var);
$t = _subprocess($t,$val);
if ($assoc) $p[$var] = $t;
else $p[] = $t;
}
return $p;
}
/* postvars' _subprocess */
// this is some kind of awful code! (but it is interesting)
// it was fun to create, but since it is only used by the single comment
// submit function, we will probably get rid of this...
function _subprocess($t, $val) {
if (!is_array($val)) {
if (function_exists($val))
$t = $val($t);
elseif (is_numeric($val)) {
if (strlen($t) > $val)
$t = substr($t,0,$val);
}
elseif (!$t)
$t = $val;
// the above "logic" negates a numerical default
return $t;
}
if (($_ = @$val['filter']) && function_exists($_))
$t = $_($t);
if (($_ = @$val['length']) && (strlen($t) > $_))
$t = substr($t,0,$_);
if (!$t && ($_ = @$val['default']))
$t = $_;
if (($_ = @$val['test']) && preg_match($_,$t))
$t = ($_ = @$val['fail']) ? $_ : '-1';
return $t;
}
/*
// Test code for postvars()
$_POST['a'] = 'string';
$_POST['b'] = '42';
var_dump(postvars('a','b','c'));
var_dump(postvars(array('a','b','c')));
var_dump(postvars(array('a','b','c'),TRUE));
$_POST['d'] = '1234567890';
$_POST['e'] = '1234567<8>90123456789012345678901234567890';
$args = array(
'a' => '', // returned as is
'b' => 'strip_tags', // filter function
'c' => 'C', // default vlaue
'd' => 9, // length restriction
'e' => array('test' => '/[<>]/',
'filter' => '',
'default' => '',
'length' => 32
)
);
echo "<br>";
var_dump(postvars($args));
$commentadd = array(
'from' => array('test' => '/[<>]/', 'length' => 32),
'subject' => 64,
'code' => 64,
'data' => 12229
);
list($from,$subj,$code,$data) = postvars($commentadd);
*/
?>