Fix bug notifications not being sent.

The code to handle sending of notification was missing. This change
makes two different options available on how notifications will be
handled which will be controlled by CRON_SERVER_IP config option.

 - If CRON_SERVER_IP is set, the server with the given IP should
   call URL ../admin/?task=cron every x minutes. If the config
   is left empty, the notification will be called once the
   incident has  been saved. (The latter meothod might cause
   server timeout if there are large numbers of subscribers!)

Other minor changes:

 - Removed old commented code

 - Removed call to syslog used for debugging
This commit is contained in:
thnilsen 2020-08-15 21:09:57 +02:00
parent 8d31ec7f7c
commit ea582aeed6
6 changed files with 100 additions and 144 deletions

View File

@ -11,6 +11,30 @@ else{
require_once("../classes/notification.php");
require_once("../template.php");
require_once("../libs/parsedown/Parsedown.php");
require_once("../classes/queue.php");
// Process the subscriber notification queue
// If CRON_SERVER_IP is not set, call notification once incident has been saved
if ( empty(CRON_SERVER_IP) )
{
if ( isset($_GET['sent']) && $_GET['sent'] == true )
{
Queue::process_queue();
}
}
else if ( isset($_GET['task']) && $_GET['task'] == 'cron' )
{
// Else, base it on call to /admin?task=cron being called from IP defined by CRON_SERVER_IP
if (! empty(CRON_SERVER_IP) && $_SERVER['REMOTE_ADDR'] == CRON_SERVER_IP )
{
Queue::process_queue();
syslog(1, "CRON server processed");
}
else {
syslog(1, "CRON called from unauthorised server");
}
}
if(isset($_COOKIE['user'])&&!isset($_SESSION['user']))
{

View File

@ -72,7 +72,7 @@ class Notification
$queue->template_data1 = $arr_data['subject'];
$queue->template_data2 = $arr_data['body'];
$task_id_email = $queue->add_task();
syslog(1, "queue email: ". $task_id_email);
//syslog(1, "queue email: ". $task_id_email);
$arr_email = array();
}
if ( SUBSCRIBE_TELEGRAM ) {
@ -81,7 +81,7 @@ class Notification
$queue->template_data1 = null;
$queue->template_data2 = $arr_data['body'];
$task_id_telegram = $queue->add_task();
syslog(1, "queue telegram: ". $task_id_telegram);
//syslog(1, "queue telegram: ". $task_id_telegram);
$arr_telegram = array();
}
@ -116,32 +116,6 @@ class Notification
$queue->task_id = $task_id_email;
$queue->add_notification($arr_email); // Add array of Email users to the notification queue list
}
/* OLD CODE to get user email/telegram data) - Move to queue handler...
while ($subscriber = $query->fetch_assoc()) {
// Fetch list of subscriber details for already found subscriber IDs
$stmt = $mysqli->prepare("SELECT typeID, userID, firstname, token FROM subscribers WHERE subscriberID = ? AND active=1");
$stmt->bind_param("i", $subscriber['subscriberIDFK']);
$stmt->execute();
$subscriberQuery = $stmt->get_result();
while ($subscriberData = $subscriberQuery->fetch_assoc()) {
$typeID = $subscriberData['typeID']; // Telegram = 1, email = 2
$userID = $subscriberData['userID'];
$firstname = $subscriberData['firstname'];
$token = $subscriberData['token'];
// Handle telegram
if ($typeID == 1) {
$this->submit_telegram($userID, $firstname);
}
// Handle email
if ($typeID == 2) {
$this->submit_email($userID, $token);
}
}
}*/
}
/**
@ -163,11 +137,9 @@ class Notification
$response = json_decode($json, true);
if (!is_array($response) || ! array_key_exists("ok", $response) || $response['ok'] != 1 ) {
syslog(1, "telegram failed: ".$userID);
return false;
}
syslog(1,"telegram ok: " .$userID);
return true;
}
@ -181,52 +153,13 @@ class Notification
public function submit_queue_email($subscriber, $subject, $msg)
{
// TODO Error handling
//$Parsedown = new Parsedown();
$mailer = new Mailer();
if ( ! $mailer->send_mail($subscriber, $subject, $msg, true) ) {
syslog(1, "email failed: " .$subscriber);
return false;
}
syslog(1, "email ok: " .$subscriber);
return true;
}
// /**
// * Sends email notifications to a subscriber.
// * Function depends on Parsedown and Mailer class being loaded.
// * @param String $userID The email address to send to
// * @param String $uthkey Users token for managing subscription
// * @return void
// */
// public function submit_email_old($userID, $token)
// {
// // TODO Error handling
// //$Parsedown = new Parsedown();
// $mailer = new Mailer();
//
// $str_mail = file_get_contents("../libs/templates/email_status_update.html");
// $str_mail = str_replace("%name%", NAME, $str_mail);
// // $smtp_mail = str_replace("%email%", $userID, $smtp_mail);
// $str_mail = str_replace("%url%", WEB_URL, $str_mail);
// $str_mail = str_replace("%service%", $this->servicenames, $str_mail);
// $str_mail = str_replace("%status%", $this->status, $str_mail);
// $str_mail = str_replace("%time%", date("c", $this->time), $str_mail);
// $str_mail = str_replace("%comment%", $Parsedown->setBreaksEnabled(true)->text($this->text), $str_mail);
// $str_mail = str_replace("%token%", $token, $str_mail);
//
// $str_mail = str_replace("%service_status_update_from%", _("Service status update from"), $str_mail);
// $str_mail = str_replace("%services_impacted%", _("Service(s) Impacted"), $str_mail);
// $str_mail = str_replace("%status_label%", _("Status"), $str_mail);
// $str_mail = str_replace("%time_label%", _("Time"), $str_mail);
// $str_mail = str_replace("%manage_subscription%", _("Manage subscription"), $str_mail);
// $str_mail = str_replace("%unsubscribe%", _("Unsubscribe"), $str_mail);
// $str_mail = str_replace("%powered_by%", _("Powered by"), $str_mail);
//
// $subject = _('Status update from') . ' - ' . NAME . ' [ ' . $this->status . ' ]';
// $mailer->send_mail($userID, $subject, $str_mail);
// }
//
public function prepare_email(){
$Parsedown = new Parsedown();

View File

@ -93,7 +93,6 @@ class Queue
}
$sql = "INSERT INTO queue_notify (task_id, status, subscriber_id, retries) VALUES ". $query;
syslog(1,$sql);
$mysqli->query($sql);
$this->set_task_status($this->all_status['ready']); // Make task available for release
@ -127,7 +126,6 @@ class Queue
$tmp = $stmt2->get_result();
$result2 = $tmp->fetch_assoc();
$typeID = $result2['type_id'];
syslog(1, $typeID . " " . $result['task_id'] . " " . $result['userID']);
// Handle telegram
if ($typeID == 1) {

View File

@ -111,7 +111,7 @@ Class Subscriber
$expireTime = strtotime("+2 hours");
$updateTime = strtotime("now");
$token = $this->generate_token();
syslog(1,"token". $token);
$stmt = $mysqli->prepare("INSERT INTO subscribers (typeID, userID, firstname, lastname, token, active, expires, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("issssiii", $typeID, $userID, $firstname, $lastname, $token, $active, $expireTime, $updateTime);
$stmt->execute();

View File

@ -51,6 +51,7 @@ define("PHP_MAILER_USER", "##phpmailer_user##"); // SMTP Authentication user
define("PHP_MAILER_PASS", "##phpmailer_pass##"); // SMTP authenticatin password
define("CUSTOM_LOGO_URL",""); // This will use the default logo if left empty
define("COPYRIGHT_TEXT",""); // Leave this empty if you don't want your copyright displayed
define("CRON_SERVER_IP",""); // IP Address that will be used to call cron task. Leave empty if not used!
// Without COPYRIGHT_TEXT Set
// 2020 Server Status Project Contributors
// With COPYRIGHT_TEXT Set