Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 42 additions & 43 deletions src/php/AdminApps/DerivedSignals.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'],
Expand Down Expand Up @@ -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]);
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
40 changes: 27 additions & 13 deletions src/php/Foundation/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions src/php/Foundation/Filespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])){
Expand Down
14 changes: 12 additions & 2 deletions src/php/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).')...';
Expand Down Expand Up @@ -827,20 +827,30 @@ 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))){
$loadAvg['Load']=floatval(trim($line));
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
Expand Down