new api with: optional parameters, multiple querytrees per database, oneline command, no "new" commands anymore, everything is static now, no more seperate pageapi, renames
This commit is contained in:
parent
83340f4993
commit
3f8ecd96c2
152
api/api.php
Normal file
152
api/api.php
Normal file
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
namespace SYSTEM\API;
|
||||
|
||||
class api {
|
||||
const ROOT_PARENTID = -1;
|
||||
const DEFAULT_GROUP = 0;
|
||||
public static function run($verifyclassname,$apiclassname,$params,$group = self::DEFAULT_GROUP,$strict = true,$default = false){
|
||||
//Verify Class
|
||||
if(!class_exists($verifyclassname)){
|
||||
throw new \SYSTEM\LOG\ERROR("Verify Class given to the api does not exist.");}
|
||||
|
||||
//API Class
|
||||
if(!class_exists($apiclassname)){
|
||||
throw new \SYSTEM\LOG\ERROR("API Class given to the api does not exist.");}
|
||||
|
||||
//check parameters
|
||||
if( !isset($params) || !is_array($params) || count($params) <= 0){
|
||||
if($default){
|
||||
return \call_user_func(array($apiclassname,'default_page'));}
|
||||
throw new \SYSTEM\LOG\ERROR("No call given for the api");}
|
||||
|
||||
//Get the Databasetree
|
||||
$tree = self::getApiTree($group);
|
||||
if(!isset($tree) || !is_array($tree) || count($tree) <= 0){
|
||||
throw new \SYSTEM\LOG\ERROR("Database Tree for Api empty - cannot proced! GROUP: ".$group);}
|
||||
|
||||
//Commands
|
||||
$commands = array();
|
||||
$parentid = self::ROOT_PARENTID;
|
||||
foreach($tree as $item){
|
||||
if( (intval($item[\SYSTEM\DBD\APITable::FIELD_TYPE]) == \SYSTEM\DBD\APITable::VALUE_TYPE_COMMAND ||
|
||||
intval($item[\SYSTEM\DBD\APITable::FIELD_TYPE]) == \SYSTEM\DBD\APITable::VALUE_TYPE_COMMAND_FLAG) &&
|
||||
intval($item[\SYSTEM\DBD\APITable::FIELD_PARENTID]) == $parentid &&
|
||||
isset($params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
|
||||
|
||||
//check parent value
|
||||
if( isset($item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]) &&
|
||||
$commands[count($commands)-1][1] != $item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]){
|
||||
continue;}
|
||||
|
||||
$commands[] = array($item,$params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]]);
|
||||
$parentid = intval($item[\SYSTEM\DBD\APITable::FIELD_ID]);
|
||||
}
|
||||
}
|
||||
|
||||
//Parameters
|
||||
$parameters = array();
|
||||
$parentid = $commands[count($commands)-1][0];
|
||||
$parentid = $parentid[\SYSTEM\DBD\APITable::FIELD_ID];
|
||||
foreach($tree as $item){
|
||||
if( intval($item[\SYSTEM\DBD\APITable::FIELD_TYPE]) == \SYSTEM\DBD\APITable::VALUE_TYPE_PARAM &&
|
||||
intval($item[\SYSTEM\DBD\APITable::FIELD_PARENTID]) == $parentid){
|
||||
|
||||
//check parent value
|
||||
if( isset($item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]) &&
|
||||
$commands[count($commands)-1][1] != $item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]){
|
||||
continue;}
|
||||
|
||||
//all parameters are required
|
||||
if(!isset($params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
|
||||
throw new \SYSTEM\LOG\ERROR('Parameter missing: '.$item[\SYSTEM\DBD\APITable::FIELD_NAME]);}
|
||||
|
||||
//verify parameter
|
||||
if( !method_exists($verifyclassname, $item[\SYSTEM\DBD\APITable::FIELD_VERIFY]) ||
|
||||
!call_user_func(array($verifyclassname,$item[\SYSTEM\DBD\APITable::FIELD_VERIFY]),$params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
|
||||
throw new \SYSTEM\LOG\ERROR('Parameter type missmacht or Missing Verifier. Param: '.$item[\SYSTEM\DBD\APITable::FIELD_NAME].' Verifier: '.$item[\SYSTEM\DBD\APITable::FIELD_VERIFY]);}
|
||||
|
||||
$parameters[] = array($item, $params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]]);
|
||||
}
|
||||
}
|
||||
|
||||
//Opt Parameters
|
||||
$parameters_opt = array();
|
||||
$parentid = $commands[count($commands)-1][0];
|
||||
$parentid = $parentid[\SYSTEM\DBD\APITable::FIELD_ID];
|
||||
foreach($tree as $item){
|
||||
if( intval($item[\SYSTEM\DBD\APITable::FIELD_TYPE]) == \SYSTEM\DBD\APITable::VALUE_TYPE_PARAM_OPT &&
|
||||
intval($item[\SYSTEM\DBD\APITable::FIELD_PARENTID]) == $parentid){
|
||||
|
||||
//check parent value
|
||||
if( isset($item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]) &&
|
||||
$commands[count($commands)-1][1] != $item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]){
|
||||
continue;}
|
||||
|
||||
//all parameters are required
|
||||
if(!isset($params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
|
||||
throw new \SYSTEM\LOG\ERROR('Parameter missing: '.$item[\SYSTEM\DBD\APITable::FIELD_NAME]);}
|
||||
|
||||
//verify parameter
|
||||
if( !method_exists($verifyclassname, $item[\SYSTEM\DBD\APITable::FIELD_VERIFY]) ||
|
||||
!$verifyclassname->$item[\SYSTEM\DBD\APITable::FIELD_VERIFY]($params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
|
||||
throw new \SYSTEM\LOG\ERROR('Parameter type missmacht or Missing Verifier. Param: '.$item[\SYSTEM\DBD\APITable::FIELD_NAME].' Verifier: '.$item[\SYSTEM\DBD\APITable::FIELD_VERIFY]);}
|
||||
|
||||
$parameters_opt[] = array($item, $params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]]);
|
||||
}
|
||||
}
|
||||
|
||||
//strict check
|
||||
if( $strict &&
|
||||
count($params) != (count($parameters) + count($commands)) ){
|
||||
throw new \SYSTEM\LOG\ERROR('Unhandled or misshandled parameters - api query is invalid');}
|
||||
|
||||
//Function Name
|
||||
$call_funcname = "";
|
||||
foreach($commands as $com){
|
||||
if(!\preg_match('^[0-9A-Za-z_]+$^', $com[1])){
|
||||
throw new \SYSTEM\LOG\ERROR('Call Command can only have letters!');}
|
||||
|
||||
if($com[0][\SYSTEM\DBD\APITable::FIELD_TYPE] == \SYSTEM\DBD\APITable::VALUE_TYPE_COMMAND_FLAG){
|
||||
$call_funcname .= '_flag_'.$com[0][\SYSTEM\DBD\APITable::FIELD_NAME];
|
||||
} else {
|
||||
$call_funcname .= '_'.$com[0][\SYSTEM\DBD\APITable::FIELD_NAME].'_'.\strtolower($com[1]);}
|
||||
}
|
||||
$call_funcname = substr($call_funcname, 1); //strip leading _
|
||||
|
||||
//Function parameters
|
||||
$call_funcparam = array();
|
||||
foreach($parameters as $param){
|
||||
$call_funcparam[] = $param[1];}
|
||||
|
||||
//Optional Function Parameters
|
||||
foreach($parameters_opt as $param){
|
||||
$call_funcparam[] = $param[1];}
|
||||
|
||||
//Does function exist
|
||||
if(!\method_exists($apiclassname, $call_funcname)){
|
||||
if($default){
|
||||
return \call_user_func(array($apiclassname,'default_page'));}
|
||||
throw new \SYSTEM\LOG\ERROR("API call is not implemented in API: ".$call_funcname);}
|
||||
|
||||
//Call Function
|
||||
return \call_user_func_array(array($apiclassname,$call_funcname),$call_funcparam);
|
||||
}
|
||||
|
||||
private static function getApiTree($group){
|
||||
$con = new \SYSTEM\DB\Connection(\SYSTEM\system::getSystemDBInfo());
|
||||
if(\SYSTEM\system::isSystemDbInfoPG()){
|
||||
$res = $con->query('SELECT * FROM '.\SYSTEM\DBD\APITable::NAME_PG .' WHERE `'.\SYSTEM\DBD\APITable::FIELD_GROUP.'` = '.$group.' ORDER BY "'.\SYSTEM\DBD\APITable::FIELD_ID.'"');
|
||||
} else {
|
||||
$res = $con->query('SELECT * FROM '.\SYSTEM\DBD\APITable::NAME_MYS.' WHERE `'.\SYSTEM\DBD\APITable::FIELD_GROUP.'` = '.$group.' ORDER BY '.\SYSTEM\DBD\APITable::FIELD_ID);
|
||||
}
|
||||
|
||||
if(!$res){
|
||||
throw new \SYSTEM\LOG\ERROR('Database Error '.pg_last_error($con));}
|
||||
|
||||
$result = array();
|
||||
while($row = $res->next()){
|
||||
$result[] = $row;}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace SYSTEM\PAGE;
|
||||
namespace SYSTEM\API;
|
||||
|
||||
abstract class PageClass {
|
||||
abstract class api_default {
|
||||
public static function default_page(){
|
||||
throw new RuntimeException("Unimplemented");}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace SYSTEM\API;
|
||||
|
||||
class apiloginclass extends \SYSTEM\API\apiclass {
|
||||
class api_login {
|
||||
/*
|
||||
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (0, 0, -1, NULL, 'call', NULL);
|
||||
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (60, 0, 0, 'account', 'action', NULL);
|
||||
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace SYSTEM\API;
|
||||
|
||||
class apiclass {}
|
||||
@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace SYSTEM;
|
||||
namespace SYSTEM\API;
|
||||
|
||||
class verifyclass {
|
||||
class verify {
|
||||
public static function ALL ($param) {return true;}
|
||||
public static function UINT ($param) {return \is_numeric($param) ? ((int)$param > 0 ? true : false) : false;}
|
||||
public static function INT ($param) {return \is_numeric($param);}
|
||||
@ -4,17 +4,19 @@ namespace SYSTEM\DBD;
|
||||
|
||||
class APITable {
|
||||
|
||||
const NAME_PG = 'system.api_calls';
|
||||
const NAME_MYS = 'system_api_calls';
|
||||
const NAME_PG = 'system.api';
|
||||
const NAME_MYS = 'system_api';
|
||||
|
||||
const FIELD_ID = 'ID';
|
||||
const FIELD_FLAG = 'flag';
|
||||
const FIELD_PARENTID = 'parentID';
|
||||
const FIELD_PARENTVALUE = 'parentValue';
|
||||
const FIELD_NAME = 'name';
|
||||
// const FIELD_ISCHACHED = 'IsCached'; use flagfield!
|
||||
const FIELD_ALLOWEDVALUES = 'allowedValues';
|
||||
const FIELD_ID = 'ID';
|
||||
const FIELD_GROUP = 'group';
|
||||
const FIELD_TYPE = 'type';
|
||||
const FIELD_PARENTID = 'parentID';
|
||||
const FIELD_PARENTVALUE = 'parentValue';
|
||||
const FIELD_NAME = 'name';
|
||||
const FIELD_VERIFY = 'verify';
|
||||
|
||||
const VALUE_FLAG_COMMAND = 0;
|
||||
const VALUE_FLAG_PARAM = 1;
|
||||
const VALUE_TYPE_COMMAND = 0;
|
||||
const VALUE_TYPE_COMMAND_FLAG = 1;
|
||||
const VALUE_TYPE_PARAM = 2;
|
||||
const VALUE_TYPE_PARAM_OPT = 3;
|
||||
}
|
||||
133
page/PageApi.php
133
page/PageApi.php
@ -1,133 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Table:
|
||||
* ID FLAG C/P PARENTCOMMANDID ISCHACHED ALLOWEDVALUES (cache only for commands, null on command = dont care)
|
||||
* 0 C 'CALL' -1 null null (calculated by the api for commands)
|
||||
* 1 C 'ALGO' 0 true null
|
||||
* 2 P 'x' 1 null GOOGLEMAPXY (function in MYVERIFY::func)
|
||||
* 3 P 'y' 1 null GOOGLEMAPXY
|
||||
* 4 P 'zoom' 1 null GOOGLEMAPZOOM
|
||||
* 5 P 'from' 1 null TIMEWIERDFORMAT
|
||||
* 6 P 'to' 1 null TIMEWIERDFORMAT
|
||||
* 7 P 'type' 1 null SENSORTYPE
|
||||
* 8 P 'provider' 1 null SENSORPROVIDER
|
||||
* 9 P 'visibility' 1 null SENSORVISIBILITY
|
||||
* 10 C 'markers' 1 null BOOL
|
||||
*
|
||||
* MYAPI::map_heatmap($PARAMS,$OPTIONS)
|
||||
* MYAPI::map_heatmap_key
|
||||
*/
|
||||
|
||||
namespace SYSTEM\PAGE;
|
||||
|
||||
class PageApi {
|
||||
|
||||
private $m_dbinfo = null;
|
||||
private $m_verifyclass = null;
|
||||
private $m_pageclass = null;
|
||||
|
||||
public function __construct(\SYSTEM\verifyclass $VerifyClass, \SYSTEM\PAGE\PageClass $PageClass,$DBInfo = null){
|
||||
$this->m_dbinfo = $DBInfo == null ? \SYSTEM\system::getSystemDBInfo() : $DBInfo;
|
||||
$this->m_verifyclass = $VerifyClass;
|
||||
$this->m_pageclass = $PageClass;
|
||||
}
|
||||
|
||||
// $call = post + get params
|
||||
// returns resultstring
|
||||
public function CALL($call = array()){
|
||||
//Get the Databasetree
|
||||
$tree = array();
|
||||
if($this->m_dbinfo instanceof \SYSTEM\DB\DBInfo){
|
||||
$tree = self::getPageTree();}
|
||||
else {
|
||||
if(!is_array($this->m_dbinfo)){
|
||||
throw new \SYSTEM\LOG\ERROR('No Connectioninfo and no call table given to the api');}
|
||||
$tree = $this->m_dbinfo;
|
||||
}
|
||||
|
||||
//Commands
|
||||
$commands = array();
|
||||
$parentid = -1;
|
||||
foreach($tree as $item){
|
||||
if( $item[\SYSTEM\DBD\PAGETable::FIELD_FLAG] == \SYSTEM\DBD\PAGETable::VALUE_FLAG_COMMAND &&
|
||||
$item[\SYSTEM\DBD\PAGETable::FIELD_PARENTID] == $parentid &&
|
||||
isset($call[$item[\SYSTEM\DBD\PAGETable::FIELD_NAME]])){
|
||||
|
||||
if( isset($item[\SYSTEM\DBD\PAGETable::FIELD_PARENTVALUE]) &&
|
||||
$commands[count($commands)-1][1] != $item[\SYSTEM\DBD\PAGETable::FIELD_PARENTVALUE]){
|
||||
continue;}
|
||||
$commands[] = array($item,$call[$item[\SYSTEM\DBD\PAGETable::FIELD_NAME]]);
|
||||
$parentid = $item[\SYSTEM\DBD\PAGETable::FIELD_ID];
|
||||
}
|
||||
}
|
||||
|
||||
//Parameters
|
||||
$command_call = '';
|
||||
$parameters = array();
|
||||
if(count($commands) > 0){
|
||||
$lastCommand = $commands[count($commands) -1 ][0];
|
||||
foreach($tree as $item){
|
||||
if( $item[\SYSTEM\DBD\PAGETable::FIELD_FLAG] == \SYSTEM\DBD\PAGETable::VALUE_FLAG_PARAM &&
|
||||
$item[\SYSTEM\DBD\PAGETable::FIELD_PARENTID] == $lastCommand[\SYSTEM\DBD\PAGETable::FIELD_ID]){
|
||||
|
||||
if( isset($item[\SYSTEM\DBD\PAGETable::FIELD_PARENTVALUE]) &&
|
||||
$commands[count($commands)-1][1] != $item[\SYSTEM\DBD\PAGETable::FIELD_PARENTVALUE]){
|
||||
continue;}
|
||||
|
||||
if(!isset($call[$item[\SYSTEM\DBD\PAGETable::FIELD_NAME]])){
|
||||
throw new \SYSTEM\LOG\ERROR('Parameter missing: '.$item[\SYSTEM\DBD\PAGETable::FIELD_NAME]);}
|
||||
|
||||
|
||||
if( !method_exists($this->m_verifyclass, $item[\SYSTEM\DBD\PAGETable::FIELD_ALLOWEDVALUES]) ||
|
||||
!$this->m_verifyclass->$item[\SYSTEM\DBD\PAGETable::FIELD_ALLOWEDVALUES]($call[$item[\SYSTEM\DBD\PAGETable::FIELD_NAME]])){
|
||||
throw new \SYSTEM\LOG\ERROR('Parameter type missmacht or Missing Verifier. Param: '.$item[\SYSTEM\DBD\PAGETable::FIELD_NAME].' Verifier: '.$item[\SYSTEM\DBD\PAGETable::FIELD_ALLOWEDVALUES]);}
|
||||
|
||||
$parameters[] = array($item, $call[$item[\SYSTEM\DBD\PAGETable::FIELD_NAME]]);
|
||||
}
|
||||
}
|
||||
|
||||
//Function Name
|
||||
foreach($commands as $com){
|
||||
if(!\preg_match('^[0-9A-Za-z_]+$^', $com[1])){
|
||||
throw new \SYSTEM\LOG\ERROR("Call Command can only have letters!");}
|
||||
|
||||
if($com[0][\SYSTEM\DBD\PAGETable::FIELD_ALLOWEDVALUES] == 'FLAG'){
|
||||
$command_call .= '_flag_'.$com[0][\SYSTEM\DBD\PAGETable::FIELD_NAME];
|
||||
} else {
|
||||
$command_call .= '_'.$com[0][\SYSTEM\DBD\PAGETable::FIELD_NAME].'_'.\strtolower($com[1]);}
|
||||
}
|
||||
$command_call = substr($command_call, 1);
|
||||
}
|
||||
|
||||
$command_call = $command_call == '' ? 'default_page': $command_call;
|
||||
|
||||
//Function parameters
|
||||
$parameter_call = array();
|
||||
if(count($parameters) > 0){
|
||||
foreach($parameters as $param){
|
||||
$parameter_call[] = $param[1];}
|
||||
}
|
||||
|
||||
if(!\method_exists($this->m_pageclass, $command_call)){
|
||||
throw new \SYSTEM\LOG\ERROR("Page call is not implemented in PageApi: ".$command_call);}
|
||||
|
||||
//Call Function
|
||||
return \call_user_func_array(array($this->m_pageclass,$command_call),$parameter_call);
|
||||
}
|
||||
|
||||
private function getPageTree(){
|
||||
|
||||
$con = new \SYSTEM\DB\Connection($this->m_dbinfo);
|
||||
$res = $con->query('SELECT * FROM '.(\SYSTEM\system::isSystemDbInfoPG() ? \SYSTEM\DBD\PAGETable::NAME_PG : \SYSTEM\DBD\PAGETable::NAME_MYS).' ORDER BY "'.\SYSTEM\DBD\PAGETable::FIELD_ID.'"');
|
||||
|
||||
if(!$res){
|
||||
throw new \SYSTEM\LOG\ERROR("Database Error ". pg_last_error());}
|
||||
|
||||
$result = array();
|
||||
while($row = $res->next()){
|
||||
$result[] = $row;}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user