feat: add ntfy as notification method (#377)

This commit is contained in:
Miguel Ribeiro 2024-06-05 22:08:36 +02:00 committed by GitHub
parent 7217088bb0
commit 65edf0963b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
29 changed files with 366 additions and 4 deletions

View File

@ -25,6 +25,7 @@
$webhookNotificationsEnabled = false;
$pushoverNotificationsEnabled = false;
$discordNotificationsEnabled = false;
$ntfyNotificationsEnabled = false;
// Get notification settings (how many days before the subscription ends should the notification be sent)
$query = "SELECT days FROM notification_settings WHERE user_id = :userId";
@ -102,6 +103,19 @@
$pushover['token'] = $row["token"];
}
// Check if Nrfy notifications are enabled and get the settings
$query = "SELECT * FROM ntfy_notifications WHERE user_id = :userId";
$stmt = $db->prepare($query);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$ntfyNotificationsEnabled = $row['enabled'];
$ntfy['host'] = $row["host"];
$ntfy['topic'] = $row["topic"];
$ntfy['headers'] = $row["headers"];
}
// Check if Webhook notifications are enabled and get the settings
$query = "SELECT * FROM webhook_notifications WHERE user_id = :userId";
$stmt = $db->prepare($query);
@ -120,7 +134,9 @@
}
}
$notificationsEnabled = $emailNotificationsEnabled || $gotifyNotificationsEnabled || $telegramNotificationsEnabled || $webhookNotificationsEnabled || $pushoverNotificationsEnabled || $discordNotificationsEnabled;
$notificationsEnabled = $emailNotificationsEnabled || $gotifyNotificationsEnabled || $telegramNotificationsEnabled ||
$webhookNotificationsEnabled || $pushoverNotificationsEnabled || $discordNotificationsEnabled ||
$ntfyNotificationsEnabled;
// If no notifications are enabled, no need to run
if (!$notificationsEnabled) {
@ -429,6 +445,50 @@
}
}
// Ntfy notifications if enabled
if ($ntfyNotificationsEnabled) {
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']) {
$message = $user['name'] . ", the following subscriptions are up for renewal:\n";
} else {
$message = "The following subscriptions are up for renewal:\n";
}
foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}
$headers = json_decode($ntfy["headers"], true);
$customheaders = array_map(function($key, $value) {
return "$key: $value";
}, array_keys($headers), $headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ntfy['host'] . '/' . $ntfy['topic']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HTTPHEADER, $customheaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if ($response === false) {
echo "Error sending notifications: " . curl_error($ch) . "<br />";
} else {
echo "Ntfy Notifications sent<br />";
}
}
}
// Webhook notifications if enabled
if ($webhookNotificationsEnabled) {
// Get webhook payload and turn it into a json object

View File

@ -49,6 +49,9 @@ if (count($requiredMigrations) === 0) {
}
foreach ($requiredMigrations as $migration) {
if (!file_exists($migration)) {
$migration = '../../' . $migration;
}
require_once $migration;
$stmtInsert = $db->prepare('INSERT INTO migrations (migration) VALUES (:migration)');

View File

@ -0,0 +1,84 @@
<?php
require_once '../../includes/connect_endpoint.php';
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["topic"]) || $data["topic"] == "" ||
!isset($data["host"]) || $data["host"] == ""
) {
$response = [
"success" => false,
"message" => translate('fill_mandatory_fields', $i18n)
];
echo json_encode($response);
} else {
$enabled = $data["enabled"];
$host = $data["host"];
$topic = $data["topic"];
$headers = $data["headers"];
$query = "SELECT COUNT(*) FROM ntfy_notifications WHERE user_id = :userId";
$stmt = $db->prepare($query);
$stmt->bindParam(":userId", $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
if ($result === false) {
$response = [
"success" => false,
"message" => translate('error_saving_notifications', $i18n)
];
echo json_encode($response);
} else {
$row = $result->fetchArray();
$count = $row[0];
if ($count == 0) {
$query = "INSERT INTO ntfy_notifications (enabled, host, topic, headers, user_id)
VALUES (:enabled, :host, :topic, :headers, :userId)";
} else {
$query = "UPDATE ntfy_notifications
SET enabled = :enabled, host = :host, topic = :topic, headers = :headers WHERE user_id = :userId";
}
$stmt = $db->prepare($query);
$stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
$stmt->bindValue(':host', $host, SQLITE3_TEXT);
$stmt->bindValue(':topic', $topic, SQLITE3_TEXT);
$stmt->bindValue(':headers', $headers, SQLITE3_TEXT);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
if ($stmt->execute()) {
$response = [
"success" => true,
"message" => translate('notifications_settings_saved', $i18n)
];
echo json_encode($response);
} else {
$response = [
"success" => false,
"message" => translate('error_saving_notifications', $i18n)
];
echo json_encode($response);
}
}
}
} else {
$response = [
"success" => false,
"message" => translate('invalid_request_method', $i18n)
];
echo json_encode($response);
}
?>

