Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
* Simple Devices: sensors alert order change
* SQLUpdate -- new optional parameters to limit fields to update
  • Loading branch information
sergejey committed May 10, 2022
1 parent 07be7e9 commit 5271bad
Show file tree
Hide file tree
Showing 9 changed files with 860 additions and 852 deletions.
97 changes: 51 additions & 46 deletions lib/common.class.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<?php

function isRebootRequired() {
function isRebootRequired()
{
$path_to_flag = ROOT . 'reboot';
return file_exists($path_to_flag);
}

function setRebootRequired($reason = '') {
function setRebootRequired($reason = '')
{
$path_to_flag = ROOT . 'reboot';
if (!$reason) $reason = time();
@SaveFile($path_to_flag,$reason);
@SaveFile($path_to_flag, $reason);
}

function resetRebootRequired() {
function resetRebootRequired()
{
$path_to_flag = ROOT . 'reboot';
if (file_exists($path_to_flag)) {
@unlink($path_to_flag);
}
}

function getSystemSerial($force_update = 0) {
function getSystemSerial($force_update = 0)
{
$serial = gg('Serial');
if (!$serial || $serial == '0' || $force_update) {
$serial = '';
Expand Down Expand Up @@ -307,15 +311,15 @@ function runScheduledJobs()
$jobs[$i]['PROCESSED'] = 1;
$jobs[$i]['STARTED'] = date('Y-m-d H:i:s');

SQLUpdate('jobs', $jobs[$i]);
SQLUpdate('jobs', $jobs[$i], array('PROCESSED', 'STARTED'));

if ($jobs[$i]['COMMANDS'] != '') {
$url = BASE_URL . '/objects/?system_call=1&job=' . $jobs[$i]['ID'];
$result = trim(getURL($url, 0));
$result = preg_replace('/<!--.+-->/is', '', $result);
if (!preg_match('/OK$/', $result)) {
//getLogger(__FILE__)->error(sprintf('Error executing job %s (%s): %s', $jobs[$i]['TITLE'], $jobs[$i]['ID'], $result));
DebMes(sprintf('Error executing job %s (%s): %s', $jobs[$i]['TITLE'], $jobs[$i]['ID'], $result) . ' (' . __FILE__ . ')','errors');
DebMes(sprintf('Error executing job %s (%s): %s', $jobs[$i]['TITLE'], $jobs[$i]['ID'], $result) . ' (' . __FILE__ . ')', 'errors');
}
}
}
Expand Down Expand Up @@ -345,21 +349,21 @@ function recognizeTime($text, &$newText = '')
$found = 0;
$new_time = time();
#$text = ($text); #???
$text = trim($text);
$text = trim($text);

if (preg_match('/'.LANG_PATTERN_DO_AFTER.' (\d+) '.LANG_PATTERN_SECOND.'.?/isu', $text, $m)) {
if (preg_match('/' . LANG_PATTERN_DO_AFTER . ' (\d+) ' . LANG_PATTERN_SECOND . '.?/isu', $text, $m)) {
$new_time = time() + $m[1];
$newText = trim(str_replace($m[0], '', $text));
$found = 1;
} elseif (preg_match('/'.LANG_PATTERN_DO_AFTER.' (\d+) '.LANG_PATTERN_MINUTE.'.?/isu', $text, $m)) {
} elseif (preg_match('/' . LANG_PATTERN_DO_AFTER . ' (\d+) ' . LANG_PATTERN_MINUTE . '.?/isu', $text, $m)) {
$new_time = time() + $m[1] * 60;
$newText = trim(str_replace($m[0], '', $text));
$found = 1;
} elseif (preg_match('/'.LANG_PATTERN_DO_AFTER.' (\d+) '.LANG_PATTERN_HOUR.'.?/isu', $text, $m)) {
} elseif (preg_match('/' . LANG_PATTERN_DO_AFTER . ' (\d+) ' . LANG_PATTERN_HOUR . '.?/isu', $text, $m)) {
$new_time = time() + $m[1] * 60 * 60;
$newText = trim(str_replace($m[0], '', $text));
$found = 1;
} elseif (preg_match('/'.LANG_PATTERN_DO_FOR.' (\d+):(\d+)/isu', $text, $m)) {
} elseif (preg_match('/' . LANG_PATTERN_DO_FOR . ' (\d+):(\d+)/isu', $text, $m)) {
$new_time = mktime($m[1], $m[2], 0, (int)date('m'), (int)date('d'), (int)date('Y'));
$newText = trim(str_replace($m[0], '', $text));
$found = 1;
Expand Down Expand Up @@ -398,12 +402,12 @@ function playSound($filename, $exclusive = 0, $priority = 0)
if (IsWindowsOS())
safe_exec(DOC_ROOT . '/rc/madplay.exe ' . $filename, $exclusive, $priority);
else {
if (defined('AUDIO_PLAYER') && AUDIO_PLAYER!='') {
if (defined('AUDIO_PLAYER') && AUDIO_PLAYER != '') {
$audio_player = AUDIO_PLAYER;
} else {
$audio_player = 'mplayer';
}
safe_exec($audio_player.' ' . $filename . " >/dev/null 2>&1", $exclusive, $priority);
safe_exec($audio_player . ' ' . $filename . " >/dev/null 2>&1", $exclusive, $priority);
}

}
Expand Down Expand Up @@ -445,7 +449,7 @@ function runScriptSafe($id, $params = 0)
unset($params['m_c_s']);
$current_call .= '.' . md5(json_encode($params));
}
if (IsSet($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != '')) {
if (isset($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != '')) {
if (isset($_GET['m_c_s']) && is_array($_GET['m_c_s']) && !empty($_GET['m_c_s'])) {
$call_stack = $_GET['m_c_s'];
}
Expand All @@ -457,22 +461,22 @@ function runScriptSafe($id, $params = 0)
return 0;
}
}

if (!is_array($params)) {
$params = array();
}

$call_stack[] = $current_call;
$params['raiseEvent'] = $raiseEvent;
$params['m_c_s'] = $call_stack;
$params['r_s_s'] = $run_SafeScript;
$params['raiseEvent'] = $raiseEvent;
$params['m_c_s'] = $call_stack;
$params['r_s_s'] = $run_SafeScript;

if (IsSet($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != '') && !$raiseEvent && $run_SafeScript) {
$result = runScript($id,$params);
if (isset($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != '') && !$raiseEvent && $run_SafeScript) {
$result = runScript($id, $params);
} else {
$params['r_s_s'] = 1;
$result = callAPI('/api/script/' . urlencode($id), 'GET', $params);
}
}
endMeasure('runScriptSafe');
return $result;
}
Expand Down Expand Up @@ -587,7 +591,7 @@ function getURL($url, $cache = 0, $username = '', $password = '', $background =
$info = curl_getinfo($ch);
$backtrace = debug_backtrace();
$callSource = $backtrace[1]['function'];
DebMes("GetURL to $url (source " . $callSource . ") finished with error: \n" . $errorInfo . "\n" . json_encode($info),'geturl_error');
DebMes("GetURL to $url (source " . $callSource . ") finished with error: \n" . $errorInfo . "\n" . json_encode($info), 'geturl_error');
}
curl_close($ch);
endMeasure('curl_post');
Expand All @@ -614,7 +618,7 @@ function getURL($url, $cache = 0, $username = '', $password = '', $background =
function postURLBackground($url, $query = array(), $cache = 0, $username = '', $password = '')
{
//DebMes("URL: ".$url,'debug1');
postURL($url, $query , $cache, $username, $password, true);
postURL($url, $query, $cache, $username, $password, true);
}

/**
Expand Down Expand Up @@ -710,7 +714,7 @@ function postURL($url, $query = array(), $cache = 0, $username = '', $password =
$info = curl_getinfo($ch);
$backtrace = debug_backtrace();
$callSource = $backtrace[1]['function'];
DebMes("GetURL to $url (source " . $callSource . ") finished with error: \n" . $errorInfo . "\n" . json_encode($info),'geturl_error');
DebMes("GetURL to $url (source " . $callSource . ") finished with error: \n" . $errorInfo . "\n" . json_encode($info), 'geturl_error');
}
curl_close($ch);
endMeasure('curl_post');
Expand Down Expand Up @@ -779,7 +783,7 @@ function execInBackground($cmd)
} else {
try {
exec($cmd . " > /dev/null &");
} catch (Exception $e) {
} catch (Exception $e) {
DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.');
}
}
Expand Down Expand Up @@ -819,13 +823,13 @@ function checkAccess($object_type, $object_id)
startMeasure('checkAccess');

if (!isset($access_rules_cached)) {
$all_rules=SQLSelect("SELECT OBJECT_TYPE, OBJECT_ID FROM security_rules");
foreach($all_rules as $rule) {
$access_rules_cached[$rule['OBJECT_TYPE'].$rule['OBJECT_ID']]=1;
$all_rules = SQLSelect("SELECT OBJECT_TYPE, OBJECT_ID FROM security_rules");
foreach ($all_rules as $rule) {
$access_rules_cached[$rule['OBJECT_TYPE'] . $rule['OBJECT_ID']] = 1;
}
}

if (!isset($access_rules_cached[$object_type.$object_id])) {
if (!isset($access_rules_cached[$object_type . $object_id])) {
endMeasure('checkAccess');
return true;
}
Expand Down Expand Up @@ -887,8 +891,8 @@ function registerError($code = 'custom', $details = '')

if (!$error_rec['KEEP_HISTORY']) {
SQLExec("DELETE FROM system_errors_data WHERE ERROR_ID=" . (int)$error_rec['ID'] . " AND ID != '" . $history_rec['ID'] . "'");
} elseif (defined('SETTINGS_ERRORS_KEEP_HISTORY') && SETTINGS_ERRORS_KEEP_HISTORY>0) {
SQLExec("DELETE FROM system_errors_data WHERE ADDED<'".date('Y-m-d H:i:s',time()-SETTINGS_ERRORS_KEEP_HISTORY*24*60*60)."'");
} elseif (defined('SETTINGS_ERRORS_KEEP_HISTORY') && SETTINGS_ERRORS_KEEP_HISTORY > 0) {
SQLExec("DELETE FROM system_errors_data WHERE ADDED<'" . date('Y-m-d H:i:s', time() - SETTINGS_ERRORS_KEEP_HISTORY * 24 * 60 * 60) . "'");
} else {
$tmp = SQLSelect("SELECT ID FROM system_errors_data WHERE ERROR_ID=" . (int)$error_rec['ID'] . " ORDER BY ID DESC LIMIT 50");
if ($tmp[0]['ID'] && count($tmp) == 50) {
Expand Down Expand Up @@ -1173,25 +1177,26 @@ function hsvToHex($h, $s, $v)
return sprintf("%02x%02x%02x", $rgb[0], $rgb[1], $rgb[2]);
}

function logAction($action_type,$details='') {
function logAction($action_type, $details = '')
{
global $session;
$rec=array();
$rec['ADDED']=date('Y-m-d H:i:s');
$rec = array();
$rec['ADDED'] = date('Y-m-d H:i:s');
if ($session->data['SITE_USERNAME']) {
$rec['USER']=$session->data['SITE_USERNAME'];
} elseif (preg_match('/^\/admin\.php/',$_SERVER['REQUEST_URI'])) {
$rec['USER']='Control Panel';
$rec['USER'] = $session->data['SITE_USERNAME'];
} elseif (preg_match('/^\/admin\.php/', $_SERVER['REQUEST_URI'])) {
$rec['USER'] = 'Control Panel';
}
if ($session->data['TERMINAL']) {
$rec['TERMINAL']=$session->data['TERMINAL'];
$rec['TERMINAL'] = $session->data['TERMINAL'];
} else {
$rec['TERMINAL']='';
$rec['TERMINAL'] = '';
}
$rec['ACTION_TYPE']=$action_type;
$rec['TITLE']=$details;
$rec['TITLE']=mb_substr($rec['TITLE'],0,250,'utf-8');
$rec['IP']=$_SERVER['REMOTE_ADDR'];
SQLInsert('actions_log',$rec);
$rec['ACTION_TYPE'] = $action_type;
$rec['TITLE'] = $details;
$rec['TITLE'] = mb_substr($rec['TITLE'], 0, 250, 'utf-8');
$rec['IP'] = $_SERVER['REMOTE_ADDR'];
SQLInsert('actions_log', $rec);

}

Expand All @@ -1214,7 +1219,7 @@ function ping($host)

function echonow($msg, $color = '')
{
DebMes(strip_tags($msg),'auto_update');
DebMes(strip_tags($msg), 'auto_update');
if ($color) {
echo '<font color="' . $color . '">';
}
Expand Down
Loading

0 comments on commit 5271bad

Please sign in to comment.