1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This is a slightly strange file. It is not designed to ever be run directly from within SMF's |
||
5 | * conventional running, but called externally to facilitate background tasks. It can be called |
||
6 | * either directly or via cron, and in either case will completely ignore anything supplied |
||
7 | * via command line, or $_GET, $_POST, $_COOKIE etc. because those things should never affect the |
||
8 | * running of this script. |
||
9 | * |
||
10 | * Because of the way this runs, etc. we do need some of SMF but not everything to try to keep this |
||
11 | * running a little bit faster. |
||
12 | * |
||
13 | * Simple Machines Forum (SMF) |
||
14 | * |
||
15 | * @package SMF |
||
16 | * @author Simple Machines https://www.simplemachines.org |
||
17 | * @copyright 2025 Simple Machines and individual contributors |
||
18 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
19 | * |
||
20 | * @version 2.1.6 |
||
21 | */ |
||
22 | |||
23 | define('SMF', 'BACKGROUND'); |
||
24 | define('SMF_VERSION', '2.1.6'); |
||
25 | define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION); |
||
26 | define('SMF_SOFTWARE_YEAR', '2025'); |
||
27 | define('FROM_CLI', empty($_SERVER['REQUEST_METHOD'])); |
||
28 | |||
29 | define('JQUERY_VERSION', '3.6.3'); |
||
30 | define('POSTGRE_TITLE', 'PostgreSQL'); |
||
31 | define('MYSQL_TITLE', 'MySQL'); |
||
32 | define('SMF_USER_AGENT', 'Mozilla/5.0 (' . php_uname('s') . ' ' . php_uname('m') . ') AppleWebKit/605.1.15 (KHTML, like Gecko) SMF/' . strtr(SMF_VERSION, ' ', '.')); |
||
33 | |||
34 | // This one setting is worth bearing in mind. If you are running this from proper cron, make sure you |
||
35 | // don't run this file any more frequently than indicated here. It might turn ugly if you do. |
||
36 | // But on proper cron you can always increase this value provided you don't go beyond max_limit. |
||
37 | define('MAX_CRON_TIME', 10); |
||
38 | // If a task fails for whatever reason it will still be marked as claimed. This is the threshold |
||
39 | // by which if a task has not completed in this time, the task should become available again. |
||
40 | define('MAX_CLAIM_THRESHOLD', 300); |
||
41 | |||
42 | // We're going to want a few globals... these are all set later. |
||
43 | global $maintenance, $msubject, $mmessage, $mbname, $language; |
||
44 | global $boardurl, $boarddir, $sourcedir, $webmaster_email; |
||
45 | global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error; |
||
46 | global $db_connection, $modSettings, $context, $sc, $user_info, $txt; |
||
47 | global $smcFunc, $ssi_db_user, $scripturl, $db_passwd, $cachedir; |
||
48 | |||
49 | if (!defined('TIME_START')) |
||
50 | define('TIME_START', microtime(true)); |
||
51 | |||
52 | // Just being safe... |
||
53 | foreach (array('db_character_set', 'cachedir') as $variable) |
||
54 | if (isset($GLOBALS[$variable])) |
||
55 | unset($GLOBALS[$variable]); |
||
56 | |||
57 | // Get the forum's settings for database and file paths. |
||
58 | require_once(dirname(__FILE__) . '/Settings.php'); |
||
59 | |||
60 | // Make absolutely sure the cache directory is defined and writable. |
||
61 | if (empty($cachedir) || !is_dir($cachedir) || !is_writable($cachedir)) |
||
62 | { |
||
63 | if (is_dir($boarddir . '/cache') && is_writable($boarddir . '/cache')) |
||
64 | $cachedir = $boarddir . '/cache'; |
||
65 | else |
||
66 | { |
||
67 | $cachedir = sys_get_temp_dir() . '/smf_cache_' . md5($boarddir); |
||
68 | @mkdir($cachedir, 0750); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | // Don't do john didley if the forum's been shut down completely. |
||
73 | if ($maintenance == 2) |
||
74 | die($mmessage); |
||
75 | |||
76 | // Fix for using the current directory as a path. |
||
77 | if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.') |
||
78 | $sourcedir = dirname(__FILE__) . substr($sourcedir, 1); |
||
79 | |||
80 | // Do nothing if we are in the middle of an install or upgrade. |
||
81 | if (!empty($upgradeData) || !empty($package_installing)) |
||
82 | obExit_cron(); |
||
83 | |||
84 | // Have we already turned this off? If so, exist gracefully. |
||
85 | if (file_exists($cachedir . '/cron.lock')) |
||
86 | obExit_cron(); |
||
87 | |||
88 | // Before we go any further, if this is not a CLI request, we need to do some checking. |
||
89 | if (!FROM_CLI) |
||
90 | { |
||
91 | // When using sub-domains with SSI and ssi_themes set, browsers will receive a "Access-Control-Allow-Origin" error. |
||
92 | // * is not ideal but the best method to preventing this from occurring. |
||
93 | header('Access-Control-Allow-Origin: *'); |
||
94 | |||
95 | // We will clean up $_GET shortly. But we want to this ASAP. |
||
96 | $ts = isset($_GET['ts']) ? (int) $_GET['ts'] : 0; |
||
97 | if ($ts <= 0 || $ts % 15 != 0 || time() - $ts < 0 || time() - $ts > 20) |
||
98 | obExit_cron(); |
||
99 | } |
||
100 | |||
101 | else |
||
102 | $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0'; |
||
103 | |||
104 | // Load the most important includes. In general, a background should be loading its own dependencies. |
||
105 | require_once($sourcedir . '/Errors.php'); |
||
106 | require_once($sourcedir . '/Load.php'); |
||
107 | require_once($sourcedir . '/Security.php'); |
||
108 | require_once($sourcedir . '/Subs.php'); |
||
109 | |||
110 | // Ensure we don't trip over disabled internal functions |
||
111 | if (version_compare(PHP_VERSION, '8.0.0', '>=')) |
||
112 | require_once($sourcedir . '/Subs-Compat.php'); |
||
113 | |||
114 | // Create a variable to store some SMF specific functions in. |
||
115 | $smcFunc = array(); |
||
116 | |||
117 | // This is our general bootstrap, a la SSI.php but with a few differences. |
||
118 | unset ($db_show_debug); |
||
119 | loadDatabase(); |
||
120 | reloadSettings(); |
||
121 | |||
122 | // Just in case there's a problem... |
||
123 | set_error_handler('smf_error_handler_cron'); |
||
124 | set_exception_handler('smf_exception_handler_cron'); |
||
125 | $sc = ''; |
||
126 | $_SERVER['QUERY_STRING'] = ''; |
||
127 | $_SERVER['REQUEST_URL'] = FROM_CLI ? 'CLI cron.php' : $boardurl . '/cron.php'; |
||
128 | |||
129 | // Now 'clean the request' (or more accurately, ignore everything we're not going to use) |
||
130 | cleanRequest_cron(); |
||
131 | |||
132 | // At this point we could reseed the RNG but I don't think we need to risk it being seeded *even more*. |
||
133 | // Meanwhile, time we got on with the real business here. |
||
134 | while ($task_details = fetch_task()) |
||
135 | { |
||
136 | $result = perform_task($task_details); |
||
137 | if ($result) |
||
138 | { |
||
139 | $smcFunc['db_query']('', ' |
||
140 | DELETE FROM {db_prefix}background_tasks |
||
141 | WHERE id_task = {int:task}', |
||
142 | array( |
||
143 | 'task' => $task_details['id_task'], |
||
144 | ) |
||
145 | ); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // If we have time, check the scheduled tasks. |
||
150 | if (time() - TIME_START < ceil(MAX_CRON_TIME / 2)) |
||
151 | { |
||
152 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
153 | |||
154 | if (empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time()) |
||
155 | AutoTask(); |
||
156 | elseif (!empty($modSettings['mail_next_send']) && $modSettings['mail_next_send'] < time()) |
||
157 | ReduceMailQueue(); |
||
158 | } |
||
159 | |||
160 | obExit_cron(); |
||
161 | exit; |
||
162 | |||
163 | /** |
||
164 | * The heart of this cron handler... |
||
165 | * |
||
166 | * @return bool|array False if there's nothing to do or an array of info about the task |
||
167 | */ |
||
168 | function fetch_task() |
||
169 | { |
||
170 | global $smcFunc; |
||
171 | |||
172 | // Check we haven't run over our time limit. |
||
173 | if (microtime(true) - TIME_START > MAX_CRON_TIME) |
||
174 | return false; |
||
175 | |||
176 | // Try to find a task. Specifically, try to find one that hasn't been claimed previously, or failing that, |
||
177 | // a task that was claimed but failed for whatever reason and failed long enough ago. We should not care |
||
178 | // what task it is, merely that it is one in the queue, the order is irrelevant. |
||
179 | $request = $smcFunc['db_query']('', ' |
||
180 | SELECT id_task, task_file, task_class, task_data, claimed_time |
||
181 | FROM {db_prefix}background_tasks |
||
182 | WHERE claimed_time < {int:claim_limit} |
||
183 | LIMIT 1', |
||
184 | array( |
||
185 | 'claim_limit' => time() - MAX_CLAIM_THRESHOLD, |
||
186 | ) |
||
187 | ); |
||
188 | if ($row = $smcFunc['db_fetch_assoc']($request)) |
||
189 | { |
||
190 | // We found one. Let's try and claim it immediately. |
||
191 | $smcFunc['db_free_result']($request); |
||
192 | $smcFunc['db_query']('', ' |
||
193 | UPDATE {db_prefix}background_tasks |
||
194 | SET claimed_time = {int:new_claimed} |
||
195 | WHERE id_task = {int:task} |
||
196 | AND claimed_time = {int:old_claimed}', |
||
197 | array( |
||
198 | 'new_claimed' => time(), |
||
199 | 'task' => $row['id_task'], |
||
200 | 'old_claimed' => $row['claimed_time'], |
||
201 | ) |
||
202 | ); |
||
203 | // Could we claim it? If so, return it back. |
||
204 | if ($smcFunc['db_affected_rows']() != 0) |
||
205 | { |
||
206 | // Update the time and go back. |
||
207 | $row['claimed_time'] = time(); |
||
208 | return $row; |
||
209 | } |
||
210 | else |
||
211 | { |
||
212 | // Uh oh, we just missed it. Try to claim another one, and let it fall through if there aren't any. |
||
213 | return fetch_task(); |
||
214 | } |
||
215 | } |
||
216 | else |
||
217 | { |
||
218 | // No dice. Clean up and go home. |
||
219 | $smcFunc['db_free_result']($request); |
||
220 | return false; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * This actually handles the task |
||
226 | * |
||
227 | * @param array $task_details An array of info about the task |
||
228 | * @return bool|void True if the task is invalid; otherwise calls the function to execute the task |
||
229 | */ |
||
230 | function perform_task($task_details) |
||
231 | { |
||
232 | global $smcFunc, $sourcedir, $boarddir; |
||
233 | |||
234 | // This indicates the file to load. |
||
235 | if (!empty($task_details['task_file'])) |
||
236 | { |
||
237 | $include = strtr(trim($task_details['task_file']), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir)); |
||
238 | if (file_exists($include)) |
||
239 | require_once($include); |
||
240 | } |
||
241 | |||
242 | if (empty($task_details['task_class'])) |
||
243 | { |
||
244 | // This would be nice to translate but the language files aren't loaded for any specific language. |
||
245 | log_error('Invalid background task specified (no class, ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')'); |
||
246 | return true; // So we clear it from the queue. |
||
247 | } |
||
248 | |||
249 | // All background tasks need to be classes. |
||
250 | elseif (class_exists($task_details['task_class']) && is_subclass_of($task_details['task_class'], 'SMF_BackgroundTask')) |
||
251 | { |
||
252 | $details = empty($task_details['task_data']) ? array() : $smcFunc['json_decode']($task_details['task_data'], true); |
||
253 | $bgtask = new $task_details['task_class']($details); |
||
254 | return $bgtask->execute(); |
||
255 | } |
||
256 | else |
||
257 | { |
||
258 | log_error('Invalid background task specified: (class: ' . $task_details['task_class'] . ', ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')'); |
||
259 | return true; // So we clear it from the queue. |
||
260 | } |
||
261 | } |
||
262 | |||
263 | // These are all our helper functions that resemble their big brother counterparts. These are not so important. |
||
264 | /** |
||
265 | * Cleans up the request variables |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | function cleanRequest_cron() |
||
270 | { |
||
271 | global $scripturl, $boardurl; |
||
272 | |||
273 | $scripturl = $boardurl . '/index.php'; |
||
274 | |||
275 | // These keys shouldn't be set...ever. |
||
276 | if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS'])) |
||
277 | die('Invalid request variable.'); |
||
0 ignored issues
–
show
|
|||
278 | |||
279 | // Save some memory.. (since we don't use these anyway.) |
||
280 | unset($GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_POST_VARS']); |
||
281 | unset($GLOBALS['HTTP_POST_FILES'], $GLOBALS['HTTP_POST_FILES']); |
||
282 | unset($GLOBALS['_GET'], $GLOBALS['_POST'], $GLOBALS['_REQUEST'], $GLOBALS['_COOKIE'], $GLOBALS['_FILES']); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * The error handling function |
||
287 | * |
||
288 | * @param int $error_level One of the PHP error level constants (see ) |
||
289 | * @param string $error_string The error message |
||
290 | * @param string $file The file where the error occurred |
||
291 | * @param int $line What line of the specified file the error occurred on |
||
292 | * @return void |
||
293 | */ |
||
294 | function smf_error_handler_cron($error_level, $error_string, $file, $line) |
||
295 | { |
||
296 | global $modSettings; |
||
297 | |||
298 | // Ignore errors that should not be logged. |
||
299 | if (error_reporting() == 0) |
||
300 | return; |
||
301 | |||
302 | $error_type = 'cron'; |
||
303 | |||
304 | log_error($error_level . ': ' . $error_string, $error_type, $file, $line); |
||
305 | |||
306 | // If this is an E_ERROR or E_USER_ERROR.... die. Violently so. |
||
307 | if ($error_level % 255 == E_ERROR) |
||
308 | die('No direct access...'); |
||
0 ignored issues
–
show
|
|||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Generic handler for uncaught exceptions. |
||
313 | * |
||
314 | * Always ends execution. |
||
315 | * |
||
316 | * @param \Throwable $e The uncaught exception. |
||
317 | */ |
||
318 | function smf_exception_handler_cron(\Throwable $e) |
||
319 | { |
||
320 | global $modSettings, $txt; |
||
321 | |||
322 | loadLanguage('Errors'); |
||
323 | |||
324 | $message = $txt[$e->getMessage()] ?? $e->getMessage(); |
||
325 | |||
326 | if (!empty($modSettings['enableErrorLogging'])) { |
||
327 | log_error($message, 'cron', $e->getFile(), $e->getLine()); |
||
328 | } |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * The exit function |
||
333 | */ |
||
334 | function obExit_cron() |
||
335 | { |
||
336 | if (FROM_CLI) |
||
337 | die(0); |
||
0 ignored issues
–
show
|
|||
338 | else |
||
339 | { |
||
340 | header('content-type: image/gif'); |
||
341 | die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B"); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | // We would like this to be defined, but we don't want to have to load more stuff than necessary. |
||
346 | // Thus we declare it here, and any legitimate background task must implement this. |
||
347 | /** |
||
348 | * Class SMF_BackgroundTask |
||
349 | */ |
||
350 | abstract class SMF_BackgroundTask |
||
351 | { |
||
352 | /** |
||
353 | * Constants for notification types. |
||
354 | */ |
||
355 | const RECEIVE_NOTIFY_EMAIL = 0x02; |
||
356 | const RECEIVE_NOTIFY_ALERT = 0x01; |
||
357 | |||
358 | /** |
||
359 | * @var array Holds the details for the task |
||
360 | */ |
||
361 | protected $_details; |
||
362 | |||
363 | /** |
||
364 | * @var array Temp property to hold the current user info while tasks make use of $user_info |
||
365 | */ |
||
366 | private $current_user_info = array(); |
||
367 | |||
368 | /** |
||
369 | * The constructor. |
||
370 | * |
||
371 | * @param array $details The details for the task |
||
372 | */ |
||
373 | public function __construct($details) |
||
374 | { |
||
375 | global $user_info; |
||
376 | |||
377 | $this->_details = $details; |
||
378 | |||
379 | $this->current_user_info = $user_info; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * The function to actually execute a task |
||
384 | * |
||
385 | * @return mixed |
||
386 | */ |
||
387 | abstract public function execute(); |
||
388 | |||
389 | /** |
||
390 | * Loads minimal info for the previously loaded user ids |
||
391 | * |
||
392 | * @param array $user_ids |
||
393 | * @return array |
||
394 | * @throws Exception |
||
395 | */ |
||
396 | public function getMinUserInfo($user_ids = array()) |
||
397 | { |
||
398 | return loadMinUserInfo($user_ids); |
||
399 | } |
||
400 | |||
401 | public function __destruct() |
||
402 | { |
||
403 | global $user_info; |
||
404 | |||
405 | $user_info = $this->current_user_info; |
||
406 | } |
||
407 | } |
||
408 | |||
409 | ?> |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.