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 2022 Simple Machines and individual contributors |
||
18 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
19 | * |
||
20 | * @version 2.1.2 |
||
21 | */ |
||
22 | |||
23 | define('SMF', 'BACKGROUND'); |
||
24 | define('SMF_VERSION', '2.1.2'); |
||
25 | define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION); |
||
26 | define('SMF_SOFTWARE_YEAR', '2022'); |
||
27 | define('FROM_CLI', empty($_SERVER['REQUEST_METHOD'])); |
||
28 | |||
29 | define('JQUERY_VERSION', '3.6.0'); |
||
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 | // Have we already turned this off? If so, exist gracefully. |
||
81 | if (file_exists($cachedir . '/cron.lock')) |
||
82 | obExit_cron(); |
||
83 | |||
84 | // Before we go any further, if this is not a CLI request, we need to do some checking. |
||
85 | if (!FROM_CLI) |
||
86 | { |
||
87 | // When using sub-domains with SSI and ssi_themes set, browsers will receive a "Access-Control-Allow-Origin" error. |
||
88 | // * is not ideal but the best method to preventing this from occurring. |
||
89 | header('Access-Control-Allow-Origin: *'); |
||
90 | |||
91 | // We will clean up $_GET shortly. But we want to this ASAP. |
||
92 | $ts = isset($_GET['ts']) ? (int) $_GET['ts'] : 0; |
||
93 | if ($ts <= 0 || $ts % 15 != 0 || time() - $ts < 0 || time() - $ts > 20) |
||
94 | obExit_cron(); |
||
95 | } |
||
96 | |||
97 | else |
||
98 | $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0'; |
||
99 | |||
100 | // Load the most important includes. In general, a background should be loading its own dependencies. |
||
101 | require_once($sourcedir . '/Errors.php'); |
||
102 | require_once($sourcedir . '/Load.php'); |
||
103 | require_once($sourcedir . '/Security.php'); |
||
104 | require_once($sourcedir . '/Subs.php'); |
||
105 | |||
106 | // Ensure we don't trip over disabled internal functions |
||
107 | if (version_compare(PHP_VERSION, '8.0.0', '>=')) |
||
108 | require_once($sourcedir . '/Subs-Compat.php'); |
||
109 | |||
110 | // Create a variable to store some SMF specific functions in. |
||
111 | $smcFunc = array(); |
||
112 | |||
113 | // This is our general bootstrap, a la SSI.php but with a few differences. |
||
114 | unset ($db_show_debug); |
||
115 | loadDatabase(); |
||
116 | reloadSettings(); |
||
117 | |||
118 | // Just in case there's a problem... |
||
119 | set_error_handler('smf_error_handler_cron'); |
||
120 | $sc = ''; |
||
121 | $_SERVER['QUERY_STRING'] = ''; |
||
122 | $_SERVER['REQUEST_URL'] = FROM_CLI ? 'CLI cron.php' : $boardurl . '/cron.php'; |
||
123 | |||
124 | // Now 'clean the request' (or more accurately, ignore everything we're not going to use) |
||
125 | cleanRequest_cron(); |
||
126 | |||
127 | // At this point we could reseed the RNG but I don't think we need to risk it being seeded *even more*. |
||
128 | // Meanwhile, time we got on with the real business here. |
||
129 | while ($task_details = fetch_task()) |
||
130 | { |
||
131 | $result = perform_task($task_details); |
||
132 | if ($result) |
||
133 | { |
||
134 | $smcFunc['db_query']('', ' |
||
135 | DELETE FROM {db_prefix}background_tasks |
||
136 | WHERE id_task = {int:task}', |
||
137 | array( |
||
138 | 'task' => $task_details['id_task'], |
||
139 | ) |
||
140 | ); |
||
141 | } |
||
142 | } |
||
143 | obExit_cron(); |
||
144 | exit; |
||
145 | |||
146 | /** |
||
147 | * The heart of this cron handler... |
||
148 | * |
||
149 | * @return bool|array False if there's nothing to do or an array of info about the task |
||
150 | */ |
||
151 | function fetch_task() |
||
152 | { |
||
153 | global $smcFunc; |
||
154 | |||
155 | // Check we haven't run over our time limit. |
||
156 | if (microtime(true) - TIME_START > MAX_CRON_TIME) |
||
157 | return false; |
||
158 | |||
159 | // Try to find a task. Specifically, try to find one that hasn't been claimed previously, or failing that, |
||
160 | // a task that was claimed but failed for whatever reason and failed long enough ago. We should not care |
||
161 | // what task it is, merely that it is one in the queue, the order is irrelevant. |
||
162 | $request = $smcFunc['db_query']('', ' |
||
163 | SELECT id_task, task_file, task_class, task_data, claimed_time |
||
164 | FROM {db_prefix}background_tasks |
||
165 | WHERE claimed_time < {int:claim_limit} |
||
166 | LIMIT 1', |
||
167 | array( |
||
168 | 'claim_limit' => time() - MAX_CLAIM_THRESHOLD, |
||
169 | ) |
||
170 | ); |
||
171 | if ($row = $smcFunc['db_fetch_assoc']($request)) |
||
172 | { |
||
173 | // We found one. Let's try and claim it immediately. |
||
174 | $smcFunc['db_free_result']($request); |
||
175 | $smcFunc['db_query']('', ' |
||
176 | UPDATE {db_prefix}background_tasks |
||
177 | SET claimed_time = {int:new_claimed} |
||
178 | WHERE id_task = {int:task} |
||
179 | AND claimed_time = {int:old_claimed}', |
||
180 | array( |
||
181 | 'new_claimed' => time(), |
||
182 | 'task' => $row['id_task'], |
||
183 | 'old_claimed' => $row['claimed_time'], |
||
184 | ) |
||
185 | ); |
||
186 | // Could we claim it? If so, return it back. |
||
187 | if ($smcFunc['db_affected_rows']() != 0) |
||
188 | { |
||
189 | // Update the time and go back. |
||
190 | $row['claimed_time'] = time(); |
||
191 | return $row; |
||
192 | } |
||
193 | else |
||
194 | { |
||
195 | // Uh oh, we just missed it. Try to claim another one, and let it fall through if there aren't any. |
||
196 | return fetch_task(); |
||
197 | } |
||
198 | } |
||
199 | else |
||
200 | { |
||
201 | // No dice. Clean up and go home. |
||
202 | $smcFunc['db_free_result']($request); |
||
203 | return false; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * This actually handles the task |
||
209 | * |
||
210 | * @param array $task_details An array of info about the task |
||
211 | * @return bool|void True if the task is invalid; otherwise calls the function to execute the task |
||
212 | */ |
||
213 | function perform_task($task_details) |
||
214 | { |
||
215 | global $smcFunc, $sourcedir, $boarddir; |
||
216 | |||
217 | // This indicates the file to load. |
||
218 | if (!empty($task_details['task_file'])) |
||
219 | { |
||
220 | $include = strtr(trim($task_details['task_file']), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir)); |
||
221 | if (file_exists($include)) |
||
222 | require_once($include); |
||
223 | } |
||
224 | |||
225 | if (empty($task_details['task_class'])) |
||
226 | { |
||
227 | // This would be nice to translate but the language files aren't loaded for any specific language. |
||
228 | log_error('Invalid background task specified (no class, ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')'); |
||
229 | return true; // So we clear it from the queue. |
||
230 | } |
||
231 | |||
232 | // All background tasks need to be classes. |
||
233 | elseif (class_exists($task_details['task_class']) && is_subclass_of($task_details['task_class'], 'SMF_BackgroundTask')) |
||
234 | { |
||
235 | $details = empty($task_details['task_data']) ? array() : $smcFunc['json_decode']($task_details['task_data'], true); |
||
236 | $bgtask = new $task_details['task_class']($details); |
||
237 | return $bgtask->execute(); |
||
238 | } |
||
239 | else |
||
240 | { |
||
241 | log_error('Invalid background task specified: (class: ' . $task_details['task_class'] . ', ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')'); |
||
242 | return true; // So we clear it from the queue. |
||
243 | } |
||
244 | } |
||
245 | |||
246 | // These are all our helper functions that resemble their big brother counterparts. These are not so important. |
||
247 | /** |
||
248 | * Cleans up the request variables |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | function cleanRequest_cron() |
||
253 | { |
||
254 | global $scripturl, $boardurl; |
||
255 | |||
256 | $scripturl = $boardurl . '/index.php'; |
||
257 | |||
258 | // These keys shouldn't be set...ever. |
||
259 | if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS'])) |
||
260 | die('Invalid request variable.'); |
||
0 ignored issues
–
show
|
|||
261 | |||
262 | // Save some memory.. (since we don't use these anyway.) |
||
263 | unset($GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_POST_VARS']); |
||
264 | unset($GLOBALS['HTTP_POST_FILES'], $GLOBALS['HTTP_POST_FILES']); |
||
265 | unset($GLOBALS['_GET'], $GLOBALS['_POST'], $GLOBALS['_REQUEST'], $GLOBALS['_COOKIE'], $GLOBALS['_FILES']); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * The error handling function |
||
270 | * |
||
271 | * @param int $error_level One of the PHP error level constants (see ) |
||
272 | * @param string $error_string The error message |
||
273 | * @param string $file The file where the error occurred |
||
274 | * @param int $line What line of the specified file the error occurred on |
||
275 | * @return void |
||
276 | */ |
||
277 | function smf_error_handler_cron($error_level, $error_string, $file, $line) |
||
278 | { |
||
279 | global $modSettings; |
||
280 | |||
281 | // Ignore errors that should not be logged. |
||
282 | if (error_reporting() == 0) |
||
283 | return; |
||
284 | |||
285 | $error_type = 'cron'; |
||
286 | |||
287 | log_error($error_level . ': ' . $error_string, $error_type, $file, $line); |
||
288 | |||
289 | // If this is an E_ERROR or E_USER_ERROR.... die. Violently so. |
||
290 | if ($error_level % 255 == E_ERROR) |
||
291 | die('No direct access...'); |
||
0 ignored issues
–
show
|
|||
292 | } |
||
293 | |||
294 | /** |
||
295 | * The exit function |
||
296 | */ |
||
297 | function obExit_cron() |
||
298 | { |
||
299 | if (FROM_CLI) |
||
300 | die(0); |
||
0 ignored issues
–
show
|
|||
301 | else |
||
302 | { |
||
303 | header('content-type: image/gif'); |
||
304 | 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"); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | // We would like this to be defined, but we don't want to have to load more stuff than necessary. |
||
309 | // Thus we declare it here, and any legitimate background task must implement this. |
||
310 | /** |
||
311 | * Class SMF_BackgroundTask |
||
312 | */ |
||
313 | abstract class SMF_BackgroundTask |
||
314 | { |
||
315 | /** |
||
316 | * Constants for notification types. |
||
317 | */ |
||
318 | const RECEIVE_NOTIFY_EMAIL = 0x02; |
||
319 | const RECEIVE_NOTIFY_ALERT = 0x01; |
||
320 | |||
321 | /** |
||
322 | * @var array Holds the details for the task |
||
323 | */ |
||
324 | protected $_details; |
||
325 | |||
326 | /** |
||
327 | * @var array Temp property to hold the current user info while tasks make use of $user_info |
||
328 | */ |
||
329 | private $current_user_info = array(); |
||
330 | |||
331 | /** |
||
332 | * The constructor. |
||
333 | * |
||
334 | * @param array $details The details for the task |
||
335 | */ |
||
336 | public function __construct($details) |
||
337 | { |
||
338 | global $user_info; |
||
339 | |||
340 | $this->_details = $details; |
||
341 | |||
342 | $this->current_user_info = $user_info; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * The function to actually execute a task |
||
347 | * |
||
348 | * @return mixed |
||
349 | */ |
||
350 | abstract public function execute(); |
||
351 | |||
352 | /** |
||
353 | * Loads minimal info for the previously loaded user ids |
||
354 | * |
||
355 | * @param array $user_ids |
||
356 | * @return array |
||
357 | * @throws Exception |
||
358 | */ |
||
359 | public function getMinUserInfo($user_ids = array()) |
||
360 | { |
||
361 | return loadMinUserInfo($user_ids); |
||
362 | } |
||
363 | |||
364 | public function __destruct() |
||
365 | { |
||
366 | global $user_info; |
||
367 | |||
368 | $user_info = $this->current_user_info; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | ?> |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.