From 586c01136e61944de780501a04e30cad81d5d22e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ma=C5=82ek?= Date: Mon, 18 May 2020 00:28:15 +0200 Subject: [PATCH 1/5] SMSAPI gateway --- docs/credits.rst | 4 + src/includes/functions.inc.php | 3 + src/psm/Txtmsg/SMSAPI.php | 150 +++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100755 src/psm/Txtmsg/SMSAPI.php diff --git a/docs/credits.rst b/docs/credits.rst index 6786fb1d..75a1aadc 100644 --- a/docs/credits.rst +++ b/docs/credits.rst @@ -85,6 +85,10 @@ The following people have contributed to the development of PHP Server Monitor: * Nexmo SMS gateway +* Mateusz Małek - https://github.com/mateuszmalek + + * SMSAPI gateway + Translators +++++++++++ diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index 8fb1c449..a4398029 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -779,6 +779,9 @@ namespace { case 'solutionsinfini': $sms = new \psm\Txtmsg\SolutionsInfini(); break; + case 'smsapi': + $sms = new \psm\Txtmsg\SMSAPI(); + break; } // copy login information from the config file diff --git a/src/psm/Txtmsg/SMSAPI.php b/src/psm/Txtmsg/SMSAPI.php new file mode 100755 index 00000000..e446c169 --- /dev/null +++ b/src/psm/Txtmsg/SMSAPI.php @@ -0,0 +1,150 @@ +. + * + * @package phpservermon + * @author Mateusz Małek + * @copyright Copyright (c) 2008-2017 Pepijn Over + * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 + * @version Release: @package_version@ + * @link http://www.phpservermonitor.org/ + * @since phpservermon 3.5 + **/ + +namespace psm\Txtmsg; + +class SMSAPI extends Core +{ + const VARIANT_INTERNATIONAL = 1; + const VARIANT_POLISH = 2; + + /** + * SMSAPI comes with two variants - designed for polish or international customers. + * + * @var int + */ + private $variant = self::VARIANT_INTERNATIONAL; + + /** + * Name of the sender. As a default the sender name is set to "Test". + * Only verified names are being accepted. + * Sender name may be set after logging into Customer Portal on Sendernames. + * @see https://www.smsapi.com/docs/#2-single-sms + * + * @var string + */ + protected $originator; + + /** + * Token used to authenticate in SMSAPI system. + * @see https://www.smsapi.com/docs/#authentication + * + * @var string + */ + protected $password; + + /** + * Send sms using the SMSAPI + * + * @var string $message + * @var array $this->recipients + * @var array $this->originator + * @var string $this->password + * @var array $recipients_chunk + * @var string $host + * + * @var mixed $result + * @var array $headers + * + * @var int $success + * @var string $error + * + * @return bool|string + */ + + public function sendSMS($message) + { + $tld = ($this->variant === static::VARIANT_INTERNATIONAL) ? "com" : "pl"; + $host = "api.smsapi.{$tld}"; + $backupHost = "api2.smsapi.{$tld}"; + + // One user at a time. + $recipients_chunk = array_chunk($this->recipients, 1); + foreach ($recipients_chunk as $recipient) + { + try { + $response = $this->processSendOperation($host, $recipient, $message); + } catch (\RuntimeException $e) { + try { + $response = $this->processSendOperation($backupHost, $recipient, $message); + } catch (\RuntimeException $e) { + return "({$recipient}) " . $e->getMessage(); + } + } + + if (isset($response->error)) { + return $response->message; + } + + return 1; + } + } + + /** + * Perform actual SMS sending operation + * + * @param $host + * @param $recipient + * @param $message + * @return object + * @throws RuntimeException + */ + private function processSendOperation($host, $recipient, $message) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "https://{$host}/sms.do"); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( + "access_token" => $this->password, + "from" => $this->originator, + "to" => $recipient, + "message" => $message, + "encoding" => "utf-8", + "normalize" => "1", + "format" => "json" + ))); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + + $result = curl_exec($ch); + + $error = false; + if (curl_errno($ch)) { + $error = curl_error($ch); + } + + curl_close($ch); + + if ($error !== false) { + throw new \RuntimeException($error); + } + + return json_decode($result); + } +} From 72de1479fa92c72f6502a16b5a0a37e76a05697a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ma=C5=82ek?= Date: Mon, 18 May 2020 09:10:16 +0200 Subject: [PATCH 2/5] Add SMSAPI to list of supported SMS gateways --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index 11837483..52c184f7 100644 --- a/README.rst +++ b/README.rst @@ -60,6 +60,7 @@ The following SMS gateways are currently available: * SolutionsInfini - * Plivo - * Callr - +* SMSAPI - From 875b0208b59c01f2bde27fdbfd0d2d309668f14c Mon Sep 17 00:00:00 2001 From: TimZ99 Date: Mon, 18 May 2020 19:41:08 +0200 Subject: [PATCH 3/5] Indentation --- src/psm/Txtmsg/SMSAPI.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/psm/Txtmsg/SMSAPI.php b/src/psm/Txtmsg/SMSAPI.php index e446c169..83601e05 100755 --- a/src/psm/Txtmsg/SMSAPI.php +++ b/src/psm/Txtmsg/SMSAPI.php @@ -86,8 +86,7 @@ class SMSAPI extends Core // One user at a time. $recipients_chunk = array_chunk($this->recipients, 1); - foreach ($recipients_chunk as $recipient) - { + foreach ($recipients_chunk as $recipient) { try { $response = $this->processSendOperation($host, $recipient, $message); } catch (\RuntimeException $e) { From 489465389113d1e829adf968527045432ee5a035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ma=C5=82ek?= Date: Mon, 18 May 2020 19:42:24 +0200 Subject: [PATCH 4/5] Show recent status logs on server details page (#921) --- .../Server/Controller/ServerController.php | 51 +++++++++++++++++++ .../module/server/server/view.tpl.html | 29 +++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/psm/Module/Server/Controller/ServerController.php b/src/psm/Module/Server/Controller/ServerController.php index 1c2cb299..28c03f8d 100644 --- a/src/psm/Module/Server/Controller/ServerController.php +++ b/src/psm/Module/Server/Controller/ServerController.php @@ -518,6 +518,15 @@ class ServerController extends AbstractServerController if (strlen($tpl_data['last_error_output']) > 255) { $tpl_data['last_error_output_truncated'] = substr($tpl_data['last_error_output'], 0, 255) . '...'; } + + // fetch server status logs + $log_entries = $this->getServerLogs($this->server_id); + for ($x = 0; $x < count($log_entries); $x++) { + $record = &$log_entries[$x]; + $record['datetime_format'] = psm_date($record['datetime']); + } + + $tpl_data['log_entries'] = $log_entries; return $this->twig->render('module/server/server/view.tpl.html', $tpl_data); } @@ -606,6 +615,10 @@ class ServerController extends AbstractServerController 'label_settings' => psm_get_lang('system', 'settings'), 'label_output' => psm_get_lang('servers', 'output'), 'label_search' => psm_get_lang('system', 'search'), + 'label_log_title' => psm_get_lang('log', 'title'), + 'label_log_no_logs' => psm_get_lang('log', 'no_logs'), + 'label_date' => psm_get_lang('system', 'date'), + 'label_message' => psm_get_lang('system', 'message'), ); } @@ -627,4 +640,42 @@ class ServerController extends AbstractServerController } return $result; } + + /** + * Get logs for a server + * @param int $server_id + * @param string $type status/email/sms + * @return \PDOStatement array + */ + protected function getServerLogs($server_id, $type = 'status') + { + $sql_join = ''; + if ($this->getUser()->getUserLevel() > PSM_USER_ADMIN) { + // restrict by user_id + $sql_join = "JOIN `" . PSM_DB_PREFIX . "users_servers` AS `us` ON ( + `us`.`user_id`={$this->getUser()->getUserId()} + AND `us`.`server_id`=`servers`.`server_id` + )"; + } + $entries = $this->db->query( + 'SELECT ' . + '`servers`.`label`, ' . + '`servers`.`ip`, ' . + '`servers`.`port`, ' . + '`servers`.`type` AS server_type, ' . + '`log`.`log_id`, ' . + '`log`.`type`, ' . + '`log`.`message`, ' . + '`log`.`datetime` ' . + 'FROM `' . PSM_DB_PREFIX . 'log` AS `log` ' . + 'JOIN `' . PSM_DB_PREFIX . 'servers` AS `servers` ON (`servers`.`server_id`=`log`.`server_id`) ' . + $sql_join . + 'WHERE `log`.`type`=\'' . $type . '\' ' . + 'AND `log`.`server_id`=' . $server_id . ' ' . + 'ORDER BY `datetime` DESC ' . + 'LIMIT 0,20' + ); + + return $entries; + } } diff --git a/src/templates/default/module/server/server/view.tpl.html b/src/templates/default/module/server/server/view.tpl.html index 20218480..7b22a0f0 100644 --- a/src/templates/default/module/server/server/view.tpl.html +++ b/src/templates/default/module/server/server/view.tpl.html @@ -354,6 +354,35 @@
{{ html_history|raw }}
+ {% if log_entries %} +
+
+
+ {{ label_log_title }} +
+
+
+ + + + + + + + + {% for entry in log_entries %} + + + + + {% endfor %} + +
{{ label_date }}{{ label_message }}
{{ entry.message|raw }}
+
+
+
+
+ {% endif %}