Skip to content

Commit

Permalink
feat: admin can manually trigger cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ribeiro committed Aug 9, 2024
1 parent d67753c commit 418d352
Show file tree
Hide file tree
Showing 28 changed files with 114 additions and 14 deletions.
15 changes: 15 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,21 @@ class="one-third" value="<?= $settings['smtp_port'] ?>" />
<span class="number-of-logos bold"><?= $logosToDelete ?></span>
<?= translate('orphaned_logos', $i18n) ?>
</div>
<h3><?= translate('cronjobs', $i18n) ?></h3>
<div>
<div class="inline">
<input type="button" value="Check for Updates" class="button tiny mobile-grow" onclick="executeCronJob('checkforupdates')">
<input type="button" value="Send Notifications" class="button tiny mobile-grow" onclick="executeCronJob('sendnotifications')">
<input type="button" value="Send Cancellation Notifications" class="button tiny mobile-grow" onclick="executeCronJob('sendcancellationnotifications')">
<input type="button" value="Send Password Reset Emails" class="button tiny mobile-grow" onclick="executeCronJob('sendresetpasswordemails')">
<input type="button" value="Send Verification Emails" class="button tiny mobile-grow" onclick="executeCronJob('sendverificationemails')">
<input type="button" value="Update Exchange Rates" class="button tiny mobile-grow" onclick="executeCronJob('updateexchange')">
<input type="button" value="Update Next Payments" class="button tiny mobile-grow" onclick="executeCronJob('updatenextpayment')">
</div>
<div class="inline">
<textarea id="cronjobResult" class="thin" readonly></textarea>
</div>
</div>
</div>
</section>

Expand Down
12 changes: 7 additions & 5 deletions endpoints/cronjobs/checkforupdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@

$db->exec("UPDATE admin SET latest_version = '$latestVersion'");

include __DIR__ . '/../../includes/version.php';

if (version_compare($latestVersion, $version) > 0) {
echo "New version available: $latestVersion";
} else {
echo "No new version available, currently on $version";
if (php_sapi_name() !== 'cli') {
include __DIR__ . '/../../includes/version.php';
if (version_compare($latestVersion, $version) > 0) {
echo "New version available: $latestVersion";
} else {
echo "No new version available, currently on $version";
}
}
?>
12 changes: 9 additions & 3 deletions endpoints/cronjobs/sendcancellationnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
$userId = $userToNotify['id'];
echo "For user: " . $userToNotify['username'] . "<br />";
if (php_sapi_name() !== 'cli') {
echo "For user: " . $userToNotify['username'] . "<br />";
}

$emailNotificationsEnabled = false;
$gotifyNotificationsEnabled = false;
Expand Down Expand Up @@ -111,7 +113,9 @@

// If no notifications are enabled, no need to run
if (!$notificationsEnabled) {
echo "Notifications are disabled. No need to run.<br />";
if (php_sapi_name() !== 'cli') {
echo "Notifications are disabled. No need to run.<br />";
}
continue;
} else {
// Get all currencies
Expand Down Expand Up @@ -461,7 +465,9 @@
}

} else {
echo "Nothing to notify.<br />";
if (php_sapi_name() !== 'cli') {
echo "Nothing to notify.<br />";
}
}

}
Expand Down
18 changes: 13 additions & 5 deletions endpoints/cronjobs/sendnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
require __DIR__ . '/../../libs/PHPMailer/SMTP.php';
require __DIR__ . '/../../libs/PHPMailer/Exception.php';

$date = new DateTime('now');
echo "\n" . $date->format('Y-m-d') . " " . $date->format('H:i:s') . "<br />\n";
if (php_sapi_name() == 'cli') {
$date = new DateTime('now');
echo "\n" . $date->format('Y-m-d') . " " . $date->format('H:i:s') . "<br />\n";
}

// Get all user ids
$query = "SELECT id, username FROM user";
Expand All @@ -20,7 +22,9 @@

while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
$userId = $userToNotify['id'];
echo "\nFor user: " . $userToNotify['username'] . "<br />";
if (php_sapi_name() !== 'cli') {
echo "\nFor user: " . $userToNotify['username'] . "<br />";
}

$days = 1;
$emailNotificationsEnabled = false;
Expand Down Expand Up @@ -146,7 +150,9 @@

