Revert "Revert "Add Telegram (not working)""

This reverts commit 465f71214d.
pull/481/head
Timz99 2017-05-19 00:28:08 +02:00
parent 465f71214d
commit 242d6afae8
22 changed files with 951 additions and 644 deletions

View File

@ -25,6 +25,9 @@
* @link http://www.phpservermonitor.org/ * @link http://www.phpservermonitor.org/
**/ **/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/src/bootstrap.php'; require __DIR__ . '/src/bootstrap.php';
psm_no_cache(); psm_no_cache();
@ -35,7 +38,9 @@ if(isset($_GET["logout"])) {
header('Location: ' . psm_build_url()); header('Location: ' . psm_build_url());
die(); die();
} }
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$mod = psm_GET('mod', PSM_MODULE_DEFAULT); $mod = psm_GET('mod', PSM_MODULE_DEFAULT);
try { try {
@ -46,3 +51,6 @@ try {
// and we really have no reason catch it // and we really have no reason catch it
$router->run(PSM_MODULE_DEFAULT); $router->run(PSM_MODULE_DEFAULT);
} }
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

View File

@ -491,6 +491,18 @@ function psm_build_pushover() {
return $pushover; return $pushover;
} }
/**
* Prepare a new Telegram util.
*
* @return \Telegram
*/
function psm_build_telegram() {
$telegram = new \Telegram();
$telegram->setToken(psm_get_conf('telegram_api_token'));
return $telegram;
}
/** /**
* Prepare a new SMS util. * Prepare a new SMS util.
* *

View File

@ -107,6 +107,12 @@ define('PSM_CURL_TIMEOUT', 10);
*/ */
define('PSM_PUSHOVER_CLONE_URL', 'https://pushover.net/apps/clone/php_server_monitor'); define('PSM_PUSHOVER_CLONE_URL', 'https://pushover.net/apps/clone/php_server_monitor');
/**
* Get telegram chat id.
*/
define('PSM_TELEGRAM_GET_ID', 'https://t.me/ChatIDBot');
/** /**
* By defining the PSM_BASE_URL, you will force the psm_build_url() to use this. * By defining the PSM_BASE_URL, you will force the psm_build_url() to use this.
* Useful for cronjobs if it cannot be auto-detected. * Useful for cronjobs if it cannot be auto-detected.

View File

@ -88,6 +88,9 @@ $sm_lang = array(
'pushover_key' => 'Pushover Key', 'pushover_key' => 'Pushover Key',
'pushover_device' => 'Pushover Device', 'pushover_device' => 'Pushover Device',
'pushover_device_description' => 'Apparaat waar de berichten naar toe gaan. Laat leeg voor alle apparaten.', 'pushover_device_description' => 'Apparaat waar de berichten naar toe gaan. Laat leeg voor alle apparaten.',
'telegram' => 'Telegram',
'telegram_description' => 'Telegram is een dienst die het gemakkelijk maakt om real-time notificaties te ontvangen. Zie <a href="https://telegram.org/">hun website</a> voor meer informatie.',
'telegram_key' => 'Telegram Chat ID',
'delete_title' => 'Verwijder gebruiker', 'delete_title' => 'Verwijder gebruiker',
'delete_message' => 'Weet je zeker dat je deze gebruiker wilt verwijderen: \'%1\'?', 'delete_message' => 'Weet je zeker dat je deze gebruiker wilt verwijderen: \'%1\'?',
'deleted' => 'Gebruiker verwijderd.', 'deleted' => 'Gebruiker verwijderd.',
@ -112,6 +115,7 @@ $sm_lang = array(
'email' => 'Email', 'email' => 'Email',
'sms' => 'SMS', 'sms' => 'SMS',
'pushover' => 'Pushover', 'pushover' => 'Pushover',
'telegram' => 'Telegram',
'no_logs' => 'No logs', 'no_logs' => 'No logs',
), ),
'servers' => array( 'servers' => array(
@ -136,6 +140,7 @@ $sm_lang = array(
'sms' => 'SMS', 'sms' => 'SMS',
'send_sms' => 'Stuur SMS', 'send_sms' => 'Stuur SMS',
'pushover' => 'Pushover', 'pushover' => 'Pushover',
'telegram' => 'Telegram',
'users' => 'Gebruikers', 'users' => 'Gebruikers',
'delete_title' => 'Verwijder server', 'delete_title' => 'Verwijder server',
'delete_message' => 'Weet je zeker dat je deze server wilt verwijderen: \'%1\'?', 'delete_message' => 'Weet je zeker dat je deze server wilt verwijderen: \'%1\'?',
@ -164,6 +169,7 @@ $sm_lang = array(
'warning_notifications_disabled_sms' => 'SMS notificaties zijn uitgeschakeld.', 'warning_notifications_disabled_sms' => 'SMS notificaties zijn uitgeschakeld.',
'warning_notifications_disabled_email' => 'Email notificaties zijn uitgeschakeld.', 'warning_notifications_disabled_email' => 'Email notificaties zijn uitgeschakeld.',
'warning_notifications_disabled_pushover' => 'Pushover notificaties zijn uitgeschakeld.', 'warning_notifications_disabled_pushover' => 'Pushover notificaties zijn uitgeschakeld.',
'warning_notifications_disabled_telegram' => 'Telegram notificaties zijn uitgeschakeld.',
'error_server_no_match' => 'Server niet gevonden.', 'error_server_no_match' => 'Server niet gevonden.',
'error_server_label_bad_length' => 'Het label moet tussen de 1 en 255 karakters lang zijn.', 'error_server_label_bad_length' => 'Het label moet tussen de 1 en 255 karakters lang zijn.',
'error_server_ip_bad_length' => 'Het domein / IP moet tussen de 1 en 255 karakters lang zijn.', 'error_server_ip_bad_length' => 'Het domein / IP moet tussen de 1 en 255 karakters lang zijn.',
@ -208,6 +214,11 @@ $sm_lang = array(
'pushover_clone_app' => 'Klik hier om je Pushover app te maken', 'pushover_clone_app' => 'Klik hier om je Pushover app te maken',
'pushover_api_token' => 'Pushover App API Token', 'pushover_api_token' => 'Pushover App API Token',
'pushover_api_token_description' => 'Voordat je Pushover kunt gebruiken moet je een <a href="%1$s" target="_blank">App registreren</a> via hun website, en daarvan de App API Token hier invullen.', 'pushover_api_token_description' => 'Voordat je Pushover kunt gebruiken moet je een <a href="%1$s" target="_blank">App registreren</a> via hun website, en daarvan de App API Token hier invullen.',
'telegram_get_id_button' => 'Vind je chat id',
'telegram_status' => 'Sta Telegram berichten toe?',
'telegram_description' => 'Telegram is een dienst die het gemakkelijk maakt om real-time notificaties te ontvangen. Zie <a href="https://telegram.org/">hun website</a> voor meer informatie.',
'telegram_api_token' => 'Telegram App API Token',
'telegram_api_token_description' => 'Voordat je Telegram kunt gebruiken moet je een Bot registreren via hun website @Botfather en daarna de API Token hier invullen.',
'alert_type' => 'Selecteer wanneer je een notificatie wilt', 'alert_type' => 'Selecteer wanneer je een notificatie wilt',
'alert_type_description' => '<b>Status verandert:</b> '. 'alert_type_description' => '<b>Status verandert:</b> '.
'Je ontvangt alleen bericht wanneer een server van status verandert. Dus van online -> offline of offline -> online.<br/>'. 'Je ontvangt alleen bericht wanneer een server van status verandert. Dus van online -> offline of offline -> online.<br/>'.
@ -225,13 +236,16 @@ $sm_lang = array(
'log_email' => 'Log emails verstuurd bij het script?', 'log_email' => 'Log emails verstuurd bij het script?',
'log_sms' => 'Log sms berichten verstuurd bij het script?', 'log_sms' => 'Log sms berichten verstuurd bij het script?',
'log_pushover' => 'Log Pushover berichten verstuurd bij het script?', 'log_pushover' => 'Log Pushover berichten verstuurd bij het script?',
'log_telegram' => 'Log Telegram berichten verstuurd bij het script?',
'updated' => 'De configuratie is gewijzigd.', 'updated' => 'De configuratie is gewijzigd.',
'tab_email' => 'Email', 'tab_email' => 'Email',
'tab_sms' => 'SMS', 'tab_sms' => 'SMS',
'tab_pushover' => 'Pushover', 'tab_pushover' => 'Pushover',
'tab_telegram' => 'Telegram',
'settings_email' => 'Email instellingen', 'settings_email' => 'Email instellingen',
'settings_sms' => 'SMS instellingen', 'settings_sms' => 'SMS instellingen',
'settings_pushover' => 'Pushover instellingen', 'settings_pushover' => 'Pushover instellingen',
'settings_telegram' => 'Telegram instellingen',
'settings_notification' => 'Notificatie instellingen', 'settings_notification' => 'Notificatie instellingen',
'settings_log' => 'Log instellingen', 'settings_log' => 'Log instellingen',
'auto_refresh' => 'Auto-refresh', 'auto_refresh' => 'Auto-refresh',
@ -245,6 +259,7 @@ $sm_lang = array(
'test_email' => 'Er zal een email verstuurd worden naar het email adres in je profiel.', 'test_email' => 'Er zal een email verstuurd worden naar het email adres in je profiel.',
'test_sms' => 'Er zal een SMS verstuurd worden naar het telefoonnummer in je profiel.', 'test_sms' => 'Er zal een SMS verstuurd worden naar het telefoonnummer in je profiel.',
'test_pushover' => 'Er zal een Pushover notificatie verstuurd worden naar de user key/device in je profiel.', 'test_pushover' => 'Er zal een Pushover notificatie verstuurd worden naar de user key/device in je profiel.',
'test_telegram' => 'Er zal een Telegram notificatie verstuurd worden naar het chat id in je profiel.',
'send' => 'Verstuur', 'send' => 'Verstuur',
'test_subject' => 'Test', 'test_subject' => 'Test',
'test_message' => 'Test bericht', 'test_message' => 'Test bericht',
@ -257,6 +272,10 @@ $sm_lang = array(
'pushover_error' => 'De volgende fout is opgetreden bij het versturen van de Pushover notificatie: %s', 'pushover_error' => 'De volgende fout is opgetreden bij het versturen van de Pushover notificatie: %s',
'pushover_error_noapp' => 'Kan test notificatie niet verzenden: er is geen Pushover App API token gevonden in de algemene configuratie.', 'pushover_error_noapp' => 'Kan test notificatie niet verzenden: er is geen Pushover App API token gevonden in de algemene configuratie.',
'pushover_error_nokey' => 'Kan test notificatie niet verzenden: er is geen Pushover key gevonden in je profiel.', 'pushover_error_nokey' => 'Kan test notificatie niet verzenden: er is geen Pushover key gevonden in je profiel.',
'telegram_sent' => 'Telegram notificatie verzonden',
'telegram_error' => 'De volgende fout is opgetreden bij het versturen van de Telegram notificatie: %s',
'telegram_error_noapi' => 'Kan test notificatie niet verzenden: er is geen Telegram Bot API token gevonden in de algemene configuratie.',
'telegram_error_noid' => 'Kan test notificatie niet verzenden: er is geen Telegram chat id gevonden in je profiel.',
'log_retention_period' => 'Log retentie periode', 'log_retention_period' => 'Log retentie periode',
'log_retention_period_description' => 'Aantal dagen dat logs van notificaties en archieven van server uptime worden bewaard. Vul 0 in om log opruiming uit te zetten.', 'log_retention_period_description' => 'Aantal dagen dat logs van notificaties en archieven van server uptime worden bewaard. Vul 0 in om log opruiming uit te zetten.',
'log_retention_days' => 'dagen', 'log_retention_days' => 'dagen',
@ -268,11 +287,13 @@ $sm_lang = array(
'off_email_body' => "De server kon niet worden bereikt:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Fout: %ERROR%<br/>Datum: %DATE%", 'off_email_body' => "De server kon niet worden bereikt:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Fout: %ERROR%<br/>Datum: %DATE%",
'off_pushover_title' => 'Server %LABEL% is DOWN', 'off_pushover_title' => 'Server %LABEL% is DOWN',
'off_pushover_message' => "De server kon niet worden bereikt:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Fout: %ERROR%<br/>Datum: %DATE%", 'off_pushover_message' => "De server kon niet worden bereikt:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Fout: %ERROR%<br/>Datum: %DATE%",
'off_telegram_message' => "Server %LABEL% is DOWN \n De server kon niet worden bereikt:\n \n Server: %LABEL% \n IP: %IP% \n Poort: %PORT% \n Fout: %ERROR% \n Datum: %DATE%",
'on_sms' => 'Server %LABEL% is RUNNING: ip=%IP%, poort=%PORT%', 'on_sms' => 'Server %LABEL% is RUNNING: ip=%IP%, poort=%PORT%',
'on_email_subject' => 'BELANGRIJK: Server %LABEL% is RUNNING', 'on_email_subject' => 'BELANGRIJK: Server %LABEL% is RUNNING',
'on_email_body' => "Server %LABEL% is weer online:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Datum: %DATE%", 'on_email_body' => "Server %LABEL% is weer online:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Datum: %DATE%",
'on_pushover_title' => 'Server %LABEL% is RUNNING', 'on_pushover_title' => 'Server %LABEL% is RUNNING',
'on_pushover_message' => "Server %LABEL% is weer online:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Datum: %DATE%", 'on_pushover_message' => "Server %LABEL% is weer online:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Datum: %DATE%",
'on_telegram_message' => "Server %LABEL% is weer online: \n \n Server: %LABEL% \n IP: %IP% \n Poort: %PORT% \n Datum: %DATE%",
), ),
'login' => array( 'login' => array(
'welcome_usermenu' => 'Welkom, %user_name%', 'welcome_usermenu' => 'Welkom, %user_name%',

View File

@ -1,33 +1,34 @@
<?php <?php
/** /**
* PHP Server Monitor * PHP Server Monitor
* Monitor your servers and websites. * Monitor your servers and websites.
* *
* This file is part of PHP Server Monitor. * This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify * PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PHP Server Monitor is distributed in the hope that it will be useful, * PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>. * along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
* *
* @package phpservermon * @package phpservermon
* @author Pepijn Over <pep@mailbox.org> * @author Pepijn Over <pep@mailbox.org>
* @copyright Copyright (c) 2008-2017 Pepijn Over <pep@mailbox.org> * @copyright Copyright (c) 2008-2017 Pepijn Over <pep@mailbox.org>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@ * @version Release: @package_version@
* @link http://www.phpservermonitor.org/ * @link http://www.phpservermonitor.org/
**/ **/
namespace psm\Module\Config\Controller; namespace psm\Module\Config\Controller;
use psm\Module\AbstractController; use psm\Module\AbstractController;
use psm\Service\Database; use psm\Service\Database;
use psm\Telegram;
class ConfigController extends AbstractController { class ConfigController extends AbstractController {
@ -41,10 +42,12 @@ class ConfigController extends AbstractController {
'email_smtp', 'email_smtp',
'sms_status', 'sms_status',
'pushover_status', 'pushover_status',
'telegram_status',
'log_status', 'log_status',
'log_email', 'log_email',
'log_sms', 'log_sms',
'log_pushover', 'log_pushover',
'log_telegram',
'show_update', 'show_update',
); );
@ -66,6 +69,7 @@ class ConfigController extends AbstractController {
'sms_gateway_password', 'sms_gateway_password',
'sms_from', 'sms_from',
'pushover_api_token', 'pushover_api_token',
'telegram_api_token',
); );
private $default_tab = 'general'; private $default_tab = 'general';
@ -135,7 +139,7 @@ class ConfigController extends AbstractController {
$tpl_data[$this->default_tab . '_active'] = 'active'; $tpl_data[$this->default_tab . '_active'] = 'active';
$testmodals = array('email', 'sms', 'pushover'); $testmodals = array('email', 'sms', 'pushover', 'telegram');
foreach($testmodals as $modal_id) { foreach($testmodals as $modal_id) {
$modal = new \psm\Util\Module\Modal($this->twig, 'test' . ucfirst($modal_id), \psm\Util\Module\Modal::MODAL_TYPE_OKCANCEL); $modal = new \psm\Util\Module\Modal($this->twig, 'test' . ucfirst($modal_id), \psm\Util\Module\Modal::MODAL_TYPE_OKCANCEL);
$this->addModal($modal); $this->addModal($modal);
@ -186,6 +190,8 @@ class ConfigController extends AbstractController {
$this->testSMS(); $this->testSMS();
} elseif(!empty($_POST['test_pushover'])) { } elseif(!empty($_POST['test_pushover'])) {
$this->testPushover(); $this->testPushover();
} elseif(!empty($_POST['test_telegram'])) {
$this->testTelegram();
} }
if($language_refresh) { if($language_refresh) {
@ -201,6 +207,8 @@ class ConfigController extends AbstractController {
$this->default_tab = 'sms'; $this->default_tab = 'sms';
} elseif(isset($_POST['pushover_submit']) || !empty($_POST['test_pushover'])) { } elseif(isset($_POST['pushover_submit']) || !empty($_POST['test_pushover'])) {
$this->default_tab = 'pushover'; $this->default_tab = 'pushover';
} elseif(isset($_POST['telegram_submit']) || !empty($_POST['test_telegram'])) {
$this->default_tab = 'telegram';
} }
} }
return $this->runAction('index'); return $this->runAction('index');
@ -287,14 +295,43 @@ class ConfigController extends AbstractController {
} }
} }
/**
* Execute telegram test
*
* @todo move test to separate class
*/
protected function testTelegram() {
$telegram = psm_build_telegram();
$telegram->setDebug(true);
$user = $this->getUser()->getUser();
$api_token = psm_get_conf('telegram_api_token');
if(!empty($api_token)) {
$this->addMessage(psm_get_lang('config', 'telegram_error_noapi'), 'error');
} elseif(empty($user->telegram_chat_id)) {
$this->addMessage(psm_get_lang('config', 'telegram_error_noid'), 'error');
} else {
$telegram = new Telegram($api_token);
$chat_id = $user['telegram_chat_id'];
$message->setMessage(psm_get_lang('config', 'test_message'));
$content = array('chat_id' => $chat_id, 'text' => $message);
$telegram->sendMessage($content);
}
die($bot_token);
}
protected function getLabels() { protected function getLabels() {
return array( return array(
'label_tab_email' => psm_get_lang('config', 'tab_email'), 'label_tab_email' => psm_get_lang('config', 'tab_email'),
'label_tab_sms' => psm_get_lang('config', 'tab_sms'), 'label_tab_sms' => psm_get_lang('config', 'tab_sms'),
'label_tab_pushover' => psm_get_lang('config', 'tab_pushover'), 'label_tab_pushover' => psm_get_lang('config', 'tab_pushover'),
'label_tab_telegram' => psm_get_lang('config', 'tab_telegram'),
'label_settings_email' => psm_get_lang('config', 'settings_email'), 'label_settings_email' => psm_get_lang('config', 'settings_email'),
'label_settings_sms' => psm_get_lang('config', 'settings_sms'), 'label_settings_sms' => psm_get_lang('config', 'settings_sms'),
'label_settings_pushover' => psm_get_lang('config', 'settings_pushover'), 'label_settings_pushover' => psm_get_lang('config', 'settings_pushover'),
'label_settings_telegram' => psm_get_lang('config', 'settings_telegram'),
'label_settings_notification' => psm_get_lang('config', 'settings_notification'), 'label_settings_notification' => psm_get_lang('config', 'settings_notification'),
'label_settings_log' => psm_get_lang('config', 'settings_log'), 'label_settings_log' => psm_get_lang('config', 'settings_log'),
'label_settings_proxy' => psm_get_lang('config', 'settings_proxy'), 'label_settings_proxy' => psm_get_lang('config', 'settings_proxy'),
@ -346,6 +383,12 @@ class ConfigController extends AbstractController {
psm_get_lang('config', 'pushover_api_token_description'), psm_get_lang('config', 'pushover_api_token_description'),
PSM_PUSHOVER_CLONE_URL PSM_PUSHOVER_CLONE_URL
), ),
'label_telegram_description' => psm_get_lang('config', 'telegram_description'),
'label_telegram_status' => psm_get_lang('config', 'telegram_status'),
'label_telegram_description' => psm_get_lang('config', 'telegram_description'),
'label_telegram_status' => psm_get_lang('config', 'telegram_status'),
'label_telegram_api_token' => psm_get_lang('config', 'telegram_api_token'),
'label_telegram_api_token_description' => psm_get_lang('config', 'telegram_api_token_description'),
'label_alert_type' => psm_get_lang('config', 'alert_type'), 'label_alert_type' => psm_get_lang('config', 'alert_type'),
'label_alert_type_description' => psm_get_lang('config', 'alert_type_description'), 'label_alert_type_description' => psm_get_lang('config', 'alert_type_description'),
'label_alert_type_status' => psm_get_lang('config', 'alert_type_status'), 'label_alert_type_status' => psm_get_lang('config', 'alert_type_status'),
@ -356,6 +399,7 @@ class ConfigController extends AbstractController {
'label_log_email' => psm_get_lang('config', 'log_email'), 'label_log_email' => psm_get_lang('config', 'log_email'),
'label_log_sms' => psm_get_lang('config', 'log_sms'), 'label_log_sms' => psm_get_lang('config', 'log_sms'),
'label_log_pushover' => psm_get_lang('config', 'log_pushover'), 'label_log_pushover' => psm_get_lang('config', 'log_pushover'),
'label_log_telegram' => psm_get_lang('config', 'log_telegram'),
'label_alert_proxy' => psm_get_lang('config', 'alert_proxy'), 'label_alert_proxy' => psm_get_lang('config', 'alert_proxy'),
'label_alert_proxy_url' => psm_get_lang('config', 'alert_proxy_url'), 'label_alert_proxy_url' => psm_get_lang('config', 'alert_proxy_url'),
'label_auto_refresh' => psm_get_lang('config', 'auto_refresh'), 'label_auto_refresh' => psm_get_lang('config', 'auto_refresh'),

View File

@ -214,6 +214,7 @@ class InstallController extends AbstractController {
'level' => PSM_USER_ADMIN, 'level' => PSM_USER_ADMIN,
'pushover_key' => '', 'pushover_key' => '',
'pushover_device' => '', 'pushover_device' => '',
'telegram_chat_id' => '',
); );
$validator = $this->container->get('util.user.validator'); $validator = $this->container->get('util.user.validator');

View File

@ -73,6 +73,7 @@ abstract class AbstractServerController extends AbstractController {
`s`.`email`, `s`.`email`,
`s`.`sms`, `s`.`sms`,
`s`.`pushover`, `s`.`pushover`,
`s`.`telegram`,
`s`.`warning_threshold`, `s`.`warning_threshold`,
`s`.`warning_threshold_counter`, `s`.`warning_threshold_counter`,
`s`.`timeout`, `s`.`timeout`,
@ -104,6 +105,7 @@ abstract class AbstractServerController extends AbstractController {
$server['email'] = psm_get_lang('system', $server['email']); $server['email'] = psm_get_lang('system', $server['email']);
$server['sms'] = psm_get_lang('system', $server['sms']); $server['sms'] = psm_get_lang('system', $server['sms']);
$server['pushover'] = psm_get_lang('system', $server['pushover']); $server['pushover'] = psm_get_lang('system', $server['pushover']);
$server['telegram'] = psm_get_lang('system', $server['telegram']);
if($server['status'] == 'on' && $server['warning_threshold_counter'] > 0) { if($server['status'] == 'on' && $server['warning_threshold_counter'] > 0) {
$server['status'] = 'warning'; $server['status'] = 'warning';

View File

@ -49,6 +49,7 @@ class LogController extends AbstractServerController {
'label_email' => psm_get_lang('log', 'email'), 'label_email' => psm_get_lang('log', 'email'),
'label_sms' => psm_get_lang('log', 'sms'), 'label_sms' => psm_get_lang('log', 'sms'),
'label_pushover' => psm_get_lang('log', 'pushover'), 'label_pushover' => psm_get_lang('log', 'pushover'),
'label_telegram' => psm_get_lang('log', 'telegram'),
'label_title' => psm_get_lang('log', 'title'), 'label_title' => psm_get_lang('log', 'title'),
'label_server' => psm_get_lang('servers', 'server'), 'label_server' => psm_get_lang('servers', 'server'),
'label_type' => psm_get_lang('log', 'type'), 'label_type' => psm_get_lang('log', 'type'),
@ -58,7 +59,7 @@ class LogController extends AbstractServerController {
'label_no_logs' => psm_get_lang('log', 'no_logs'), 'label_no_logs' => psm_get_lang('log', 'no_logs'),
'tabs' => array(), 'tabs' => array(),
); );
$log_types = array('status', 'email', 'sms', 'pushover'); $log_types = array('status', 'email', 'sms', 'pushover', 'telegram');
foreach($log_types as $key) { foreach($log_types as $key) {
$records = $this->getEntries($key); $records = $this->getEntries($key);

View File

@ -92,6 +92,7 @@ class ServerController extends AbstractServerController {
'email' => 'icon-envelope', 'email' => 'icon-envelope',
'sms' => 'icon-mobile', 'sms' => 'icon-mobile',
'pushover' => 'icon-pushover', 'pushover' => 'icon-pushover',
'telegram' => 'icon-telegram',
); );
$servers = $this->getServers(); $servers = $this->getServers();
@ -206,10 +207,11 @@ class ServerController extends AbstractServerController {
'edit_email_selected_' . $edit_server['email'] => 'selected="selected"', 'edit_email_selected_' . $edit_server['email'] => 'selected="selected"',
'edit_sms_selected_' . $edit_server['sms'] => 'selected="selected"', 'edit_sms_selected_' . $edit_server['sms'] => 'selected="selected"',
'edit_pushover_selected_' . $edit_server['pushover'] => 'selected="selected"', 'edit_pushover_selected_' . $edit_server['pushover'] => 'selected="selected"',
'edit_telegram_selected_' . $edit_server['telegram'] => 'selected="selected"',
)); ));
} }
$notifications = array('email', 'sms', 'pushover'); $notifications = array('email', 'sms', 'pushover', 'telegram');
foreach($notifications as $notification) { foreach($notifications as $notification) {
if(psm_get_conf($notification . '_status') == 0) { if(psm_get_conf($notification . '_status') == 0) {
$tpl_data['warning_' . $notification] = true; $tpl_data['warning_' . $notification] = true;
@ -268,6 +270,7 @@ class ServerController extends AbstractServerController {
'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no', 'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no',
'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no', 'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no',
'pushover' => in_array($_POST['pushover'], array('yes', 'no')) ? $_POST['pushover'] : 'no', 'pushover' => in_array($_POST['pushover'], array('yes', 'no')) ? $_POST['pushover'] : 'no',
'telegram' => in_array($_POST['telegram'], array('yes', 'no')) ? $_POST['telegram'] : 'no',
); );
// make sure websites start with http:// // make sure websites start with http://
if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') { if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') {
@ -472,6 +475,7 @@ class ServerController extends AbstractServerController {
'label_sms' => psm_get_lang('servers', 'sms'), 'label_sms' => psm_get_lang('servers', 'sms'),
'label_send_sms' => psm_get_lang('servers', 'send_sms'), 'label_send_sms' => psm_get_lang('servers', 'send_sms'),
'label_pushover' => psm_get_lang('servers', 'pushover'), 'label_pushover' => psm_get_lang('servers', 'pushover'),
'label_telegram' => psm_get_lang('servers', 'telegram'),
'label_users' => psm_get_lang('servers', 'users'), 'label_users' => psm_get_lang('servers', 'users'),
'label_warning_threshold' => psm_get_lang('servers', 'warning_threshold'), 'label_warning_threshold' => psm_get_lang('servers', 'warning_threshold'),
'label_warning_threshold_description' => psm_get_lang('servers', 'warning_threshold_description'), 'label_warning_threshold_description' => psm_get_lang('servers', 'warning_threshold_description'),

View File

@ -35,7 +35,7 @@ class ProfileController extends AbstractController {
* Editable fields for the profile * Editable fields for the profile
* @var array $profile_fields * @var array $profile_fields
*/ */
protected $profile_fields = array('name', 'user_name', 'mobile', 'pushover_key', 'pushover_device', 'email'); protected $profile_fields = array('name', 'user_name', 'mobile', 'pushover_key', 'pushover_device', 'telegram_chat_id', 'email');
function __construct(Database $db, \Twig_Environment $twig) { function __construct(Database $db, \Twig_Environment $twig) {
parent::__construct($db, $twig); parent::__construct($db, $twig);
@ -66,6 +66,10 @@ class ProfileController extends AbstractController {
'label_pushover_key' => psm_get_lang('users', 'pushover_key'), 'label_pushover_key' => psm_get_lang('users', 'pushover_key'),
'label_pushover_device' => psm_get_lang('users', 'pushover_device'), 'label_pushover_device' => psm_get_lang('users', 'pushover_device'),
'label_pushover_device_description' => psm_get_lang('users', 'pushover_device_description'), 'label_pushover_device_description' => psm_get_lang('users', 'pushover_device_description'),
'label_telegram_get_id_button' => psm_get_lang('config', 'telegram_get_id_button'),
'label_telegram' => psm_get_lang('users', 'telegram'),
'label_telegram_description' => psm_get_lang('users', 'telegram_description'),
'label_telegram_chat_id' => psm_get_lang('users', 'telegram_chat_id'),
'label_email' => psm_get_lang('users', 'email'), 'label_email' => psm_get_lang('users', 'email'),
'label_save' => psm_get_lang('system', 'save'), 'label_save' => psm_get_lang('system', 'save'),
'form_action' => psm_build_url(array( 'form_action' => psm_build_url(array(

View File

@ -89,7 +89,7 @@ class UserController extends AbstractController {
$users = $this->db->select( $users = $this->db->select(
PSM_DB_PREFIX.'users', PSM_DB_PREFIX.'users',
null, null,
array('user_id', 'user_name', 'level', 'name', 'mobile', 'pushover_key', 'pushover_device', 'email'), array('user_id', 'user_name', 'level', 'name', 'mobile', 'pushover_key', 'pushover_device', 'telegram_chat_id', 'email'),
null, null,
array('name') array('name')
); );
@ -133,7 +133,7 @@ class UserController extends AbstractController {
*/ */
protected function executeEdit() { protected function executeEdit() {
$user_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $user_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$fields_prefill = array('name', 'user_name', 'mobile', 'pushover_key', 'pushover_device', 'email'); $fields_prefill = array('name', 'user_name', 'mobile', 'pushover_key', 'pushover_device', 'telegram_chat_id', 'email');
if($user_id == 0) { if($user_id == 0) {
// insert mode // insert mode
@ -215,7 +215,7 @@ class UserController extends AbstractController {
} }
$user_id = (isset($_GET['id'])) ? intval($_GET['id']) : 0; $user_id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
$fields = array('name', 'user_name', 'password', 'password_repeat', 'level', 'mobile', 'pushover_key', 'pushover_device', 'email'); $fields = array('name', 'user_name', 'password', 'password_repeat', 'level', 'mobile', 'pushover_key', 'pushover_device', 'telegram_chat_id', 'email');
$clean = array(); $clean = array();
foreach($fields as $field) { foreach($fields as $field) {
if(isset($_POST[$field])) { if(isset($_POST[$field])) {
@ -333,6 +333,9 @@ class UserController extends AbstractController {
'label_pushover_key' => psm_get_lang('users', 'pushover_key'), 'label_pushover_key' => psm_get_lang('users', 'pushover_key'),
'label_pushover_device' => psm_get_lang('users', 'pushover_device'), 'label_pushover_device' => psm_get_lang('users', 'pushover_device'),
'label_pushover_device_description' => psm_get_lang('users', 'pushover_device_description'), 'label_pushover_device_description' => psm_get_lang('users', 'pushover_device_description'),
'label_telegram' => psm_get_lang('users', 'telegram'),
'label_telegram_description' => psm_get_lang('users', 'telegram_description'),
'label_telegram_chat_id' => psm_get_lang('users', 'telegram_chat_id'),
'label_email' => psm_get_lang('users', 'email'), 'label_email' => psm_get_lang('users', 'email'),
'label_servers' => psm_get_lang('menu', 'server'), 'label_servers' => psm_get_lang('menu', 'server'),
'label_action' => psm_get_lang('system', 'action'), 'label_action' => psm_get_lang('system', 'action'),

View File

@ -0,0 +1,67 @@
<?php
namespace psm\Telegram;
class Telegram {
$file = 'text.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
private $data = array();
public function __construct($bot_token) {
$this->bot_token = $bot_token;
$this->data = $this->getData();
}
public function endpoint($api, array $content, $post = true) {
$url = 'https://api.telegram.org/bot' . $this->bot_token . '/' . $api;
if ($post)
$reply = $this->sendAPIRequest($url, $content);
else
$reply = $this->sendAPIRequest($url, array(), false);
return json_decode($reply, true);
}
public function sendMessage(array $content) {
return $this->endpoint("sendMessage", $content);
}
public function getData() {
if (empty($this->data)) {
$rawData = file_get_contents("php://input");
return json_decode($rawData, true);
} else {
return $this->data;
}
}
/// Set the data currently used
public function setData(array $data) {
$this->data = $data;
}
private function sendAPIRequest($url, array $content, $post = true) {
if (isset($content['chat_id'])) {
$url = $url . "?chat_id=" . $content['chat_id'];
unset($content['chat_id']);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if($result === false) {
$result = json_encode(array('ok'=>false, 'curl_error_code' => curl_errno($ch), 'curl_error' => curl_error($ch)));
}
curl_close($ch);
//return $result;
}
die($url);
}

View File

@ -1,38 +1,38 @@
<?php <?php
/** /**
* PHP Server Monitor * PHP Server Monitor
* Monitor your servers and websites. * Monitor your servers and websites.
* *
* This file is part of PHP Server Monitor. * This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify * PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PHP Server Monitor is distributed in the hope that it will be useful, * PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>. * along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
* *
* @package phpservermon * @package phpservermon
* @author Pepijn Over <pep@mailbox.org> * @author Pepijn Over <pep@mailbox.org>
* @copyright Copyright (c) 2008-2017 Pepijn Over <pep@mailbox.org> * @copyright Copyright (c) 2008-2017 Pepijn Over <pep@mailbox.org>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@ * @version Release: @package_version@
* @link http://www.phpservermonitor.org/ * @link http://www.phpservermonitor.org/
* @since phpservermon 2.1.0 * @since phpservermon 2.1.0
**/ **/
namespace psm\Util\Install; namespace psm\Util\Install;
/** /**
* Installer class. * Installer class.
* *
* Executes the queries to install/upgrade phpservermon. * Executes the queries to install/upgrade phpservermon.
*/ */
class Installer { class Installer {
/** /**
@ -126,7 +126,7 @@ class Installer {
$this->log('Populating database...'); $this->log('Populating database...');
$queries = array(); $queries = array();
$queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "servers` (`ip`, `port`, `label`, `type`, `pattern`, `status`, `error`, `rtime`, `last_online`, `last_check`, `active`, `email`, `sms`, `pushover`) VALUES ('http://sourceforge.net/index.php', 80, 'SourceForge', 'website', '', 'on', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'yes', 'yes', 'yes', 'yes'), ('smtp.gmail.com', 465, 'Gmail SMTP', 'service', '', 'on', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'yes', 'yes', 'yes', 'yes')"; $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "servers` (`ip`, `port`, `label`, `type`, `pattern`, `status`, `error`, `rtime`, `last_online`, `last_check`, `active`, `email`, `sms`, `pushover`, `telegram`) VALUES ('http://sourceforge.net/index.php', 80, 'SourceForge', 'website', '', 'on', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'yes', 'yes', 'yes', 'yes'), ('smtp.gmail.com', 465, 'Gmail SMTP', 'service', '', 'on', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'yes', 'yes', 'yes', 'yes', 'yes')";
$queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "users_servers` (`user_id`,`server_id`) VALUES (1, 1), (1, 2);"; $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "users_servers` (`user_id`,`server_id`) VALUES (1, 1), (1, 2);";
$queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUE $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUE
('language', 'en_US'), ('language', 'en_US'),
@ -150,12 +150,15 @@ class Installer {
('sms_from', '1234567890'), ('sms_from', '1234567890'),
('pushover_status', '0'), ('pushover_status', '0'),
('pushover_api_token', ''), ('pushover_api_token', ''),
('telegram_status', '0'),
('telegram_api_token', ''),
('password_encrypt_key', '" . sha1(microtime()) . "'), ('password_encrypt_key', '" . sha1(microtime()) . "'),
('alert_type', 'status'), ('alert_type', 'status'),
('log_status', '1'), ('log_status', '1'),
('log_email', '1'), ('log_email', '1'),
('log_sms', '1'), ('log_sms', '1'),
('log_pushover', '1'), ('log_pushover', '1'),
('log_telegram', '1'),
('log_retention_period', '365'), ('log_retention_period', '365'),
('version', '" . PSM_VERSION . "'), ('version', '" . PSM_VERSION . "'),
('version_update_check', '" . PSM_VERSION . "'), ('version_update_check', '" . PSM_VERSION . "'),
@ -189,6 +192,7 @@ class Installer {
`mobile` varchar(15) NOT NULL, `mobile` varchar(15) NOT NULL,
`pushover_key` varchar(255) NOT NULL, `pushover_key` varchar(255) NOT NULL,
`pushover_device` varchar(255) NOT NULL, `pushover_device` varchar(255) NOT NULL,
`telegram_chat_id` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL, `email` varchar(255) NOT NULL,
PRIMARY KEY (`user_id`), PRIMARY KEY (`user_id`),
UNIQUE KEY `unique_username` (`user_name`) UNIQUE KEY `unique_username` (`user_name`)
@ -207,7 +211,7 @@ class Installer {
PSM_DB_PREFIX . 'log' => "CREATE TABLE `" . PSM_DB_PREFIX . "log` ( PSM_DB_PREFIX . 'log' => "CREATE TABLE `" . PSM_DB_PREFIX . "log` (
`log_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `log_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`server_id` int(11) unsigned NOT NULL, `server_id` int(11) unsigned NOT NULL,
`type` enum('status','email','sms','pushover') NOT NULL, `type` enum('status','email','sms','pushover','telegram') NOT NULL,
`message` varchar(255) NOT NULL, `message` varchar(255) NOT NULL,
`datetime` timestamp NOT NULL default CURRENT_TIMESTAMP, `datetime` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`log_id`) PRIMARY KEY (`log_id`)
@ -233,6 +237,7 @@ class Installer {
`email` enum('yes','no') NOT NULL default 'yes', `email` enum('yes','no') NOT NULL default 'yes',
`sms` enum('yes','no') NOT NULL default 'no', `sms` enum('yes','no') NOT NULL default 'no',
`pushover` enum('yes','no') NOT NULL default 'yes', `pushover` enum('yes','no') NOT NULL default 'yes',
`telegram` enum('yes','no') NOT NULL default 'yes',
`warning_threshold` mediumint(1) unsigned NOT NULL DEFAULT '1', `warning_threshold` mediumint(1) unsigned NOT NULL DEFAULT '1',
`warning_threshold_counter` mediumint(1) unsigned NOT NULL DEFAULT '0', `warning_threshold_counter` mediumint(1) unsigned NOT NULL DEFAULT '0',
`timeout` smallint(1) unsigned NULL DEFAULT NULL, `timeout` smallint(1) unsigned NULL DEFAULT NULL,
@ -419,11 +424,16 @@ class Installer {
psm_update_conf('pushover_status', 0); psm_update_conf('pushover_status', 0);
psm_update_conf('log_pushover', 1); psm_update_conf('log_pushover', 1);
psm_update_conf('pushover_api_token', ''); psm_update_conf('pushover_api_token', '');
psm_update_conf('telegram_status', 0);
psm_update_conf('log_telegram', 1);
psm_update_conf('telegram_api_token', '');
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` ADD `pushover_key` VARCHAR( 255 ) NOT NULL AFTER `mobile`;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` ADD `pushover_key` VARCHAR( 255 ) NOT NULL AFTER `mobile`;";
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` ADD `pushover_device` VARCHAR( 255 ) NOT NULL AFTER `pushover_key`;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` ADD `pushover_device` VARCHAR( 255 ) NOT NULL AFTER `pushover_key`;";
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` ADD `telegram_chat_id` VARCHAR( 255 ) NOT NULL AFTER `pushover_device`;";
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `pushover` ENUM( 'yes','no' ) NOT NULL DEFAULT 'yes' AFTER `sms`;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `pushover` ENUM( 'yes','no' ) NOT NULL DEFAULT 'yes' AFTER `sms`;";
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "log` CHANGE `type` `type` ENUM( 'status', 'email', 'sms', 'pushover' ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `telegram` ENUM( 'yes','no' ) NOT NULL DEFAULT 'yes' AFTER `pushover`;";
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "log` CHANGE `type` `type` ENUM( 'status', 'email', 'sms', 'pushover', 'telegram' ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;";
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `timeout` smallint(1) unsigned NULL DEFAULT NULL;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `timeout` smallint(1) unsigned NULL DEFAULT NULL;";
@ -478,4 +488,4 @@ class Installer {
// Drop old user_id('s) column // Drop old user_id('s) column
$this->execSQL("ALTER TABLE `" . PSM_DB_PREFIX . "log` DROP COLUMN `user_id`;"); $this->execSQL("ALTER TABLE `" . PSM_DB_PREFIX . "log` DROP COLUMN `user_id`;");
} }
} }

View File

@ -56,7 +56,7 @@ class UpdateManager extends ContainerAware {
)"; )";
} }
$sql = "SELECT `s`.`server_id`,`s`.`ip`,`s`.`port`,`s`.`label`,`s`.`type`,`s`.`pattern`,`s`.`status`,`s`.`active`,`s`.`email`,`s`.`sms`,`s`.`pushover` $sql = "SELECT `s`.`server_id`,`s`.`ip`,`s`.`port`,`s`.`label`,`s`.`type`,`s`.`pattern`,`s`.`status`,`s`.`active`,`s`.`email`,`s`.`sms`,`s`.`pushover`,`s`.`telegram`
FROM `".PSM_DB_PREFIX."servers` AS `s` FROM `".PSM_DB_PREFIX."servers` AS `s`
{$sql_join} {$sql_join}
WHERE `active`='yes' "; WHERE `active`='yes' ";

View File

@ -33,6 +33,7 @@
*/ */
namespace psm\Util\Server\Updater; namespace psm\Util\Server\Updater;
use psm\Service\Database; use psm\Service\Database;
use psm\Telegram;
class StatusNotifier { class StatusNotifier {
@ -55,11 +56,17 @@ class StatusNotifier {
protected $send_sms = false; protected $send_sms = false;
/** /**
* Send sms? * Send pushover?
* @var boolean $send_pushover * @var boolean $send_pushover
*/ */
protected $send_pushover = false; protected $send_pushover = false;
/**
* Send telegram?
* @var boolean $send_telegram
*/
protected $send_telegram = false;
/** /**
* Save log records? * Save log records?
* @var boolean $save_log * @var boolean $save_log
@ -96,6 +103,7 @@ class StatusNotifier {
$this->send_emails = psm_get_conf('email_status'); $this->send_emails = psm_get_conf('email_status');
$this->send_sms = psm_get_conf('sms_status'); $this->send_sms = psm_get_conf('sms_status');
$this->send_pushover = psm_get_conf('pushover_status'); $this->send_pushover = psm_get_conf('pushover_status');
$this->send_telegram = psm_get_conf('telegram_status');
$this->save_logs = psm_get_conf('log_status'); $this->save_logs = psm_get_conf('log_status');
} }
@ -121,7 +129,7 @@ class StatusNotifier {
$this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array( $this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array(
'server_id' => $server_id, 'server_id' => $server_id,
), array( ), array(
'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'error', 'active', 'email', 'sms', 'pushover', 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'error', 'active', 'email', 'sms', 'pushover', 'telegram',
)); ));
if(empty($this->server)) { if(empty($this->server)) {
return false; return false;
@ -188,6 +196,12 @@ class StatusNotifier {
$this->notifyByPushover($users); $this->notifyByPushover($users);
} }
// check if telegram is enabled for this server
if($this->send_telegram && $this->server['telegram'] == 'yes') {
// yay lets wake those nerds up!
$this->notifyByTelegram($users);
}
return $notify; return $notify;
} }
@ -277,6 +291,46 @@ class StatusNotifier {
$pushover->send(); $pushover->send();
} }
} }
/**
* This functions performs the telegram notifications
*
* @param array $users
* @return boolean
*/
protected function notifyByTelegram($users) {
// Remove users that have no telegram_chat_id
foreach($users as $k => $user) {
if (trim($user['telegram_chat_id']) == '') {
unset($users[$k]);
}
}
// Validation
if (empty($users)) {
return;
}
// Telegram
$message = psm_parse_msg($this->status_new, 'telegram_message', $this->server);
$message = str_replace('<br/>', "\n", $message);
// Log
if(psm_get_conf('log_telegram')) {
$log_id = psm_add_log($this->server_id, 'telegram', $message);
}
foreach($users as $user) {
// Log
if(!empty($log_id)) {
psm_add_log_user($log_id, $user['user_id']);
}
$bot_token = psm_get_conf('telegram_api_token');
$telegram = new Telegram($bot_token);
$chat_id = $user['telegram_chat_id'];
$content = array('chat_id' => $chat_id, 'text' => $message);
$telegram->sendMessage($content);
}
}
/** /**
* This functions performs the text message notifications * This functions performs the text message notifications

View File

@ -5,6 +5,7 @@
<li class="{{ email_active }}"><a href="#config-email" data-toggle="tab">{{ label_tab_email }}</a></li> <li class="{{ email_active }}"><a href="#config-email" data-toggle="tab">{{ label_tab_email }}</a></li>
<li class="{{ sms_active }}"><a href="#config-sms" data-toggle="tab">{{ label_tab_sms }}</a></li> <li class="{{ sms_active }}"><a href="#config-sms" data-toggle="tab">{{ label_tab_sms }}</a></li>
<li class="{{ pushover_active }}"><a href="#config-pushover" data-toggle="tab">{{ label_tab_pushover }}</a></li> <li class="{{ pushover_active }}"><a href="#config-pushover" data-toggle="tab">{{ label_tab_pushover }}</a></li>
<li class="{{ telegram_active }}"><a href="#config-telegram" data-toggle="tab">{{ label_tab_telegram }}</a></li>
</ul> </ul>
<div class="tab-content well"> <div class="tab-content well">
<div id="config-general" class="tab-pane {{ general_active }}"> <div id="config-general" class="tab-pane {{ general_active }}">
@ -265,6 +266,38 @@
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div id="config-telegram" class="tab-pane {{ telegram_active }}">
<fieldset>
<legend>{{ label_settings_telegram }}</legend>
<div class="control-group">
<div class="controls">
<label class="checkbox" for="telegram_status"><input type="checkbox" id="telegram_status" name="telegram_status[]" {{ telegram_status_checked|raw }} /> {{ label_telegram_status }}</label>
<p class="help-block">{{ label_telegram_description|raw }}</p>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox"><input type="checkbox" id="log_telegram" name="log_telegram[]" {{ log_telegram_checked|raw }} /> {{ label_log_telegram }}</label>
</div>
</div>
<div class="control-group">
<label class="control-label" for="telegram_api_token">{{ label_telegram_api_token }}</label>
<div class="controls">
<input type="text" id="telegram_api_token" name="telegram_api_token" value="{{ telegram_api_token }}" maxlength="255" />
<p class="help-block">{{ label_telegram_api_token_description|raw }}</p>
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-primary show-modal" data-modal-id="testtelegram">{{ label_test }}</button>
<input type="hidden" name="test_telegram" value="0" />
</div>
</div>
<div class="form-actions">
<button class="btn btn-success" type="submit" name="telegram_submit">{{ label_save }}</button>
</div>
</fieldset>
</div>
</div> </div>
{{ macro.csrf_input() }} {{ macro.csrf_input() }}
</form> </form>

View File

@ -4,6 +4,7 @@
<li><a href="#log_email_content" data-toggle="tab">{{ label_email }}</a></li> <li><a href="#log_email_content" data-toggle="tab">{{ label_email }}</a></li>
<li><a href="#log_sms_content" data-toggle="tab">{{ label_sms }}</a></li> <li><a href="#log_sms_content" data-toggle="tab">{{ label_sms }}</a></li>
<li><a href="#log_pushover_content" data-toggle="tab">{{ label_pushover }}</a></li> <li><a href="#log_pushover_content" data-toggle="tab">{{ label_pushover }}</a></li>
<li><a href="#log_telegram_content" data-toggle="tab">{{ label_telegram }}</a></li>
</ul> </ul>
<div class="tab-content well"> <div class="tab-content well">
{% for tab in tabs %} {% for tab in tabs %}

View File

@ -23,7 +23,7 @@
<td class="nowrap visible-desktop"><div class="table-cell-title">{{ server.type }}</div></td> <td class="nowrap visible-desktop"><div class="table-cell-title">{{ server.type }}</div></td>
<td class="nowrap hidden-phone"><div class="table-cell-title">{{ server.rtime }} s</div></td> <td class="nowrap hidden-phone"><div class="table-cell-title">{{ server.rtime }} s</div></td>
<td class="nowrap hidden-phone"><div class="table-cell-title">{{ server.last_online }}</div></td> <td class="nowrap hidden-phone"><div class="table-cell-title">{{ server.last_online }}</div></td>
<td class="tight hidden-phone"><div class="table-cell-title"><i class="{{ server.active_icon }}" title="{{ server.active_title }}"></i> <i class="{{ server.email_icon }}" title="{{ label_email }}"></i> <i class="{{ server.sms_icon }}" title="{{ label_sms }}"></i> <i class="{{ server.pushover_icon }}" title="Pushover"></i></div></td> <td class="tight hidden-phone"><div class="table-cell-title"><i class="{{ server.active_icon }}" title="{{ server.active_title }}"></i> <i class="{{ server.email_icon }}" title="{{ label_email }}"></i> <i class="{{ server.sms_icon }}" title="{{ label_sms }}"></i> <i class="{{ server.pushover_icon }}" title="Pushover"></i> <i class="{{ server.telegram_icon }}" title="Telegram"></i> </div></td>
<td class="tight hidden-phone"> <td class="tight hidden-phone">
<div class="table-cell-title"> <div class="table-cell-title">
<a class="btn btn-small" href="{{ server.url_view|raw }}"><i class="icon-chart"></i></a> <a class="btn btn-small" href="{{ server.url_view|raw }}"><i class="icon-chart"></i></a>
@ -51,7 +51,7 @@
<div class="table-row"> <div class="table-row">
<div class="table-cell"><i class="{{ server.type_icon }}" title="{{ server.type }}"></i> {{ server.ip_short|raw }}</div> <div class="table-cell"><i class="{{ server.type_icon }}" title="{{ server.type }}"></i> {{ server.ip_short|raw }}</div>
<div class="table-cell tight"> <div class="table-cell tight">
<i class="{{ server.active_icon }}" title="{{ server.active_title }}"></i> <i class="{{ server.email_icon }}" title="{{ label_email }}"></i> <i class="{{ server.sms_icon }}" title="{{ label_sms }}"></i> <i class="{{ server.pushover_icon }}" title="Pushover"></i> <i class="{{ server.active_icon }}" title="{{ server.active_title }}"></i> <i class="{{ server.email_icon }}" title="{{ label_email }}"></i> <i class="{{ server.sms_icon }}" title="{{ label_sms }}"></i> <i class="{{ server.pushover_icon }}" title="Pushover"></i> <i class="{{ server.telegram_icon }}" title="Telegram"></i>
</div> </div>
</div> </div>
</div> </div>

View File

@ -139,6 +139,17 @@
</select> </select>
</div> </div>
</div> </div>
<div class="control-group {{ control_class_telegram }}">
<label class="control-label" for="telegram">{{ label_telegram }}
{% if warning_telegram %} <p class="help-inline"><i class="icon-warning-sign" data-toggle="tooltip" title="{{ label_warning_telegram }}"></i></p> {% endif %}
</label>
<div class="controls">
<select id="telegram" name="telegram">
<option value="yes" {{ edit_telegram_selected_yes|raw }}>{{ label_yes }}</option>
<option value="no" {{ edit_telegram_selected_no|raw }}>{{ label_no }}</option>
</select>
</div>
</div>
</fieldset> </fieldset>
<fieldset> <fieldset>

View File

@ -69,6 +69,10 @@
<td>{{ label_pushover }}:</td> <td>{{ label_pushover }}:</td>
<td>{{ pushover }}</td> <td>{{ pushover }}</td>
</tr> </tr>
<tr>
<td>{{ label_telegram }}:</td>
<td>{{ telegram }}</td>
</tr>
<tr> <tr>
<td>{{ label_timeout }}:</td> <td>{{ label_timeout }}:</td>
<td>{{ timeout }} s</td> <td>{{ timeout }} s</td>

View File

@ -62,6 +62,17 @@
<input type="text" id="pushover_device" name="pushover_device" value="{{ pushover_device }}" maxlength="255" data-toggle="tooltip" title="{{ label_pushover_device_description }}" /> <input type="text" id="pushover_device" name="pushover_device" value="{{ pushover_device }}" maxlength="255" data-toggle="tooltip" title="{{ label_pushover_device_description }}" />
</div> </div>
</div> </div>
<div class="control-group">
<label class="control-label">{{ label_telegram }}</label>
<div class="controls">{{ label_telegram_description|raw }} </div>
</div>
<div class="control-group">
<label class="control-label" for="telegram_chat_id">{{ label_telegram_get_id }}</label>
<div class="controls">
<p><button class="btn btn-primary" onclick="window.open('https://t.me/ChatIDBot');return false;">{{ label_telegram_get_id_button }}</button></p>
<input type="text" id="telegram_chat_id" name="telegram_chat_id" value="{{ telegram_chat_id }}" maxlength="255" />
</div>
</div>
</div> </div>
</div> </div>
<div class="row-fluid"> <div class="row-fluid">

View File

@ -1,4 +1,4 @@
{% import 'main/macros.tpl.html' as macro %} {% import 'main/macros.tpl.html' as macro %}
<form class="form-horizontal well" action="{{ url_save|raw }}" method="post"> <form class="form-horizontal well" action="{{ url_save|raw }}" method="post">
{{ macro.csrf_input() }} {{ macro.csrf_input() }}
<fieldset> <fieldset>
@ -55,17 +55,27 @@
<div class="controls">{{ label_pushover_description|raw }} </div> <div class="controls">{{ label_pushover_description|raw }} </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<label class="control-label" for="mobile">{{ label_pushover_key }}</label> <label class="control-label" for="pushover_key">{{ label_pushover_key }}</label>
<div class="controls"> <div class="controls">
<input type="text" id="pushover_key" name="pushover_key" value="{{ edit_value_pushover_key }}" maxlength="255" /> <input type="text" id="pushover_key" name="pushover_key" value="{{ edit_value_pushover_key }}" maxlength="255" />
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<label class="control-label" for="mobile">{{ label_pushover_device }}</label> <label class="control-label" for="pushover_device">{{ label_pushover_device }}</label>
<div class="controls"> <div class="controls">
<input type="text" id="pushover_device" name="pushover_device" value="{{ edit_value_pushover_device }}" maxlength="255" data-toggle="tooltip" title="{{ label_pushover_device_description }}" /> <input type="text" id="pushover_device" name="pushover_device" value="{{ edit_value_pushover_device }}" maxlength="255" data-toggle="tooltip" title="{{ label_pushover_device_description }}" />
</div> </div>
</div> </div>
<div class="control-group">
<label class="control-label">{{ label_telegram }}</label>
<div class="controls">{{ label_telegram_description|raw }} </div>
</div>
<div class="control-group">
<label class="control-label" for="telegram_chat_id">{{ label_telegram_chat_id }}</label>
<div class="controls">
<input type="text" id="telegram_chat_id" name="telegram_chat_id" value="{{ edit_value_telegram_chat_id }}" maxlength="255" />
</div>
</div>
<div class="control-group"> <div class="control-group">
<label class="control-label" for="server_id">{{ label_servers }}</label> <label class="control-label" for="server_id">{{ label_servers }}</label>
<div class="controls"> <div class="controls">