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
+54 -54
View File
@@ -11,14 +11,14 @@ Class Subscriber
public $lastname = null;
public $userID = ""; // Holds email, telegram id etc
public $token = null;
public $active = 0;
public $typeID = null; // Holds subscription type ID
public $active = 0;
public $typeID = null; // Holds subscription type ID
function __construct() {
$this->firstname = null;
$this->lastname = null;
$this->userID = "";
$this->userID = "";
$this->token = null;
$this->active = 0;
$this->typeID = null;
@@ -39,12 +39,12 @@ Class Subscriber
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$this->token = $row['token'];
$this->token = $row['token'];
//$this->get_subscriber_by_token($this->token);
return $row['token'];
}
return false;
}
public function get_subscriber_by_token($token)
{
@@ -56,12 +56,12 @@ Class Subscriber
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$this->id = $row['subscriberID'];
$this->populate(); //
$this->populate(); //
return true;
}
return false;
}
public function get_subscriber_by_userid($create = false)
{
global $mysqli;
@@ -69,7 +69,7 @@ Class Subscriber
$stmt->bind_param("si", $this->userID, $this->typeID );
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$this->id = $row['subscriberID'];
@@ -84,7 +84,7 @@ Class Subscriber
return false;
}
}
public function populate()
{
global $mysqli;
@@ -111,12 +111,12 @@ 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();
//$query = $stmt->get_result();
$this->id = $mysqli->insert_id;
$this->typeID = $typeID;
$this->userID = $userID;
@@ -126,7 +126,7 @@ Class Subscriber
$this->active = $active;
return $this->id;
}
public function update($subscriberID)
{
global $mysqli;
@@ -135,49 +135,49 @@ Class Subscriber
$stmt->bind_param("ii", $updateTime, $subscriberID);
$stmt->execute();
return true;
}
public function activate($subscriberID)
{
global $mysqli;
global $mysqli;
$updateTime = strtotime("now");
$stmt = $mysqli->prepare("UPDATE subscribers SET update_time = ?, expires = ? WHERE subscriberID = ?");
$tmp = null;
$stmt->bind_param("iii", $updateTime, $tmp, $subscriberID);
$stmt->execute();
return true;
}
public function delete($subscriberID)
{
global $mysqli;
$stmt = $mysqli->prepare("DELETE FROM services_subscriber WHERE subscriberIDFK = ?");
$stmt->bind_param("i", $subscriberID);
$stmt->execute();
//$query = $stmt->get_result();
$stmt = $mysqli->prepare("DELETE FROM subscribers WHERE subscriberID = ?");
$stmt->bind_param("i", $subscriberID);
$stmt->execute();
//$query = $stmt->get_result();
return true;
}
public function check_userid_exist()
{
global $mysqli;
$stmt = $mysqli->prepare("SELECT subscriberID, userID, token, active FROM subscribers WHERE typeID=? AND userID=? LIMIT 1");
$stmt->bind_param("is", $this->typeID, $this->userID);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
$this->id = $row['subscriberID'];
$this->populate();
@@ -185,33 +185,33 @@ Class Subscriber
}
return false;
}
public function is_active_subscriber($token)
public function is_active_subscriber($token)
{
global $mysqli;
$stmt = $mysqli->prepare("SELECT subscriberID, token, userID, active, expires FROM subscribers WHERE token LIKE ? LIMIT 1");
$stmt->bind_param("s", $token );
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
} else {
// No data found, fail gently...
return false;
return false;
}
// If account is not already active, check if we are within timeframe of exipre +2h
// If account is not already active, check if we are within timeframe of exipre +2h
// and active if so, otherwise,delete account and return falsev
if ( $row['active'] <> 1 ) {
// Calculate time range for when subscription need to be validated
// Calculate time range for when subscription need to be validated
$time_end = $row['expires'];
$time_start = $time_end - (3600*2); // TODO - make this interval configurable via a config option
$time_start = $time_end - (3600*2); // TODO - make this interval configurable via a config option
$time_now = time();
if ( ($time_now > $time_start) && ($time_now < $time_end) ) {
// Timefram is within range, active user..
$stmt2 = $mysqli->prepare("UPDATE subscribers SET active=1, expires=null WHERE subscriberID = ?");
@@ -223,7 +223,7 @@ Class Subscriber
$this->userID = $row['userID'];
$this->token = $row['token'];
return true;
} else {
// Timeframe outside of given scope -> delete account
$stmt2 = $mysqli->prepare("DELETE FROM subscribers WHERE subscriberID = ?");
@@ -240,9 +240,9 @@ Class Subscriber
$this->id = $row['subscriberID'];
$this->userID = $row['userID'];
$this->token = $row['token'];
return true;
return true;
}
/**
* Generate a new 64 byte token (32 bytes converted from bin2hex = 64 bytes)
* @return string token
@@ -255,10 +255,10 @@ Class Subscriber
$token = openssl_random_pseudo_bytes(32); //Generate a random string.
$token = bin2hex($token); //Convert the binary data into hexadecimal representation.
} else {
// Use alternative token generator if openssl isn't available...
$token = make_alt_token(32, 32);
// Use alternative token generator if openssl isn't available...
$token = make_alt_token(32, 32);
}
// Make sure token doesn't already exist in db
$stmt = $mysqli->prepare("SELECT subscriberID FROM subscribers WHERE token LIKE ?");
echo $mysqli->error;
@@ -266,16 +266,16 @@ Class Subscriber
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0 ) {
// token already exists, call self again
$token = $this->generate_token();
// token already exists, call self again
$token = $this->generate_token();
}
return $token;
}
/**
* Alternative token generator if openssl_random_pseudo_bytes is not available
* Original code by jsheets at shadonet dot com from http://php.net/manual/en/function.mt-rand.php
* Original code by jsheets at shadonet dot com from http://php.net/manual/en/function.mt-rand.php
* @params int min_length Minimum length of token
* @params int max_length Maximum length of token
* @return String token
@@ -283,31 +283,31 @@ Class Subscriber
public function make_alt_token($min_length = 32, $max_length = 64)
{
$key = '';
// build range and shuffle range using ASCII table
for ($i=0; $i<=255; $i++) {
$range[] = chr($i);
}
// shuffle our range 3 times
for ($i=0; $i<=3; $i++) {
shuffle($range);
}
// loop for random number generation
for ($i = 0; $i < mt_rand($min_length, $max_length); $i++) {
$key .= $range[mt_rand(0, count($range)-1)];
}
$return = bin2hex($key);
if (!empty($return)) {
return $return;
} else {
return 0;
}
}
public function set_logged_in()
{
$_SESSION['subscriber_valid'] = true;
@@ -316,7 +316,7 @@ Class Subscriber
$_SESSION['subscriber_typeid'] = $this->typeID; //email
$_SESSION['subscriber_token'] = $this->token;
}
public function set_logged_off()
{
unset($_SESSION['subscriber_valid']);
@@ -325,5 +325,5 @@ Class Subscriber
unset($_SESSION['subscriber_id']);
unset($_SESSION['subscriber_token']);
}
}
}