diff --git a/src/php/AdminApps/DerivedSignals.php b/src/php/AdminApps/DerivedSignals.php index 7d3fe5b..664395a 100644 --- a/src/php/AdminApps/DerivedSignals.php +++ b/src/php/AdminApps/DerivedSignals.php @@ -21,8 +21,43 @@ class DerivedSignals implements \SourcePot\Datapool\Interfaces\App{ 'Y-m-d'=>'Y-m-d 12:30:00', 'Y-m'=>'Y-m-15 12:30:00', 'Y'=>'Y-06-15 12:30:00', - ]; + ]; + + private const TIMESPAN_OPTIONS=[ + 'Y-m-d H:i'=>'Minute', + 'Y-m-d H'=>'Hour', + 'Y-m-d'=>'Day', + 'Y-m'=>'Month', + 'Y'=>'Year', + ]; + private const PROCESSING_OPTIONS=[ + 'avg'=>'Average', + 'min'=>'Min', + 'minExZero'=>'Min exclude zero', + 'max'=>'Max', + 'range'=>'Range', + 'sum'=>'Sum', + 'count'=>'Count', + ]; + + private const CONTENTSTRUCTURE_PARAMS=[ + 'Timespan'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>self::TIMESPAN_OPTIONS,'keep-element-content'=>TRUE,'excontainer'=>TRUE], + 'Timezone'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>\SourcePot\Datapool\Root::TIMEZONES,'value'=>\SourcePot\Datapool\Root::DB_TIMEZONE,'keep-element-content'=>TRUE,'excontainer'=>TRUE], + 'yMin'=>['method'=>'element','tag'=>'input','type'=>'text','excontainer'=>TRUE], + 'yMax'=>['method'=>'element','tag'=>'input','type'=>'text','excontainer'=>TRUE], + 'Data type'=>['method'=>'select','excontainer'=>TRUE,'value'=>'float','options'=>\SourcePot\Datapool\Foundation\Computations::DATA_TYPES,'keep-element-content'=>TRUE], + 'description'=>['method'=>'element','tag'=>'input','type'=>'text','excontainer'=>TRUE], + ]; + + private const CONTENTSTRUCTURE_RULES=[ + 'Operation'=>['method'=>'select','excontainer'=>TRUE,'value'=>'+','options'=>['+'=>'+','-'=>'-','*'=>'*'],'keep-element-content'=>TRUE,'excontainer'=>TRUE], + 'Signal'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>[],'keep-element-content'=>TRUE,'excontainer'=>TRUE], + 'Processing'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>self::PROCESSING_OPTIONS,'keep-element-content'=>TRUE,'excontainer'=>TRUE], + 'Offset'=>['method'=>'element','tag'=>'input','type'=>'text','value'=>0,'excontainer'=>TRUE], + 'Scaler'=>['method'=>'element','tag'=>'input','type'=>'text','value'=>1,'excontainer'=>TRUE], + ]; + private $entryTable=''; private $entryTemplate=[ 'Expires'=>['type'=>'DATETIME','value'=>\SourcePot\Datapool\Root::NULL_DATE,'Description'=>'If the current date is later than the Expires-date the entry will be deleted. On insert-entry the init-value is used only if the Owner is not anonymous, set to 10mins otherwise.'], @@ -94,23 +129,7 @@ private function signalsDerived(array $selector):string public function derivedSignalParams($arr):array { - // rules - $timespanOptions=[ - 'Y-m-d H:i'=>'Minute', - 'Y-m-d H'=>'Hour', - 'Y-m-d'=>'Day', - 'Y-m'=>'Month', - 'Y'=>'Year', - ]; - $contentStructure=[ - 'Timespan'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>$timespanOptions,'keep-element-content'=>TRUE,'excontainer'=>TRUE], - 'Timezone'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>\SourcePot\Datapool\Root::TIMEZONES,'value'=>\SourcePot\Datapool\Root::DB_TIMEZONE,'keep-element-content'=>TRUE,'excontainer'=>TRUE], - 'min'=>['method'=>'element','tag'=>'input','type'=>'text','excontainer'=>TRUE], - 'max'=>['method'=>'element','tag'=>'input','type'=>'text','excontainer'=>TRUE], - 'description'=>['method'=>'element','tag'=>'input','type'=>'text','excontainer'=>TRUE], - ]; - // rules list - $arr['contentStructure']=$contentStructure; + $arr['contentStructure']=self::CONTENTSTRUCTURE_PARAMS; $caption='Params for '.$arr['selector']['Group'].' → '.$arr['selector']['Folder']; $row=$this->oc['SourcePot\Datapool\Tools\HTMLbuilder']->entry2row($arr); $arr['html']=$this->oc['SourcePot\Datapool\Tools\HTMLbuilder']->table(['matrix'=>['Parameter'=>$row],'style'=>'clear:left;','hideHeader'=>FALSE,'hideKeys'=>TRUE,'keep-element-content'=>TRUE,'caption'=>$caption]); @@ -119,33 +138,14 @@ public function derivedSignalParams($arr):array public function derivedSignalRules($arr):array { - // rules - $signalOptions=$this->oc['SourcePot\Datapool\Foundation\Signals']->getOptions(['Group'=>'signal']); - $processingOptions=[ - 'avg'=>'Average', - 'min'=>'Min', - 'minExZero'=>'Min exclude zero', - 'max'=>'Max', - 'range'=>'Range', - 'sum'=>'Sum', - 'count'=>'Count', - ]; - $contentStructure=[ - 'Operation'=>['method'=>'select','excontainer'=>TRUE,'value'=>'+','options'=>['+'=>'+','-'=>'-','*'=>'*'],'keep-element-content'=>TRUE,'excontainer'=>TRUE], - 'Signal'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>$signalOptions,'keep-element-content'=>TRUE,'excontainer'=>TRUE], - 'Processing'=>['method'=>'select','excontainer'=>TRUE,'value'=>'','options'=>$processingOptions,'keep-element-content'=>TRUE,'excontainer'=>TRUE], - 'Offset'=>['method'=>'element','tag'=>'input','type'=>'text','value'=>0,'excontainer'=>TRUE], - 'Scaler'=>['method'=>'element','tag'=>'input','type'=>'text','value'=>1,'excontainer'=>TRUE], - ]; - // rules list + $contentStructure=self::CONTENTSTRUCTURE_RULES; + $contentStructure['Signal']['options']=$this->oc['SourcePot\Datapool\Foundation\Signals']->getOptions(['Group'=>'signal']); $arr['contentStructure']=$contentStructure; $arr['caption']='Rules for '.$arr['selector']['Group'].' → '.$arr['selector']['Folder']; $arr['html']=$this->oc['SourcePot\Datapool\Tools\HTMLbuilder']->entryListEditor($arr); return $arr; } - - public function signal2derivedSignal(array $signal):void { // get relevant derived signals @@ -187,16 +187,15 @@ public function signal2derivedSignal(array $signal):void } } // signal params - $signalParams=['description'=>$params['description']]; - $signalParams['yMin']=$params['min']??NULL; - $signalParams['yMax']=$params['max']??NULL; + $params['yMin']=(empty(strval($params['yMin'])))?NULL:$params['yMin']; + $params['yMax']=(empty(strval($params['yMax'])))?NULL:$params['yMax']; $targetTimeZone=new \DateTimeZone($params['Timezone']); $nowDateTime=new \DateTime('@'.time()); $nowDateTime->setTimezone($targetTimeZone); $dateTimeStr=$nowDateTime->format(self::BASE_DATETIME[$params['Timespan']]); $signalDateTime=new \DateTime($dateTimeStr,new \DateTimeZone($params['Timezone'])); $signalTimeStamp=$signalDateTime->getTimestamp(); - $this->oc['SourcePot\Datapool\Foundation\Signals']->updateSignal(__CLASS__,$params['Group'],$params['Folder'],$result,'float',$signalParams,$signalTimeStamp); + $this->oc['SourcePot\Datapool\Foundation\Signals']->updateSignal(__CLASS__,$params['Group'],$params['Folder'],$result,$params['Data type']??'float',$params,$signalTimeStamp); } } diff --git a/src/php/Foundation/Access.php b/src/php/Foundation/Access.php index 7d446b2..73c909d 100644 --- a/src/php/Foundation/Access.php +++ b/src/php/Foundation/Access.php @@ -154,15 +154,19 @@ public function emailId($email){ return $emailId; } - public function loginId($email,$password){ + public function loginId($email,$password):string + { $emailId=$this->emailId($email); $userPass=$password.$emailId; $loginId=password_hash($userPass,PASSWORD_DEFAULT); return $loginId; } - public function verfiyPassword($email,$password,$loginId){ - if (empty($email) || empty($password) || empty($loginId)){return FALSE;} + public function verfiyPassword($email,$password,$loginId):bool + { + if (empty($email) || empty($password) || empty($loginId)){ + return FALSE; + } $emailId=$this->emailId($email); $userPass=$password.$emailId; if (password_verify($userPass,$loginId)===TRUE){ @@ -173,7 +177,8 @@ public function verfiyPassword($email,$password,$loginId){ } } - private function rehashPswIfNeeded($user,$userPass,$loginId){ + private function rehashPswIfNeeded($user,$userPass,$loginId):bool + { if (password_needs_rehash($loginId,PASSWORD_DEFAULT)){ $user['LoginId']=password_hash($userPass,PASSWORD_DEFAULT); $this->oc['SourcePot\Datapool\Foundation\Database']->updateEntry($user,TRUE); @@ -184,31 +189,38 @@ private function rehashPswIfNeeded($user,$userPass,$loginId){ } } - public function isAdmin($user=FALSE){ + public function isAdmin($user=FALSE):bool + { return $this->hasRights($user,'ADMIN_R'); } - public function isContentAdmin($user=FALSE){ + public function isContentAdmin($user=FALSE):bool + { return $this->hasRights($user,'ALL_CONTENTADMIN_R'); } - public function isMember($user=FALSE){ + public function isMember($user=FALSE):bool + { return $this->hasRights($user,'ALL_MEMBER_R'); } - public function isPublic($user=FALSE){ + public function isPublic($user=FALSE):bool + { return $this->hasRights($user,'PUBLIC_R'); } - public function isRegistered($user=FALSE){ + public function isRegistered($user=FALSE):bool + { return $this->hasRights($user,'REGISTERED_R'); } - public function hasRights($user=FALSE,string $right='ADMIN_R') + public function hasRights($user=FALSE,string $right='ADMIN_R'):bool { if (empty($user)){ $user=$this->oc['SourcePot\Datapool\Root']->getCurrentUser(); - if (empty($user)){return FALSE;} + if (empty($user)){ + return FALSE; + } } if (($user['Privileges'] & $this->access[$right])>0){ return TRUE; @@ -217,11 +229,13 @@ public function hasRights($user=FALSE,string $right='ADMIN_R') } } - public function hasAccess($user=FALSE,int $right=32768) + public function hasAccess($user=FALSE,int $right=32768):bool { if (empty($user)){ $user=$this->oc['SourcePot\Datapool\Root']->getCurrentUser(); - if (empty($user)){return FALSE;} + if (empty($user)){ + return FALSE; + } } if (($user['Privileges'] & $right)>0){ return TRUE; diff --git a/src/php/Foundation/Filespace.php b/src/php/Foundation/Filespace.php index a141c73..22e13e0 100644 --- a/src/php/Foundation/Filespace.php +++ b/src/php/Foundation/Filespace.php @@ -714,13 +714,18 @@ public function addFile2entry(array $entry,string $file):array $this->oc['logger']->log('notice','Function "{class} → {function}()" failed to scan for pdf-attachments: {msg}',$context); } } else if (stripos(strval($entry['Params']['File']['Extension']),'csv')!==FALSE){ - $entry['Params']['File']['Spreadsheet']=$this->oc['SourcePot\Datapool\Tools\CSVtools']->csvIterator($file,$entry['Params']['File']['Extension'])->current(); - $entry['Params']['File']['SpreadsheetIteratorClass']='SourcePot\Datapool\Tools\CSVtools'; - $entry['Params']['File']['SpreadsheetIteratorMethod']='csvIterator'; + $entry['Params']['File']['Spreadsheet']=$this->oc['SourcePot\Datapool\Tools\CSVtools']->csvIterator($file,$entry['Params']['File']['Extension'])->current(); + $entry['Params']['File']['SpreadsheetIteratorClass']='SourcePot\Datapool\Tools\CSVtools'; + $entry['Params']['File']['SpreadsheetIteratorMethod']='csvIterator'; } else if (stripos(strval($entry['Params']['File']['Extension']),'xls')!==FALSE){ - $entry['Params']['File']['Spreadsheet']=$this->oc['SourcePot\Datapool\Tools\XLStools']->iterator($file,$entry['Params']['File']['Extension'])->current(); - $entry['Params']['File']['SpreadsheetIteratorClass']='SourcePot\Datapool\Tools\XLStools'; - $entry['Params']['File']['SpreadsheetIteratorMethod']='iterator'; + $entry['Params']['File']['Spreadsheet']=$this->oc['SourcePot\Datapool\Tools\XLStools']->iterator($file,$entry['Params']['File']['Extension'])->current(); + $entry['Params']['File']['SpreadsheetIteratorClass']='SourcePot\Datapool\Tools\XLStools'; + $entry['Params']['File']['SpreadsheetIteratorMethod']='iterator'; + } else if (stripos(strval($entry['Params']['File']['Extension']),'txt')!==FALSE){ + $entry['Content']['File content']=file_get_contents($file); + } else if (stripos(strval($entry['Params']['File']['Extension']),'json')!==FALSE){ + $fileContent=file_get_contents($file)?:'{}'; + $entry['Content']=$this->oc['SourcePot\Datapool\Tools\MiscTools']->json2arr($fileContent); } // add file to entry if (empty($entry['EntryId'])){ diff --git a/src/php/Root.php b/src/php/Root.php index 5e81827..7ab3cb4 100644 --- a/src/php/Root.php +++ b/src/php/Root.php @@ -397,7 +397,7 @@ public function run():array // job Processing $loadAvg=$this->getLoadAvg(); $loadAvgThreshold=self::JOB_AVERAGE_LOAD_THRESHOLD_MULTIPLIER*$loadAvg['avg']; - if ($loadAvg['Load [%]']<$loadAvgThreshold){ + if ($loadAvg['Load [%]']<$loadAvgThreshold || $loadAvg['Load [%]']===0){ $arr=$this->oc['SourcePot\Datapool\Foundation\Job']->trigger($arr); } else { $arr['page html']='Job skipped due to high server load (threshold='.round($loadAvgThreshold).')...'; @@ -827,13 +827,15 @@ public function getLoadAvg():array { $loadAvg=['OS'=>PHP_OS,'Load'=>0]; if (stripos(PHP_OS,'linux')!==FALSE){ + // Linux OS if (function_exists('sys_getloadavg')){ $loadAvg['Load']=sys_getloadavg()[0]; } else { $data=file_get_contents('/proc/loadavg'); - $loadAvg['Load']=explode(' ', $data)[0]; + $loadAvg['Load']=explode(' ',$data?:0)[0]; } } else if (stripos(PHP_OS,'win')!==FALSE){ + // Windows OS exec("wmic cpu get loadpercentage /all",$execReturn); foreach($execReturn as $line) { if (is_numeric(trim($line))){ @@ -841,6 +843,14 @@ public function getLoadAvg():array break; } } + } else { + // other OS (e.g. MacOS) + if (function_exists('sys_getloadavg')){ + $loadAvg['Load']=sys_getloadavg()[0]; + } else { + $data=file_get_contents('/proc/loadavg'); + $loadAvg['Load']=explode(' ',$data?:0)[0]; + } } $loadAvg['Load [%]']=round($loadAvg['Load']*100); // add measurements to signal