diff --git a/endpoints/cronjobs/conf.php b/endpoints/cronjobs/conf.php index 0c48285..eabfc2e 100644 --- a/endpoints/cronjobs/conf.php +++ b/endpoints/cronjobs/conf.php @@ -2,4 +2,5 @@ #Webroot path $webPath = "/var/www/html/"; + ?> \ No newline at end of file diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.php index 274467a..727feb6 100644 --- a/endpoints/cronjobs/sendnotifications.php +++ b/endpoints/cronjobs/sendnotifications.php @@ -11,6 +11,8 @@ $gotifyNotificationsEnabled = false; $telegramNotificationsEnabled = false; $webhookNotificationsEnabled = false; + $pushoverNotificationsEnabled = false; + $discordNotificationsEnabled = false; // Get notification settings (how many days before the subscription ends should the notification be sent) $query = "SELECT days FROM notification_settings"; @@ -35,6 +37,17 @@ $email['fromEmail'] = $row["from_email"] ? $row["from_email"] : "wallos@wallosapp.com"; } + // Check if Discord notifications are enabled and get the settings + $query = "SELECT * FROM discord_notifications"; + $result = $db->query($query); + + if ($row = $result->fetchArray(SQLITE3_ASSOC)) { + $discordNotificationsEnabled = $row['enabled']; + $discord['webhook_url'] = $row["webhook_url"]; + $discord['bot_username'] = $row["bot_username"]; + $discord['bot_avatar_url'] = $row["bot_avatar_url"]; + } + // Check if Gotify notifications are enabled and get the settings $query = "SELECT * FROM gotify_notifications"; $result = $db->query($query); @@ -55,6 +68,16 @@ $telegram['chatId'] = $row["chat_id"]; } + // Check if Pushover notifications are enabled and get the settings + $query = "SELECT * FROM pushover_notifications"; + $result = $db->query($query); + + if ($row = $result->fetchArray(SQLITE3_ASSOC)) { + $pushoverNotificationsEnabled = $row['enabled']; + $pushover['user_key'] = $row["user_key"]; + $pushover['token'] = $row["token"]; + } + // Check if Webhook notifications are enabled and get the settings $query = "SELECT * FROM webhook_notifications"; $result = $db->query($query); @@ -71,7 +94,7 @@ } } - $notificationsEnabled = $emailNotificationsEnabled || $gotifyNotificationsEnabled || $telegramNotificationsEnabled || $webhookNotificationsEnabled; + $notificationsEnabled = $emailNotificationsEnabled || $gotifyNotificationsEnabled || $telegramNotificationsEnabled || $webhookNotificationsEnabled || $pushoverNotificationsEnabled || $discordNotificationsEnabled; // If no notifications are enabled, no need to run if (!$notificationsEnabled) { @@ -181,6 +204,68 @@ } } + // Discord notifications if enabled + if ($discordNotificationsEnabled) { + 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); + + $title = translate('wallos_notification', $i18n); + + $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"; + } + + $postfields = [ + 'content' => $message, + 'embeds' => [ + [ + 'title' => $title, + 'description' => $message, + 'color' => hexdec("FF0000") + ] + ] + ]; + + if (!empty($discord['bot_username'])) { + $postfields['username'] = $discord['bot_username']; + } + + if (!empty($discord['bot_avatar_url'])) { + $postfields['avatar_url'] = $discord['bot_avatar_url']; + } + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $discord['webhook_url']); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields)); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json' + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + curl_close($ch); + + if ($result === false) { + echo "Error sending notifications: " . curl_error($ch); + } else { + echo "Discord Notifications sent
"; + } + } + } + // Gotify notifications if enabled if ($gotifyNotificationsEnabled) { foreach ($notify as $userId => $perUser) { @@ -271,6 +356,48 @@ } } + // Pushover notifications if enabled + if ($pushoverNotificationsEnabled) { + 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"; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "https://api.pushover.net/1/messages.json"); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ + 'token' => $pushover['token'], + 'user' => $pushover['user_key'], + 'message' => $message, + ])); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + + curl_close($ch); + + if ($result === false) { + echo "Error sending notifications: " . curl_error($ch); + } else { + echo "Pushover Notifications sent
"; + } + } + } + // Webhook notifications if enabled if ($webhookNotificationsEnabled) { // Get webhook payload and turn it into a json object diff --git a/endpoints/notifications/savediscordnotifications.php b/endpoints/notifications/savediscordnotifications.php new file mode 100644 index 0000000..47ac2cc --- /dev/null +++ b/endpoints/notifications/savediscordnotifications.php @@ -0,0 +1,72 @@ + 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["url"]) || $data["url"] == "" + ) { + $response = [ + "success" => false, + "message" => translate('fill_mandatory_fields', $i18n) + ]; + echo json_encode($response); + } else { + $enabled = $data["enabled"]; + $webhook_url = $data["url"]; + $bot_username = $data["bot_username"]; + $bot_avatar_url = $data["bot_avatar"]; + + $query = "SELECT COUNT(*) FROM discord_notifications"; + $result = $db->querySingle($query); + + if ($result === false) { + $response = [ + "success" => false, + "message" => translate('error_saving_notifications', $i18n) + ]; + echo json_encode($response); + } else { + if ($result == 0) { + $query = "INSERT INTO discord_notifications (enabled, webhook_url, bot_username, bot_avatar_url) + VALUES (:enabled, :webhook_url, :bot_username, :bot_avatar_url)"; + } else { + $query = "UPDATE pushover_notifications + SET enabled = :enabled, webhook_url = :webhook_url, bot_username = :bot_username, bot_avatar_url = :bot_avatar_url"; + } + + $stmt = $db->prepare($query); + $stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER); + $stmt->bindValue(':webhook_url', $webhook_url, SQLITE3_TEXT); + $stmt->bindValue(':bot_username', $bot_username, SQLITE3_TEXT); + $stmt->bindValue(':bot_avatar_url', $bot_avatar_url, SQLITE3_TEXT); + + 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); + } + } + } + } + +?> \ No newline at end of file diff --git a/endpoints/notifications/saveemailnotifications.php b/endpoints/notifications/saveemailnotifications.php index 8ecbd52..3042d25 100644 --- a/endpoints/notifications/saveemailnotifications.php +++ b/endpoints/notifications/saveemailnotifications.php @@ -21,7 +21,7 @@ ) { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; echo json_encode($response); } else { @@ -42,7 +42,7 @@ if ($result === false) { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } else { @@ -73,7 +73,7 @@ } else { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } diff --git a/endpoints/notifications/savegotifynotifications.php b/endpoints/notifications/savegotifynotifications.php index 3fe903c..93db541 100644 --- a/endpoints/notifications/savegotifynotifications.php +++ b/endpoints/notifications/savegotifynotifications.php @@ -19,7 +19,7 @@ ) { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; echo json_encode($response); } else { @@ -33,7 +33,7 @@ if ($result === false) { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } else { @@ -59,7 +59,7 @@ } else { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } diff --git a/endpoints/notifications/savenotificationsettings.php b/endpoints/notifications/savenotificationsettings.php index b603fc5..bcb5841 100644 --- a/endpoints/notifications/savenotificationsettings.php +++ b/endpoints/notifications/savenotificationsettings.php @@ -17,7 +17,7 @@ if (!isset($data["days"]) || $data['days'] == "") { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; echo json_encode($response); } else { @@ -28,7 +28,7 @@ if ($result === false) { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } else { @@ -51,7 +51,7 @@ } else { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } @@ -60,7 +60,7 @@ } else { $response = [ "success" => false, - "errorMessage" => "Invalid request method" + "message" => "Invalid request method" ]; echo json_encode($response); exit(); diff --git a/endpoints/notifications/savepushovernotifications.php b/endpoints/notifications/savepushovernotifications.php new file mode 100644 index 0000000..6f6a38e --- /dev/null +++ b/endpoints/notifications/savepushovernotifications.php @@ -0,0 +1,71 @@ + 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["user_key"]) || $data["user_key"] == "" || + !isset($data["token"]) || $data["token"] == "" + ) { + $response = [ + "success" => false, + "message" => translate('fill_mandatory_fields', $i18n) + ]; + echo json_encode($response); + } else { + $enabled = $data["enabled"]; + $user_key = $data["user_key"]; + $token = $data["token"]; + + $query = "SELECT COUNT(*) FROM pushover_notifications"; + $result = $db->querySingle($query); + + if ($result === false) { + $response = [ + "success" => false, + "message" => translate('error_saving_notifications', $i18n) + ]; + echo json_encode($response); + } else { + if ($result == 0) { + $query = "INSERT INTO pushover_notifications (enabled, user_key, token) + VALUES (:enabled, :user_key, :token)"; + } else { + $query = "UPDATE pushover_notifications + SET enabled = :enabled, user_key = :user_key, token = :token"; + } + + $stmt = $db->prepare($query); + $stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER); + $stmt->bindValue(':user_key', $user_key, 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, + "message" => translate('error_saving_notifications', $i18n) + ]; + echo json_encode($response); + } + } + } + } + +?> \ No newline at end of file diff --git a/endpoints/notifications/savetelegramnotifications.php b/endpoints/notifications/savetelegramnotifications.php index 1b86334..5640e7b 100644 --- a/endpoints/notifications/savetelegramnotifications.php +++ b/endpoints/notifications/savetelegramnotifications.php @@ -19,7 +19,7 @@ ) { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; echo json_encode($response); } else { @@ -33,7 +33,7 @@ if ($result === false) { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } else { @@ -59,7 +59,7 @@ } else { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } diff --git a/endpoints/notifications/savewebhooknotifications.php b/endpoints/notifications/savewebhooknotifications.php index 6bb41c5..a80b96d 100644 --- a/endpoints/notifications/savewebhooknotifications.php +++ b/endpoints/notifications/savewebhooknotifications.php @@ -19,7 +19,7 @@ ) { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; echo json_encode($response); } else { @@ -34,7 +34,7 @@ if ($result === false) { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } else { @@ -61,7 +61,7 @@ } else { $response = [ "success" => false, - "errorMessage" => translate('error_saving_notifications', $i18n) + "message" => translate('error_saving_notifications', $i18n) ]; echo json_encode($response); } diff --git a/endpoints/notifications/testdiscordnotifications.php b/endpoints/notifications/testdiscordnotifications.php new file mode 100644 index 0000000..5322ac0 --- /dev/null +++ b/endpoints/notifications/testdiscordnotifications.php @@ -0,0 +1,90 @@ + 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["url"]) || $data["url"] == "" + ) { + $response = [ + "success" => false, + "message" => translate('fill_mandatory_fields', $i18n) + ]; + echo json_encode($response); + } else { + // Set the message parameters + $title = translate('wallos_notification', $i18n); + $message = translate('test_notification', $i18n); + + $webhook_url = $data["url"]; + $bot_username = $data["bot_username"]; + $bot_avatar_url = $data["bot_avatar"]; + + $postfields = [ + 'content' => $message, + 'embeds' => [ + [ + 'title' => $title, + 'description' => $message, + 'color' => hexdec("FF0000") + ] + ] + ]; + + if (!empty($bot_username)) { + $postfields['username'] = $bot_username; + } + + if (!empty($bot_avatar_url)) { + $postfields['avatar_url'] = $bot_avatar_url; + } + + $ch = curl_init(); + + // Set the URL and other options + curl_setopt($ch, CURLOPT_URL, $webhook_url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields)); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json' + ]); + 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) + ])); +} + +?> \ No newline at end of file diff --git a/endpoints/notifications/testemailnotifications.php b/endpoints/notifications/testemailnotifications.php index 5657619..2896ae1 100644 --- a/endpoints/notifications/testemailnotifications.php +++ b/endpoints/notifications/testemailnotifications.php @@ -26,7 +26,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { ) { $response = [ "success" => false, - "errorMessage" => translate('fill_all_fields', $i18n) + "message" => translate('fill_all_fields', $i18n) ]; die(json_encode($response)); } else { @@ -76,7 +76,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { } else { $response = [ "success" => false, - "errorMessage" => translate('email_error', $i18n) . $mail->ErrorInfo + "message" => translate('email_error', $i18n) . $mail->ErrorInfo ]; die(json_encode($response)); } diff --git a/endpoints/notifications/testgotifynotifications.php b/endpoints/notifications/testgotifynotifications.php index f1782cc..93c8728 100644 --- a/endpoints/notifications/testgotifynotifications.php +++ b/endpoints/notifications/testgotifynotifications.php @@ -19,7 +19,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { ) { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; die(json_encode($response)); } else { diff --git a/endpoints/notifications/testpushovernotifications.php b/endpoints/notifications/testpushovernotifications.php new file mode 100644 index 0000000..dcb8a15 --- /dev/null +++ b/endpoints/notifications/testpushovernotifications.php @@ -0,0 +1,71 @@ + 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["user_key"]) || $data["user_key"] == "" || + !isset($data["token"]) || $data["token"] == "" + ) { + $response = [ + "success" => false, + "message" => translate('fill_mandatory_fields', $i18n) + ]; + echo json_encode($response); + } else { + // Set the message parameters + $message = translate('test_notification', $i18n); + + $user_key = $data["user_key"]; + $token = $data["token"]; + + $ch = curl_init(); + + // Set the URL and other options + curl_setopt($ch, CURLOPT_URL, "https://api.pushover.net/1/messages.json"); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ + 'token' => $token, + 'user' => $user_key, + 'message' => $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) + ])); +} + +?> \ No newline at end of file diff --git a/endpoints/notifications/testtelegramnotifications.php b/endpoints/notifications/testtelegramnotifications.php index 53634da..06f38ac 100644 --- a/endpoints/notifications/testtelegramnotifications.php +++ b/endpoints/notifications/testtelegramnotifications.php @@ -19,7 +19,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { ) { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; echo json_encode($response); } else { diff --git a/endpoints/notifications/testwebhooknotifications.php b/endpoints/notifications/testwebhooknotifications.php index 40afed4..e4dfe51 100644 --- a/endpoints/notifications/testwebhooknotifications.php +++ b/endpoints/notifications/testwebhooknotifications.php @@ -21,7 +21,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { ) { $response = [ "success" => false, - "errorMessage" => translate('fill_mandatory_fields', $i18n) + "message" => translate('fill_mandatory_fields', $i18n) ]; die(json_encode($response)); } else { diff --git a/includes/header.php b/includes/header.php index a1baf32..e16d208 100644 --- a/includes/header.php +++ b/includes/header.php @@ -46,6 +46,7 @@ > +