<?php
/**
 * cron_lib.php
 * Small helpers so every cron script can:
 *  - check whether it's been switched off from Website Manage
 *  - record when it last ran (and a short summary) for the admin UI
 *
 * If the `cron_jobs` table hasn't been created yet (migration not run),
 * these fail "open" (treat the job as enabled) so nothing breaks.
 */

function cron_is_enabled($conn, $job_key) {
    $key = mysqli_real_escape_string($conn, $job_key);
    $res = @mysqli_query($conn, "SELECT is_enabled FROM cron_jobs WHERE job_key = '$key' LIMIT 1");
    if (!$res || mysqli_num_rows($res) === 0) {
        return true; // no control row (or table missing) -> don't block
    }
    $row = mysqli_fetch_assoc($res);
    return ((int)$row['is_enabled']) === 1;
}

function cron_mark_ran($conn, $job_key, $summary = '') {
    $key = mysqli_real_escape_string($conn, $job_key);
    $sum = mysqli_real_escape_string($conn, $summary);
    @mysqli_query($conn, "UPDATE cron_jobs SET last_run_at = NOW(), last_run_summary = '$sum' WHERE job_key = '$key'");
}
