updated api - now supports statics - basic statics _lang and _result are now part of api_system, api_system extends api_login
This commit is contained in:
parent
ddce65b351
commit
2e7d4399c1
38
api/api.php
38
api/api.php
@ -23,7 +23,9 @@ class api {
|
||||
return self::do_default($default, $apiclassname);} //throws
|
||||
|
||||
//Get the Databasetree
|
||||
$tree = self::getApiTree($group); //throws
|
||||
$tree = self::getApiTree($group); //throws
|
||||
|
||||
$statics = self::do_statics($params, $tree, $apiclassname, $verifyclassname, $default); //throws
|
||||
|
||||
//Commands
|
||||
$commands = self::do_commands($params, $tree);
|
||||
@ -41,7 +43,7 @@ class api {
|
||||
$parameters_opt = self::do_parameters_opt($params, $tree, $parentid, $commands[count($commands)-1][1],$verifyclassname); //throws
|
||||
|
||||
//strict check
|
||||
self::do_strict($strict, $params, $parameters, $commands, $parameters_opt); //throws
|
||||
self::do_strict($strict, $params, $statics, $commands, $parameters, $parameters_opt); //throws
|
||||
|
||||
//Function Name
|
||||
$call_funcname = self::do_func_name($commands); //throws
|
||||
@ -78,6 +80,33 @@ class api {
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function do_statics($params,$tree,$apiclassname,$verifyclassname,$default){
|
||||
$statics = array();
|
||||
$parentid = self::ROOT_PARENTID;
|
||||
foreach($tree as $item){
|
||||
if( intval($item[\SYSTEM\DBD\APITable::FIELD_TYPE]) == \SYSTEM\DBD\APITable::VALUE_TYPE_STATIC &&
|
||||
intval($item[\SYSTEM\DBD\APITable::FIELD_PARENTID]) == $parentid &&
|
||||
isset($params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
|
||||
|
||||
$statics[] = array($item,$params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]]);
|
||||
$call_funcname = 'static_'.$item[\SYSTEM\DBD\APITable::FIELD_NAME];
|
||||
|
||||
//verify func
|
||||
if(!\method_exists($apiclassname, $call_funcname)){
|
||||
return self::do_default($default, $apiclassname, $call_funcname);} //throws
|
||||
|
||||
//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]);}
|
||||
|
||||
\call_user_func_array(array($apiclassname,$call_funcname),array($params[$item[\SYSTEM\DBD\APITable::FIELD_NAME]]));
|
||||
}
|
||||
}
|
||||
|
||||
return $statics;
|
||||
}
|
||||
|
||||
private static function do_default($default, $apiclassname, $call_funcname = null){
|
||||
if($default){ //should we call the default function or throw an error?
|
||||
return \call_user_func(array($apiclassname,'default_page'));}
|
||||
@ -86,11 +115,12 @@ class api {
|
||||
throw new \SYSTEM\LOG\ERROR("API call is not implemented in API: ".$call_funcname);
|
||||
}
|
||||
|
||||
private static function do_strict($strict, $params, $parameters, $commands, $parameters_opt){
|
||||
private static function do_strict($strict, $params, $statics, $commands, $parameters, $parameters_opt){
|
||||
if( $strict &&
|
||||
count($params) != (count($parameters) + count($commands) + count($parameters_opt)) ){
|
||||
count($params) != (count($statics) + count($parameters) + count($commands) + count($parameters_opt))){
|
||||
throw new \SYSTEM\LOG\ERROR( 'Unhandled or misshandled parameters - api query is invalid'.
|
||||
'; given: '.count($params).
|
||||
'; statics: '.count($statics).
|
||||
'; req_command: '.count($commands).
|
||||
'; req_param: '.count($parameters).
|
||||
'; opt_param: '.count($parameters_opt).
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace SYSTEM\API;
|
||||
|
||||
class api_system {
|
||||
class api_system extends 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);
|
||||
@ -17,7 +17,7 @@ class api_system {
|
||||
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (68,1,60,'create','locale','LANG');
|
||||
*/
|
||||
|
||||
public static function call_account_action_login($username, $password_sha, $password_md5){
|
||||
/*public static function call_account_action_login($username, $password_sha, $password_md5){
|
||||
return \SYSTEM\SECURITY\Security::login($username, $password_sha, $password_md5);}
|
||||
public static function call_account_action_logout(){
|
||||
return \SYSTEM\SECURITY\Security::logout();}
|
||||
@ -26,11 +26,16 @@ class api_system {
|
||||
public static function call_account_action_check($rightid){
|
||||
return \SYSTEM\SECURITY\Security::check($rightid);}
|
||||
public static function call_account_action_create($username, $password_sha, $email, $locale){
|
||||
return \SYSTEM\SECURITY\Security::create($username, $password_sha, $email, $locale);}
|
||||
return \SYSTEM\SECURITY\Security::create($username, $password_sha, $email, $locale);}*/
|
||||
|
||||
public static function call_locale($request,$lang){
|
||||
return \SYSTEM\LOG\JsonResult::toString(\SYSTEM\locale::getStrings($request, $lang));}
|
||||
|
||||
public static function call_img($cat,$id = null){
|
||||
return \SYSTEM\IMG\img::get($cat, $id, true);}
|
||||
|
||||
public static function static__lang($lang){
|
||||
\SYSTEM\locale::set($lang);}
|
||||
public static function static__result($result){
|
||||
\SYSTEM\CONFIG\config::set(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_DEFAULT_RESULT, $result);}
|
||||
}
|
||||
@ -13,4 +13,5 @@ class verify {
|
||||
public static function JSON ($param) {return (self::ARY($param) || \json_decode(\stripslashes($param))) ? true : false;} //ary cuz when sent via direct json, all json is alrdy converted to an array.
|
||||
public static function ARY ($param) {return \is_array($param);}
|
||||
public static function LANG ($param) {return \SYSTEM\locale::isLang($param);}
|
||||
public static function RESULT ($param) {return ($param == 'json' || $param == 'msgpack');}
|
||||
};
|
||||
@ -8,6 +8,7 @@ class config_ids {
|
||||
const SYS_CONFIG_PATH_BASEURL = 2;
|
||||
const SYS_CONFIG_PATH_BASEPATH = 3;
|
||||
const SYS_CONFIG_PATH_SYSTEMPATHREL = 4;
|
||||
const SYS_CONFIG_DEFAULT_RESULT = 5;
|
||||
|
||||
const SYS_CONFIG_DB_TYPE = 11; //mys = 1 pg = 2
|
||||
const SYS_CONFIG_DB_TYPE_MYS= 1;
|
||||
|
||||
@ -19,4 +19,5 @@ class APITable {
|
||||
const VALUE_TYPE_COMMAND_FLAG = 1;
|
||||
const VALUE_TYPE_PARAM = 2;
|
||||
const VALUE_TYPE_PARAM_OPT = 3;
|
||||
const VALUE_TYPE_STATIC = 4;
|
||||
}
|
||||
@ -7,9 +7,7 @@ class JsonResult extends \SYSTEM\LOG\AbstractResult {
|
||||
const JSONRESULT_OK = true;
|
||||
const JSONRESULT_ERROR = false;
|
||||
|
||||
public static function toString($json_array, $status = self::JSONRESULT_OK, $start_time = NULL){
|
||||
//send Header
|
||||
\SYSTEM\HEADER::JSON();
|
||||
public static function toString($json_array, $status = self::JSONRESULT_OK, $start_time = NULL){
|
||||
|
||||
if($start_time == NULL){
|
||||
$start_time = \SYSTEM\time::getStartTime();}
|
||||
@ -17,9 +15,19 @@ class JsonResult extends \SYSTEM\LOG\AbstractResult {
|
||||
$json = array();
|
||||
$json['querytime'] = round(microtime(true) - $start_time,5);
|
||||
$json['status'] = $status;
|
||||
$json['result'] = $json_array;
|
||||
|
||||
return json_encode($json);
|
||||
$json['result'] = $json_array;
|
||||
|
||||
if(\SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_DEFAULT_RESULT) == 'json'){
|
||||
//send Header
|
||||
\SYSTEM\HEADER::JSON();
|
||||
|
||||
return json_encode($json);
|
||||
} else {
|
||||
//send Header
|
||||
\SYSTEM\HEADER::JSON();
|
||||
|
||||
return msgpack_pack($json);
|
||||
}
|
||||
}
|
||||
|
||||
//Return Exception as string
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user