View File

@ -70,6 +70,12 @@
}
}
}
} else {
$response = [
"success" => false,
"message" => translate('invalid_request_method', $i18n)
];
echo json_encode($response);
}
?>

View File

@ -0,0 +1,70 @@
<?php
require_once '../../includes/connect_endpoint.php';
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["host"]) || $data["host"] == "" ||
!isset($data["topic"]) || $data["topic"] == ""
) {
$response = [
"success" => false,
"message" => translate('fill_mandatory_fields', $i18n)
];
echo json_encode($response);
} else {
$host = $data["host"];
$topic = $data["topic"];
$headers = json_decode($data["headers"], true);
$customheaders = array_map(function($key, $value) {
return "$key: $value";
}, array_keys($headers), $headers);
$url = "$host/$topic";
// Set the message parameters
$message = translate('test_notification', $i18n);
$ch = curl_init();
// Set the URL and other options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HTTPHEADER, $customheaders);
// 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 {
print_r($response);
}
die(json_encode([
"success" => true,
"message" => translate('notification_sent_successfuly', $i18n)
]));
}
}
?>

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "Bot Avatar URL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover User Key",
'host' => "Host",
'topic' => "Topic",
"categories" => "Kategorien",
"save_category" => "Kategorie speichern",
"delete_category" => "Kategorie löschen",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "Discord Bot Avatar URL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover User Key",
'host' => "Host",
'topic' => "Θέμα",
"categories" => "Κατηγορίες",
"save_category" => "Αποθήκευση κατηγορίας",
"delete_category" => "Διαγραφή κατηγορίας",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "Discord Bot Avatar URL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover User Key",
'host' => "Host",
'topic' => "Topic",
"categories" => "Categories",
"save_category" => "Save Category",
"delete_category" => "Delete Category",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL del avatar del bot",
"pushover" => "Pushover",
"pushover_user_key" => "Clave de usuario",
'host' => "Host",
'topic' => "Topico",
"categories" => "Categorías",
"save_category" => "Guardar Categoría",
"delete_category" => "Eliminar Categoría",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL de l'avatar du bot Discord",
"pushover" => "Pushover",
"pushover_user_key" => "Clé utilisateur Pushover",
'host' => "Hôte",
'topic' => "Sujet",
"categories" => "Catégories",
"save_category" => "Enregistrer la catégorie",
"delete_category" => "Supprimer la catégorie",

View File

@ -160,6 +160,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL dell'avatar del bot",
"pushover" => "Pushover",
"pushover_user_key" => "Chiave utente",
'host' => "Host",
'topic' => "Topic",
'categories' => 'Categorie',
'save_category' => 'Salva categoria',
'delete_category' => 'Elimina categoria',

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "DiscordボットアバターURL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushoverユーザーキー",
'host' => "ホスト",
'topic' => "トピック",
"categories" => "カテゴリ",
"save_category" => "カテゴリを保存",
"delete_category" => "カテゴリを削除",

View File