// If no notifications are enabled, no need to run
if (!$notificationsEnabled) {
echo "Notifications are disabled. No need to run.<br />";
if (php_sapi_name() !== 'cli') {
echo "Notifications are disabled. No need to run.<br />";
}
continue;
} else {
// Get all currencies
Expand Down Expand Up @@ -574,7 +580,9 @@


} else {
echo "Nothing to notify.<br />";
if (php_sapi_name() !== 'cli') {
echo "Nothing to notify.<br />";
}
}

}
Expand Down
6 changes: 6 additions & 0 deletions endpoints/cronjobs/sendresetpasswordemails.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@
}
} else {
// There are no SMTP settings
if (php_sapi_name() !== 'cli') {
echo "SMTP settings are not configured. Please configure SMTP settings in the admin page.";
}
exit();
}
} else {
// There are no password reset emails to be sent
if (php_sapi_name() !== 'cli') {
echo "There are no password reset emails to be sent.";
}
exit();
}

Expand Down
11 changes: 10 additions & 1 deletion endpoints/cronjobs/sendverificationemails.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
$admin = $result->fetchArray(SQLITE3_ASSOC);

if ($admin['require_email_verification'] == 0) {
die("Email verification is not required.");
if (php_sapi_name() !== 'cli') {
echo "Email verification is not required.";
}
die();
}

$query = "SELECT * FROM email_verification WHERE email_sent = 0";
Expand Down Expand Up @@ -76,10 +79,16 @@
}
} else {
// There are no SMTP settings
if (php_sapi_name() !== 'cli') {
echo "SMTP settings are not configured. Please configure SMTP settings in the admin page.";
}
exit();
}
} else {
// There are no verification emails to be sent
if (php_sapi_name() !== 'cli') {
echo "No verification emails to be sent.";
}
exit();
}

Expand Down
5 changes: 5 additions & 0 deletions endpoints/cronjobs/updateexchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

// Get all user ids

if (php_sapi_name() == 'cli') {
$date = new DateTime('now');
echo "\n" . $date->format('Y-m-d') . " " . $date->format('H:i:s') . "<br />\n";
}

$query = "SELECT id, username FROM user";
$stmt = $db->prepare($query);
$usersToUpdateExchange = $stmt->execute();
Expand Down
5 changes: 5 additions & 0 deletions endpoints/cronjobs/updatenextpayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
require_once 'validate.php';
require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

if (php_sapi_name() == 'cli') {
$date = new DateTime('now');
echo "\n" . $date->format('Y-m-d') . " " . $date->format('H:i:s') . "<br />\n";
}

$currentDate = new DateTime();
$currentDateString = $currentDate->format('Y-m-d');

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Neueste Version",
"on_current_version" => "Sie verwenden die neueste Version von Wallos.",
"show_update_notification" => "Benachrichtigung über Updates auf dem Dashboard anzeigen",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "E-Mail verifiziert",
"email_verification_failed" => "E-Mail konnte nicht verifiziert werden",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/el.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Τελευταία Έκδοση",
"on_current_version" => "Χρησιμοποιείτε την τελευταία έκδοση του Wallos.",
"show_update_notification" => "Εμφάνιση ειδοποίησης για ενημερώσεις στο dashboard",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "Το email επιβεβαιώθηκε",
"email_verification_failed" => "Η επαλήθευση email απέτυχε",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Latest Version",
"on_current_version" => "You're running the latest version of Wallos.",
"show_update_notification" => "Show notification for updates on the dashboard",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "Email verified successfully",
"email_verification_failed" => "Email verification failed",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Última Versión",
"on_current_version" => "Está utilizando la última versión de Wallos.",
"show_update_notification" => "Mostrar notificación de actualizaciones en el dashboard",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "Correo electrónico verificado",
"email_verification_failed" => "Error al verificar el correo electrónico",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Dernière version",
"on_current_version" => "Vous utilisez la dernière version de Wallos.",
"show_update_notification" => "Afficher la notification de mise à jour sur le tableau de bord",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "Votre adresse courriel a été vérifiée avec succès",
"email_verification_failed" => "La vérification de l'adresse courriel a échoué",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@
"latest_version" => "Ultima versione",
"on_current_version" => "Stai utilizzando l'ultima versione di Wallos.",
"show_update_notification" => "Mostra notifica di aggiornamento sulla dashboard",
"cronjobs" => "Cronjobs",

