35 lines · 1.3 KB
Raw Download
1
<?php
2
// Scheduled trigger for the daily email report. Point a cron job or uptime
3
// monitor at cron.php?token=... (the URL is shown on report.php), or run
4
// "php cron.php <token>" from a scheduler. Add &force=1 (or "force" as the
5
// second CLI argument) to send immediately regardless of schedule.
6
require_once __DIR__ . '/inc/report.php';
7
8
header('Content-Type: text/plain; charset=UTF-8');
9
header('Cache-Control: no-store');
10
11
$token = (string)($_GET['token'] ?? $argv[1] ?? '');
12
$stored = setting_get('cron_token');
13
if ($stored === null || $token === '' || !hash_equals($stored, $token)) {
14
    http_response_code(403);
15
    exit("Forbidden.\n");
16
}
17
18
$force = isset($_GET['force']) || in_array('force', array_slice($argv ?? [], 2), true);
19
20
if ($force) {
21
    $to = (string)setting_get('report_email', '');
22
    if ($to === '') {
23
        exit("No report email configured — set one on report.php first.\n");
24
    }
25
    $ok = send_report_email($to, $err);
26
    setting_set('report_last_status', ($ok ? "Sent to $to" : 'FAILED: ' . $err) . ' — ' . date('Y-m-d H:i') . ' (forced)');
27
    if (!$ok) {
28
        http_response_code(500);
29
    }
30
    exit(($ok ? "Sent to $to" : "FAILED: $err") . "\n");
31
}
32
33
maybe_send_daily_report();
34
echo 'OK. Last report: ' . (setting_get('report_last_status') ?? 'never sent') . "\n";
35