@ -153,7 +153,9 @@ $i18n = [
"discord_bot_username" => "디스코드 봇 유저명",
"discord_bot_avatar_url" => "디스코드 봇 아바타 URL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover User Key",
"pushover_user_key" => "Pushover User Key",~
'host' => "호스트",
'topic' => "토픽",
"categories" => "카테고리",
"save_category" => "카테고리 저장",
"delete_category" => "카테고리 삭제",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL awatara bota",
"pushover" => "Pushover",
"pushover_user_key" => "Klucz użytkownika",
'host' => "Host",
'topic' => "Temat",
"categories" => "Kategorie",
"save_category" => "Zapisz kategorię",
"delete_category" => "Usuń kategorię",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL do Avatar do Bot",
"pushover" => "Pushover",
"pushover_user_key" => "Chave de Utilizador Pushover",
'host' => "Host",
'topic' => "Tópico",
"categories" => "Categorias",
"save_category" => "Guardar Categoria",
"delete_category" => "Apagar Categoria",

View File

@ -152,6 +152,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL do Avatar",
"pushover" => "Pushover",
"pushover_user_key" => "Chave do Usuário",
'host' => "Host",
'topic' => "Tópico",
"categories" => "Categorias",
"save_category" => "Salvar categoria",
"delete_category" => "Excluir categoria",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL-адрес аватара бота Discord",
"pushover" => "Pushover",
"pushover_user_key" => "Ключ пользователя Pushover",
'host' => "Хост",
'topic' => "Тема",
"categories" => "Категории",
"save_category" => "Сохранить категорию",
"delete_category" => "Удалить категорию",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "URL avatarja Discordovega bota",
"pushover" => "Pushover",
"pushover_user_key" => "Uporabniški ključ Pushover",
'host' => "Gostitelj",
'topic' => "Tema",
"categories" => "Kategorije",
"save_category" => "Shrani kategorijo",
"delete_category" => "Izbriši kategorijo",

View File

@ -153,6 +153,8 @@ $i18n = [
"discord_bot_avatar_url" => "Дискорд бот URL аватара",
"pushover" => "Пушовер",
"pushover_user_key" => "Пушовер кориснички кључ",
'host' => "Домаћин",
'topic' => "Тема",
"categories" => "Категорије",
"save_category" => "Сачувај категорију",
"delete_category" => "Избриши категорију",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "Discord bot URL avatara",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover korisnički ključ",
'host' => "Host",
'topic' => "Tema",
"categories" => "Kategorije",
"save_category" => "Sačuvaj kategoriju",
"delete_category" => "Izbriši kategoriju",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "Discord Bot Avatar URL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover Kullanıcı Anahtarı",
'host' => "Host",
'topic' => "Konu",
"categories" => "Kategoriler",
"save_category" => "Kategoriyi Kaydet",
"delete_category" => "Kategoriyi Sil",

View File

@ -162,6 +162,8 @@ $i18n = [
"discord_bot_avatar_url" => "Discord 机器人头像 URL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover 用户密钥",
'host' => "主机",
'topic' => "主题",
"categories" => "分类",
"save_category" => "保存分类",
"delete_category" => "删除分类",

View File

@ -154,6 +154,8 @@ $i18n = [
"discord_bot_avatar_url" => "Discord 機器人頭像 URL",
"pushover" => "Pushover",
"pushover_user_key" => "Pushover 使用者金鑰",
'host' => "主機",
'topic' => "主題",
"categories" => "分類",
"save_category" => "儲存分類",
"delete_category" => "刪除分類",

View File

@ -1,3 +1,3 @@
<?php
$version = "v2.2.1";
$version = "v2.3.0";
?>

15
migrations/000021.php Normal file
View File

@ -0,0 +1,15 @@
<?php
/*
* This migration adds tables to store the data about a new notification method Ntfy
*/
/** @noinspection PhpUndefinedVariableInspection */
$db->exec('CREATE TABLE IF NOT EXISTS ntfy_notifications (
enabled BOOLEAN DEFAULT 0,
host TEXT DEFAULT "",
topic TEXT DEFAULT "",
headers TEXT DEFAULT "",
user_id INTEGER,
FOREIGN KEY (user_id) REFERENCES user(id)
)');

View File

@ -281,3 +281,40 @@ function testNotificationsDiscordButton() {
makeFetchCall('endpoints/notifications/testdiscordnotifications.php', data, button);
}
function testNotificationsNtfyButton() {
const button = document.getElementById("testNotificationsNtfy");
button.disabled = true;
const host = document.getElementById("ntfyhost").value;
const topic = document.getElementById("ntfytopic").value;
const headers = document.getElementById("ntfyheaders").value;
const data = {
host: host,
topic: topic,
headers: headers
};
makeFetchCall('endpoints/notifications/testntfynotifications.php', data, button);
}
function saveNotificationsNtfyButton() {
const button = document.getElementById("saveNotificationsNtfy");
button.disabled = true;
const enabled = document.getElementById("ntfyenabled").checked ? 1 : 0;
const host = document.getElementById("ntfyhost").value;
const topic = document.getElementById("ntfytopic").value;
const headers = document.getElementById("ntfyheaders").value;
const data = {
enabled: enabled,
host: host,
topic: topic,
headers: headers
};
makeFetchCall('endpoints/notifications/saventfynotifications.php', data, button);
}

View File

@ -309,6 +309,28 @@
$notificationsTelegram['chat_id'] = "";
}
// Ntfy notifications
$sql = "SELECT * FROM ntfy_notifications WHERE user_id = :userId LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$rowCount = 0;
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$notificationsNtfy['enabled'] = $row['enabled'];
$notificationsNtfy['host'] = $row['host'];
$notificationsNtfy['topic'] = $row['topic'];
$notificationsNtfy['headers'] = $row['headers'];
$rowCount++;
}
if ($rowCount == 0) {
$notificationsNtfy['enabled'] = 0;
$notificationsNtfy['host'] = "";
$notificationsNtfy['topic'] = "";
$notificationsNtfy['headers'] = "";
}
// Webhook notifications
$sql = "SELECT * FROM webhook_notifications WHERE user_id = :userId LIMIT 1";
$stmt = $db->prepare($sql);
@ -537,6 +559,31 @@
</div>
</div>
</section>
<section class="account-notifications-section">
<header class="account-notification-section-header" onclick="openNotificationsSettings('ntfy');">
<h3>
<i class="fa-solid fa-terminal"></i> Ntfy
</h3>
</header>
<div class="account-notification-section-settings" data-type="ntfy">
<div class="form-group-inline">
<input type="checkbox" id="ntfyenabled" name="ntfyenabled" <?= $notificationsNtfy['enabled'] ? "checked" : "" ?>>
<label for="ntfyenabled" class="capitalize"><?= translate('enabled', $i18n) ?></label>
</div>
<div class="form-group-inline">
<input type="text" name="ntfyhost" id="ntfyhost" placeholder="<?= translate('host', $i18n) ?>" value="<?= $notificationsNtfy['host'] ?>" />
</div>
<div class="form-group-inline">
<input type="text" name="ntfytopic" id="ntfytopic" placeholder="<?= translate('topic', $i18n) ?>" value="<?= $notificationsNtfy['topic'] ?>" />
</div>
<div class="form-group-inline">
<textarea class="thin" name="ntfyheaders" id="ntfyheaders" placeholder="<?= translate('custom_headers', $i18n) ?>"><?= $notificationsNtfy['headers'] ?></textarea>
</div>
<div class="buttons">
<input type="button" class="secondary-button thin" value="<?= translate('test', $i18n) ?>" id="testNotificationsNtfy" onClick="testNotificationsNtfyButton()"/>
<input type="submit" class="thin" value="<?= translate('save', $i18n) ?>" id="saveNotificationsNtfy" onClick="saveNotificationsNtfyButton()"/>
</div>
</section>
<section class="account-notifications-section">
<header class="account-notification-section-header" onclick="openNotificationsSettings('webhook');">
<h3>

View File

@ -88,10 +88,14 @@ select:disabled {
cursor: not-allowed;
}
button.secondary-button,
button.button.secondary-button,
input[type="button"].secondary-button {
background-color: #222;
}
button.button.secondary-button:hover,
button.secondary-button:hover,
input[type="button"].secondary-button:hover {
background-color: #111;
}

View File

@ -669,7 +669,7 @@ header #avatar {
}
.account-notifications-section {
border: 1px solid #ccc;
border: 1px solid #aaa;
border-radius: 8px;
margin-bottom: 10px;
overflow: hidden;