query($query); if ($row = $result->fetchArray(SQLITE3_ASSOC)) { $notificationsEnabled = $row['enabled']; $days = $row['days']; } else { echo "Notifications are disabled. No need to run."; } if ($notificationsEnabled) { $currencies = array(); $query = "SELECT * FROM currencies"; $result = $db->query($query); while ($row = $result->fetchArray(SQLITE3_ASSOC)) { $currencyId = $row['id']; $currencies[$currencyId] = $row; } $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)) { $nextPaymentDate = new DateTime($rowSubscription['next_payment']); $difference = $currentDate->diff($nextPaymentDate)->days + 1; 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']; $i++; } } if (!empty($notify)) { $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"; } $ntfy_title = 'Wallos Notification'; $ntfy_body = $message; file_get_contents('https://ntfy.sh/canvas-progress-east', false, stream_context_create([ 'http' => [ 'method' => 'POST', // PUT also works 'header' => "Content-Type: text/plain\r\n" . "Title: Subscription Alert", 'content' => $message ] ])); } } else { echo "Nothing to notify."; } } ?>