Add "arg" function for parsing http (or other) parameters

This commit is contained in:
Bofh 2022-12-24 01:45:21 +01:00
parent b0c912a300
commit 5022d34c28
1 changed files with 61 additions and 0 deletions

View File

@ -75,6 +75,67 @@ function no_http() {
die('<h3>Forbidden, this method can only be called from shell</h3>');
}
function arg($arg, $default=null, $validator=null) {
$arg = trim($arg);
$type = 'string';
$require = false;
if (strpos($arg,'*') === 0) {
$require = true;
$arg = substr($arg, 1);
}
if (strpos($arg,'s:') === 0) {
$type = 'string';
$arg = substr($arg, 2);
} else if (strpos($arg,'i:') === 0) {
$type = 'integer';
$arg = substr($arg, 2);
} else if (strpos($arg,'d:') === 0) {
$type = 'double';
$arg = substr($arg, 2);
} else if (strpos($arg,'b64:') === 0) {
$type = 'base64';
$arg = substr($arg, 4);
}
$ps = explode('.', $arg);
$value = null;
$arr = null;
switch (strtolower($ps[0])) {
case 'get':
$arr = '_GET';
break;
case 'post':
$arr = '_POST';
break;
}
if ($arr === null)
$arr = $ps[0];
eval('$value = isset($'.$arr.'["'.$ps[1].'"]) ? $'.$arr.'["'.$ps[1].'"] : null;');
if ($value === null)
return !$require ? $default : apiresult(['error' => '<'.$arg.'> parameter must be specified'], 400);
$value = trim($value);
if ($type === 'string')
$value = strval($value);
else if ($type === 'integer')
$value = intval($value);
else if ($type === 'double')
$value = doubleval($value);
else if ($type === 'base64')
$value = base64_decode($value);
if ($validator !== null) {
$bool = false;
eval('$bool = '.$validator.'($value);');
if (!$bool)
return apiresult(['error' => '<'.$arg.'> parameter validation has failed'], 400);
}
return $value;
}
function mod_php($module_name=null) {
$null_mod = false;
if ($module_name === null) {