#3 code documentation api

This commit is contained in:
Ulf Gebhardt 2016-06-06 08:47:14 +02:00
parent b7637f426c
commit 3cd8276512
7 changed files with 442 additions and 47 deletions

View File

@ -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){

View File

@ -1,9 +1,26 @@
<?php
/**
* System - PHP Framework
*
* PHP Version 5.6
*
* @copyright 2016 Ulf Gebhardt (http://www.webcraft-media.de)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/webcraftmedia/system
* @package system_api
*/
namespace SYSTEM\API;
/**
* API Default class providing defaulting capabilities and Hashbang-Crawling-Scheme.
*/
abstract class api_default extends api_system {
//https://developers.google.com/webmasters/ajax-crawling/docs/getting-started
//https://developers.google.com/webmasters/ajax-crawling/docs/specification#pages-without-hash-fragments
//mojotrollz.eu:80/web/flingit/?_escaped_fragment_=start%3Bhash.ce5504f67533ab3d881a32e1dcdd330aaeb27f19
/**
* Static Call handler for Hashbang-Crawling Requests
*
* @param string $_escaped_fragment_ Hashbang-Encoded State
* @return string Returns your API-HTML result as HTML-Snapshot
*/
public static function static__escaped_fragment_($_escaped_fragment_){
\libxml_use_internal_errors(true);
$html = new \DOMDocument();
@ -15,9 +32,9 @@ abstract class api_default extends api_system {
foreach($state as $row){
$frag = new \DOMDocument();
parse_str(\parse_url($row['url'],PHP_URL_QUERY), $params);
$class = static::get_class($params);
$class = static::get_class();
if($class){
$frag->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);
}

View File

@ -1,26 +1,113 @@
<?php
/**
* System - PHP Framework
*
* PHP Version 5.6
*
* @copyright 2016 Ulf Gebhardt (http://www.webcraft-media.de)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/webcraftmedia/system
* @package system_api
*/
namespace SYSTEM\API;
/**
* API Login class providing System Account Functionality.
*/
class api_login {
/**
* System Account Login
*
* @param string $username Username
* @param sha1 $password_sha1 User Password SHA1 String
* @param lang $locale Locale which the User wants to login with
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_login($username, $password_sha1,$locale = null){
return \SYSTEM\SECURITY\security::login($username, $password_sha1,$locale,true);}
/**
* System Account Logout
*
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_logout(){
return \SYSTEM\SECURITY\security::logout(true);}
/**
* System Account isloggedin check
*
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_isloggedin(){
return \SYSTEM\SECURITY\security::isLoggedIn(true);}
/**
* System Account Right Check
*
* @param int $rightid RightID of the right to be checked
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_check($rightid){
return \SYSTEM\SECURITY\security::check($rightid,true);}
/**
* System Account Create
*
* @param string $username Username
* @param sha1 $password_sha1 User Password SHA1 String
* @param email $email Email of the new User
* @param lang $locale Locale which the User wants to register
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_create($username, $password_sha1, $email, $locale){
return \SYSTEM\SECURITY\security::create($username, $password_sha1, $email, $locale,true);}
/**
* System Account Request Confirm EMail Token
*
* @param string $username Username
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_confirm_email($username){
return \SYSTEM\SECURITY\security::confirm_email($username);}
/**
* System Account Confirm Tokens
*
* @param string $token Token to do specifics with your Account
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_confirm($token){
return \SYSTEM\SECURITY\security::confirm($token,true);}
/**
* System Account Request Reset Password Token
*
* @param string $username Username
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_reset_password($username){
return \SYSTEM\SECURITY\security::reset_password($username);}
/**
* System Account Change Password
*
* @param string $username Username
* @param sha1 $old_password_sha1 Users Old Password SHA1 String
* @param sha1 $new_password_sha1 Users New Password SHA1 String
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_change_password($username,$old_password_sha1,$new_password_sha1){
return \SYSTEM\SECURITY\security::change_password($username,$old_password_sha1,$new_password_sha1);}
/**
* System Account Request Change EMail Token
*
* @param string $username Username
* @param email $new_email Users new EMail Address
* @return JSON Returns JSON result with success/failure status
*/
public static function call_account_action_change_email($username,$new_email){
return \SYSTEM\SECURITY\security::change_email($username,$new_email);}
}

View File

@ -1,31 +1,101 @@
<?php
/**
* System - PHP Framework
*
* PHP Version 5.6
*
* @copyright 2016 Ulf Gebhardt (http://www.webcraft-media.de)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/webcraftmedia/system
* @package system_api
*/
namespace SYSTEM\API;
/**
* API System class providing System Functionality.
*/
class api_system extends api_login{
/**
* System run Cron Call
*
* @return JSON Returns JSON result with success/failure status
*/
public static function call_cron(){
return \SYSTEM\CRON\cron::run();}
/**
* System Text Request Call
*
* @param mixed $request Request String for Text API
* @param lang $lang Request Language for given text
* @return JSON Returns JSON result with data/failure status
*/
public static function call_text($request,$lang){
return \SYSTEM\LOG\JsonResult::toString(\SYSTEM\locale::getStrings($request, $lang));}
/**
* System File Request Call
*
* @param string $cat File category
* @param string $id File name
* @return mixed Returns JSON result with failure status or streams the File
*/
public static function call_files($cat,$id = null){
return \SYSTEM\FILES\files::get($cat, $id, true);}
/**
* System State-Pages Request Call
*
* @param int $group Page Group for Statesystem
* @param string $state Full name of the State.
* @return mixed Returns JSON result with data/failure status
*/
public static function call_pages($group,$state){
return \SYSTEM\PAGE\State::get($group,$state);}
/**
* Static Call to change Language for the current Session/User
*
* @param lang $lang Language requested
* @return null Returns nothing
*/
public static function static__lang($lang){
\SYSTEM\locale::set($lang);}
\SYSTEM\locale::set($lang);}
/**
* Static Call to change Result-Type for the current Session/User
*
* @param result $result Resulttype
* @return null Returns nothing
*/
public static function static__result($result){
\SYSTEM\CONFIG\config::set(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_DEFAULT_RESULT, $result);}
public static function static__($_){
//cache avoidance
}
/**
* Static Time-Parameter _ Call to avoid caching and API Errors for JS requests
*
* @param mixed $_ Anticaching Parameter of some Browsers
* @return null Returns nothing
*/
public static function static__($_){}
/**
* API Bug Call to report Bugs
*
* @param string $message Bugreport Message
* @param JSON $data Bugreport Data
* @return null Returns JSON result with success/failure status
*/
public static function call_bug($message,$data){
return \SYSTEM\SAI\saimod_sys_todo::report($message,$data);}
/**
* API Cache Call to request cached Data (usually js and css)
*
* @param int $id Cache id to be queried
* @param sha1 $ident Cache ident to be queried
* @return mixed Returns cached Data from Database
*/
public static function call_cache($id,$ident){
return \SYSTEM\CACHE\cache::get($id, $ident,true);}
}

View File

@ -1,5 +1,19 @@
<?php
/**
* System - PHP Framework
*
* PHP Version 5.6
*
* @copyright 2016 Ulf Gebhardt (http://www.webcraft-media.de)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/webcraftmedia/system
* @package system_sql
*/
namespace SYSTEM\SQL;
/**
* SQL to get System Api Tree by group
*/
class SYS_API_TREE extends \SYSTEM\DB\QP {
public static function get_class(){return \get_class();}
public static function pgsql(){return

View File

@ -1,21 +1,129 @@
<?php
/**
* System - PHP Framework
*
* PHP Version 5.6
*
* @copyright 2016 Ulf Gebhardt (http://www.webcraft-media.de)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/webcraftmedia/system
* @package system_api
*/
namespace SYSTEM\API;
/**
* API default verify Class for Parameter validation
*/
class verify {
/**
* Verify All - everything is allowed
*
* @param mixed $param Input Paramter
* @return bool Returns boolean if the Value matches the type
*/
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 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);}
};
/**
* 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);}
}

View File

@ -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]);