Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.
feat: add url and notes as variables for the notifications webhook
Miguel Ribeiro790defb2b1d1
diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.phpindex 7e47bf6..3996d83 100644--- a/endpoints/cronjobs/sendnotifications.php+++ b/endpoints/cronjobs/sendnotifications.php@@ -141,7 +141,11 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {$webhook['payload'] = $row["payload"];$webhook['iterator'] = $row["iterator"];if ($webhook['iterator'] === "") {- $webhook['iterator'] = "subscriptions";+ $webhook['iterator'] = "{{subscriptions}}";+ } else {+ if (strpos($webhook['iterator'], "{{") === false) {+ $webhook['iterator'] = "{{" . $webhook['iterator'] . "}}";+ }}}@@ -215,6 +219,8 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {$notify[$rowSubscription['payer_user_id']][$i]['payer'] = $household[$rowSubscription['payer_user_id']]['name'];$notify[$rowSubscription['payer_user_id']][$i]['date'] = $rowSubscription['next_payment'];$notify[$rowSubscription['payer_user_id']][$i]['days'] = $daysToCompare;+ $notify[$rowSubscription['payer_user_id']][$i]['url'] = $rowSubscription['url'];+ $notify[$rowSubscription['payer_user_id']][$i]['notes'] = $rowSubscription['notes'];$i++;}}@@ -532,10 +538,16 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {if ($webhookNotificationsEnabled) {// Get webhook payload and turn it into a json object- $payload = str_replace("{{days_until}}", $days, $webhook['payload']); // The default value for all subscriptions+ $payload = str_replace("{{days_until}}", $days, $webhook['payload']);+$payload_json = json_decode($payload, true);- $subscription_template = $payload_json["{{subscriptions}}"];+ if ($payload_json === null) {+ echo "Error parsing payload JSON<br />";+ continue;+ }++ $subscription_template = $payload_json[$webhook['iterator']];$subscriptions = [];foreach ($notify as $userId => $perUser) {@@ -560,7 +572,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {$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]);- $temp_subscription[$key] = str_replace("{{subscription_days_until_payment}}", $subscription['days'], $temp_subscription[$key]); // The de facto value for this subscription+ $temp_subscription[$key] = str_replace("{{subscription_days_until_payment}}", $subscription['days'], $temp_subscription[$key]);+ $temp_subscription[$key] = str_replace("{{subscription_url}}", $subscription['url'], $temp_subscription[$key]);+ $temp_subscription[$key] = str_replace("{{subscription_notes}}", $subscription['notes'], $temp_subscription[$key]);}}$subscriptions[] = $temp_subscription;@@ -568,10 +582,14 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {}}+ // remove {{ and }} from the iterator+ $payload_iterator = str_replace("{{", "", $webhook['iterator']);+ $payload_iterator = str_replace("}}", "", $payload_iterator);+$payload_json["{{subscriptions}}"] = $subscriptions;- $payload_json[$webhook['iterator']] = $subscriptions;+ $payload_json[$payload_iterator] = $subscriptions;unset($payload_json["{{subscriptions}}"]);-+$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $webhook['url']);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $webhook['request_method']);diff --git a/endpoints/notifications/savewebhooknotifications.php b/endpoints/notifications/savewebhooknotifications.phpindex c3d099e..b4815ee 100644--- a/endpoints/notifications/savewebhooknotifications.php+++ b/endpoints/notifications/savewebhooknotifications.php@@ -26,6 +26,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {$url = $data["webhook_url"];$headers = $data["headers"];$payload = $data["payload"];+ $iterator = $data["iterator"];$query = "SELECT COUNT(*) FROM webhook_notifications WHERE user_id = :userId";$stmt = $db->prepare($query);@@ -42,11 +43,11 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {$row = $result->fetchArray();$count = $row[0];if ($count == 0) {- $query = "INSERT INTO webhook_notifications (enabled, url, headers, payload, user_id)- VALUES (:enabled, :url, :headers, :payload, :userId)";+ $query = "INSERT INTO webhook_notifications (enabled, url, headers, payload, iterator, user_id)+ VALUES (:enabled, :url, :headers, :payload, :iterator, :userId)";} else {$query = "UPDATE webhook_notifications- SET enabled = :enabled, url = :url, headers = :headers, payload = :payload WHERE user_id = :userId";+ SET enabled = :enabled, url = :url, headers = :headers, payload = :payload, iterator = :iterator WHERE user_id = :userId";}$stmt = $db->prepare($query);@@ -54,6 +55,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {$stmt->bindValue(':url', $url, SQLITE3_TEXT);$stmt->bindValue(':headers', $headers, SQLITE3_TEXT);$stmt->bindValue(':payload', $payload, SQLITE3_TEXT);+ $stmt->bindValue(':iterator', $iterator, SQLITE3_TEXT);$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);if ($stmt->execute()) {diff --git a/includes/version.php b/includes/version.phpindex 9dd8477..e5a44f1 100644--- a/includes/version.php+++ b/includes/version.php@@ -1,3 +1,3 @@<?php-$version = "v2.28.0";+$version = "v2.29.0";?>\ No newline at end of filediff --git a/scripts/notifications.js b/scripts/notifications.jsindex 515f49a..217c87f 100644--- a/scripts/notifications.js+++ b/scripts/notifications.js@@ -112,12 +112,14 @@ function saveNotificationsWebhookButton() {const webhook_url = document.getElementById("webhookurl").value;const headers = document.getElementById("webhookcustomheaders").value;const payload = document.getElementById("webhookpayload").value;+ const iterator = document.getElementById("webhookiteratorkey").value;const data = {enabled: enabled,webhook_url: webhook_url,headers: headers,- payload: payload+ payload: payload,+ iterator: iterator};makeFetchCall('endpoints/notifications/savewebhooknotifications.php', data, button);@@ -132,13 +134,15 @@ function testNotificationsWebhookButton() {const url = document.getElementById("webhookurl").value;const customheaders = document.getElementById("webhookcustomheaders").value;const payload = document.getElementById("webhookpayload").value;+ const iterator = document.getElementById("webhookiteratorkey").value;const data = {enabled: enabled,requestmethod: requestmethod,url: url,customheaders: customheaders,- payload: payload+ payload: payload,+ iterator: iterator};makeFetchCall('endpoints/notifications/testwebhooknotifications.php', data, button);diff --git a/settings.php b/settings.phpindex 1b1a93f..b4f2e8e 100644--- a/settings.php+++ b/settings.php@@ -275,8 +275,10 @@ $userData['currency_symbol'] = "€";"currency": "{{subscription_currency}}","category": "{{subscription_category}}","date": "{{subscription_date}}",- "payer": "{{subscription_payer}}"- "days": "{{subscription_days_until_payment}}"+ "payer": "{{subscription_payer}}",+ "days": "{{subscription_days_until_payment}}",+ "notes": "{{subscription_notes}}",+ "url": "{{subscription_url}}"}]@@ -617,7 +619,7 @@ $userData['currency_symbol'] = "€";<i class="fa-solid fa-circle-info"></i> <?= translate('variables_available', $i18n) ?>:{{days_until}}, {{subscription_name}}, {{subscription_price}}, {{subscription_currency}},{{subscription_category}}, {{subscription_date}}, {{subscription_payer}},- {{subscription_days_until_payment}}+ {{subscription_days_until_payment}}, {{subscription_notes}}, {{subscription_url}}</p><p></div>