img proto for image handling and management, system std api, cleanup

This commit is contained in:
Ulf Gebhardt 2013-11-12 15:57:03 +01:00
parent de6d3fb0b4
commit 3578743828
4 changed files with 60 additions and 165 deletions

View File

@ -1,158 +0,0 @@
<?php
/*
API CALL:
COMMANDS OPTIONS PARAMETERS via post/get
COMMANDS:
Handled by the Api, verified and processed via database table
COMMAND(ID,VALUE)
PARAMS:
Verified and typechecked by the api
PARAM(ID,VALUE)
*/
// $api = new \API\API(..);
// return $api->CALL($call);
/*
* 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\API;
class Api {
private $m_dbinfo = null;
private $m_verifyclass = null;
private $m_apiclass = null;
public function __construct(\SYSTEM\verifyclass $VerifyClass, \SYSTEM\API\apiclass $ApiClass,$DBInfo = null){
$this->m_dbinfo = $DBInfo == null ? \SYSTEM\system::getSystemDBInfo() : $DBInfo;
$this->m_verifyclass = $VerifyClass;
$this->m_apiclass = $ApiClass;
}
// $call = post + get params
// returns resultstring
public function CALL($call = array()){
if( !isset($call) || !is_array($call) || count($call) <= 0){
throw new \SYSTEM\LOG\ERROR("No call given for the api");}
//Get the Databasetree
$tree = array();
if($this->m_dbinfo instanceof \SYSTEM\DB\DBInfo){
$tree = self::getApiTree();
if(!is_array($tree)){
throw new \SYSTEM\LOG\ERROR("Database Tree for Api empty - cannot proced!");}
} 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( intval($item[\SYSTEM\DBD\APITable::FIELD_FLAG]) == \SYSTEM\DBD\APITable::VALUE_FLAG_COMMAND &&
intval($item[\SYSTEM\DBD\APITable::FIELD_PARENTID]) == $parentid &&
isset($call[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
if( isset($item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]) &&
$commands[count($commands)-1][1] != $item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]){
continue;
}
$commands[] = array($item,$call[$item[\SYSTEM\DBD\APITable::FIELD_NAME]]);
$parentid = intval($item[\SYSTEM\DBD\APITable::FIELD_ID]);
}
}
//Parameters
$parameters = array();
$lastCommand = $commands[count($commands)-1][0];
foreach($tree as $item){
if( intval($item[\SYSTEM\DBD\APITable::FIELD_FLAG]) == \SYSTEM\DBD\APITable::VALUE_FLAG_PARAM &&
intval($item[\SYSTEM\DBD\APITable::FIELD_PARENTID]) == $lastCommand[\SYSTEM\DBD\APITable::FIELD_ID]){
if( isset($item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]) &&
$commands[count($commands)-1][1] != $item[\SYSTEM\DBD\APITable::FIELD_PARENTVALUE]){
continue;}
if(!isset($call[$item[\SYSTEM\DBD\APITable::FIELD_NAME]])){
throw new \SYSTEM\LOG\ERROR('Parameter missing: '.$item[\SYSTEM\DBD\APITable::FIELD_NAME]);}
if( !method_exists($this->m_verifyclass, $item[\SYSTEM\DBD\APITable::FIELD_ALLOWEDVALUES]) ||
!$this->m_verifyclass->$item[\SYSTEM\DBD\APITable::FIELD_ALLOWEDVALUES]($call[$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_ALLOWEDVALUES]);}
$parameters[] = array($item, $call[$item[\SYSTEM\DBD\APITable::FIELD_NAME]]);
}
}
if(count($call) != (count($parameters) + count($commands)) ){
throw new \SYSTEM\LOG\ERROR('Unhandled or misshandled parameters - api query is invalid');}
//Function Name
$command_call = "";
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_ALLOWEDVALUES] == 'FLAG'){
$command_call .= '_flag_'.$com[0][\SYSTEM\DBD\APITable::FIELD_NAME];
} else {
$command_call .= '_'.$com[0][\SYSTEM\DBD\APITable::FIELD_NAME].'_'.\strtolower($com[1]);}
}
$command_call = substr($command_call, 1);
//Function parameters
$parameter_call = array();
foreach($parameters as $param){
$parameter_call[] = $param[1];}
if(!\method_exists($this->m_apiclass, $command_call)){
throw new \SYSTEM\LOG\ERROR("API call is not implemented in API: ".$command_call);}
//Call Function
return \call_user_func_array(array($this->m_apiclass,$command_call),$parameter_call);
}
private function getApiTree(){
$con = new \SYSTEM\DB\Connection($this->m_dbinfo);
if(\SYSTEM\system::isSystemDbInfoPG()){
$res = $con->query('SELECT * FROM '.\SYSTEM\DBD\APITable::NAME_PG.' ORDER BY "'.\SYSTEM\DBD\APITable::FIELD_ID.'"');
} else {
$res = $con->query('SELECT * FROM '.\SYSTEM\DBD\APITable::NAME_MYS.' ORDER BY '.\SYSTEM\DBD\APITable::FIELD_ID);
}
unset($con);
if(!$res){
throw new \SYSTEM\LOG\ERROR('Database Error '.pg_last_error($con));}
$result = array();
while($row = $res->next()){
$result[] = $row;}
return $result;
}
}

36
api/api_system.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace SYSTEM\API;
class api_system {
/*
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);
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (61,1,60,'login','username','USERNAME');
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (62,1,60,'login','password_sha','PASSHASH');
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (63,1,60,'login','password_md5','PASSHASH');
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (64,1,60,'check','rightid','UINT');
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (65,1,60,'create','username','USERNAME');
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (66,1,60,'create','password_sha','PASSHASH');
INSERT INTO `system_api_calls` (`ID`, `Flag`, `ParentID`, `ParentValue`, `Name`, `AllowedValues`) VALUES (67,1,60,'create','email','EMAIL');
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){
return \SYSTEM\SECURITY\Security::login($username, $password_sha, $password_md5);}
public static function call_account_action_logout(){
return \SYSTEM\SECURITY\Security::logout();}
public static function call_account_action_isloggedin(){
return \SYSTEM\SECURITY\Security::isLoggedIn();}
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);}
public static function call_locale($request,$lang){
return JsonResult::toString(\SYSTEM\time::getStartTime(), \SYSTEM\locale::getStrings($request, $lang));}
public static function call_img($cat,$id = null){
return \SYSTEM\IMG\img::get($cat, $id, true);}
}

View File

@ -36,6 +36,7 @@ spl_autoload_register('__autoload_system');
\SYSTEM\autoload::registerFolder(dirname(__FILE__).'/sai','SYSTEM\SAI');
\SYSTEM\autoload::registerFolder(dirname(__FILE__).'/docu','SYSTEM\DOCU');
\SYSTEM\autoload::registerFolder(dirname(__FILE__).'/img','SYSTEM\IMG');
require_once dirname(__FILE__).'/lib/autoload.inc.php';
require_once dirname(__FILE__).'/docu/register_sys_docu.php';

View File

@ -1,16 +1,32 @@
<?php
namespace SYSTEM;
namespace SYSTEM\IMG;
class img {
protected static function folder_path(){
throw new \RuntimeException("Not implemeted");}
class img {
private static $imgfolders = array(); //only strings! (catname => array(2.path 3.mask))*
public static function registerFolder($cat,$path,$mask){
self::$imgfolders[$cat] = array($path,$mask);}
public static function get($cat,$id = null,$returnasjson=false){
$result = null;
if(array_key_exists($cat, self::$imgfolders)){
$folder = self::getFolder(self::$imgfolders[$cat][0],self::$imgfolders[$cat][1]);
$result = ($id == null) ? $folder : (array_key_exists($id, $folder)) ? file_get_contents($folder[$id]) : null;}
public static function get($hash){
if($returnasjson && $result == null){
throw new \SYSTEM\LOG\ERROR("No matching Cat '".$cat."' or Key '".$id."' found or Folder is empty.");}
return $returnasjson ? \SYSTEM\LOG\JsonResult::toString($result) : $result;
}
public static function put($file){
public static function put($cat,$id,$contents){
throw new \SYSTEM\LOG\ERROR("not implemented");}
private static function getFolder($folder,$mask){
$files = array();
foreach (glob($folder.$mask) as $file) {
$files[] = $file;}
return $files;
}
}