From bf0a2d89cc46cd109accfe1773fffd435b159dad Mon Sep 17 00:00:00 2001 From: exilitywork <55747343+exilitywork@users.noreply.github.com> Date: Tue, 8 Dec 2020 10:33:13 +0300 Subject: [PATCH 1/3] Update bot.class.php Added Telegram API availability check --- inc/bot.class.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/inc/bot.class.php b/inc/bot.class.php index 8410aeb..484aa3d 100644 --- a/inc/bot.class.php +++ b/inc/bot.class.php @@ -40,6 +40,16 @@ static public function setConfig($key, $value) { } static public function sendMessage($to, $content) { + $test_conn = self::isSiteAvailable("https://api.telegram.org/", 2) ? true : false; + if (!$test_conn) { + $logfile="/var/www/glpi/files/_log/telegrambot.log"; + if (!file_exists($logfile)) { + $newfile = fopen($logfile, 'w+'); + fclose($newfile); + } + error_log(date("Y-m-d H:i:s")." - ERROR: Telegram API is unavailable now!\n", 3, $logfile); + return; + } $chat_id = self::getChatID($to); $telegram = self::getTelegramInstance(); $result = Request::sendMessage(['chat_id' => $chat_id, 'text' => $content]); @@ -101,5 +111,28 @@ static private function getDBCredentials() { 'database' => $DB->dbdefault, ); } + + /** + * URL availability check + * + * @param string $url URL to check + * @param int $timeout connection timeout in seconds + * + * @return bool true - URL available, false - not available + */ + static private function isSiteAvailable($url, $timeout) { + if(!filter_var($url, FILTER_VALIDATE_URL)){ + return false; + } + + $curlInit = curl_init($url); + curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,$timeout); + curl_setopt($curlInit,CURLOPT_HEADER,true); + curl_setopt($curlInit,CURLOPT_NOBODY,true); + curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); + $response = curl_exec($curlInit); + curl_close($curlInit); + return $response ? true : false; + } } From 1a6cacdd54c42d257b4970650cb4383dac971381 Mon Sep 17 00:00:00 2001 From: exilitywork <55747343+exilitywork@users.noreply.github.com> Date: Tue, 8 Dec 2020 11:57:27 +0300 Subject: [PATCH 2/3] Update bot.class.php Fixed sniff violations --- inc/bot.class.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/inc/bot.class.php b/inc/bot.class.php index 484aa3d..7be4e5c 100644 --- a/inc/bot.class.php +++ b/inc/bot.class.php @@ -45,7 +45,7 @@ static public function sendMessage($to, $content) { $logfile="/var/www/glpi/files/_log/telegrambot.log"; if (!file_exists($logfile)) { $newfile = fopen($logfile, 'w+'); - fclose($newfile); + fclose($newfile); } error_log(date("Y-m-d H:i:s")." - ERROR: Telegram API is unavailable now!\n", 3, $logfile); return; @@ -111,9 +111,9 @@ static private function getDBCredentials() { 'database' => $DB->dbdefault, ); } - + /** - * URL availability check + * URL availability check * * @param string $url URL to check * @param int $timeout connection timeout in seconds @@ -121,18 +121,18 @@ static private function getDBCredentials() { * @return bool true - URL available, false - not available */ static private function isSiteAvailable($url, $timeout) { - if(!filter_var($url, FILTER_VALIDATE_URL)){ + if (!filter_var($url, FILTER_VALIDATE_URL)) { return false; } - + $curlInit = curl_init($url); - curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,$timeout); - curl_setopt($curlInit,CURLOPT_HEADER,true); - curl_setopt($curlInit,CURLOPT_NOBODY,true); - curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); + curl_setopt($curlInit, CURLOPT_CONNECTTIMEOUT, $timeout); + curl_setopt($curlInit, CURLOPT_HEADER, true); + curl_setopt($curlInit, CURLOPT_NOBODY, true); + curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curlInit); curl_close($curlInit); return $response ? true : false; - } + } } From 0dd75ede29856394600b9ee1d1e6cfaa1e433c7f Mon Sep 17 00:00:00 2001 From: exilitywork <55747343+exilitywork@users.noreply.github.com> Date: Tue, 16 Feb 2021 08:54:03 +0300 Subject: [PATCH 3/3] Minor fixes for Telegram API's connection check --- inc/bot.class.php | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/inc/bot.class.php b/inc/bot.class.php index 7be4e5c..8fb6d99 100644 --- a/inc/bot.class.php +++ b/inc/bot.class.php @@ -40,19 +40,22 @@ static public function setConfig($key, $value) { } static public function sendMessage($to, $content) { + global $DB; + $test_conn = self::isSiteAvailable("https://api.telegram.org/", 2) ? true : false; if (!$test_conn) { - $logfile="/var/www/glpi/files/_log/telegrambot.log"; + $logfile = GLPI_LOG_DIR."/telegrambot.log"; if (!file_exists($logfile)) { $newfile = fopen($logfile, 'w+'); - fclose($newfile); + fclose($newfile); } error_log(date("Y-m-d H:i:s")." - ERROR: Telegram API is unavailable now!\n", 3, $logfile); return; + } else { + $chat_id = self::getChatID($to); + $telegram = self::getTelegramInstance(); + $result = Request::sendMessage(['chat_id' => $chat_id, 'text' => $content]); } - $chat_id = self::getChatID($to); - $telegram = self::getTelegramInstance(); - $result = Request::sendMessage(['chat_id' => $chat_id, 'text' => $content]); } static public function getUpdates() { @@ -112,27 +115,19 @@ static private function getDBCredentials() { ); } - /** - * URL availability check - * - * @param string $url URL to check - * @param int $timeout connection timeout in seconds - * - * @return bool true - URL available, false - not available - */ static private function isSiteAvailable($url, $timeout) { - if (!filter_var($url, FILTER_VALIDATE_URL)) { - return false; + if(!filter_var($url, FILTER_VALIDATE_URL)){ + return false; } - + $curlInit = curl_init($url); - curl_setopt($curlInit, CURLOPT_CONNECTTIMEOUT, $timeout); - curl_setopt($curlInit, CURLOPT_HEADER, true); - curl_setopt($curlInit, CURLOPT_NOBODY, true); - curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,$timeout); + curl_setopt($curlInit,CURLOPT_HEADER,true); + curl_setopt($curlInit,CURLOPT_NOBODY,true); + curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); $response = curl_exec($curlInit); curl_close($curlInit); return $response ? true : false; - } + } }