From 357874382867e331a2ab30d5fab3e73e85548864 Mon Sep 17 00:00:00 2001 From: rylon Date: Tue, 12 Nov 2013 15:57:03 +0100 Subject: [PATCH] img proto for image handling and management, system std api, cleanup --- api/Api.old.php | 158 --------------------------------------------- api/api_system.php | 36 +++++++++++ autoload.inc.php | 1 + img/img.php | 30 +++++++-- 4 files changed, 60 insertions(+), 165 deletions(-) delete mode 100644 api/Api.old.php create mode 100644 api/api_system.php diff --git a/api/Api.old.php b/api/Api.old.php deleted file mode 100644 index 914e25f..0000000 --- a/api/Api.old.php +++ /dev/null @@ -1,158 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/api/api_system.php b/api/api_system.php new file mode 100644 index 0000000..fe0d086 --- /dev/null +++ b/api/api_system.php @@ -0,0 +1,36 @@ + 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; } }