[ 'time' => 60 * 60 * 1.5, 'function' => 'CronAdminSession', ], 'awaiting_email_confirmation' => [ 'time' => 60 * 60 * 24 * 3, 'function' => 'CronPurgeInactiveUsers', ], ]; // List of cron jobs to remove. $remove = []; $update_query = bugdar::$db->prepare(" INSERT INTO " . TABLE_PREFIX . "cron (name, lastrun) VALUES (:name, :lastrun) ON DUPLICATE KEY UPDATE lastrun = :lastrun "); // Filter the |$jobs| array, removing jobs that do not need to run. $last_runs = bugdar::$db->query("SELECT * FROM " . TABLE_PREFIX . "cron"); while ($last_run = $last_runs->fetch()) { $name = $last_run['name']; if (!isset($jobs[$name])) { $remove[] = $name; continue; } $earliest_run_time = TIMENOW - $jobs[$name]['time']; if ($earliest_run_time < $last_run['lastrun']) { unset($jobs[$name]); } } // Run the jobs. $jobs_run = 0; foreach ($jobs as $name => $job) { require("./includes/cron/$name.php"); $job['function'](); $update_query->execute([ 'name' => $name, 'lastrun' => TIMENOW, ]); ++$jobs_run; } // Remove stale jobs. if (count($remove)) { $remove_query = bugdar::$db->prepare("DELETE FROM " . TABLE_PREFIX . "cron WHERE name = ?"); foreach ($remove as $remove) { $remove_query->execute([ $remove ]); } } header('Content-Type: application/json'); echo '(' . json_encode([ 'jobsRun' => $jobs_run ]) . ')';