// Email Verification
"email_verified" => "L'indirizzo email è stato verificato con successo",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/jp.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
"latest_version" => "最新バージョン",
"on_current_version" => "最新バージョンのWallosを使用しています。",
"show_update_notification" => "ダッシュボードに更新通知を表示する",
"cronjobs" => "クロンジョブズ",
// Email Verification
"email_verified" => "メールアドレスが確認されました",
"email_verification_failed" => "メールアドレスの確認に失敗しました",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/ko.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
"latest_version" => "최신 버전",
"on_current_version" => "최신 버전의 Wallos를 사용 중입니다.",
"show_update_notification" => "대시보드에 업데이트 알림 표시",
"cronjobs" => "크론잡",
// Email Verification
"email_verified" => "이메일 인증 완료",
"email_verification_failed" => "이메일 인증 실패",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pl.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Najnowsza wersja",
"on_current_version" => "Używasz najnowszej wersji Wallos.",
"show_update_notification" => "Pokaż powiadomienie o aktualizacjach na dashboardzie",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "E-mail został zweryfikowany",
"email_verification_failed" => "Weryfikacja e-maila nie powiodła się",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Última Versão",
"on_current_version" => "Está a usar a versão mais recente do Wallos.",
"show_update_notification" => "Mostrar notificação de atualizações no dashboard",
"Cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "Email verificado",
"email_verification_failed" => "Verificação de email falhou",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pt_br.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" =>"Última versão",
"on_current_version" => "Você está na última versão do Wallos.",
"show_update_notification" => "Mostrar notificação de atualização no dashboard",
"Cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "Email verificado",
"email_verification_failed" => "Falha na verificação do email",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Последняя версия",
"on_current_version" => "Вы используете последнюю версию Wallos.",
"show_update_notification" => "Показывать уведомление об обновлениях на дашборде",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "Ваш адрес электронной почты подтвержден. Теперь вы можете войти.",
"email_verification_failed" => "Не удалось подтвердить ваш адрес электронной почты.",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sl.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
"latest_version" => "Najnovejša različica",
"on_current_version" => "Uporabljate najnovejšo različico Wallos.",
"show_update_notification" => "Prikaži obvestilo o posodobitvah na dashboardu",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "E-pošta je bila uspešno preverjena",
"email_verification_failed" => "Preverjanje e-pošte ni uspelo",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sr.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Најновија верзија",
"on_current_version" => "Користите најновију верзију Wallos-а.",
"show_update_notification" => "Прикажи обавештење о ажурирањима на dashboardu",
"cronjobs" => "Цроњобс",
// Email Verification
"email_verified" => "Е-пошта је верификована",
"email_verification_failed" => "Верификација е-поште није успела",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sr_lat.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "Najnovija verzija",
"on_current_version" => "Koristite najnoviju verziju Wallos-a.",
"show_update_notification" => "Prikaži obaveštenje o ažuriranjima na dashboardu",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "E-pošta je uspešno verifikovana",
"email_verification_failed" => "Verifikacija e-pošte nije uspela",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "En Son Sürüm",
"on_current_version" => "Wallos'un en son sürümünü kullanıyorsunuz.",
"show_update_notification" => "Gösterge panelinde güncelleme bildirimini göster",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "E-posta doğrulandı",
"email_verification_failed" => "E-posta doğrulaması başarısız oldu",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/zh_cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
"latest_version" => "最新版本",
"on_current_version" => "您正在运行最新版本的 Wallos。",
"show_update_notification" => "在仪表板上显示更新通知",
"cronjobs" => "Cronjobs",

// Email Verification
"email_verified" => "电子邮件已验证",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/zh_tw.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"latest_version" => "最新版本",
"on_current_version" => "您正在運行最新版本的 Wallos。",
"show_update_notification" => "在儀表板上顯示更新通知",
"cronjobs" => "Cronjobs",
// Email Verification
"email_verified" => "電子郵件已驗證",
"email_verification_failed" => "電子郵件驗證失敗",
Expand Down
18 changes: 18 additions & 0 deletions scripts/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,22 @@ function toggleUpdateNotification() {
})
.catch(error => showErrorMessage('Error:', error));

}

function executeCronJob(job) {
const url = `endpoints/cronjobs/${job}.php`;
const resultTextArea = document.getElementById('cronjobResult');

fetch(url)
.then(response => {
return response.text();
})
.then(data => {
const formattedData = data.replace(/<br\s*\/?>/gi, '\n');
resultTextArea.value = formattedData;
})
.catch(error => {
console.error('Fetch error:', error);
showErrorMessage('Error:', error);
});
}
8 changes: 8 additions & 0 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,14 @@ header #avatar {
flex-basis: 50%;
}

.inline {
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 15px;
margin-bottom: 20px;
}

@media (max-width: 768px) {
.form-group .inline .mobile-split-50 {
flex-basis: 50%;
Expand Down

0 comments on commit 418d352

Please sign in to comment.