Skip to content

Commit

Permalink
feat: add new notification methods (telegram, webhooks, gotify) (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellite authored May 5, 2024
1 parent 5fff7c5 commit a408031
Show file tree
Hide file tree
Showing 37 changed files with 1,655 additions and 231 deletions.
4 changes: 2 additions & 2 deletions .tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*
!.gitignore
*
!.gitignore
1 change: 0 additions & 1 deletion endpoints/cronjobs/conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

#Webroot path
$webPath = "/var/www/html/";

?>
347 changes: 291 additions & 56 deletions endpoints/cronjobs/sendnotifications.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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"] == "" ||
Expand All @@ -20,7 +26,6 @@
echo json_encode($response);
} else {
$enabled = $data["enabled"];
$days = $data["days"];
$smtpAddress = $data["smtpaddress"];
$smtpPort = $data["smtpport"];
$encryption = "tls";
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
69 changes: 69 additions & 0 deletions endpoints/notifications/savegotifynotifications.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
?>
67 changes: 67 additions & 0 deletions endpoints/notifications/savenotificationsettings.php
Original file line number Diff line number Diff line change
@@ -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();
}
69 changes: 69 additions & 0 deletions endpoints/notifications/savetelegramnotifications.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
?>
71 changes: 71 additions & 0 deletions endpoints/notifications/savewebhooknotifications.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
?>
Loading

0 comments on commit a408031

Please sign in to comment.