diff --git a/base.php b/base.php index 5dc5568..d485723 100644 --- a/base.php +++ b/base.php @@ -75,6 +75,67 @@ function no_http() { die('

Forbidden, this method can only be called from shell

'); } +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) {