/** * YandexTTS * @param mixed $message Message * @param mixed $lang Language (default 'ru-RU') * @return int|string */ function YandexTTS($message, $lang = 'ru-RU') { $filename = md5($message) . '_ya.mp3'; $cachedVoiceDir = ROOT . 'cached/voice'; $cachedFileName = $cachedVoiceDir . '/' . $filename; $base_url = 'https://tts.voicetech.yandex.net/generate?'; if (file_exists($cachedFileName)) { @touch($cachedFileName); return $cachedFileName; } $qs = http_build_query(array('format' => 'mp3', 'lang' => $lang, 'speaker' => 'omazh', 'key' => SETTINGS_YANDEX_TTS_KEY, 'text' => $message)); try { $contents = file_get_contents($base_url . $qs); } catch (Exception $e) { registerError('yandextts', get_class($e) . ', ' . $e->getMessage()); } if (isset($contents)) { CreateDir($cachedVoiceDir); SaveFile($cachedFileName, $contents); return $cachedFileName; } return 0; }
function YandexTTS($message, $lang = 'ru') { $filename = md5($message) . '_ya.mp3'; if (file_exists(ROOT . 'cached/voice/' . $filename)) { @touch(ROOT . 'cached/voice/' . $filename); return ROOT . 'cached/voice/' . $filename; } $base_url = 'https://tts.voicetech.yandex.net/generate?'; $qs = http_build_query(array('format' => 'mp3', 'lang' => 'ru-RU', 'speaker' => 'omazh', 'key' => SETTINGS_YANDEX_TTS_KEY, 'text' => $message)); try { $contents = file_get_contents($base_url . $qs); } catch (Exception $e) { registerError('yandextts', get_class($e) . ', ' . $e->getMessage()); } if ($contents) { if (!is_dir(ROOT . 'cached/voice')) { @mkdir(ROOT . 'cached/voice', 0777); } SaveFile(ROOT . 'cached/voice/' . $filename, $contents); return ROOT . 'cached/voice/' . $filename; } else { return 0; } }
/** * Title * * Description * * @access public */ function GoogleTTS($message, $lang = 'ru') { $filename = md5($message) . '.mp3'; if (file_exists(ROOT . 'cached/voice/' . $filename)) { @touch(ROOT . 'cached/voice/' . $filename); return ROOT . 'cached/voice/' . $filename; } $base_url = 'http://translate.google.com/translate_tts?'; $qs = http_build_query(array('tl' => $lang, 'ie' => 'UTF-8', 'q' => $message)); try { $contents = file_get_contents($base_url . $qs); } catch (Exception $e) { registerError('googletts', get_class($e) . ', ' . $e->getMessage()); } if ($contents) { if (!is_dir(ROOT . 'cached/voice')) { @mkdir(ROOT . 'cached/voice', 0777); } SaveFile(ROOT . 'cached/voice/' . $filename, $contents); return ROOT . 'cached/voice/' . $filename; } else { return 0; } }
/** * Title * * Description * * @access public */ function checkAllVars($force = 0) { // ping hosts if ($force) { $pings = SQLSelect("SELECT * FROM webvars WHERE 1"); } else { $pings = SQLSelect("SELECT * FROM webvars WHERE CHECK_NEXT<=NOW()"); } $total = count($pings); for ($i = 0; $i < $total; $i++) { $host = $pings[$i]; if (!$force) { echo date('H:i:s') . " Checking webvar: " . processTitle($host['HOSTNAME']) . "\n"; } if (!$host['HOSTNAME']) { continue; } $online_interval = $host['ONLINE_INTERVAL']; if (!$online_interval) { $online_interval = 60; } $host['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + $online_interval); SQLUpdate('webvars', $host); // checking //web host $old_status = $host['LATEST_VALUE']; if ($host['AUTH'] && $host['USERNAME']) { $content = getURL(processTitle($host['HOSTNAME']), $host['ONLINE_INTERVAL'], $host['USERNAME'], $host['PASSWORD']); } else { $content = getURL(processTitle($host['HOSTNAME']), $host['ONLINE_INTERVAL']); } if ($host['ENCODING'] != '') { $content = iconv($host['ENCODING'], "UTF-8", $content); } $ok = 1; $new_status = ''; if ($host['SEARCH_PATTERN']) { if (preg_match('/' . $host['SEARCH_PATTERN'] . '/is', $content, $m)) { //$new_status=$m[1]; $total1 = count($m); for ($i1 = 1; $i1 < $total1; $i1++) { $new_status .= $m[$i1]; } } else { $ok = 0; // result did not matched } } else { $new_status = $content; } if ($host['CHECK_PATTERN'] && !preg_match('/' . $host['CHECK_PATTERN'] . '/is', $new_status)) { $ok = 0; // result did not pass the check } if (strlen($new_status) > 50 * 1024) { $new_status = substr($new_status, 0, 50 * 1024); } if (!$ok) { $host['LOG'] = date('Y-m-d H:i:s') . ' incorrect value:' . $new_status . "\n" . $host['LOG']; $tmp = explode("\n", $host['LOG']); $total = count($tmp); if ($total > 50) { $tmp = array_slice($tmp, 0, 50); $host['LOG'] = implode("\n", $tmp); } SQLUpdate('webvars', $host); continue; } $host['CHECK_LATEST'] = date('Y-m-d H:i:s'); $host['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + $online_interval); if ($old_status != $new_status) { $host['LOG'] = date('Y-m-d H:i:s') . ' new value:' . $new_status . "\n" . $host['LOG']; $tmp = explode("\n", $host['LOG']); $total = count($tmp); if ($total > 50) { $tmp = array_slice($tmp, 0, 50); $host['LOG'] = implode("\n", $tmp); } } $host['LATEST_VALUE'] = $new_status; SQLUpdate('webvars', $host); if ($host['LINKED_OBJECT'] != '' && $host['LINKED_PROPERTY'] != '') { getObject($host['LINKED_OBJECT'])->setProperty($host['LINKED_PROPERTY'], $new_status); } if ($old_status != $new_status && $old_status != '') { $params = array('VALUE' => $new_status); // do some status change actions $run_script_id = 0; $run_code = ''; // got online if ($host['SCRIPT_ID']) { $run_script_id = $host['SCRIPT_ID']; } elseif ($host['CODE']) { $run_code = $host['CODE']; } if ($run_script_id) { //run script runScript($run_script_id, $params); } elseif ($run_code) { //run code try { $code = $run_code; $success = eval($code); if ($success === false) { DebMes("Error in webvar code: " . $code); registerError('webvars', "Error in webvar code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('webvars', get_class($e) . ', ' . $e->getMessage()); } } } } }
/** * Title * * Description * * @access public */ function checkAllHosts($limit = 1000) { // ping hosts $pings = SQLSelect("SELECT * FROM pinghosts WHERE CHECK_NEXT<=NOW() ORDER BY CHECK_NEXT LIMIT " . $limit); $total = count($pings); for ($i = 0; $i < $total; $i++) { $host = $pings[$i]; echo "Checking " . $host['HOSTNAME'] . "\n"; $online_interval = $host['ONLINE_INTERVAL']; if (!$online_interval) { $online_interval = 60; } $offline_interval = $host['OFFLINE_INTERVAL']; if (!$offline_interval) { $offline_interval = $online_interval; } if ($host['STATUS'] == '1') { $host['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + $online_interval); } else { $host['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + $offline_interval); } SQLUpdate('pinghosts', $host); $online = 0; // checking if (!$host['TYPE']) { //ping host $online = ping(processTitle($host['HOSTNAME'])); } else { //web host $online = getURL(processTitle($host['HOSTNAME']), 0); SaveFile("./cached/host_" . $host['ID'] . '.html', $online); if ($host['SEARCH_WORD'] != '' && !is_integer(strpos($online, $host['SEARCH_WORD']))) { $online = 0; } if ($online) { $online = 1; } } if ($online) { $new_status = 1; } else { $new_status = 2; } $old_status = $host['STATUS']; if ($host['COUNTER_REQUIRED']) { $old_status_expected = $host['STATUS_EXPECTED']; $host['STATUS_EXPECTED'] = $new_status; if ($old_status_expected != $host['STATUS_EXPECTED']) { $host['COUNTER_CURRENT'] = 0; $host['LOG'] = date('Y-m-d H:i:s') . ' tries counter reset (status: ' . $host['STATUS_EXPECTED'] . ')' . "\n" . $host['LOG']; } elseif ($host['STATUS'] != $host['STATUS_EXPECTED']) { $host['COUNTER_CURRENT']++; $host['LOG'] = date('Y-m-d H:i:s') . ' tries counter increased to ' . $host['COUNTER_CURRENT'] . ' (status: ' . $host['STATUS_EXPECTED'] . ')' . "\n" . $host['LOG']; } if ($host['COUNTER_CURRENT'] >= $host['COUNTER_REQUIRED']) { $host['STATUS'] = $host['STATUS_EXPECTED']; $host['COUNTER_CURRENT'] = 0; } else { $interval = min($online_interval, $offline_interval, 20); $online_interval = $interval; $offline_interval = $interval; } } else { $host['STATUS'] = $new_status; $host['STATUS_EXPECTED'] = $host['STATUS']; $host['COUNTER_CURRENT'] = 0; } $host['CHECK_LATEST'] = date('Y-m-d H:i:s'); if ($host['LINKED_OBJECT'] != '' && $host['LINKED_PROPERTY'] != '') { setGlobal($host['LINKED_OBJECT'] . '.' . $host['LINKED_PROPERTY'], $host['STATUS']); } if ($host['STATUS'] == '1') { $host['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + $online_interval); } else { $host['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + $offline_interval); } if ($old_status != $host['STATUS']) { if ($host['STATUS'] == 2) { $host['LOG'] .= date('Y-m-d H:i:s') . ' Host is offline' . "\n"; } elseif ($host['STATUS'] == 1) { $host['LOG'] .= date('Y-m-d H:i:s') . ' Host is online' . "\n"; } $tmp = explode("\n", $host['LOG']); $total = count($tmp); if ($total > 50) { $tmp = array_slice($tmp, 0, 50); $host['LOG'] = implode("\n", $tmp); } } SQLUpdate('pinghosts', $host); if ($old_status != $host['STATUS'] && $old_status != 0) { // do some status change actions $run_script_id = 0; $run_code = ''; if ($old_status == 2 && $host['STATUS'] == 1) { // got online if ($host['SCRIPT_ID_ONLINE']) { $run_script_id = $host['SCRIPT_ID_ONLINE']; } elseif ($host['CODE_ONLINE']) { $run_code = $host['CODE_ONLINE']; } } elseif ($old_status == 1 && $host['STATUS'] == 2) { // got offline if ($host['SCRIPT_ID_OFFLINE']) { $run_script_id = $host['SCRIPT_ID_OFFLINE']; } elseif ($host['CODE_OFFLINE']) { $run_code = $host['CODE_OFFLINE']; } } if ($run_script_id) { //run script runScript($run_script_id); } elseif ($run_code) { //run code try { $code = $run_code; $success = eval($code); if ($success === false) { DebMes("Error in hosts online code: " . $code); registerError('ping_hosts', "Error in hosts online code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('ping_hosts', get_class($e) . ', ' . $e->getMessage()); } } } } }
/** * Title * * Description * * @access public */ function checkState($id) { $rec = SQLSelectOne("SELECT * FROM elm_states WHERE ID='" . $id . "'"); if (!checkAccess('scene_elements', $rec['ELEMENT_ID'])) { $status = 0; return $status; } startMeasure('state_dynamic' . $rec['IS_DYNAMIC']); if (!$rec['IS_DYNAMIC']) { $status = 1; } elseif ($rec['IS_DYNAMIC'] == 1) { if ($rec['LINKED_OBJECT'] != '' && $rec['LINKED_PROPERTY'] != '') { $value = gg(trim($rec['LINKED_OBJECT']) . '.' . trim($rec['LINKED_PROPERTY'])); } elseif ($rec['LINKED_PROPERTY'] != '') { $value = gg($rec['LINKED_PROPERTY']); } else { $value = -1; } if (($rec['CONDITION'] == 2 || $rec['CONDITION'] == 3) && $rec['CONDITION_VALUE'] != '' && !is_numeric($rec['CONDITION_VALUE']) && !preg_match('/^%/', $rec['CONDITION_VALUE'])) { $rec['CONDITION_VALUE'] = '%' . $rec['CONDITION_VALUE'] . '%'; } if (is_integer(strpos($rec['CONDITION_VALUE'], "%"))) { $rec['CONDITION_VALUE'] = processTitle($rec['CONDITION_VALUE']); } if ($rec['CONDITION'] == 1 && $value == $rec['CONDITION_VALUE']) { $status = 1; } elseif ($rec['CONDITION'] == 2 && (double) $value > (double) $rec['CONDITION_VALUE']) { $status = 1; } elseif ($rec['CONDITION'] == 3 && (double) $value < (double) $rec['CONDITION_VALUE']) { $status = 1; } elseif ($rec['CONDITION'] == 4 && $value != $rec['CONDITION_VALUE']) { $status = 1; } else { $status = 0; } } elseif ($rec['IS_DYNAMIC'] == 2) { $display = 0; if (is_integer(strpos($rec['CONDITION_ADVANCED'], "%"))) { $rec['CONDITION_ADVANCED'] = processTitle($rec['CONDITION_ADVANCED']); } try { $code = $rec['CONDITION_ADVANCED']; $success = eval($code); if ($success === false) { DebMes("Error in scene code: " . $code); registerError('scenes', "Error in scene code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('scenes', get_class($e) . ', ' . $e->getMessage()); } $status = $display; } endMeasure('state_dynamic' . $rec['IS_DYNAMIC']); if ($rec['CURRENT_STATE'] != $status) { startMeasure('stateUpdate'); $rec['CURRENT_STATE'] = $status; SQLExec('UPDATE elm_states SET CURRENT_STATE=' . $rec['CURRENT_STATE'] . ' WHERE ID=' . (int) $rec['ID']); endMeasure('stateUpdate'); } return $status; }
/** * Title * * Description * * @access public */ function getURL($url, $cache = 600, $username = '', $password = '') { $cache_file = ROOT . 'cached/urls/' . preg_replace('/\\W/is', '_', str_replace('http://', '', $url)) . '.html'; if (!$cache || !is_file($cache_file) || time() - filemtime($cache_file) > $cache) { //download try { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.14'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // bad style, I know... curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_TIMEOUT, 15); if ($username != '' || $password != '') { curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); } $tmpfname = ROOT . 'cached/cookie.txt'; curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname); curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname); $result = curl_exec($ch); } catch (Exception $e) { registerError('geturl', $url . ' ' . get_class($e) . ', ' . $e->getMessage()); } if ($cache > 0) { if (!is_dir(ROOT . 'cached/urls')) { @mkdir(ROOT . 'cached/urls', 0777); } SaveFile($cache_file, $result); } } else { $result = LoadFile($cache_file); } return $result; }
$gpsaction['EXECUTED'] = date('Y-m-d H:i:s'); $gpsaction['LOG'] = $gpsaction['EXECUTED'] . " Executed\n" . $gpsaction['LOG']; SQLUpdate('gpsactions', $gpsaction); if ($gpsaction['SCRIPT_ID']) { runScript($gpsaction['SCRIPT_ID']); } elseif ($gpsaction['CODE']) { try { $code = $gpsaction['CODE']; $success = eval($code); if ($success === false) { DebMes("Error in GPS action code: " . $code); registerError('gps_action', "Code execution error: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('gps_action', get_class($e) . ', ' . $e->getMessage()); } } } } } else { $sqlQuery = "SELECT *\n FROM gpslog\n WHERE DEVICE_ID = '" . $device['ID'] . "'\n AND ID != '" . $rec['ID'] . "'\n ORDER BY ADDED DESC\n LIMIT 1"; $tmp = SQLSelectOne($sqlQuery); if ($tmp['LOCATION_ID'] == $locations[$i]['ID']) { //Debmes("Device (" . $device['TITLE'] . ") LEFT location " . $locations[$i]['TITLE']); // left location $sqlQuery = "SELECT *\n FROM gpsactions\n WHERE LOCATION_ID = '" . $locations[$i]['ID'] . "'\n AND ACTION_TYPE = 0\n AND USER_ID = '" . $device['USER_ID'] . "'"; $gpsaction = SQLSelectOne($sqlQuery); if ($gpsaction['ID']) { $gpsaction['EXECUTED'] = date('Y-m-d H:i:s'); $gpsaction['LOG'] = $gpsaction['EXECUTED'] . " Executed\n" . $gpsaction['LOG'];
function runPatternExitAction($id, $script_exit) { if ($script_exit) { try { $code = $script_exit; $success = eval($code); if ($success === false) { DebMes("Error in pattern exit code: " . $code); registerError('patterns', "Error in pattern exit code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('patterns', get_class($e) . ', ' . $e->getMessage()); } } }
/** * Title * * Description * * @access public */ function postToPushbullet($ph) { $push_bullet_apikey = trim(SETTINGS_PUSHBULLET_KEY); $p = new PushBullet($push_bullet_apikey); if (mb_strlen($title, 'UTF-8') > 100) { $title = mb_substr($title, 0, 100, 'UTF-8') . '...'; $data = $ph; } else { $title = $ph; $data = ''; } if (defined('SETTINGS_PUSHBULLET_DEVICE_ID')) { $devices = explode(',', SETTINGS_PUSHBULLET_DEVICE_ID); $total = count($devices); for ($i = 0; $i < $total; $i++) { $push_bullet_device_id = trim($devices[$i]); if ($push_bullet_device_id) { try { $res = $p->pushNote($push_bullet_device_id, $title, $data); } catch (Exception $e) { registerError('pushbullet', get_class($e) . ', ' . $e->getMessage()); } } } } else { $res = $p->getDevices(); $devices = $res->devices; $total = count($devices); for ($i = 0; $i < $total; $i++) { if ($devices[$i]->iden) { try { $res = $p->pushNote($devices[$i]->iden, $title, $data); } catch (Exception $e) { registerError('pushbullet', get_class($e) . ', ' . $e->getMessage()); } } } } }
/** * MySQL database error handler * * @param string $query used query string * @access private */ function Error($query = "") { registerError('sql', mysql_errno() . ": " . mysql_error() . "\n{$query}"); new error(mysql_errno() . ": " . mysql_error() . "<br>{$query}", 1); return 1; }
/** * Title * * Description * * @access public */ function callMethod($name, $params = 0, $parent = 0) { startMeasure('callMethod'); $original_method_name = $this->object_title . '.' . $name; startMeasure('callMethod (' . $original_method_name . ')'); if (!$parent) { $id = $this->getMethodByName($name, $this->class_id, $this->id); } else { $id = $this->getMethodByName($name, $this->class_id, 0); } if ($id) { $method = SQLSelectOne("SELECT * FROM methods WHERE ID='" . $id . "'"); $method['EXECUTED'] = date('Y-m-d H:i:s'); if (!$method['OBJECT_ID']) { if (!$params) { $params = array(); } $params['ORIGINAL_OBJECT_TITLE'] = $this->object_title; } if ($params) { $method['EXECUTED_PARAMS'] = serialize($params); } SQLUpdate('methods', $method); if ($method['OBJECT_ID'] && $method['CALL_PARENT'] == 1) { $this->callMethod($name, $params, 1); } if ($method['SCRIPT_ID']) { /* $script=SQLSelectOne("SELECT * FROM scripts WHERE ID='".$method['SCRIPT_ID']."'"); $code=$script['CODE']; */ runScript($method['SCRIPT_ID']); } else { $code = $method['CODE']; } if ($code != '') { /* if (defined('SETTINGS_DEBUG_HISTORY') && SETTINGS_DEBUG_HISTORY==1) { $class_object=SQLSelectOne("SELECT NOLOG FROM classes WHERE ID='".$this->class_id."'"); if (!$class_object['NOLOG']) { $prevLog=SQLSelectOne("SELECT ID, UNIX_TIMESTAMP(ADDED) as UNX FROM history WHERE OBJECT_ID='".$this->id."' AND METHOD_ID='".$method['ID']."' ORDER BY ID DESC LIMIT 1"); if ($prevLog['ID']) { $prevRun=$prevLog['UNX']; $prevRunPassed=time()-$prevLog['UNX']; } $h=array(); $h['ADDED']=date('Y-m-d H:i:s'); $h['OBJECT_ID']=$this->id; $h['METHOD_ID']=$method['ID']; $h['DETAILS']=serialize($params); if ($parent) { $h['DETAILS']='(parent method) '.$h['DETAILS']; } $h['DETAILS'].="\n".'code: '."\n".$code; SQLInsert('history', $h); } } */ try { $success = eval($code); if ($success === false) { getLogger($this)->error(sprintf('Error in "%s.%s" method. Code: %s', $this->object_title, $name, $code)); registerError('method', sprintf('Exception in "%s.%s" method Code: %s', $this->object_title, $name, $code)); } } catch (Exception $e) { getLogger($this)->error(sprintf('Exception in "%s.%s" method', $this->object_title, $name), $e); registerError('method', sprintf('Exception in "%s.%s" method ' . $e->getMessage(), $this->object_title, $name)); } } endMeasure('callMethod', 1); endMeasure('callMethod (' . $original_method_name . ')', 1); if ($method['OBJECT_ID'] && $method['CALL_PARENT'] == 2) { $parent_success = $this->callMethod($name, $params, 1); } if (isset($success)) { return $success; } else { return $parent_success; } } else { endMeasure('callMethod (' . $original_method_name . ')', 1); endMeasure('callMethod', 1); return false; } }
/** * MySQL database error handler * * @param string $query used query string * @access private * @return int */ public function Error($query = "") { registerError('sql', mysqli_errno($this->dbh) . ": " . mysqli_error($this->dbh) . "\n{$query}"); new error(mysqli_errno($this->dbh) . ": " . mysqli_error($this->dbh) . "<br>{$query}", 1); return 1; }
function execCommand($chat_id, $command) { $user = SQLSelectOne("SELECT * FROM tlg_user WHERE USER_ID LIKE '" . DBSafe($chat_id) . "';"); $cmd = SQLSelectOne("SELECT * FROM tlg_cmd INNER JOIN tlg_user_cmd on tlg_cmd.ID=tlg_user_cmd.CMD_ID where tlg_user_cmd.USER_ID=" . $user['ID'] . " and ACCESS>0 and '" . DBSafe($command) . "' LIKE CONCAT(TITLE,'%');"); if ($cmd['ID']) { $this->log("execCommand => Find command"); if ($cmd['CODE']) { $this->log("execCommand => Execute user`s code command"); try { $text = $command; $success = eval($cmd['CODE']); $this->log("Command:" . $text . " Result:" . $success); if ($success == false) { //нет в выполняемом куске кода return } else { $content = array('chat_id' => $chat_id, 'text' => $success, 'parse_mode' => 'HTML'); $this->sendContent($content); $this->log("Send result to " . $chat_id . ". Command:" . $text . " Result:" . $success); } } catch (Exception $e) { registerError('telegram', sprintf('Exception in "%s" method ' . $e->getMessage(), $text)); } } } }
} } else { DebMes("object [" . $object . "] not found"); } } elseif ($job != '') { $job = SQLSelectOne("SELECT * FROM jobs WHERE ID='" . (int) $job . "'"); if ($job['ID']) { try { $code = $job['COMMANDS']; $success = eval($code); if ($success === false) { DebMes("Error in scheduled job code: " . $code); registerError('scheduled_jobs', "Error in scheduled job code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('scheduled_jobs', get_class($e) . ', ' . $e->getMessage()); } echo "OK"; } } elseif ($script != '') { //echo "\nRunning script: ".$script; //DebMes("Running script: ".$script); runScript($script, $_REQUEST); } $db->Disconnect(); // closing database connection endMeasure('TOTAL'); // end calculation of execution time //performanceReport(); // print performance report // ob_end_flush();
echo "OK" . PHP_EOL; } } echo "ALL CYCLES STARTED" . PHP_EOL; if (!is_array($restart_threads)) { $restart_threads = array('cycle_execs.php', 'cycle_main.php', 'cycle_ping.php', 'cycle_scheduler.php', 'cycle_states.php', 'cycle_webvars.php'); } while (false !== ($result = $threads->iteration())) { if (!empty($result)) { //echo "Res: " . $result . PHP_EOL . "---------------------" . PHP_EOL; $closePattern = '/THREAD CLOSED:.+?(\\.\\/scripts\\/cycle\\_.+?\\.php)/is'; if (preg_match_all($closePattern, $result, $matches) && !file_exists('./reboot')) { $total_m = count($matches[1]); for ($im = 0; $im < $total_m; $im++) { $closed_thread = $matches[1][$im]; foreach ($restart_threads as $item) { if (preg_match('/' . $item . '/is', $closed_thread)) { //restart DebMes("RESTARTING: " . $closed_thread); echo "RESTARTING: " . $closed_thread . PHP_EOL; registerError('cycle_stop', $closed_thread); $pipe_id = $threads->newThread($closed_thread); } } } } } } unlink('./reboot'); // closing database connection $db->Disconnect();
function handleFatalErrors($buffer) { G::LoadClass('case'); $oCase = new Cases(); if (preg_match('/(error<\\/b>:)(.+)(<br)/', $buffer, $regs)) { $err = preg_replace('/<.*?>/', '', $regs[2]); $aAux = explode(' in ', $err); $sCode = $_SESSION['_CODE_']; unset($_SESSION['_CODE_']); registerError(2, $aAux[0], 0, $sCode); if (strpos($_SERVER['REQUEST_URI'], '/cases/cases_Step') !== false) { if (strpos($_SERVER['REQUEST_URI'], '&ACTION=GENERATE') !== false) { $aNextStep = $oCase->getNextStep($_SESSION['PROCESS'], $_SESSION['APPLICATION'], $_SESSION['INDEX'], $_SESSION['STEP_POSITION']); if ($_SESSION['TRIGGER_DEBUG']['ISSET']) { $_SESSION['TRIGGER_DEBUG']['TIME'] = 'AFTER'; $_SESSION['TRIGGER_DEBUG']['BREAKPAGE'] = $aNextStep['PAGE']; $aNextStep['PAGE'] = $aNextStep['PAGE'] . '&breakpoint=triggerdebug'; } global $oPMScript; if (isset($oPMScript) && isset($_SESSION['APPLICATION'])) { $oPMScript->aFields['__ERROR__'] = $aAux[0]; $oCase->updateCase($_SESSION['APPLICATION'], array('APP_DATA' => $oPMScript->aFields)); } G::header('Location: ' . $aNextStep['PAGE']); die; } $_SESSION['_NO_EXECUTE_TRIGGERS_'] = 1; global $oPMScript; if (isset($oPMScript) && isset($_SESSION['APPLICATION'])) { $oPMScript->aFields['__ERROR__'] = $aAux[0]; $oCase->updateCase($_SESSION['APPLICATION'], array('APP_DATA' => $oPMScript->aFields)); } G::header('Location: ' . $_SERVER['REQUEST_URI']); die; } else { $aNextStep = $oCase->getNextStep($_SESSION['PROCESS'], $_SESSION['APPLICATION'], $_SESSION['INDEX'], $_SESSION['STEP_POSITION']); if ($_SESSION['TRIGGER_DEBUG']['ISSET']) { $_SESSION['TRIGGER_DEBUG']['TIME'] = 'AFTER'; $_SESSION['TRIGGER_DEBUG']['BREAKPAGE'] = $aNextStep['PAGE']; $aNextStep['PAGE'] = $aNextStep['PAGE'] . '&breakpoint=triggerdebug'; } if (strpos($aNextStep['PAGE'], 'TYPE=ASSIGN_TASK&UID=-1') !== false) { G::SendMessageText('Fatal error in trigger', 'error'); } global $oPMScript; if (isset($oPMScript) && isset($_SESSION['APPLICATION'])) { $oPMScript->aFields['__ERROR__'] = $aAux[0]; $oCase->updateCase($_SESSION['APPLICATION'], array('APP_DATA' => $oPMScript->aFields)); } G::header('Location: ' . $aNextStep['PAGE']); die; } } return $buffer; }
/** * Summary of context_timeout * @param mixed $id ID * @param mixed $user_id User ID * @return void */ function context_timeout($id, $user_id) { global $session; $user = SQLSelectOne("SELECT * FROM users WHERE ID = '" . (int) $user_id . "'"); $session->data['SITE_USER_ID'] = $user['ID']; $context = SQLSelectOne("SELECT * FROM patterns WHERE ID = '" . (int) $id . "'"); if (!$context['TIMEOUT_CONTEXT_ID']) { context_activate(0); } if ($context['TIMEOUT_SCRIPT']) { try { $code = $context['TIMEOUT_SCRIPT']; $success = eval($code); if ($success === false) { DebMes("Error in context timeout code: " . $code); registerError('context_timeout_action', "Error in context timeout code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('context_timeout_action', get_class($e) . ', ' . $e->getMessage()); } } if ($context['TIMEOUT_CONTEXT_ID']) { context_activate((int) $context['TIMEOUT_CONTEXT_ID']); } }
/** * BackEnd * * Module backend * * @access public */ function admin(&$out) { global $ajax; if ($ajax) { global $op; global $item_id; if ($op == 'get_details') { startMeasure('getDetails'); global $labels; global $values; $res = array(); //echo "Debug labels: $labels \nValues: $values\n"; $res['LABELS'] = array(); $labels = explode(',', $labels); $total = count($labels); $seen = array(); for ($i = 0; $i < $total; $i++) { $item_id = trim($labels[$i]); if (!$item_id || $seen[$item_id]) { continue; } $seen[$item_id] = 1; $item = SQLSelectOne("SELECT * FROM commands WHERE ID='" . (int) $item_id . "'"); if ($item['ID']) { if ($item['TYPE'] == 'custom') { $item['DATA'] = processTitle($item['DATA'], $this); $data = $item['DATA']; } else { $item['TITLE'] = processTitle($item['TITLE'], $this); $data = $item['TITLE']; } if ($item['RENDER_DATA'] != $item['DATA'] || $item['RENDER_TITLE'] != $item['TITLE']) { $tmp = SQLSelectOne("SELECT * FROM commands WHERE ID='" . $item['ID'] . "'"); $tmp['RENDER_TITLE'] = $item['TITLE']; $tmp['RENDER_DATA'] = $item['DATA']; $tmp['RENDER_UPDATED'] = date('Y-m-d H:i:s'); SQLUpdate('commands', $tmp); } if (preg_match('/#[\\w\\d]{6}/is', $data, $m)) { $color = $m[0]; $data = trim(str_replace($m[0], '<style>#item' . $item['ID'] . ' .ui-btn-active {background-color:' . $color . ';border-color:' . $color . '}</style>', $data)); } $res['LABELS'][] = array('ID' => $item['ID'], 'DATA' => $data); } } $res['VALUES'] = array(); $values = explode(',', $values); $total = count($values); $seen = array(); for ($i = 0; $i < $total; $i++) { $item_id = trim($values[$i]); if (!$item_id || $seen[$item_id]) { continue; } $seen[$item_id] = 1; $item = SQLSelectOne("SELECT * FROM commands WHERE ID='" . (int) $item_id . "'"); if ($item['ID']) { $data = $item['CUR_VALUE']; $res['VALUES'][] = array('ID' => $item['ID'], 'DATA' => $data); } } $res['LATEST_REQUEST'] = time(); echo json_encode($res); endMeasure('getDetails'); exit; } if ($op == 'get_label') { startMeasure('getLabel'); $item = SQLSelectOne("SELECT * FROM commands WHERE ID='" . (int) $item_id . "'"); startMeasure('getLabel ' . $item['TITLE'], 1); if ($item['ID']) { $res = array(); if ($item['TYPE'] == 'custom') { $item['DATA'] = processTitle($item['DATA'], $this); $res['DATA'] = $item['DATA']; } else { $item['TITLE'] = processTitle($item['TITLE'], $this); $res['DATA'] = $item['TITLE']; } if ($item['RENDER_DATA'] != $item['DATA'] || $item['RENDER_TITLE'] != $item['TITLE']) { $tmp = SQLSelectOne("SELECT * FROM commands WHERE ID='" . $item['ID'] . "'"); $tmp['RENDER_TITLE'] = $item['TITLE']; $tmp['RENDER_DATA'] = $item['DATA']; $tmp['RENDER_UPDATED'] = date('Y-m-d H:i:s'); SQLUpdate('commands', $tmp); } echo json_encode($res); } endMeasure('getLabel ' . $item['TITLE'], 1); endMeasure('getLabel', 1); exit; } if ($op == 'get_value') { startMeasure('getValue'); $item = SQLSelectOne("SELECT * FROM commands WHERE ID='" . (int) $item_id . "'"); if ($item['ID']) { $res = array(); $res['DATA'] = $item['CUR_VALUE']; echo json_encode($res); } endMeasure('getValue', 1); exit; } if ($op == 'value_changed') { global $new_value; $item = SQLSelectOne("SELECT * FROM commands WHERE ID='" . (int) $item_id . "'"); if ($item['ID']) { $old_value = $item['CUR_VALUE']; $item['CUR_VALUE'] = $new_value; SQLUpdate('commands', $item); if ($item['LINKED_PROPERTY'] != '') { //$old_value=gg($item['LINKED_OBJECT'].'.'.$item['LINKED_PROPERTY']); sg($item['LINKED_OBJECT'] . '.' . $item['LINKED_PROPERTY'], $item['CUR_VALUE'], array($this->name => 'ID!=' . $item['ID'])); } $params = array('VALUE' => $item['CUR_VALUE'], 'OLD_VALUE' => $old_value); if ($item['ONCHANGE_METHOD'] != '') { if (!$item['LINKED_OBJECT']) { $item['LINKED_OBJECT'] = $item['ONCHANGE_OBJECT']; } getObject($item['LINKED_OBJECT'])->callMethod($item['ONCHANGE_METHOD'], $params); //ONCHANGE_OBJECT //DebMes("calling method ".$item['ONCHANGE_OBJECT'].".".$item['ONCHANGE_METHOD']." with ".$item['CUR_VALUE']); } if ($item['SCRIPT_ID']) { //DebMes('Running on_change script #'.$item['SCRIPT_ID']); runScript($item['SCRIPT_ID'], $params); } if ($item['CODE']) { //DebMes("Running on_change code"); try { $code = $item['CODE']; $success = eval($code); if ($success === false) { DebMes("Error menu item code: " . $code); registerError('menu_item', "Error menu item code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); registerError('menu_item', get_class($e) . ', ' . $e->getMessage()); } } } echo "OK"; } exit; } if (isset($this->data_source) && !$_GET['data_source'] && !$_POST['data_source']) { $out['SET_DATASOURCE'] = 1; } if ($this->data_source == 'commands' || $this->data_source == '') { if ($this->view_mode == '' || $this->view_mode == 'search_commands') { startMeasure('searchCommands'); $this->search_commands($out); endMeasure('searchCommands', 1); } if ($this->view_mode == 'moveup' && $this->id) { $this->reorder_items($this->id, 'up'); $this->redirect("?"); } if ($this->view_mode == 'movedown' && $this->id) { $this->reorder_items($this->id, 'down'); $this->redirect("?"); } if ($this->view_mode == 'edit_commands') { $this->edit_commands($out, $this->id); } if ($this->view_mode == 'clone_commands') { $rec = SQLSelectOne("SELECT * FROM commands WHERE ID='" . $this->id . "'"); unset($rec['ID']); $rec['TITLE'] = $rec['TITLE'] . ' (copy)'; $rec['ID'] = SQLInsert('commands', $rec); $this->redirect("?id=" . $rec['ID'] . "&view_mode=edit_commands"); } if ($this->view_mode == 'delete_commands') { $this->delete_commands($this->id); $this->redirect("?"); } } }
/** * Title * * Description * * @access public */ function runScript($id, $params = '') { $rec = SQLSelectOne("SELECT * FROM scripts WHERE ID='" . (int) $id . "' OR TITLE LIKE '" . DBSafe($id) . "'"); if ($rec['ID']) { $rec['EXECUTED'] = date('Y-m-d H:i:s'); if ($params) { $rec['EXECUTED_PARAMS'] = serialize($params); } SQLUpdate('scripts', $rec); try { $code = $rec['CODE']; $success = eval($code); if ($success === false) { getLogger($this)->error(sprintf('Error in script "%s". Code: %s', $rec['TITLE'], $code)); registerError('script', sprintf('Error in script "%s". Code: %s', $rec['TITLE'])); } return $success; } catch (Exception $e) { getLogger($this)->error(sprintf('Error in script "%s"', $rec['TITLE']), $e); registerError('script', sprintf('Error in script "%s": ' . $e->getMessage(), $rec['TITLE'])); } } }