Fix DB_MySQL_PDO::escape_binary().
[bugdar.git] / cron.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2020 Blue Static
6 || # This program is free software; you can redistribute it and/or modify
7 || # it under the terms of the GNU General Public License as published by
8 || # the Free Software Foundation; version 2 of the License.
9 || #
10 || # This program is distributed in the hope that it will be useful, but
11 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 || # more details.
14 || #
15 || # You should have received a copy of the GNU General Public License along
16 || # with this program; if not, write to the Free Software Foundation, Inc.,
17 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 || ###################################################################
19 \*=====================================================================*/
20
21 define('NO_TEMPLATES', true);
22
23 require_once('./global.php');
24
25 $jobs = [
26 'admin_session' => [
27 'time' => 60 * 60 * 1.5,
28 'function' => 'CronAdminSession',
29 ],
30 'awaiting_email_confirmation' => [
31 'time' => 60 * 60 * 24 * 3,
32 'function' => 'CronPurgeInactiveUsers',
33 ],
34 ];
35
36 // List of cron jobs to remove.
37 $remove = [];
38
39 $update_query = bugdar::$db->prepare("
40 INSERT INTO " . TABLE_PREFIX . "cron
41 (name, lastrun)
42 VALUES
43 (:name, :lastrun)
44 ON DUPLICATE KEY
45 UPDATE lastrun = :lastrun
46 ");
47
48 // Filter the |$jobs| array, removing jobs that do not need to run.
49 $last_runs = bugdar::$db->query("SELECT * FROM " . TABLE_PREFIX . "cron");
50 while ($last_run = $last_runs->fetch()) {
51 $name = $last_run['name'];
52 if (!isset($jobs[$name])) {
53 $remove[] = $name;
54 continue;
55 }
56
57 $earliest_run_time = TIMENOW - $jobs[$name]['time'];
58 if ($earliest_run_time < $last_run['lastrun']) {
59 unset($jobs[$name]);
60 }
61 }
62
63 // Run the jobs.
64 $jobs_run = 0;
65 foreach ($jobs as $name => $job) {
66 require("./includes/cron/$name.php");
67 $job['function']();
68
69 $update_query->execute([
70 'name' => $name,
71 'lastrun' => TIMENOW,
72 ]);
73
74 ++$jobs_run;
75 }
76
77 // Remove stale jobs.
78 if (count($remove)) {
79 $remove_query = bugdar::$db->prepare("DELETE FROM " . TABLE_PREFIX . "cron WHERE name = ?");
80 foreach ($remove as $remove) {
81 $remove_query->execute([ $remove ]);
82 }
83 }
84
85 header('Content-Type: application/json');
86 echo '(' . json_encode([ 'jobsRun' => $jobs_run ]) . ')';