feat: add new notification methods (telegram, webhooks, gotify) (#295)
This commit is contained in:
@@ -2,5 +2,4 @@
|
||||
|
||||
#Webroot path
|
||||
$webPath = "/var/www/html/";
|
||||
|
||||
?>
|
||||
@@ -6,36 +6,109 @@
|
||||
require_once 'conf.php';
|
||||
require_once $webPath . 'includes/connect_endpoint_crontabs.php';
|
||||
|
||||
$query = "SELECT * FROM notifications WHERE id = 1";
|
||||
$days = 1;
|
||||
$emailNotificationsEnabled = false;
|
||||
$gotifyNotificationsEnabled = false;
|
||||
$telegramNotificationsEnabled = false;
|
||||
$webhookNotificationsEnabled = false;
|
||||
|
||||
// Get notification settings (how many days before the subscription ends should the notification be sent)
|
||||
$query = "SELECT days FROM notification_settings";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$notificationsEnabled = $row['enabled'];
|
||||
$days = $row['days'];
|
||||
$smtpAddress = $row["smtp_address"];
|
||||
$smtpPort = $row["smtp_port"];
|
||||
$encryption = $row["encryption"];
|
||||
$smtpUsername = $row["smtp_username"];
|
||||
$smtpPassword = $row["smtp_password"];
|
||||
$fromEmail = $row["from_email"] ? $row["from_email"] : "wallos@wallosapp.com";
|
||||
} else {
|
||||
echo "Notifications are disabled. No need to run.";
|
||||
}
|
||||
|
||||
if ($notificationsEnabled) {
|
||||
|
||||
// Check if email notifications are enabled and get the settings
|
||||
$query = "SELECT * FROM email_notifications";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$emailNotificationsEnabled = $row['enabled'];
|
||||
$email['smtpAddress'] = $row["smtp_address"];
|
||||
$email['smtpPort'] = $row["smtp_port"];
|
||||
$email['encryption'] = $row["encryption"];
|
||||
$email['smtpUsername'] = $row["smtp_username"];
|
||||
$email['smtpPassword'] = $row["smtp_password"];
|
||||
$email['fromEmail'] = $row["from_email"] ? $row["from_email"] : "wallos@wallosapp.com";
|
||||
}
|
||||
|
||||
// Check if Gotify notifications are enabled and get the settings
|
||||
$query = "SELECT * FROM gotify_notifications";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$gotifyNotificationsEnabled = $row['enabled'];
|
||||
$gotify['serverUrl'] = $row["url"];
|
||||
$gotify['appToken'] = $row["token"];
|
||||
}
|
||||
|
||||
// Check if Telegram notifications are enabled and get the settings
|
||||
$query = "SELECT * FROM telegram_notifications";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$telegramNotificationsEnabled = $row['enabled'];
|
||||
$telegram['botToken'] = $row["bot_token"];
|
||||
$telegram['chatId'] = $row["chat_id"];
|
||||
}
|
||||
|
||||
// Check if Webhook notifications are enabled and get the settings
|
||||
$query = "SELECT * FROM webhook_notifications";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$webhookNotificationsEnabled = $row['enabled'];
|
||||
$webhook['url'] = $row["url"];
|
||||
$webhook['request_method'] = $row["request_method"];
|
||||
$webhook['headers'] = $row["headers"];
|
||||
$webhook['payload'] = $row["payload"];
|
||||
$webhook['iterator'] = $row["iterator"];
|
||||
if ($webhook['iterator'] === "") {
|
||||
$webhook['iterator'] = "subscriptions";
|
||||
}
|
||||
}
|
||||
|
||||
$notificationsEnabled = $emailNotificationsEnabled || $gotifyNotificationsEnabled || $telegramNotificationsEnabled || $webhookNotificationsEnabled;
|
||||
|
||||
// If no notifications are enabled, no need to run
|
||||
if (!$notificationsEnabled) {
|
||||
echo "Notifications are disabled. No need to run.";
|
||||
exit();
|
||||
} else {
|
||||
// Get all currencies
|
||||
$currencies = array();
|
||||
$query = "SELECT * FROM currencies";
|
||||
$result = $db->query($query);
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$currencyId = $row['id'];
|
||||
$currencies[$currencyId] = $row;
|
||||
$currencies[$row['id']] = $row;
|
||||
}
|
||||
|
||||
// Get all household members
|
||||
$stmt = $db->prepare('SELECT * FROM household');
|
||||
$resultHousehold = $stmt->execute();
|
||||
|
||||
$household = [];
|
||||
while ($rowHousehold = $resultHousehold->fetchArray(SQLITE3_ASSOC)) {
|
||||
$household[$rowHousehold['id']] = $rowHousehold;
|
||||
}
|
||||
|
||||
// Get all categories
|
||||
$stmt = $db->prepare('SELECT * FROM categories');
|
||||
$resultCategories = $stmt->execute();
|
||||
|
||||
$categories = [];
|
||||
while ($rowCategory = $resultCategories->fetchArray(SQLITE3_ASSOC)) {
|
||||
$categories[$rowCategory['id']] = $rowCategory;
|
||||
}
|
||||
|
||||
$stmt = $db->prepare('SELECT * FROM subscriptions WHERE notify = :notify AND inactive = :inactive ORDER BY payer_user_id ASC');
|
||||
$stmt->bindValue(':notify', 1, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':inactive', 0, SQLITE3_INTEGER);
|
||||
$resultSubscriptions = $stmt->execute();
|
||||
|
||||
|
||||
$notify = []; $i = 0;
|
||||
$currentDate = new DateTime('now');
|
||||
while ($rowSubscription = $resultSubscriptions->fetchArray(SQLITE3_ASSOC)) {
|
||||
@@ -44,62 +117,224 @@
|
||||
if ($difference === $days) {
|
||||
$notify[$rowSubscription['payer_user_id']][$i]['name'] = $rowSubscription['name'];
|
||||
$notify[$rowSubscription['payer_user_id']][$i]['price'] = $rowSubscription['price'] . $currencies[$rowSubscription['currency_id']]['symbol'];
|
||||
$notify[$rowSubscription['payer_user_id']][$i]['currency'] = $currencies[$rowSubscription['currency_id']]['name'];
|
||||
$notify[$rowSubscription['payer_user_id']][$i]['category'] = $categories[$rowSubscription['category_id']]['name'];
|
||||
$notify[$rowSubscription['payer_user_id']][$i]['payer'] = $household[$rowSubscription['payer_user_id']]['name'];
|
||||
$notify[$rowSubscription['payer_user_id']][$i]['date'] = $rowSubscription['next_payment'];
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($notify)) {
|
||||
|
||||
require $webPath . 'libs/PHPMailer/PHPMailer.php';
|
||||
require $webPath . 'libs/PHPMailer/SMTP.php';
|
||||
require $webPath . 'libs/PHPMailer/Exception.php';
|
||||
// Email notifications if enabled
|
||||
if ($emailNotificationsEnabled) {
|
||||
require $webPath . 'libs/PHPMailer/PHPMailer.php';
|
||||
require $webPath . 'libs/PHPMailer/SMTP.php';
|
||||
require $webPath . 'libs/PHPMailer/Exception.php';
|
||||
|
||||
$stmt = $db->prepare('SELECT * FROM user WHERE id = :id');
|
||||
$stmt->bindValue(':id', 1, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
$defaultUser = $result->fetchArray(SQLITE3_ASSOC);
|
||||
$defaultEmail = $defaultUser['email'];
|
||||
$defaultName = $defaultUser['username'];
|
||||
|
||||
foreach ($notify as $userId => $perUser) {
|
||||
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
|
||||
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
|
||||
|
||||
foreach ($perUser as $subscription) {
|
||||
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
|
||||
}
|
||||
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->CharSet="UTF-8";
|
||||
$mail->isSMTP();
|
||||
|
||||
$mail->Host = $smtpAddress;
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = $smtpUsername;
|
||||
$mail->Password = $smtpPassword;
|
||||
$mail->SMTPSecure = $encryption;
|
||||
$mail->Port = $smtpPort;
|
||||
|
||||
$stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
|
||||
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
|
||||
$stmt = $db->prepare('SELECT * FROM user WHERE id = :id');
|
||||
$stmt->bindValue(':id', 1, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
$user = $result->fetchArray(SQLITE3_ASSOC);
|
||||
$defaultUser = $result->fetchArray(SQLITE3_ASSOC);
|
||||
$defaultEmail = $defaultUser['email'];
|
||||
$defaultName = $defaultUser['username'];
|
||||
|
||||
$email = !empty($user['email']) ? $user['email'] : $defaultEmail;
|
||||
$name = !empty($user['name']) ? $user['name'] : $defaultName;
|
||||
|
||||
$mail->setFrom($fromEmail, '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;
|
||||
foreach ($notify as $userId => $perUser) {
|
||||
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
|
||||
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
|
||||
|
||||
foreach ($perUser as $subscription) {
|
||||
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
|
||||
}
|
||||
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->CharSet="UTF-8";
|
||||
$mail->isSMTP();
|
||||
|
||||
$mail->Host = $email['smtpAddress'];
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = $email['smtpUsername'];
|
||||
$mail->Password = $email['smtpPassword'];
|
||||
$mail->SMTPSecure = $email['encryption'];
|
||||
$mail->Port = $email['smtpPort'];
|
||||
|
||||
$stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
|
||||
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
$user = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
$emailaddress = !empty($user['email']) ? $user['email'] : $defaultEmail;
|
||||
$name = !empty($user['name']) ? $user['name'] : $defaultName;
|
||||
|
||||
$mail->setFrom($email['fromEmail'], 'Wallos App');
|
||||
$mail->addAddress($emailaddress, $name);
|
||||
|
||||
$mail->Subject = 'Wallos Notification';
|
||||
$mail->Body = $message;
|
||||
|
||||
if ($mail->send()) {
|
||||
echo "Email Notifications sent<br />";
|
||||
} else {
|
||||
echo "Error sending notifications: " . $mail->ErrorInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gotify notifications if enabled
|
||||
if ($gotifyNotificationsEnabled) {
|
||||
foreach ($notify as $userId => $perUser) {
|
||||
// Get name of user from household table
|
||||
$stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
|
||||
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
$user = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
|
||||
if ($user['name']) {
|
||||
$message = $user['name'] . ", the following subscriptions are up for renewal " . $dayText . ":\n";
|
||||
} else {
|
||||
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
|
||||
}
|
||||
|
||||
foreach ($perUser as $subscription) {
|
||||
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'message' => $message,
|
||||
'priority' => 5
|
||||
);
|
||||
|
||||
$data_string = json_encode($data);
|
||||
|
||||
$ch = curl_init($gotify['serverUrl'] . '/message?token=' . $gotify['appToken']);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($data_string))
|
||||
);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
if ($result === false) {
|
||||
echo "Error sending notifications: " . curl_error($ch);
|
||||
} else {
|
||||
echo "Gotify Notifications sent<br />";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Telegram notifications if enabled
|
||||
if ($telegramNotificationsEnabled) {
|
||||
foreach ($notify as $userId => $perUser) {
|
||||
// Get name of user from household table
|
||||
$stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
|
||||
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
$user = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
|
||||
if ($user['name']) {
|
||||
$message = $user['name'] . ", the following subscriptions are up for renewal " . $dayText . ":\n";
|
||||
} else {
|
||||
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
|
||||
}
|
||||
|
||||
foreach ($perUser as $subscription) {
|
||||
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'chat_id' => $telegram['chatId'],
|
||||
'text' => $message
|
||||
);
|
||||
|
||||
$data_string = json_encode($data);
|
||||
|
||||
$ch = curl_init('https://api.telegram.org/bot' . $telegram['botToken'] . '/sendMessage');
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($data_string))
|
||||
);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
if ($result === false) {
|
||||
echo "Error sending notifications: " . curl_error($ch);
|
||||
} else {
|
||||
echo "Telegram Notifications sent<br />";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Webhook notifications if enabled
|
||||
if ($webhookNotificationsEnabled) {
|
||||
// Get webhook payload and turn it into a json object
|
||||
|
||||
$payload = str_replace("{{days_until}}", $days, $webhook['payload']);
|
||||
$payload_json = json_decode($payload, true);
|
||||
|
||||
$subscription_template = $payload_json["{{subscriptions}}"];
|
||||
$subscriptions = [];
|
||||
|
||||
foreach ($notify as $userId => $perUser) {
|
||||
// Get name of user from household table
|
||||
$stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
|
||||
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
$user = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
if ($user['name']) {
|
||||
$payer = $user['name'];
|
||||
}
|
||||
|
||||
foreach ($perUser as $k => $subscription) {
|
||||
$temp_subscription = $subscription_template[0];
|
||||
|
||||
foreach ($temp_subscription as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$temp_subscription[$key] = str_replace("{{subscription_name}}", $subscription['name'], $value);
|
||||
$temp_subscription[$key] = str_replace("{{subscription_price}}", $subscription['price'], $temp_subscription[$key]);
|
||||
$temp_subscription[$key] = str_replace("{{subscription_currency}}", $subscription['currency'], $temp_subscription[$key]);
|
||||
$temp_subscription[$key] = str_replace("{{subscription_category}}", $subscription['category'], $temp_subscription[$key]);
|
||||
$temp_subscription[$key] = str_replace("{{subscription_payer}}", $subscription['payer'], $temp_subscription[$key]);
|
||||
$temp_subscription[$key] = str_replace("{{subscription_date}}", $subscription['date'], $temp_subscription[$key]);
|
||||
}
|
||||
}
|
||||
$subscriptions[] = $temp_subscription;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$payload_json["{{subscriptions}}"] = $subscriptions;
|
||||
$payload_json[$webhook['iterator']] = $subscriptions;
|
||||
unset($payload_json["{{subscriptions}}"]);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $webhook['url']);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $webhook['request_method']);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload_json));
|
||||
if (!empty($customheaders)) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $webhook['headers']);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($response === false) {
|
||||
echo "Error sending notifications: " . curl_error($ch);
|
||||
} else {
|
||||
echo "Webhook Notifications sent<br />";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
echo "Nothing to notify.";
|
||||
}
|
||||
|
||||
+12
-8
@@ -2,12 +2,18 @@
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["days"]) || $data['days'] == "" ||
|
||||
!isset($data["smtpaddress"]) || $data["smtpaddress"] == "" ||
|
||||
!isset($data["smtpport"]) || $data["smtpport"] == "" ||
|
||||
!isset($data["smtpusername"]) || $data["smtpusername"] == "" ||
|
||||
@@ -20,7 +26,6 @@
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$enabled = $data["enabled"];
|
||||
$days = $data["days"];
|
||||
$smtpAddress = $data["smtpaddress"];
|
||||
$smtpPort = $data["smtpport"];
|
||||
$encryption = "tls";
|
||||
@@ -31,7 +36,7 @@
|
||||
$smtpPassword = $data["smtppassword"];
|
||||
$fromEmail = $data["fromemail"];
|
||||
|
||||
$query = "SELECT COUNT(*) FROM notifications";
|
||||
$query = "SELECT COUNT(*) FROM email_notifications";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result === false) {
|
||||
@@ -42,17 +47,16 @@
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
if ($result == 0) {
|
||||
$query = "INSERT INTO notifications (enabled, days, smtp_address, smtp_port, smtp_username, smtp_password, from_email, encryption)
|
||||
VALUES (:enabled, :days, :smtpAddress, :smtpPort, :smtpUsername, :smtpPassword, :fromEmail, :encryption)";
|
||||
$query = "INSERT INTO email_notifications (enabled, smtp_address, smtp_port, smtp_username, smtp_password, from_email, encryption)
|
||||
VALUES (:enabled, :smtpAddress, :smtpPort, :smtpUsername, :smtpPassword, :fromEmail, :encryption)";
|
||||
} else {
|
||||
$query = "UPDATE notifications
|
||||
SET enabled = :enabled, days = :days, smtp_address = :smtpAddress, smtp_port = :smtpPort,
|
||||
$query = "UPDATE email_notifications
|
||||
SET enabled = :enabled, smtp_address = :smtpAddress, smtp_port = :smtpPort,
|
||||
smtp_username = :smtpUsername, smtp_password = :smtpPassword, from_email = :fromEmail, encryption = :encryption";
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':days', $days, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':smtpAddress', $smtpAddress, SQLITE3_TEXT);
|
||||
$stmt->bindValue(':smtpPort', $smtpPort, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':smtpUsername', $smtpUsername, SQLITE3_TEXT);
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["gotify_url"]) || $data["gotify_url"] == "" ||
|
||||
!isset($data["token"]) || $data["token"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_mandatory_fields', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$enabled = $data["enabled"];
|
||||
$url = $data["gotify_url"];
|
||||
$token = $data["token"];
|
||||
|
||||
$query = "SELECT COUNT(*) FROM gotify_notifications";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result === false) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
if ($result == 0) {
|
||||
$query = "INSERT INTO gotify_notifications (enabled, url, token)
|
||||
VALUES (:enabled, :url, :token)";
|
||||
} else {
|
||||
$query = "UPDATE gotify_notifications
|
||||
SET enabled = :enabled, url = :url, token = :token";
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':url', $url, SQLITE3_TEXT);
|
||||
$stmt->bindValue(':token', $token, SQLITE3_TEXT);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$response = [
|
||||
"success" => true,
|
||||
"message" => translate('notifications_settings_saved', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (!isset($data["days"]) || $data['days'] == "") {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_mandatory_fields', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$days = $data["days"];
|
||||
$query = "SELECT COUNT(*) FROM notification_settings";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result === false) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
if ($result == 0) {
|
||||
$query = "INSERT INTO notification_settings (days)
|
||||
VALUES (:days)";
|
||||
} else {
|
||||
$query = "UPDATE notification_settings SET days = :days";
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(':days', $days, SQLITE3_INTEGER);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$response = [
|
||||
"success" => true,
|
||||
"message" => translate('notifications_settings_saved', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => "Invalid request method"
|
||||
];
|
||||
echo json_encode($response);
|
||||
exit();
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["bot_token"]) || $data["bot_token"] == "" ||
|
||||
!isset($data["chat_id"]) || $data["chat_id"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_mandatory_fields', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$enabled = $data["enabled"];
|
||||
$bot_token = $data["bot_token"];
|
||||
$chat_id = $data["chat_id"];
|
||||
|
||||
$query = "SELECT COUNT(*) FROM telegram_notifications";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result === false) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
if ($result == 0) {
|
||||
$query = "INSERT INTO telegram_notifications (enabled, bot_token, chat_id)
|
||||
VALUES (:enabled, :bot_token, :chat_id)";
|
||||
} else {
|
||||
$query = "UPDATE telegram_notifications
|
||||
SET enabled = :enabled, bot_token = :bot_token, chat_id = :chat_id";
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':bot_token', $bot_token, SQLITE3_TEXT);
|
||||
$stmt->bindValue(':chat_id', $chat_id, SQLITE3_TEXT);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$response = [
|
||||
"success" => true,
|
||||
"message" => translate('notifications_settings_saved', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["webhook_url"]) || $data["webhook_url"] == "" ||
|
||||
!isset($data["payload"]) || $data["payload"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_mandatory_fields', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$enabled = $data["enabled"];
|
||||
$url = $data["webhook_url"];
|
||||
$headers = $data["headers"];
|
||||
$payload = $data["payload"];
|
||||
|
||||
$query = "SELECT COUNT(*) FROM webhook_notifications";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result === false) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
if ($result == 0) {
|
||||
$query = "INSERT INTO webhook_notifications (enabled, url, headers, payload)
|
||||
VALUES (:enabled, :url, :headers, :payload)";
|
||||
} else {
|
||||
$query = "UPDATE webhook_notifications
|
||||
SET enabled = :enabled, url = :url, headers = :headers, payload = :payload";
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':url', $url, SQLITE3_TEXT);
|
||||
$stmt->bindValue(':headers', $headers, SQLITE3_TEXT);
|
||||
$stmt->bindValue(':payload', $payload, SQLITE3_TEXT);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$response = [
|
||||
"success" => true,
|
||||
"message" => translate('notifications_settings_saved', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('error_saving_notifications', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
+10
-4
@@ -7,6 +7,13 @@ use PHPMailer\PHPMailer\Exception;
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
@@ -21,14 +28,13 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_all_fields', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
die(json_encode($response));
|
||||
} else {
|
||||
$enxryption = "tls";
|
||||
if (isset($data["encryption"])) {
|
||||
$encryption = $data["encryption"];
|
||||
}
|
||||
|
||||
|
||||
require '../../libs/PHPMailer/PHPMailer.php';
|
||||
require '../../libs/PHPMailer/SMTP.php';
|
||||
require '../../libs/PHPMailer/Exception.php';
|
||||
@@ -66,13 +72,13 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
"success" => true,
|
||||
"message" => translate('notification_sent_successfuly', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
die(json_encode($response));
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('email_error', $i18n) . $mail->ErrorInfo
|
||||
];
|
||||
echo json_encode($response);
|
||||
die(json_encode($response));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["gotify_url"]) || $data["gotify_url"] == "" ||
|
||||
!isset($data["token"]) || $data["token"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_mandatory_fields', $i18n)
|
||||
];
|
||||
die(json_encode($response));
|
||||
} else {
|
||||
// Set the message parameters
|
||||
$title = translate('wallos_notification', $i18n);
|
||||
$message = translate('test_notification', $i18n);
|
||||
$priority = 5;
|
||||
|
||||
$url = $data["gotify_url"];
|
||||
$token = $data["token"];
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
// Set the URL and other options
|
||||
curl_setopt($ch, CURLOPT_URL, $url . "/message?token=" . $token);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
||||
'title' => $title,
|
||||
'message' => $message,
|
||||
'priority' => $priority,
|
||||
]));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Close the cURL session
|
||||
curl_close($ch);
|
||||
|
||||
// Check if the message was sent successfully
|
||||
if ($response === false) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('notification_failed', $i18n)
|
||||
]));
|
||||
} else {
|
||||
die(json_encode([
|
||||
"success" => true,
|
||||
"message" => translate('notification_sent_successfuly', $i18n)
|
||||
]));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate("invalid_request_method", $i18n)
|
||||
]));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["bottoken"]) || $data["bottoken"] == "" ||
|
||||
!isset($data["chatid"]) || $data["chatid"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_mandatory_fields', $i18n)
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
// Set the message parameters
|
||||
$title = translate('wallos_notification', $i18n);
|
||||
$message = translate('test_notification', $i18n);
|
||||
|
||||
$botToken = $data["bottoken"];
|
||||
$chatId = $data["chatid"];
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
// Set the URL and other options
|
||||
curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot" . $botToken . "/sendMessage");
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
||||
'chat_id' => $chatId,
|
||||
'text' => $message,
|
||||
]));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Close the cURL session
|
||||
curl_close($ch);
|
||||
|
||||
// Check if the message was sent successfully
|
||||
if ($response === false) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('notification_failed', $i18n)
|
||||
]));
|
||||
} else {
|
||||
die(json_encode([
|
||||
"success" => true,
|
||||
"message" => translate('notification_sent_successfuly', $i18n)
|
||||
]));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate("invalid_request_method", $i18n)
|
||||
]));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('session_expired', $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["requestmethod"]) || $data["requestmethod"] == "" ||
|
||||
!isset($data["url"]) || $data["url"] == "" ||
|
||||
!isset($data["payload"]) || $data["payload"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => translate('fill_mandatory_fields', $i18n)
|
||||
];
|
||||
die(json_encode($response));
|
||||
} else {
|
||||
// Set the message parameters
|
||||
$title = translate('wallos_notification', $i18n);
|
||||
$message = translate('test_notification', $i18n);
|
||||
|
||||
$requestmethod = $data["requestmethod"];
|
||||
$url = $data["url"];
|
||||
$payload = $data["payload"];
|
||||
$customheaders = json_decode($data["customheaders"], true);
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
// Set the URL and other options
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestmethod);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
if (!empty($customheaders)) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $customheaders);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
// Execute the request
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Close the cURL session
|
||||
curl_close($ch);
|
||||
|
||||
// Check if the message was sent successfully
|
||||
if ($response === false) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate('notification_failed', $i18n)
|
||||
]));
|
||||
} else {
|
||||
die(json_encode([
|
||||
"success" => true,
|
||||
"message" => translate('notification_sent_successfuly', $i18n)
|
||||
]));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => translate("invalid_request_method", $i18n)
|
||||
]));
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user