V1.0 add email notifications

This commit is contained in:
ellite
2023-11-10 11:45:01 +01:00
parent 65786aa191
commit 7c2e7f00b9
22 changed files with 7161 additions and 23 deletions
+6
View File
@@ -0,0 +1,6 @@
<?php
#Webroot path
$webPath = "/var/www/html/";
?>
+61 -3
View File
@@ -1,9 +1,11 @@
<?php
$databaseFile = '/var/www/html/db/wallos.db';
require_once 'conf.php';
$databaseFile = $webPath . 'db/wallos.db';
if (!file_exists($databaseFile)) {
echo "Database does not exist. Creating it...";
echo "Database does not exist. Creating it...\n";
$db = new SQLite3($databaseFile, SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
$db->busyTimeout(5000);
@@ -36,6 +38,7 @@ if (!file_exists($databaseFile)) {
payment_method_id INTEGER,
payer_user_id INTEGER,
category_id INTEGER,
notify BOOLEAN DEFAULT false,
FOREIGN KEY(currency_id) REFERENCES currencies(id),
FOREIGN KEY(cycle) REFERENCES cycles(id),
FOREIGN KEY(frequency) REFERENCES frequencies(id),
@@ -92,6 +95,16 @@ if (!file_exists($databaseFile)) {
date DATE NOT NULL
)');
$db-exec('CREATE TABLE notifications (
id INTEGER PRIMARY KEY,
enabled BOOLEAN DEFAULT false,
days INTEGER,
smtp_address VARCHAR(255),
smtp_port INTEGER,
smtp_username VARCHAR(255),
smtp_password VARCHAR(255)
)');
$db->exec("INSERT INTO categories (id, name) VALUES
(1, 'No category'),
(2, 'Entertainment'),
@@ -218,8 +231,53 @@ if (!file_exists($databaseFile)) {
(30, 'VeriFone', 'verifone.png'),
(31, 'WebMoney', 'webmoney.png')");
echo "Database created.\n";
} else {
echo "Database already exist. Skipping...";
echo "Database already exist. Checking for upgrades...\n";
$databaseFile = $webPath . 'db/wallos.db';
$db = new SQLite3($databaseFile);
$db->busyTimeout(5000);
if (!$db) {
die('Connection to the database failed.');
}
# v0.9 to v1.0
# Added new notifications table
# Added notify column to subscriptions table
$result = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='notifications'");
if (!$result->fetchArray(SQLITE3_ASSOC)) {
$db->exec('CREATE TABLE notifications (
id INTEGER PRIMARY KEY,
enabled BOOLEAN DEFAULT false,
days INTEGER,
smtp_address VARCHAR(255),
smtp_port INTEGER,
smtp_username VARCHAR(255),
smtp_password VARCHAR(255)
)');
echo "Table 'notifications' created.\n";
} else {
echo "Table 'notifications' already exists.\n";
}
$result = $db->query("PRAGMA table_info(subscriptions)");
$notifyColumnExists = false;
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
if ($row['name'] === 'notify') {
$notifyColumnExists = true;
break;
}
}
if (!$notifyColumnExists) {
$db->exec('ALTER TABLE subscriptions ADD COLUMN notify BOOLEAN DEFAULT false');
echo "Column 'notify' added to table 'subscriptions'.\n";
} else {
echo "Column 'notify' already exists in table 'subscriptions'.\n";
}
}
?>
+89
View File
@@ -0,0 +1,89 @@
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'conf.php';
require_once $webPath . 'includes/connect_endpoint_crontabs.php';
$query = "SELECT * FROM notifications WHERE id = 1";
$result = $db->query($query);
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$notificationsEnabled = $row['enabled'];
$days = $row['days'];
$smtpAddress = $row["smtp_address"];
$smtpPort = $row["smtp_port"];
$smtpUsername = $row["smtp_username"];
$smtpPassword = $row["smtp_password"];
} else {
echo "Notifications are disabled. No need to run.";
}
if ($notificationsEnabled) {
$currencies = array();
$query = "SELECT * FROM currencies";
$result = $db->query($query);
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$currencyId = $row['id'];
$currencies[$currencyId] = $row;
}
$querySubscriptions = "SELECT * FROM subscriptions WHERE notify = 1";
$resultSubscriptions = $db->query($querySubscriptions);
$notify = []; $i = 0;
$currentDate = new DateTime('now');
while ($rowSubscription = $resultSubscriptions->fetchArray(SQLITE3_ASSOC)) {
$nextPaymentDate = new DateTime($rowSubscription['next_payment']);
$difference = $currentDate->diff($nextPaymentDate)->days + 1;
if ($difference === $days) {
$notify[$i]['name'] = $rowSubscription['name'];
$notify[$i]['price'] = $rowSubscription['price'] . $currencies[$rowSubscription['currency_id']]['symbol'];
$i++;
}
}
if (!empty($notify)) {
require $webPath . 'libs/PHPMailer/PHPMailer.php';
require $webPath . 'libs/PHPMailer/SMTP.php';
require $webPath . 'libs/PHPMailer/Exception.php';
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days"
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
foreach ($notify as $subscription) {
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
}
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = $smtpAddress;
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->SMTPSecure = 'tls';
$mail->Port = $smtpPort;
$getUser = "SELECT * FROM user WHERE id = 1";
$user = $db->querySingle($getUser, true);
$email = $user['email'];
$name = $user['username'];
$mail->setFrom('wallos@wallosapp.com', 'Wallos App');
$mail->addAddress($email, $name);
$mail->Subject = 'Wallos Notification';
$mail->Body = $message;
if ($mail->send()) {
echo "Notifications sent";
} else {
echo "Error sending notifications: " . $mail->ErrorInfo;
}
} else {
echo "Nothing to notify.";
}
}
?>
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
require_once '/var/www/html/includes/connect_endpoint_crontabs.php';
require_once 'conf.php';
require_once $webPath . 'includes/connect_endpoint_crontabs.php';
$query = "SELECT api_key FROM fixer";
$result = $db->query($query);
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
require_once '/var/www/html/includes/connect_endpoint_crontabs.php';
require_once 'conf.php';
require_once $webPath . 'includes/connect_endpoint_crontabs.php';
$currentDate = new DateTime();
$currentDateString = $currentDate->format('Y-m-d');