diff --git a/api/api.php b/api/api.php index c9ff66c..dc49130 100644 --- a/api/api.php +++ b/api/api.php @@ -9,27 +9,31 @@ * @link https://github.com/webcraftmedia/system * @package system_api */ - namespace SYSTEM\API; /** * API Class provided by System for Smart API's. */ class api { + /** int Root Node ID */ const ROOT_PARENTID = -1; + /** int Default API Group */ const DEFAULT_GROUP = 0; + /** bool Default parse strict setting */ const DEFAULT_STRICT = true; + /** bool Default parse to default setting */ const DEFAULT_DEFAULT = false; /** * Run the API Mechanism with your Data. * - * @param Class $verifyclassname - * @param Class $apiclassname - * @param array $params - * @param int $group - * @param bool $strict - * @param bool $default + * @param string $verifyclassname Your class on which the parameter restriction is parsed upon + * @param string $apiclassname Your class which provides your API + * @param array $params Parameters given to the API + * @param int $group API Group to be used to parse Call + * @param bool $strict Parse the API in a strict way + * @param bool $default Defaulting to default_page setting + * @return mixed Returns your API result or an JSON Error */ public static function run( $verifyclassname,$apiclassname, $params,$group = self::DEFAULT_GROUP, @@ -83,6 +87,12 @@ class api { return \call_user_func_array(array($apiclassname,$call_funcname),$call_funcparam); } + /** + * Internal API function to retrive the Databasetree for requested API + * + * @param int $group API Group to be read + * @return array Array of API rules + */ private static function getApiTree($group){ $result = \SYSTEM\SQL\SYS_API_TREE::QA(array($group)); if(!isset($result) || !is_array($result) || count($result) <= 0){ @@ -90,6 +100,16 @@ class api { return $result; } + /** + * Internal API function to parse statics for requested API + * + * @param array $params Array of Params given to the API + * @param array $tree Array of Rules for given API + * @param string $apiclassname Classname of the users API class + * @param string $verifyclassname Classname of the users Verify Class + * @param bool $default Defaulting to default_page setting + * @return array Array of static API rules + */ private static function do_statics($params,$tree,$apiclassname,$verifyclassname,$default){ $statics = array(); $parentid = self::ROOT_PARENTID; @@ -117,6 +137,14 @@ class api { return $statics; } + /** + * Internal API function to parse default redirect for requested API + * + * @param bool $default Defaulting to default_page setting + * @param string $apiclassname Classname of the users API class + * @param string $call_funcname Functionname which was called + * @return array Array of static API rules + */ 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'));} @@ -125,6 +153,17 @@ class api { throw new \SYSTEM\LOG\ERROR("API call is not implemented in API: ".$call_funcname); } + /** + * Internal API function to strict parse the API + * + * @param bool $strict Strict settings + * @param array $params Parameters given to the API + * @param array $statics Array with API statics + * @param array $commands Array with API commands + * @param array $parameters Array with API parameters + * @param array $parameters_opt Array with API optional parameters + * @return null Returns nothing or throws an Strict Error + */ private static function do_strict($strict, $params, $statics, $commands, $parameters, $parameters_opt){ if( $strict && count($params) != (count($statics) + count($parameters) + count($commands) + count($parameters_opt))){ @@ -137,6 +176,13 @@ class api { '; url: ' .$_SERVER["REQUEST_URI"]);} } + /** + * Internal API function to parse commands for requested API + * + * @param array $params Parameters given to the API + * @param array $tree Array with API rules + * @return array Returns array with parsed Commands + */ private static function do_commands($params,$tree){ $commands = array(); $parentid = self::ROOT_PARENTID; @@ -159,6 +205,16 @@ class api { return $commands; } + /** + * Internal API function to parse parameters for requested API + * + * @param array $params Parameters given to the API + * @param array $tree Array with API rules + * @param int $parentid Parentid of last Tree Element + * @param string $lastcommandvalue Last value of last Command + * @param string $verifyclassname Verify Class given to the API + * @return array Returns array with parsed Parameters + */ private static function do_parameters($params,$tree,$parentid,$lastcommandvalue,$verifyclassname){ $parameters = array(); foreach($tree as $item){ @@ -186,6 +242,16 @@ class api { return $parameters; } + /** + * Internal API function to parse optional parameters for requested API + * + * @param array $params Parameters given to the API + * @param array $tree Array with API rules + * @param int $parentid Parentid of last Tree Element + * @param string $lastcommandvalue Last value of last Command + * @param string $verifyclassname Verify Class given to the API + * @return array Returns array with parsed optional Parameters + */ private static function do_parameters_opt($params,$tree,$parentid,$lastcommandvalue,$verifyclassname){ $parameters_opt = array(); foreach($tree as $item){ @@ -213,6 +279,12 @@ class api { return $parameters_opt; } + /** + * Internal API function to build function name for requested API + * + * @param array $commands Array of all Commands for the given API call + * @return string Returns Function name + */ private static function do_func_name($commands){ $call_funcname = ''; foreach($commands as $com){ @@ -229,6 +301,13 @@ class api { return $call_funcname; } + /** + * Internal API function to build function paramters for requested API + * + * @param array $parameters Array of all Paramters for the given API call + * @param array $parameters_opt Array of all optional Paramters for the given API call + * @return array Returns array with Paramters + */ private static function do_func_params($parameters,$parameters_opt){ $call_funcparam = array(); foreach($parameters as $param){ diff --git a/api/api_default.php b/api/api_default.php index 4f49037..cf363f1 100644 --- a/api/api_default.php +++ b/api/api_default.php @@ -1,9 +1,26 @@ loadHTML(\SYSTEM\API\api::run('\SYSTEM\API\verify', $class, static::get_params($params), static::get_apigroup(), true, false)); + $frag->loadHTML(\SYSTEM\API\api::run('\SYSTEM\API\verify', $class, $params, static::get_apigroup(), true, false)); if($error = \libxml_get_last_error()){ //new \SYSTEM\LOG\ERROR('Parse Error: '.$error->message.' line:'.$error->line.' html: '.$frag->saveHTML()); \libxml_clear_errors();} @@ -31,10 +48,10 @@ abstract class api_default extends api_system { } } //Title - if(array_key_exists('title', $state[0])){ + if((count($state)>=1) && array_key_exists('title', $state[0])){ $html->getElementsByTagName('title')[0]->nodeValue = $state[0]['title'];} //Meta - if(array_key_exists('meta', $state[0])){ + if((count($state)>=1) && array_key_exists('meta', $state[0])){ $meta = $html->getElementsByTagName('meta');//[0]->nodeValue = $state[0]['title']; foreach($state[0]['meta'] as $metaname=>$metavalue){ $found = false; @@ -55,15 +72,33 @@ abstract class api_default extends api_system { new \SYSTEM\LOG\COUNTER("API was called sucessfully."); die(); } - public static function get_apigroup(){ - throw new \RuntimeException("Unimplemented");} - public static function get_class($params = null){ - return self::class;} - public static function get_params($params){ - return $params;} - public static function get_default_state(){ - throw new \RuntimeException("Unimplemented");} - public static function default_page($_escaped_fragment_ = null){ - throw new \RuntimeException("Unimplemented");} + /** + * API Group function - implement this function and return the Groupnumber + * + * @return int Returns your API-Group number + */ + public abstract static function get_apigroup(); + + /** + * API Class function - implement this function and return the Classname + * + * @return string Returns your API-Class name + */ + public static function get_class(){ + return self::class;} + + /** + * API Default State function - implement this function and return the String of the default-state + * + * @return string Returns your API-Default-State + */ + public abstract static function get_default_state(); + + /** + * API Default Page function - implement this function and return the Default Page + * + * @return string Returns your API-Default-State + */ + public abstract static function default_page($_escaped_fragment_ = null); } \ No newline at end of file diff --git a/api/api_login.php b/api/api_login.php index d058006..e683251 100644 --- a/api/api_login.php +++ b/api/api_login.php @@ -1,26 +1,113 @@ 0 ? true : false) : false;} - public static function UINT0 ($param) {return \is_numeric($param) ? ((int)$param >= 0 ? true : false) : false;} - public static function INT ($param) {return \is_numeric($param);} - public static function TIMEUNIX ($param) {return \is_numeric($param) ? ((int)$param > 0 ? true : false) : false;} - public static function DATE ($param) {return \strtotime($param);} - public static function STRING ($param) {return \is_string($param);} - public static function BOOL ($param) {return \is_bool($param) || $param == '0' || $param == '1';} - public static function FLOAT ($param) {return \is_float(\floatval($param));} - 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');} - public static function EMAIL ($param) {return filter_var($param, FILTER_VALIDATE_EMAIL);} -}; \ No newline at end of file + /** + * Verify UINT - Positive Integer excluding 0 + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function UINT ($param) {return \is_numeric($param) ? ((int)$param > 0 ? true : false) : false;} + + /** + * Verify UINT0 - Positive Integer including 0 + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function UINT0 ($param) {return \is_numeric($param) ? ((int)$param >= 0 ? true : false) : false;} + + /** + * Verify INT - all integers + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function INT ($param) {return \is_numeric($param);} + + /** + * Verify TIMEUNIX - unixtimestamp number + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function TIMEUNIX ($param) {return \is_numeric($param) ? ((int)$param > 0 ? true : false) : false;} + + /** + * Verify DATE - string parseable as date + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function DATE ($param) {return \strtotime($param);} + + /** + * Verify STRING - allow every string - thats all + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function STRING ($param) {return \is_string($param);} + + /** + * Verify BOOL - allow booleans + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function BOOL ($param) {return \is_bool($param) || $param == '0' || $param == '1';} + + /** + * Verify FLOAT - every float value + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function FLOAT ($param) {return \is_float(\floatval($param));} + + /** + * Verify JSON - JSON format check + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + 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. + + /** + * Verify ARY - array check + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function ARY ($param) {return \is_array($param);} + + /** + * Verify LANG - verify supported languages + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function LANG ($param) {return \SYSTEM\locale::isLang($param);} + + /** + * Verify RESULT - verify possible resulttypes + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function RESULT ($param) {return ($param == 'json' || $param == 'msgpack');} + + /** + * Verify EMAIL - verify email + * + * @param mixed $param Input Paramter + * @return bool Returns boolean if the Value matches the type + */ + public static function EMAIL ($param) {return filter_var($param, FILTER_VALIDATE_EMAIL);} +} \ No newline at end of file diff --git a/sai/sai/SaiModule.php b/sai/sai/SaiModule.php index e6583e4..45c3bba 100644 --- a/sai/sai/SaiModule.php +++ b/sai/sai/SaiModule.php @@ -4,6 +4,8 @@ namespace SYSTEM\SAI; class SaiModule extends \SYSTEM\API\api_default{ public static function get_apigroup(){ return 42;} + public static function get_default_state(){ + return 'start';} public static function get_class($params = NULL){ if(isset($params[\SYSTEM\SAI\saigui::SAI_MOD_POSTFIELD])){ $classname = \str_replace('.', '\\', $params[\SYSTEM\SAI\saigui::SAI_MOD_POSTFIELD]);