mirror of
https://github.com/ShaYmez/FreeSTAR-Status-Engine.git
synced 2025-05-23 17:52:26 -04:00
Added queue to handle sending of subscribers notifications
This commit is contained in:
parent
62e6f08e4f
commit
e3d8e3ecf8
@ -1,4 +1,4 @@
|
||||
<?php
|
||||
<?php
|
||||
$offset = 0;
|
||||
if (isset($_GET['ajax']))
|
||||
{
|
||||
@ -18,8 +18,11 @@ if (isset($_GET['delete']))
|
||||
{
|
||||
Incident::delete($_GET['delete']);
|
||||
}
|
||||
if (isset($_GET['tasks'])) {
|
||||
Queue::process_queue();
|
||||
}
|
||||
|
||||
Template::render_header(_("Dashboard"), true);
|
||||
Template::render_header(_("Dashboard"), true);
|
||||
?>
|
||||
|
||||
<div class="text-center">
|
||||
@ -45,7 +48,7 @@ Template::render_header(_("Dashboard"), true);
|
||||
<?php
|
||||
} ?>
|
||||
<div id="status-container" class="clearfix">
|
||||
<?php
|
||||
<?php
|
||||
if (isset($_POST['services']) && !is_array($_POST['services']))
|
||||
{
|
||||
$post_services = array($_POST['services']);
|
||||
@ -82,7 +85,7 @@ Template::render_header(_("Dashboard"), true);
|
||||
</div>
|
||||
</div>
|
||||
<select class="form-control pull-left" id="type" name="type">
|
||||
<?php
|
||||
<?php
|
||||
if (isset($_POST['type']))
|
||||
{
|
||||
$selected_status = $_POST['type'];
|
||||
|
@ -9,18 +9,18 @@ use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
// TODO Any other way to handle this include? Should we just do the include form each index.php?
|
||||
if (file_exists("libs/php_idn/idna.php")) {
|
||||
require_once("libs/php_idn/idna.php");
|
||||
require_once("libs/php_idn/idna.php");
|
||||
} else {
|
||||
// Include for Admin section
|
||||
require_once("../libs/php_idn/idna.php");
|
||||
require_once("../libs/php_idn/idna.php");
|
||||
}
|
||||
|
||||
class Mailer {
|
||||
|
||||
|
||||
public function __construct(){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generic function to submit mail messages via mail og PHPmailer()
|
||||
* @param String $to Email address to which mail should be sent
|
||||
@ -29,44 +29,43 @@ class Mailer {
|
||||
* @return boolean True if success
|
||||
*/
|
||||
public function send_mail($to, $subject, $message, $html = true) {
|
||||
// TODO -Handle $to as an array in order to send to muliple recipients without having
|
||||
// to call the entire send_mail function over and over..
|
||||
// TODO -Handle $to as an array in order to send to muliple recipients without having
|
||||
// to call the entire send_mail function over and over..
|
||||
|
||||
$content_type = ($html) ? 'text/html' : 'text/plain';
|
||||
|
||||
|
||||
// Convert IDN/punycode domain to ascii
|
||||
// TODO Handle IDN in left hand side of email address
|
||||
if ( $this->is_utf8($to) ) {
|
||||
$elements = explode('@', $to);
|
||||
$domainpart = EncodePunycodeIDN(array_pop($elements)); // Convert domain part to ascii
|
||||
$to = $elements[0] . '@' . $domainpart; // Reassemble tge full email address
|
||||
syslog(1,"email: " .$to);
|
||||
}
|
||||
|
||||
|
||||
// Send using PHP mailer if it is enabled
|
||||
if ( PHP_MAILER ) {
|
||||
require_once(PHP_MAILER_PATH .'/Exception.php'); /* Exception class. */
|
||||
require_once(PHP_MAILER_PATH .'/PHPMailer.php'); /* The main PHPMailer class. */
|
||||
|
||||
|
||||
if ( PHP_MAILER_SMTP ) {
|
||||
require_once(PHP_MAILER_PATH .'/SMTP.php'); /* SMTP class, needed if you want to use SMTP. */
|
||||
}
|
||||
|
||||
|
||||
$phpmail = new PHPMailer(false);
|
||||
|
||||
$phpmail->setFrom(MAILER_ADDRESS, MAILER_NAME);
|
||||
$phpmail->addReplyTo(MAILER_ADDRESS, MAILER_NAME);
|
||||
//$phpmail->Debugoutput = error_log;
|
||||
|
||||
// Define SMTP parameters if enabled
|
||||
|
||||
// Define SMTP parameters if enabled
|
||||
if ( PHP_MAILER_SMTP ) {
|
||||
|
||||
$phpmail->isSMTP();
|
||||
|
||||
$phpmail->isSMTP();
|
||||
$phpmail->Host = PHP_MAILER_HOST;
|
||||
$phpmail->Port = PHP_MAILER_PORT;
|
||||
$phpmail->SMTPSecure = PHP_MAILER_SECURE;
|
||||
//$phpmail->SMTPDebug = 2; // Enable for debugging
|
||||
|
||||
|
||||
// Handle authentication for SMTP if enabled
|
||||
if ( !empty(PHP_MAILER_USER) ) {
|
||||
$phpmail->SMTPAuth = true;
|
||||
@ -74,41 +73,41 @@ class Mailer {
|
||||
$phpmail->Password = PHP_MAILER_PASS;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$phpmail->addAddress($to);
|
||||
$phpmail->Subject = $subject;
|
||||
// Send HMTL mail
|
||||
// Send HMTL mail
|
||||
if ( $html ) {
|
||||
$phpmail->msgHtml($message);
|
||||
$phpmail->AltBody = $this->convert_html_to_plain_txt($message, false);
|
||||
$phpmail->AltBody = $this->convert_html_to_plain_txt($message, false);
|
||||
} else {
|
||||
$phpmail->Body = $message; // Send plain text
|
||||
}
|
||||
|
||||
|
||||
$phpmail->isHtml($html); // use htmlmail if enabled
|
||||
if ( ! $phpmail->send() ) {
|
||||
// TODO Log error message $phpmail->ErrorInfo;
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
} else {
|
||||
// Use standard PHP mail() function
|
||||
$headers = "Content-Type: $content_type; \"charset=utf-8\" ".PHP_EOL;
|
||||
$headers .= "MIME-Version: 1.0 ".PHP_EOL;
|
||||
$headers .= "From: ".MAILER_NAME.' <'.MAILER_ADDRESS.'>'.PHP_EOL;
|
||||
$headers .= "Reply-To: ".MAILER_NAME.' <'.MAILER_ADDRESS.'>'.PHP_EOL;
|
||||
|
||||
|
||||
mail($to, $subject, $message, $headers);
|
||||
// TODO log error message if mail fails
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* Tries to verify the domain using dns request against an MX record of the domain part
|
||||
* of the passed email address. The code also handles IDN/Punycode formatted addresses which
|
||||
* contains utf8 characters.
|
||||
* of the passed email address. The code also handles IDN/Punycode formatted addresses which
|
||||
* contains utf8 characters.
|
||||
* Original code from https://stackoverflow.com/questions/19261987/how-to-check-if-an-email-address-is-real-or-valid-using-php/19262381
|
||||
* @param String $email Email address to check
|
||||
* @return boolean True if MX record exits, false if otherwise
|
||||
@ -120,8 +119,8 @@ class Mailer {
|
||||
syslog(1,$domain);
|
||||
return checkdnsrr($domain, 'MX');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check if string contains non-english characters (detect IDN/Punycode enabled domains)
|
||||
* Original code from: https://stackoverflow.com/questions/13120475/detect-non-english-chars-in-a-string
|
||||
@ -136,7 +135,7 @@ class Mailer {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Takes the input from an HTML email and convert it to plain text
|
||||
* This is commonly used when sending HTML emails as a backup for email clients who can only view, or who choose to only view,
|
||||
@ -144,32 +143,32 @@ class Mailer {
|
||||
* plain text emails
|
||||
* @param string $content The body part of the email to convert to plain text.
|
||||
* @param boolean $remove_links Set to true if links should be removed from email
|
||||
* @return String pain text version
|
||||
* @return String pain text version
|
||||
*/
|
||||
public function convert_html_to_plain_txt($content, $remove_links=false){
|
||||
// TODO does not handle unsubscribe/manage subscription text very well.
|
||||
// Replace HTML line breaks with text line breaks
|
||||
$plain_text = str_ireplace(array("<br>","<br />"), "\n\r", $content);
|
||||
|
||||
|
||||
// Remove the content between the tags that wouldn't normally get removed with the strip_tags function
|
||||
$plain_text = preg_replace(array('@<head[^>]*?>.*?</head>@siu',
|
||||
'@<style[^>]*?>.*?</style>@siu',
|
||||
'@<script[^>]*?.*?</script>@siu',
|
||||
'@<noscript[^>]*?.*?</noscript>@siu',
|
||||
), "", $plain_text); // Remove everything from between the tags that doesn't get removed with strip_tags function
|
||||
|
||||
|
||||
// If the user has chosen to preserve the addresses from links
|
||||
if(!$remove_links){
|
||||
$plain_text = strip_tags(preg_replace('/<a href="(.*)">/', ' $1 ', $plain_text));
|
||||
}
|
||||
|
||||
|
||||
// Remove HTML spaces
|
||||
$plain_text = str_replace(" ", "", $plain_text);
|
||||
|
||||
|
||||
// Replace multiple line breaks with a single line break
|
||||
$plain_text = preg_replace("/(\s){3,}/","\r\n\r\n",trim($plain_text));
|
||||
|
||||
|
||||
return $plain_text;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
require_once(__DIR__ . "/queue.php");
|
||||
/**
|
||||
* Class that encapsulates everything that can be done with notifications
|
||||
*/
|
||||
@ -46,19 +47,77 @@ class Notification
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop over the list of subscribers to notify depending on impacted service(s) and
|
||||
* Loop over the list of subscribers to notify depending on impacted service(s) and
|
||||
* call the differnet notification handles.
|
||||
* @return void
|
||||
*/
|
||||
*/
|
||||
public function notify_subscribers()
|
||||
{
|
||||
global $mysqli;
|
||||
// Fetch list of unique subscribers for given service
|
||||
// Direct inclusion of variable withour using prepare justified by the fact that
|
||||
// Direct inclusion of variable without using prepare justified by the fact that
|
||||
// this->serviceids are not user submitted
|
||||
$sql = "SELECT DISTINCT subscriberIDFK FROM services_subscriber WHERE serviceIDFK IN (" . $this->serviceids . ")";
|
||||
$query = $mysqli->query($sql);
|
||||
|
||||
// Create the queue tasks for email/telegram notifications
|
||||
$queue = new Queue();
|
||||
$queue->status = $queue->all_status['populating'];
|
||||
$queue->user_id = $_SESSION['user'];
|
||||
|
||||
$arr_data = array();
|
||||
if ( SUBSCRIBE_EMAIL ) {
|
||||
$arr_data = $this->prepare_email(); // Make up the base message and subject for email
|
||||
$queue->type_id = $queue->all_type_id['notify_email'];
|
||||
$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);
|
||||
$arr_email = array();
|
||||
}
|
||||
if ( SUBSCRIBE_TELEGRAM ) {
|
||||
$arr_data = $this->prepare_telegram();
|
||||
$queue->type_id = $queue->all_type_id['notify_telegram'];
|
||||
$queue->template_data1 = null;
|
||||
$queue->template_data2 = $arr_data['body'];
|
||||
$task_id_telegram = $queue->add_task();
|
||||
syslog(1, "queue telegram: ". $task_id_telegram);
|
||||
$arr_telegram = array();
|
||||
}
|
||||
|
||||
while ($subscriber = $query->fetch_assoc()) {
|
||||
// Fetch list of subscriber details for already found subscriber IDs
|
||||
$stmt = $mysqli->prepare("SELECT typeID 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
|
||||
|
||||
// Handle telegram
|
||||
if ($typeID == 1 && SUBSCRIBE_TELEGRAM) {
|
||||
$arr_telegram[] = $subscriber['subscriberIDFK'];
|
||||
}
|
||||
// Handle email
|
||||
if ($typeID == 2 && SUBSCRIBE_EMAIL) {
|
||||
$arr_email[] = $subscriber['subscriberIDFK'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( SUBSCRIBE_TELEGRAM) {
|
||||
$queue->task_id = $task_id_telegram;
|
||||
$queue->add_notification($arr_telegram); // Add array of Telegram users to the notification queue list
|
||||
}
|
||||
if ( SUBSCRIBE_EMAIL ) {
|
||||
$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");
|
||||
@ -82,27 +141,33 @@ class Notification
|
||||
$this->submit_email($userID, $token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends Telegram notification message using their web api.
|
||||
* @param string $userID The Telegram userid to send to
|
||||
* @param string $firstname The users firstname
|
||||
* @return boolean
|
||||
* @param string $msg Body of message
|
||||
* @return boolean true = Sent / False = failed
|
||||
*/
|
||||
public function submit_telegram($userID, $firstname)
|
||||
{
|
||||
public function submit_queue_telegram($userID, $firstname, $msg)
|
||||
{
|
||||
// TODO Handle limitations (Max 30 different subscribers per second)
|
||||
// TODO Error handling
|
||||
$msg = _("Hi %s!\nThere is a status update for service(s): %s\nThe new status is: %s\nTitle: %s\n\n%s\n\n<a href='%s'>View online</a>");
|
||||
$msg = sprintf($msg, $firstname, $this->servicenames, $this->status, $this->title, $this->text, WEB_URL);
|
||||
|
||||
$tg_message = urlencode($msg);
|
||||
$response = json_decode(file_get_contents("https://api.telegram.org/bot" . TG_BOT_API_TOKEN . "/sendMessage?chat_id=" . $userID . "&parse_mode=HTML&text=" . $tg_message), true);
|
||||
if (! array_key_exists("ok", $response) || $response['ok'] != 1 ) {
|
||||
$msg = sprintf($msg, $firstname);
|
||||
|
||||
$tg_message = array('text' => $msg, 'chat_id' => $userID, 'parse_mode' => 'HTML');
|
||||
$json = @file_get_contents("https://api.telegram.org/bot" . TG_BOT_API_TOKEN . "/sendMessage?" . http_build_query($tg_message) );
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
@ -113,12 +178,58 @@ class Notification
|
||||
* @param String $uthkey Users token for managing subscription
|
||||
* @return void
|
||||
*/
|
||||
public function submit_email($userID, $token)
|
||||
public function submit_queue_email($subscriber, $subject, $msg)
|
||||
{
|
||||
// TODO Error handling
|
||||
$Parsedown = new Parsedown();
|
||||
//$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();
|
||||
$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);
|
||||
@ -127,8 +238,8 @@ class Notification
|
||||
$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("%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);
|
||||
@ -136,7 +247,18 @@ class Notification
|
||||
$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);
|
||||
|
||||
$val = array();
|
||||
$val['subject'] = $subject;
|
||||
$val['body'] = $str_mail;
|
||||
return $val;
|
||||
}
|
||||
}
|
||||
|
||||
public function prepare_telegram(){
|
||||
$msg = _("Hi #s!\nThere is a status update for service(s): %s\nThe new status is: %s\nTitle: %s\n\n%s\n\n<a href='%s'>View online</a>");
|
||||
$val['body'] = sprintf($msg, $this->servicenames, $this->status, $this->title, $this->text, WEB_URL);
|
||||
return $val;
|
||||
}
|
||||
}
|
||||
|
163
classes/queue.php
Normal file
163
classes/queue.php
Normal file
@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class for creating and managing the queue system
|
||||
*/
|
||||
class Queue
|
||||
{
|
||||
public $task_id;
|
||||
public $type_id;
|
||||
public $status;
|
||||
public $template_data1; // i.e. Subject for email
|
||||
public $template_data2; // i.e. HTML email body
|
||||
public $create_time;
|
||||
public $completed_time;
|
||||
public $num_errors;
|
||||
public $user_id;
|
||||
public $all_type_id = array('notify_telegram' => 1,
|
||||
'notify_email' => 2);
|
||||
|
||||
public $all_status = array('populating' => 1,
|
||||
'ready' => 2,
|
||||
'processing' => 3,
|
||||
'completed' => 4,
|
||||
'failed' => 5);
|
||||
|
||||
public function add_task() {
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->prepare("INSERT INTO queue_task (type_id, status, template_data1, template_data2, created_time, user_id) VALUES (?,?,?,?,?,?)");
|
||||
if ( false===$stmt ) {
|
||||
//die('prepare() failed: ' . htmlspecialchars($mysqli->error));
|
||||
echo $mysqli->errno();
|
||||
|
||||
}
|
||||
#if ( false === $stmt ) { syslog(1, "Error :". $mysqli->error); }
|
||||
$now = time();
|
||||
$res = $stmt->bind_param("iissii", $this->type_id, $this->status, $this->template_data1, $this->template_data2, $now, $this->user_id);
|
||||
if ( false === $res ) {
|
||||
echo "error";
|
||||
die();
|
||||
}
|
||||
$stmt->execute();
|
||||
$query = $stmt->get_result();
|
||||
print $query;
|
||||
$this->task_id = $mysqli->insert_id;
|
||||
return $this->task_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove task from the queue
|
||||
* @return void
|
||||
*/
|
||||
public function delete_task($task_id){
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->prepare("DELETE FROM queue_task WHERE id = ?");
|
||||
$stmt->bind_param("i", $task_id);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update status for given task
|
||||
* @param int $new_status The new current status of the task. Must be selected from the $all_status array.
|
||||
* @return void
|
||||
*/
|
||||
public function set_task_status($new_status) {
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->prepare("UPDATE queue_task SET status = ? WHERE id = ?");
|
||||
$stmt->bind_param("ii", $new_status, $this->task_id);
|
||||
$stmt->execute();
|
||||
$this->status = $new_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add notification queue data for given task
|
||||
* @param array $arr_data Array filled with subscriber_id
|
||||
* @return void
|
||||
*/
|
||||
public function add_notification($arr_data) {
|
||||
global $mysqli;
|
||||
|
||||
//Default status = 1, retres = 0, task_id = $this->task_id
|
||||
|
||||
// Build query manually since mysqli doesn't cater well for multi insert..
|
||||
$count = count($arr_data); // Let's find number of elements
|
||||
$counter = 0;
|
||||
$query = '';
|
||||
$seperator = ',';
|
||||
$sub_query = '(%d, %d, %d ,%d)%s';
|
||||
|
||||
foreach ($arr_data as $value) {
|
||||
$counter++;
|
||||
if ($counter == $count) { $seperator = ''; } // Make sure last character for SQL query is correct
|
||||
$query .= sprintf($sub_query, $this->task_id, 1, $value, 0, $seperator);
|
||||
}
|
||||
$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
|
||||
}
|
||||
|
||||
public function update_notfication_retries($task_id, $subscriber_id) {
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->prepare("UPDATE queue_notify SET retries = retries+1 WHERE task_id = ? AND subscriber_id = ?");
|
||||
$stmt->bind_param("ii", $task_id, $subscriber_id);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public function delete_notification($task_id, $subscriber_id) {
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->prepare("DELETE FROM queue_notify WHERE task_id = ? AND subscriber_id = ?");
|
||||
$stmt->bind_param("ii", $task_id, $subscriber_id);
|
||||
$stmt->execute();
|
||||
|
||||
}
|
||||
|
||||
// TODO: Fix max attempts for notifications
|
||||
public function process_queue(){
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->query("SELECT qn.id, qn.task_id, qn.status, qn.subscriber_id, qn.retries, sub.firstname, sub.userID, sub.token FROM queue_notify AS qn INNER JOIN subscribers AS sub ON qn.subscriber_id = sub.subscriberID WHERE qn.status NOT LIKE 2 AND sub.active=1");
|
||||
while ( $result = $stmt->fetch_assoc() ) {
|
||||
|
||||
$i = 2;
|
||||
$stmt2 = $mysqli->prepare("SELECT * FROM queue_task WHERE id = ? AND status = ?");
|
||||
$stmt2->bind_param("ii", $result['task_id'], $i);
|
||||
$stmt2->execute();
|
||||
$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) {
|
||||
$msg = str_replace("#s", $result['firstname'], $result2['template_data2']);
|
||||
if ( ! Notification::submit_queue_telegram($result['userID'], $result['firstname'], $msg) ) {
|
||||
Queue::update_notfication_retries($result['task_id'], $result['subscriber_id']); // Sent
|
||||
} else {
|
||||
Queue::delete_notification($result['task_id'], $result['subscriber_id']); // Failed
|
||||
}
|
||||
}
|
||||
|
||||
// Handle email
|
||||
if ($typeID == 2) {
|
||||
$msg = str_replace("%token%", $result['token'], $result2['template_data2']);
|
||||
if ( ! Notification::submit_queue_email($result['userID'], $result2['template_data1'], $msg) ) {
|
||||
Queue::update_notification_retries($result['task_id'], $result['subscriber_id']); // Sent
|
||||
|
||||
} else {
|
||||
Queue::delete_notification($result['task_id'], $result['subscriber_id']); // Failed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if queue log is empty and if so delete the queue_task
|
||||
$stmt = $mysqli->query("SELECT id, (SELECT COUNT(*) FROM queue_notify AS qn WHERE qn.task_id = queue_task.id) AS count FROM queue_task");
|
||||
while ( $result = $stmt->fetch_assoc() ) {
|
||||
if ( $result['count'] == 0 ) {
|
||||
Queue::delete_task($result['id']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -50,7 +50,7 @@ class Service implements JsonSerializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes submitted form and adds service unless problem is encountered,
|
||||
* Processes submitted form and adds service unless problem is encountered,
|
||||
* calling this is possible only for admin or higher rank. Also checks requirements
|
||||
* for char limits.
|
||||
* @return void
|
||||
@ -143,7 +143,7 @@ class Service implements JsonSerializable
|
||||
}
|
||||
|
||||
echo '<div id="status-big" class="status '.$classes[$worst].'">';
|
||||
|
||||
|
||||
if ($statuses[$worst] == count($array))
|
||||
{
|
||||
echo $all[$worst];
|
||||
|
Loading…
x
Reference in New Issue
Block a user