Skip to content

Commit

Permalink
feat: admin can manually trigger cronjobs
Browse files Browse the repository at this point in the history
fix: only allow the system and admin to run the cronjobs
fix: reduce size of the log files of the cronjobs
  • Loading branch information
ellite authored Aug 9, 2024
1 parent 0c87fe2 commit 1946ac9
Show file tree
Hide file tree
Showing 32 changed files with 145 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ See instructions to run Wallos below.
0 9 * * * php /var/www/html/endpoints/cronjobs/sendnotifications.php >> /var/log/cron/sendnotifications.log 2>&1
*/2 * * * * php /var/www/html/endpoints/cronjobs/sendverificationemails.php >> /var/log/cron/sendverificationemail.log 2>&1
*/2 * * * * php /var/www/html/endpoints/cronjobs/sendresetpasswordemails.php >> /var/log/cron/sendresetpasswordemails.log 2>&1
0 */6 * * * php /var/www/html/endpoints/cronjobs/checkforupdates.php >> /var/log/cron/checkforupdates.log 2>&1
```

5. If your web root is not `/var/www/html/` adjust the cronjobs above accordingly.
Expand Down
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: 11 additions & 1 deletion endpoints/cronjobs/checkforupdates.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

require_once 'validate.php';
require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

$options = [
'http' => [
'header' => "User-Agent: MyApp\r\n"
'header' => "User-Agent: Wallos\r\n"
]
];

Expand All @@ -27,4 +28,13 @@

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


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";
}
}
?>
13 changes: 10 additions & 3 deletions endpoints/cronjobs/sendcancellationnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once 'validate.php';
require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

require __DIR__ . '/../../libs/PHPMailer/PHPMailer.php';
Expand All @@ -16,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 @@ -110,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 @@ -460,7 +465,9 @@
}

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

}
Expand Down
19 changes: 14 additions & 5 deletions endpoints/cronjobs/sendnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once 'validate.php';
require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

require __DIR__ . '/../../libs/PHPMailer/PHPMailer.php';
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 @@ -19,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 @@ -145,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 @@ -573,7 +580,9 @@


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

}
Expand Down
7 changes: 7 additions & 0 deletions endpoints/cronjobs/sendresetpasswordemails.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once 'validate.php';
require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

$query = "SELECT * FROM admin";
Expand Down Expand Up @@ -72,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
12 changes: 11 additions & 1 deletion endpoints/cronjobs/sendverificationemails.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once 'validate.php';
require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

$query = "SELECT * FROM admin";
Expand All @@ -11,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 @@ -75,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
6 changes: 6 additions & 0 deletions endpoints/cronjobs/updateexchange.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php
require_once 'validate.php';
require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

// 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
6 changes: 6 additions & 0 deletions endpoints/cronjobs/updatenextpayment.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php

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
16 changes: 16 additions & 0 deletions endpoints/cronjobs/validate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

session_start();

$userId = 0;
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
$userId = $_SESSION['userId'];
}

if (php_sapi_name() !== 'cli') {
if ($userId !== 1) {
die("Unauthorized");
}
}

?>
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
Loading

0 comments on commit 1946ac9

Please sign in to comment.