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 http://www.simplemachines.org |
17
|
|
|
* @copyright 2019 Simple Machines and individual contributors |
18
|
|
|
* @license http://www.simplemachines.org/about/smf/license.php BSD |
19
|
|
|
* |
20
|
|
|
* @version 2.1 RC1 |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
define('SMF', 'BACKGROUND'); |
24
|
|
|
define('SMF_VERSION', '2.1 RC1'); |
25
|
|
|
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION); |
26
|
|
|
define('SMF_SOFTWARE_YEAR', '2019'); |
27
|
|
|
define('FROM_CLI', empty($_SERVER['REQUEST_METHOD'])); |
28
|
|
|
|
29
|
|
|
// This one setting is worth bearing in mind. If you are running this from proper cron, make sure you |
30
|
|
|
// don't run this file any more frequently than indicated here. It might turn ugly if you do. |
31
|
|
|
// But on proper cron you can always increase this value provided you don't go beyond max_limit. |
32
|
|
|
define('MAX_CRON_TIME', 10); |
33
|
|
|
// If a task fails for whatever reason it will still be marked as claimed. This is the threshold |
34
|
|
|
// by which if a task has not completed in this time, the task should become available again. |
35
|
|
|
define('MAX_CLAIM_THRESHOLD', 300); |
36
|
|
|
|
37
|
|
|
// We're going to want a few globals... these are all set later. |
38
|
|
|
global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language; |
39
|
|
|
global $boardurl, $boarddir, $sourcedir, $webmaster_email; |
40
|
|
|
global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error; |
41
|
|
|
global $db_connection, $modSettings, $context, $sc, $user_info, $txt; |
42
|
|
|
global $smcFunc, $ssi_db_user, $scripturl, $db_passwd, $cachedir; |
43
|
|
|
|
44
|
|
|
define('TIME_START', microtime(true)); |
45
|
|
|
|
46
|
|
|
// Just being safe... |
47
|
|
|
foreach (array('db_character_set', 'cachedir') as $variable) |
48
|
|
|
if (isset($GLOBALS[$variable])) |
49
|
|
|
unset($GLOBALS[$variable]); |
50
|
|
|
|
51
|
|
|
// Get the forum's settings for database and file paths. |
52
|
|
|
require_once(dirname(__FILE__) . '/Settings.php'); |
53
|
|
|
|
54
|
|
|
// Make absolutely sure the cache directory is defined. |
55
|
|
|
if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache')) |
56
|
|
|
$cachedir = $boarddir . '/cache'; |
57
|
|
|
|
58
|
|
|
// Don't do john didley if the forum's been shut down completely. |
59
|
|
|
if ($maintenance == 2) |
60
|
|
|
die($mmessage); |
61
|
|
|
|
62
|
|
|
// Fix for using the current directory as a path. |
63
|
|
|
if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.') |
64
|
|
|
$sourcedir = dirname(__FILE__) . substr($sourcedir, 1); |
65
|
|
|
|
66
|
|
|
// Have we already turned this off? If so, exist gracefully. |
67
|
|
|
if (file_exists($cachedir . '/cron.lock')) |
68
|
|
|
obExit_cron(); |
69
|
|
|
|
70
|
|
|
// Before we go any further, if this is not a CLI request, we need to do some checking. |
71
|
|
|
if (!FROM_CLI) |
72
|
|
|
{ |
73
|
|
|
// We will clean up $_GET shortly. But we want to this ASAP. |
74
|
|
|
$ts = isset($_GET['ts']) ? (int) $_GET['ts'] : 0; |
75
|
|
|
if ($ts <= 0 || $ts % 15 != 0 || time() - $ts < 0 || time() - $ts > 20) |
76
|
|
|
obExit_cron(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Load the most important includes. In general, a background should be loading its own dependencies. |
80
|
|
|
require_once($sourcedir . '/Errors.php'); |
81
|
|
|
require_once($sourcedir . '/Load.php'); |
82
|
|
|
require_once($sourcedir . '/Subs.php'); |
83
|
|
|
|
84
|
|
|
// Create a variable to store some SMF specific functions in. |
85
|
|
|
$smcFunc = array(); |
86
|
|
|
|
87
|
|
|
// This is our general bootstrap, a la SSI.php but with a few differences. |
88
|
|
|
unset ($db_show_debug); |
89
|
|
|
loadDatabase(); |
90
|
|
|
reloadSettings(); |
91
|
|
|
|
92
|
|
|
// Just in case there's a problem... |
93
|
|
|
set_error_handler('smf_error_handler_cron'); |
94
|
|
|
$sc = ''; |
95
|
|
|
$_SERVER['QUERY_STRING'] = ''; |
96
|
|
|
$_SERVER['REQUEST_URL'] = FROM_CLI ? 'CLI cron.php' : $boardurl . '/cron.php'; |
97
|
|
|
|
98
|
|
|
// Now 'clean the request' (or more accurately, ignore everything we're not going to use) |
99
|
|
|
cleanRequest_cron(); |
100
|
|
|
|
101
|
|
|
// At this point we could reseed the RNG but I don't think we need to risk it being seeded *even more*. |
102
|
|
|
// Meanwhile, time we got on with the real business here. |
103
|
|
|
while ($task_details = fetch_task()) |
104
|
|
|
{ |
105
|
|
|
$result = perform_task($task_details); |
106
|
|
|
if ($result) |
107
|
|
|
{ |
108
|
|
|
$smcFunc['db_query']('', ' |
109
|
|
|
DELETE FROM {db_prefix}background_tasks |
110
|
|
|
WHERE id_task = {int:task}', |
111
|
|
|
array( |
112
|
|
|
'task' => $task_details['id_task'], |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
obExit_cron(); |
118
|
|
|
exit; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* The heart of this cron handler... |
122
|
|
|
* |
123
|
|
|
* @return bool|array False if there's nothing to do or an array of info about the task |
124
|
|
|
*/ |
125
|
|
|
function fetch_task() |
126
|
|
|
{ |
127
|
|
|
global $smcFunc; |
128
|
|
|
|
129
|
|
|
// Check we haven't run over our time limit. |
130
|
|
|
if (microtime(true) - TIME_START > MAX_CRON_TIME) |
131
|
|
|
return false; |
132
|
|
|
|
133
|
|
|
// Try to find a task. Specifically, try to find one that hasn't been claimed previously, or failing that, |
134
|
|
|
// a task that was claimed but failed for whatever reason and failed long enough ago. We should not care |
135
|
|
|
// what task it is, merely that it is one in the queue, the order is irrelevant. |
136
|
|
|
$request = $smcFunc['db_query']('', ' |
137
|
|
|
SELECT id_task, task_file, task_class, task_data, claimed_time |
138
|
|
|
FROM {db_prefix}background_tasks |
139
|
|
|
WHERE claimed_time < {int:claim_limit} |
140
|
|
|
LIMIT 1', |
141
|
|
|
array( |
142
|
|
|
'claim_limit' => time() - MAX_CLAIM_THRESHOLD, |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
if ($row = $smcFunc['db_fetch_assoc']($request)) |
146
|
|
|
{ |
147
|
|
|
// We found one. Let's try and claim it immediately. |
148
|
|
|
$smcFunc['db_free_result']($request); |
149
|
|
|
$smcFunc['db_query']('', ' |
150
|
|
|
UPDATE {db_prefix}background_tasks |
151
|
|
|
SET claimed_time = {int:new_claimed} |
152
|
|
|
WHERE id_task = {int:task} |
153
|
|
|
AND claimed_time = {int:old_claimed}', |
154
|
|
|
array( |
155
|
|
|
'new_claimed' => time(), |
156
|
|
|
'task' => $row['id_task'], |
157
|
|
|
'old_claimed' => $row['claimed_time'], |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
// Could we claim it? If so, return it back. |
161
|
|
|
if ($smcFunc['db_affected_rows']() != 0) |
162
|
|
|
{ |
163
|
|
|
// Update the time and go back. |
164
|
|
|
$row['claimed_time'] = time(); |
165
|
|
|
return $row; |
166
|
|
|
} |
167
|
|
|
else |
168
|
|
|
{ |
169
|
|
|
// Uh oh, we just missed it. Try to claim another one, and let it fall through if there aren't any. |
170
|
|
|
return fetch_task(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
else |
174
|
|
|
{ |
175
|
|
|
// No dice. Clean up and go home. |
176
|
|
|
$smcFunc['db_free_result']($request); |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* This actually handles the task |
183
|
|
|
* |
184
|
|
|
* @param array $task_details An array of info about the task |
185
|
|
|
* @return bool|void True if the task is invalid; otherwise calls the function to execute the task |
186
|
|
|
*/ |
187
|
|
|
function perform_task($task_details) |
188
|
|
|
{ |
189
|
|
|
global $smcFunc, $sourcedir, $boarddir; |
190
|
|
|
|
191
|
|
|
// This indicates the file to load. |
192
|
|
|
if (!empty($task_details['task_file'])) |
193
|
|
|
{ |
194
|
|
|
$include = strtr(trim($task_details['task_file']), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir)); |
195
|
|
|
if (file_exists($include)) |
196
|
|
|
require_once($include); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (empty($task_details['task_class'])) |
200
|
|
|
{ |
201
|
|
|
// This would be nice to translate but the language files aren't loaded for any specific language. |
202
|
|
|
log_error('Invalid background task specified (no class, ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')'); |
203
|
|
|
return true; // So we clear it from the queue. |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// All background tasks need to be classes. |
207
|
|
|
elseif (class_exists($task_details['task_class']) && is_subclass_of($task_details['task_class'], 'SMF_BackgroundTask')) |
208
|
|
|
{ |
209
|
|
|
$details = empty($task_details['task_data']) ? array() : $smcFunc['json_decode']($task_details['task_data'], true); |
210
|
|
|
$bgtask = new $task_details['task_class']($details); |
211
|
|
|
return $bgtask->execute(); |
212
|
|
|
} |
213
|
|
|
else |
214
|
|
|
{ |
215
|
|
|
log_error('Invalid background task specified: (class: ' . $task_details['task_class'] . ', ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')'); |
216
|
|
|
return true; // So we clear it from the queue. |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// These are all our helper functions that resemble their big brother counterparts. These are not so important. |
221
|
|
|
/** |
222
|
|
|
* Cleans up the request variables |
223
|
|
|
* |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
|
function cleanRequest_cron() |
227
|
|
|
{ |
228
|
|
|
global $scripturl, $boardurl; |
229
|
|
|
|
230
|
|
|
$scripturl = $boardurl . '/index.php'; |
231
|
|
|
|
232
|
|
|
// These keys shouldn't be set...ever. |
233
|
|
|
if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS'])) |
234
|
|
|
die('Invalid request variable.'); |
|
|
|
|
235
|
|
|
|
236
|
|
|
// Save some memory.. (since we don't use these anyway.) |
237
|
|
|
unset($GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_POST_VARS']); |
238
|
|
|
unset($GLOBALS['HTTP_POST_FILES'], $GLOBALS['HTTP_POST_FILES']); |
239
|
|
|
unset($GLOBALS['_GET'], $GLOBALS['_POST'], $GLOBALS['_REQUEST'], $GLOBALS['_COOKIE'], $GLOBALS['_FILES']); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* The error handling function |
244
|
|
|
* |
245
|
|
|
* @param int $error_level One of the PHP error level constants (see ) |
246
|
|
|
* @param string $error_string The error message |
247
|
|
|
* @param string $file The file where the error occurred |
248
|
|
|
* @param int $line What line of the specified file the error occurred on |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
function smf_error_handler_cron($error_level, $error_string, $file, $line) |
252
|
|
|
{ |
253
|
|
|
global $modSettings; |
254
|
|
|
|
255
|
|
|
// Ignore errors if we're ignoring them or they are strict notices from PHP 5 |
256
|
|
|
if (error_reporting() == 0) |
257
|
|
|
return; |
258
|
|
|
|
259
|
|
|
$error_type = 'cron'; |
260
|
|
|
|
261
|
|
|
log_error($error_level . ': ' . $error_string, $error_type, $file, $line); |
262
|
|
|
|
263
|
|
|
// If this is an E_ERROR or E_USER_ERROR.... die. Violently so. |
264
|
|
|
if ($error_level % 255 == E_ERROR) |
265
|
|
|
die('No direct access...'); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* The exit function |
270
|
|
|
*/ |
271
|
|
|
function obExit_cron() |
272
|
|
|
{ |
273
|
|
|
if (FROM_CLI) |
274
|
|
|
die(0); |
|
|
|
|
275
|
|
|
else |
276
|
|
|
{ |
277
|
|
|
header('content-type: image/gif'); |
278
|
|
|
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"); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// We would like this to be defined, but we don't want to have to load more stuff than necessary. |
283
|
|
|
// Thus we declare it here, and any legitimate background task must implement this. |
284
|
|
|
/** |
285
|
|
|
* Class SMF_BackgroundTask |
286
|
|
|
*/ |
287
|
|
|
abstract class SMF_BackgroundTask |
288
|
|
|
{ |
289
|
|
|
/** |
290
|
|
|
* Constants for notfication types. |
291
|
|
|
*/ |
292
|
|
|
const RECEIVE_NOTIFY_EMAIL = 0x02; |
293
|
|
|
const RECEIVE_NOTIFY_ALERT = 0x01; |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @var array Holds the details for the task |
297
|
|
|
*/ |
298
|
|
|
protected $_details; |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* The constructor. |
302
|
|
|
* |
303
|
|
|
* @param array $details The details for the task |
304
|
|
|
*/ |
305
|
|
|
public function __construct($details) |
306
|
|
|
{ |
307
|
|
|
$this->_details = $details; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* The function to actually execute a task |
312
|
|
|
* |
313
|
|
|
* @return mixed |
314
|
|
|
*/ |
315
|
|
|
abstract public function execute(); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
?> |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.