1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is automatically called and handles all manner of scheduled things. |
||
5 | * |
||
6 | * Simple Machines Forum (SMF) |
||
7 | * |
||
8 | * @package SMF |
||
9 | * @author Simple Machines https://www.simplemachines.org |
||
10 | * @copyright 2022 Simple Machines and individual contributors |
||
11 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
12 | * |
||
13 | * @version 2.1.0 |
||
14 | */ |
||
15 | |||
16 | if (!defined('SMF')) |
||
17 | die('No direct access...'); |
||
18 | |||
19 | /** |
||
20 | * This function works out what to do! |
||
21 | */ |
||
22 | function AutoTask() |
||
23 | { |
||
24 | global $smcFunc; |
||
25 | |||
26 | // We bail out of index.php too early for these to be called. |
||
27 | frameOptionsHeader(); |
||
28 | corsPolicyHeader(); |
||
29 | |||
30 | // Requests from a CORS response may send a options to find if the requst is valid. Simply bail out here, the cors header have been sent already. |
||
31 | if (isset($_SERVER['HTTP_X_SMF_AJAX']) && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') |
||
32 | { |
||
33 | send_http_status(204); |
||
34 | die; |
||
35 | } |
||
36 | |||
37 | // Special case for doing the mail queue. |
||
38 | if (isset($_GET['scheduled']) && $_GET['scheduled'] == 'mailq') |
||
39 | ReduceMailQueue(); |
||
40 | else |
||
41 | { |
||
42 | $task_string = ''; |
||
43 | |||
44 | // Select the next task to do. |
||
45 | $request = $smcFunc['db_query']('', ' |
||
46 | SELECT id_task, task, next_time, time_offset, time_regularity, time_unit, callable |
||
47 | FROM {db_prefix}scheduled_tasks |
||
48 | WHERE disabled = {int:not_disabled} |
||
49 | AND next_time <= {int:current_time} |
||
50 | ORDER BY next_time ASC |
||
51 | LIMIT 1', |
||
52 | array( |
||
53 | 'not_disabled' => 0, |
||
54 | 'current_time' => time(), |
||
55 | ) |
||
56 | ); |
||
57 | if ($smcFunc['db_num_rows']($request) != 0) |
||
58 | { |
||
59 | // The two important things really... |
||
60 | $row = $smcFunc['db_fetch_assoc']($request); |
||
61 | |||
62 | // When should this next be run? |
||
63 | $next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']); |
||
64 | |||
65 | // How long in seconds it the gap? |
||
66 | $duration = $row['time_regularity']; |
||
67 | if ($row['time_unit'] == 'm') |
||
68 | $duration *= 60; |
||
69 | elseif ($row['time_unit'] == 'h') |
||
70 | $duration *= 3600; |
||
71 | elseif ($row['time_unit'] == 'd') |
||
72 | $duration *= 86400; |
||
73 | elseif ($row['time_unit'] == 'w') |
||
74 | $duration *= 604800; |
||
75 | |||
76 | // If we were really late running this task actually skip the next one. |
||
77 | if (time() + ($duration / 2) > $next_time) |
||
78 | $next_time += $duration; |
||
79 | |||
80 | // Update it now, so no others run this! |
||
81 | $smcFunc['db_query']('', ' |
||
82 | UPDATE {db_prefix}scheduled_tasks |
||
83 | SET next_time = {int:next_time} |
||
84 | WHERE id_task = {int:id_task} |
||
85 | AND next_time = {int:current_next_time}', |
||
86 | array( |
||
87 | 'next_time' => $next_time, |
||
88 | 'id_task' => $row['id_task'], |
||
89 | 'current_next_time' => $row['next_time'], |
||
90 | ) |
||
91 | ); |
||
92 | $affected_rows = $smcFunc['db_affected_rows'](); |
||
93 | |||
94 | // What kind of task are we handling? |
||
95 | if (!empty($row['callable'])) |
||
96 | $task_string = $row['callable']; |
||
97 | |||
98 | // Default SMF task or old mods? |
||
99 | elseif (function_exists('scheduled_' . $row['task'])) |
||
100 | $task_string = 'scheduled_' . $row['task']; |
||
101 | |||
102 | // One last resource, the task name. |
||
103 | elseif (!empty($row['task'])) |
||
104 | $task_string = $row['task']; |
||
105 | |||
106 | // The function must exist or we are wasting our time, plus do some timestamp checking, and database check! |
||
107 | if (!empty($task_string) && (!isset($_GET['ts']) || $_GET['ts'] == $row['next_time']) && $affected_rows) |
||
108 | { |
||
109 | ignore_user_abort(true); |
||
110 | |||
111 | // Get the callable. |
||
112 | $callable_task = call_helper($task_string, true); |
||
113 | |||
114 | // Perform the task. |
||
115 | if (!empty($callable_task)) |
||
116 | $completed = call_user_func($callable_task); |
||
117 | |||
118 | else |
||
119 | $completed = false; |
||
120 | |||
121 | // Log that we did it ;) |
||
122 | if ($completed) |
||
123 | { |
||
124 | $total_time = round(microtime(true) - TIME_START, 3); |
||
125 | $smcFunc['db_insert']('', |
||
126 | '{db_prefix}log_scheduled_tasks', |
||
127 | array( |
||
128 | 'id_task' => 'int', 'time_run' => 'int', 'time_taken' => 'float', |
||
129 | ), |
||
130 | array( |
||
131 | $row['id_task'], time(), (int) $total_time, |
||
132 | ), |
||
133 | array() |
||
134 | ); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | $smcFunc['db_free_result']($request); |
||
139 | |||
140 | // Get the next timestamp right. |
||
141 | $request = $smcFunc['db_query']('', ' |
||
142 | SELECT next_time |
||
143 | FROM {db_prefix}scheduled_tasks |
||
144 | WHERE disabled = {int:not_disabled} |
||
145 | ORDER BY next_time ASC |
||
146 | LIMIT 1', |
||
147 | array( |
||
148 | 'not_disabled' => 0, |
||
149 | ) |
||
150 | ); |
||
151 | // No new task scheduled yet? |
||
152 | if ($smcFunc['db_num_rows']($request) === 0) |
||
153 | $nextEvent = time() + 86400; |
||
154 | else |
||
155 | list ($nextEvent) = $smcFunc['db_fetch_row']($request); |
||
156 | $smcFunc['db_free_result']($request); |
||
157 | |||
158 | updateSettings(array('next_task_time' => $nextEvent)); |
||
159 | } |
||
160 | |||
161 | // Shall we return? |
||
162 | if (!isset($_GET['scheduled'])) |
||
163 | return true; |
||
164 | |||
165 | // Finally, send some stuff... |
||
166 | header('expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
||
167 | header('last-modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
||
168 | header('content-type: image/gif'); |
||
169 | 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"); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Do some daily cleaning up. |
||
174 | */ |
||
175 | function scheduled_daily_maintenance() |
||
176 | { |
||
177 | global $smcFunc, $modSettings, $sourcedir, $boarddir, $db_type, $image_proxy_enabled; |
||
178 | |||
179 | // First clean out the cache. |
||
180 | clean_cache(); |
||
181 | |||
182 | // If warning decrement is enabled and we have people who have not had a new warning in 24 hours, lower their warning level. |
||
183 | list (, , $modSettings['warning_decrement']) = explode(',', $modSettings['warning_settings']); |
||
184 | if ($modSettings['warning_decrement']) |
||
185 | { |
||
186 | // Find every member who has a warning level... |
||
187 | $request = $smcFunc['db_query']('', ' |
||
188 | SELECT id_member, warning |
||
189 | FROM {db_prefix}members |
||
190 | WHERE warning > {int:no_warning}', |
||
191 | array( |
||
192 | 'no_warning' => 0, |
||
193 | ) |
||
194 | ); |
||
195 | $members = array(); |
||
196 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
197 | $members[$row['id_member']] = $row['warning']; |
||
198 | $smcFunc['db_free_result']($request); |
||
199 | |||
200 | // Have some members to check? |
||
201 | if (!empty($members)) |
||
202 | { |
||
203 | // Find out when they were last warned. |
||
204 | $request = $smcFunc['db_query']('', ' |
||
205 | SELECT id_recipient, MAX(log_time) AS last_warning |
||
206 | FROM {db_prefix}log_comments |
||
207 | WHERE id_recipient IN ({array_int:member_list}) |
||
208 | AND comment_type = {string:warning} |
||
209 | GROUP BY id_recipient', |
||
210 | array( |
||
211 | 'member_list' => array_keys($members), |
||
212 | 'warning' => 'warning', |
||
213 | ) |
||
214 | ); |
||
215 | $member_changes = array(); |
||
216 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
217 | { |
||
218 | // More than 24 hours ago? |
||
219 | if ($row['last_warning'] <= time() - 86400) |
||
220 | $member_changes[] = array( |
||
221 | 'id' => $row['id_recipient'], |
||
222 | 'warning' => $members[$row['id_recipient']] >= $modSettings['warning_decrement'] ? $members[$row['id_recipient']] - $modSettings['warning_decrement'] : 0, |
||
223 | ); |
||
224 | } |
||
225 | $smcFunc['db_free_result']($request); |
||
226 | |||
227 | // Have some members to change? |
||
228 | if (!empty($member_changes)) |
||
229 | foreach ($member_changes as $change) |
||
230 | $smcFunc['db_query']('', ' |
||
231 | UPDATE {db_prefix}members |
||
232 | SET warning = {int:warning} |
||
233 | WHERE id_member = {int:id_member}', |
||
234 | array( |
||
235 | 'warning' => $change['warning'], |
||
236 | 'id_member' => $change['id'], |
||
237 | ) |
||
238 | ); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | // Do any spider stuff. |
||
243 | if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1) |
||
244 | { |
||
245 | require_once($sourcedir . '/ManageSearchEngines.php'); |
||
246 | consolidateSpiderStats(); |
||
247 | } |
||
248 | |||
249 | // Clean up some old login history information. |
||
250 | $smcFunc['db_query']('', ' |
||
251 | DELETE FROM {db_prefix}member_logins |
||
252 | WHERE time < {int:oldLogins}', |
||
253 | array( |
||
254 | 'oldLogins' => time() - (!empty($modSettings['loginHistoryDays']) ? 60 * 60 * 24 * $modSettings['loginHistoryDays'] : 2592000), |
||
255 | ) |
||
256 | ); |
||
257 | |||
258 | // Run Imageproxy housekeeping |
||
259 | if (!empty($image_proxy_enabled)) |
||
260 | { |
||
261 | require_once($boarddir . '/proxy.php'); |
||
262 | $proxy = new ProxyServer(); |
||
263 | $proxy->housekeeping(); |
||
264 | } |
||
265 | |||
266 | // Delete old profile exports |
||
267 | if (!empty($modSettings['export_expiry']) && file_exists($modSettings['export_dir']) && is_dir($modSettings['export_dir'])) |
||
268 | { |
||
269 | $expiry_date = round(TIME_START - $modSettings['export_expiry'] * 86400); |
||
270 | $export_files = glob(rtrim($modSettings['export_dir'], '/\\') . DIRECTORY_SEPARATOR . '*'); |
||
271 | |||
272 | foreach ($export_files as $export_file) |
||
273 | { |
||
274 | if (!in_array(basename($export_file), array('index.php', '.htaccess')) && filemtime($export_file) <= $expiry_date) |
||
275 | @unlink($export_file); |
||
276 | } |
||
277 | } |
||
278 | |||
279 | // Delete old alerts. |
||
280 | if (!empty($modSettings['alerts_auto_purge'])) |
||
281 | { |
||
282 | $smcFunc['db_query']('', ' |
||
283 | DELETE FROM {db_prefix}user_alerts |
||
284 | WHERE is_read > 0 |
||
285 | AND is_read < {int:purge_before}', |
||
286 | array( |
||
287 | 'purge_before' => time() - 86400 * $modSettings['alerts_auto_purge'], |
||
288 | ) |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | // Anyone else have something to do? |
||
293 | call_integration_hook('integrate_daily_maintenance'); |
||
294 | |||
295 | // Log we've done it... |
||
296 | return true; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Send out a daily email of all subscribed topics. |
||
301 | */ |
||
302 | function scheduled_daily_digest() |
||
303 | { |
||
304 | global $is_weekly, $txt, $mbname, $scripturl, $sourcedir, $smcFunc, $context, $modSettings; |
||
305 | |||
306 | // We'll want this... |
||
307 | require_once($sourcedir . '/Subs-Post.php'); |
||
308 | loadEssentialThemeData(); |
||
309 | |||
310 | $is_weekly = !empty($is_weekly) ? 1 : 0; |
||
311 | |||
312 | // Right - get all the notification data FIRST. |
||
313 | $request = $smcFunc['db_query']('', ' |
||
314 | SELECT ln.id_topic, COALESCE(t.id_board, ln.id_board) AS id_board, mem.email_address, mem.member_name, |
||
315 | mem.lngfile, mem.id_member |
||
316 | FROM {db_prefix}log_notify AS ln |
||
317 | JOIN {db_prefix}members AS mem ON (mem.id_member = ln.id_member) |
||
318 | LEFT JOIN {db_prefix}topics AS t ON (ln.id_topic != {int:empty_topic} AND t.id_topic = ln.id_topic) |
||
319 | WHERE mem.is_activated = {int:is_activated}', |
||
320 | array( |
||
321 | 'empty_topic' => 0, |
||
322 | 'is_activated' => 1, |
||
323 | ) |
||
324 | ); |
||
325 | $members = array(); |
||
326 | $langs = array(); |
||
327 | $notify = array(); |
||
328 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
329 | { |
||
330 | if (!isset($members[$row['id_member']])) |
||
331 | { |
||
332 | $members[$row['id_member']] = array( |
||
333 | 'email' => $row['email_address'], |
||
334 | 'name' => $row['member_name'], |
||
335 | 'id' => $row['id_member'], |
||
336 | 'lang' => $row['lngfile'], |
||
337 | ); |
||
338 | $langs[$row['lngfile']] = $row['lngfile']; |
||
339 | } |
||
340 | |||
341 | // Store this useful data! |
||
342 | $boards[$row['id_board']] = $row['id_board']; |
||
343 | if ($row['id_topic']) |
||
344 | $notify['topics'][$row['id_topic']][] = $row['id_member']; |
||
345 | else |
||
346 | $notify['boards'][$row['id_board']][] = $row['id_member']; |
||
347 | } |
||
348 | $smcFunc['db_free_result']($request); |
||
349 | |||
350 | if (empty($boards)) |
||
351 | return true; |
||
352 | |||
353 | // Just get the board names. |
||
354 | $request = $smcFunc['db_query']('', ' |
||
355 | SELECT id_board, name |
||
356 | FROM {db_prefix}boards |
||
357 | WHERE id_board IN ({array_int:board_list})', |
||
358 | array( |
||
359 | 'board_list' => $boards, |
||
360 | ) |
||
361 | ); |
||
362 | $boards = array(); |
||
363 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
364 | $boards[$row['id_board']] = $row['name']; |
||
365 | $smcFunc['db_free_result']($request); |
||
366 | |||
367 | if (empty($boards)) |
||
368 | return true; |
||
369 | |||
370 | // Get the actual topics... |
||
371 | $request = $smcFunc['db_query']('', ' |
||
372 | SELECT ld.note_type, t.id_topic, t.id_board, t.id_member_started, m.id_msg, m.subject, |
||
373 | b.name AS board_name |
||
374 | FROM {db_prefix}log_digest AS ld |
||
375 | JOIN {db_prefix}topics AS t ON (t.id_topic = ld.id_topic |
||
376 | AND t.id_board IN ({array_int:board_list})) |
||
377 | JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
378 | JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
379 | WHERE ' . ($is_weekly ? 'ld.daily != {int:daily_value}' : 'ld.daily IN (0, 2)'), |
||
380 | array( |
||
381 | 'board_list' => array_keys($boards), |
||
382 | 'daily_value' => 2, |
||
383 | ) |
||
384 | ); |
||
385 | $types = array(); |
||
386 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
387 | { |
||
388 | if (!isset($types[$row['note_type']][$row['id_board']])) |
||
389 | $types[$row['note_type']][$row['id_board']] = array( |
||
390 | 'lines' => array(), |
||
391 | 'name' => $row['board_name'], |
||
392 | 'id' => $row['id_board'], |
||
393 | ); |
||
394 | |||
395 | if ($row['note_type'] == 'reply') |
||
396 | { |
||
397 | if (isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) |
||
398 | $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['count']++; |
||
399 | else |
||
400 | $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array( |
||
401 | 'id' => $row['id_topic'], |
||
402 | 'subject' => un_htmlspecialchars($row['subject']), |
||
403 | 'count' => 1, |
||
404 | ); |
||
405 | } |
||
406 | elseif ($row['note_type'] == 'topic') |
||
407 | { |
||
408 | if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) |
||
409 | $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array( |
||
410 | 'id' => $row['id_topic'], |
||
411 | 'subject' => un_htmlspecialchars($row['subject']), |
||
412 | ); |
||
413 | } |
||
414 | else |
||
415 | { |
||
416 | if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) |
||
417 | $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array( |
||
418 | 'id' => $row['id_topic'], |
||
419 | 'subject' => un_htmlspecialchars($row['subject']), |
||
420 | 'starter' => $row['id_member_started'], |
||
421 | ); |
||
422 | } |
||
423 | |||
424 | $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array(); |
||
425 | if (!empty($notify['topics'][$row['id_topic']])) |
||
426 | $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array_merge($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'], $notify['topics'][$row['id_topic']]); |
||
427 | if (!empty($notify['boards'][$row['id_board']])) |
||
428 | $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array_merge($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'], $notify['boards'][$row['id_board']]); |
||
429 | } |
||
430 | $smcFunc['db_free_result']($request); |
||
431 | |||
432 | if (empty($types)) |
||
433 | return true; |
||
434 | |||
435 | // Let's load all the languages into a cache thingy. |
||
436 | $langtxt = array(); |
||
437 | foreach ($langs as $lang) |
||
438 | { |
||
439 | loadLanguage('Post', $lang); |
||
440 | loadLanguage('index', $lang); |
||
441 | loadLanguage('EmailTemplates', $lang); |
||
442 | $langtxt[$lang] = array( |
||
443 | 'subject' => $txt['digest_subject_' . ($is_weekly ? 'weekly' : 'daily')], |
||
444 | 'char_set' => $txt['lang_character_set'], |
||
445 | 'intro' => sprintf($txt['digest_intro_' . ($is_weekly ? 'weekly' : 'daily')], $mbname), |
||
446 | 'new_topics' => $txt['digest_new_topics'], |
||
447 | 'topic_lines' => $txt['digest_new_topics_line'], |
||
448 | 'new_replies' => $txt['digest_new_replies'], |
||
449 | 'mod_actions' => $txt['digest_mod_actions'], |
||
450 | 'replies_one' => $txt['digest_new_replies_one'], |
||
451 | 'replies_many' => $txt['digest_new_replies_many'], |
||
452 | 'sticky' => $txt['digest_mod_act_sticky'], |
||
453 | 'lock' => $txt['digest_mod_act_lock'], |
||
454 | 'unlock' => $txt['digest_mod_act_unlock'], |
||
455 | 'remove' => $txt['digest_mod_act_remove'], |
||
456 | 'move' => $txt['digest_mod_act_move'], |
||
457 | 'merge' => $txt['digest_mod_act_merge'], |
||
458 | 'split' => $txt['digest_mod_act_split'], |
||
459 | 'bye' => sprintf($txt['regards_team'], $context['forum_name']), |
||
460 | ); |
||
461 | |||
462 | call_integration_hook('integrate_daily_digest_lang', array(&$langtxt, $lang)); |
||
463 | } |
||
464 | |||
465 | // The preferred way... |
||
466 | require_once($sourcedir . '/Subs-Notify.php'); |
||
467 | $prefs = getNotifyPrefs(array_keys($members), array('msg_notify_type', 'msg_notify_pref'), true); |
||
468 | |||
469 | // Right - send out the silly things - this will take quite some space! |
||
470 | $members_sent = array(); |
||
471 | foreach ($members as $mid => $member) |
||
472 | { |
||
473 | $frequency = isset($prefs[$mid]['msg_notify_pref']) ? $prefs[$mid]['msg_notify_pref'] : 0; |
||
474 | $notify_types = !empty($prefs[$mid]['msg_notify_type']) ? $prefs[$mid]['msg_notify_type'] : 1; |
||
475 | |||
476 | // Did they not elect to choose this? |
||
477 | if ($frequency < 3 || $frequency == 4 && !$is_weekly || $frequency == 3 && $is_weekly || $notify_types == 4) |
||
478 | continue; |
||
479 | |||
480 | // Right character set! |
||
481 | $context['character_set'] = empty($modSettings['global_character_set']) ? $langtxt[$lang]['char_set'] : $modSettings['global_character_set']; |
||
482 | |||
483 | // Do the start stuff! |
||
484 | $email = array( |
||
485 | 'subject' => $mbname . ' - ' . $langtxt[$lang]['subject'], |
||
486 | 'body' => $member['name'] . ',' . "\n\n" . $langtxt[$lang]['intro'] . "\n" . $scripturl . '?action=profile;area=notification;u=' . $member['id'] . "\n", |
||
487 | 'email' => $member['email'], |
||
488 | ); |
||
489 | |||
490 | // All new topics? |
||
491 | if (isset($types['topic'])) |
||
492 | { |
||
493 | $titled = false; |
||
494 | foreach ($types['topic'] as $id => $board) |
||
495 | foreach ($board['lines'] as $topic) |
||
496 | if (in_array($mid, $topic['members'])) |
||
497 | { |
||
498 | if (!$titled) |
||
499 | { |
||
500 | $email['body'] .= "\n" . $langtxt[$lang]['new_topics'] . ':' . "\n" . '-----------------------------------------------'; |
||
501 | $titled = true; |
||
502 | } |
||
503 | $email['body'] .= "\n" . sprintf($langtxt[$lang]['topic_lines'], $topic['subject'], $board['name']); |
||
504 | } |
||
505 | if ($titled) |
||
506 | $email['body'] .= "\n"; |
||
507 | } |
||
508 | |||
509 | // What about replies? |
||
510 | if (isset($types['reply'])) |
||
511 | { |
||
512 | $titled = false; |
||
513 | foreach ($types['reply'] as $id => $board) |
||
514 | foreach ($board['lines'] as $topic) |
||
515 | if (in_array($mid, $topic['members'])) |
||
516 | { |
||
517 | if (!$titled) |
||
518 | { |
||
519 | $email['body'] .= "\n" . $langtxt[$lang]['new_replies'] . ':' . "\n" . '-----------------------------------------------'; |
||
520 | $titled = true; |
||
521 | } |
||
522 | $email['body'] .= "\n" . ($topic['count'] == 1 ? sprintf($langtxt[$lang]['replies_one'], $topic['subject']) : sprintf($langtxt[$lang]['replies_many'], $topic['count'], $topic['subject'])); |
||
523 | } |
||
524 | |||
525 | if ($titled) |
||
526 | $email['body'] .= "\n"; |
||
527 | } |
||
528 | |||
529 | // Finally, moderation actions! |
||
530 | if ($notify_types < 3) |
||
531 | { |
||
532 | $titled = false; |
||
533 | foreach ($types as $note_type => $type) |
||
534 | { |
||
535 | if ($note_type == 'topic' || $note_type == 'reply') |
||
536 | continue; |
||
537 | |||
538 | foreach ($type as $id => $board) |
||
539 | foreach ($board['lines'] as $topic) |
||
540 | if (in_array($mid, $topic['members'])) |
||
541 | { |
||
542 | if (!$titled) |
||
543 | { |
||
544 | $email['body'] .= "\n" . $langtxt[$lang]['mod_actions'] . ':' . "\n" . '-----------------------------------------------'; |
||
545 | $titled = true; |
||
546 | } |
||
547 | $email['body'] .= "\n" . sprintf($langtxt[$lang][$note_type], $topic['subject']); |
||
548 | } |
||
549 | } |
||
550 | } |
||
551 | |||
552 | call_integration_hook('integrate_daily_digest_email', array(&$email, $types, $notify_types, $langtxt)); |
||
553 | |||
554 | if ($titled) |
||
555 | $email['body'] .= "\n"; |
||
556 | |||
557 | // Then just say our goodbyes! |
||
558 | $email['body'] .= "\n\n" . sprintf($txt['regards_team'], $context['forum_name']); |
||
559 | |||
560 | // Send it - low priority! |
||
561 | sendmail($email['email'], $email['subject'], $email['body'], null, 'digest', false, 4); |
||
562 | |||
563 | $members_sent[] = $mid; |
||
564 | } |
||
565 | |||
566 | // Clean up... |
||
567 | if ($is_weekly) |
||
568 | { |
||
569 | $smcFunc['db_query']('', ' |
||
570 | DELETE FROM {db_prefix}log_digest |
||
571 | WHERE daily != {int:not_daily}', |
||
572 | array( |
||
573 | 'not_daily' => 0, |
||
574 | ) |
||
575 | ); |
||
576 | $smcFunc['db_query']('', ' |
||
577 | UPDATE {db_prefix}log_digest |
||
578 | SET daily = {int:daily_value} |
||
579 | WHERE daily = {int:not_daily}', |
||
580 | array( |
||
581 | 'daily_value' => 2, |
||
582 | 'not_daily' => 0, |
||
583 | ) |
||
584 | ); |
||
585 | } |
||
586 | else |
||
587 | { |
||
588 | // Clear any only weekly ones, and stop us from sending daily again. |
||
589 | $smcFunc['db_query']('', ' |
||
590 | DELETE FROM {db_prefix}log_digest |
||
591 | WHERE daily = {int:daily_value}', |
||
592 | array( |
||
593 | 'daily_value' => 2, |
||
594 | ) |
||
595 | ); |
||
596 | $smcFunc['db_query']('', ' |
||
597 | UPDATE {db_prefix}log_digest |
||
598 | SET daily = {int:both_value} |
||
599 | WHERE daily = {int:no_value}', |
||
600 | array( |
||
601 | 'both_value' => 1, |
||
602 | 'no_value' => 0, |
||
603 | ) |
||
604 | ); |
||
605 | } |
||
606 | |||
607 | // Just in case the member changes their settings mark this as sent. |
||
608 | if (!empty($members_sent)) |
||
609 | { |
||
610 | $smcFunc['db_query']('', ' |
||
611 | UPDATE {db_prefix}log_notify |
||
612 | SET sent = {int:is_sent} |
||
613 | WHERE id_member IN ({array_int:member_list})', |
||
614 | array( |
||
615 | 'member_list' => $members_sent, |
||
616 | 'is_sent' => 1, |
||
617 | ) |
||
618 | ); |
||
619 | } |
||
620 | |||
621 | // Log we've done it... |
||
622 | return true; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Like the daily stuff - just seven times less regular ;) |
||
627 | */ |
||
628 | function scheduled_weekly_digest() |
||
629 | { |
||
630 | global $is_weekly; |
||
631 | |||
632 | // We just pass through to the daily function - avoid duplication! |
||
633 | $is_weekly = true; |
||
634 | return scheduled_daily_digest(); |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Send a group of emails from the mail queue. |
||
639 | * |
||
640 | * @param bool|int $number The number to send each loop through or false to use the standard limits |
||
641 | * @param bool $override_limit Whether to bypass the limit |
||
642 | * @param bool $force_send Whether to forcibly send the messages now (useful when using cron jobs) |
||
643 | * @return bool Whether things were sent |
||
644 | */ |
||
645 | function ReduceMailQueue($number = false, $override_limit = false, $force_send = false) |
||
646 | { |
||
647 | global $modSettings, $smcFunc, $sourcedir, $txt, $language; |
||
648 | |||
649 | // Are we intending another script to be sending out the queue? |
||
650 | if (!empty($modSettings['mail_queue_use_cron']) && empty($force_send)) |
||
651 | return false; |
||
652 | |||
653 | // Just in case we run into a problem. |
||
654 | if (!isset($txt)) |
||
655 | { |
||
656 | loadEssentialThemeData(); |
||
657 | loadLanguage('Errors', $language, false); |
||
658 | loadLanguage('index', $language, false); |
||
659 | } |
||
660 | |||
661 | // By default send 5 at once. |
||
662 | if (!$number) |
||
663 | $number = empty($modSettings['mail_quantity']) ? 5 : $modSettings['mail_quantity']; |
||
664 | |||
665 | // If we came with a timestamp, and that doesn't match the next event, then someone else has beaten us. |
||
666 | if (isset($_GET['ts']) && $_GET['ts'] != $modSettings['mail_next_send'] && empty($force_send)) |
||
667 | return false; |
||
668 | |||
669 | // By default move the next sending on by 10 seconds, and require an affected row. |
||
670 | if (!$override_limit) |
||
671 | { |
||
672 | $delay = !empty($modSettings['mail_queue_delay']) ? $modSettings['mail_queue_delay'] : (!empty($modSettings['mail_limit']) && $modSettings['mail_limit'] < 5 ? 10 : 5); |
||
673 | |||
674 | $smcFunc['db_query']('', ' |
||
675 | UPDATE {db_prefix}settings |
||
676 | SET value = {string:next_mail_send} |
||
677 | WHERE variable = {literal:mail_next_send} |
||
678 | AND value = {string:last_send}', |
||
679 | array( |
||
680 | 'next_mail_send' => time() + $delay, |
||
681 | 'last_send' => $modSettings['mail_next_send'], |
||
682 | ) |
||
683 | ); |
||
684 | if ($smcFunc['db_affected_rows']() == 0) |
||
685 | return false; |
||
686 | $modSettings['mail_next_send'] = time() + $delay; |
||
687 | } |
||
688 | |||
689 | // If we're not overriding how many are we allow to send? |
||
690 | if (!$override_limit && !empty($modSettings['mail_limit'])) |
||
691 | { |
||
692 | list ($mt, $mn) = @explode('|', $modSettings['mail_recent']); |
||
693 | |||
694 | // Nothing worth noting... |
||
695 | if (empty($mn) || $mt < time() - 60) |
||
696 | { |
||
697 | $mt = time(); |
||
698 | $mn = $number; |
||
699 | } |
||
700 | // Otherwise we have a few more we can spend? |
||
701 | elseif ($mn < $modSettings['mail_limit']) |
||
702 | { |
||
703 | $mn += $number; |
||
704 | } |
||
705 | // No more I'm afraid, return! |
||
706 | else |
||
707 | return false; |
||
708 | |||
709 | // Reflect that we're about to send some, do it now to be safe. |
||
710 | updateSettings(array('mail_recent' => $mt . '|' . $mn)); |
||
711 | } |
||
712 | |||
713 | // Now we know how many we're sending, let's send them. |
||
714 | $request = $smcFunc['db_query']('', ' |
||
715 | SELECT id_mail, recipient, body, subject, headers, send_html, time_sent, private |
||
716 | FROM {db_prefix}mail_queue |
||
717 | ORDER BY priority ASC, id_mail ASC |
||
718 | LIMIT {int:limit}', |
||
719 | array( |
||
720 | 'limit' => $number, |
||
721 | ) |
||
722 | ); |
||
723 | $ids = array(); |
||
724 | $emails = array(); |
||
725 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
726 | { |
||
727 | // We want to delete these from the database ASAP, so just get the data and go. |
||
728 | $ids[] = $row['id_mail']; |
||
729 | $emails[] = array( |
||
730 | 'to' => $row['recipient'], |
||
731 | 'body' => $row['body'], |
||
732 | 'subject' => $row['subject'], |
||
733 | 'headers' => $row['headers'], |
||
734 | 'send_html' => $row['send_html'], |
||
735 | 'time_sent' => $row['time_sent'], |
||
736 | 'private' => $row['private'], |
||
737 | ); |
||
738 | } |
||
739 | $smcFunc['db_free_result']($request); |
||
740 | |||
741 | // Delete, delete, delete!!! |
||
742 | if (!empty($ids)) |
||
743 | $smcFunc['db_query']('', ' |
||
744 | DELETE FROM {db_prefix}mail_queue |
||
745 | WHERE id_mail IN ({array_int:mail_list})', |
||
746 | array( |
||
747 | 'mail_list' => $ids, |
||
748 | ) |
||
749 | ); |
||
750 | |||
751 | // Don't believe we have any left? |
||
752 | if (count($ids) < $number) |
||
753 | { |
||
754 | // Only update the setting if no-one else has beaten us to it. |
||
755 | $smcFunc['db_query']('', ' |
||
756 | UPDATE {db_prefix}settings |
||
757 | SET value = {string:no_send} |
||
758 | WHERE variable = {literal:mail_next_send} |
||
759 | AND value = {string:last_mail_send}', |
||
760 | array( |
||
761 | 'no_send' => '0', |
||
762 | 'last_mail_send' => $modSettings['mail_next_send'], |
||
763 | ) |
||
764 | ); |
||
765 | } |
||
766 | |||
767 | if (empty($ids)) |
||
768 | return false; |
||
769 | |||
770 | if (!empty($modSettings['mail_type']) && $modSettings['smtp_host'] != '') |
||
771 | require_once($sourcedir . '/Subs-Post.php'); |
||
772 | |||
773 | // Send each email, yea! |
||
774 | $failed_emails = array(); |
||
775 | foreach ($emails as $email) |
||
776 | { |
||
777 | if (empty($modSettings['mail_type']) || $modSettings['smtp_host'] == '') |
||
778 | { |
||
779 | $email['subject'] = strtr($email['subject'], array("\r" => '', "\n" => '')); |
||
780 | if (!empty($modSettings['mail_strip_carriage'])) |
||
781 | { |
||
782 | $email['body'] = strtr($email['body'], array("\r" => '')); |
||
783 | $email['headers'] = strtr($email['headers'], array("\r" => '')); |
||
784 | } |
||
785 | |||
786 | // No point logging a specific error here, as we have no language. PHP error is helpful anyway... |
||
787 | $result = mail(strtr($email['to'], array("\r" => '', "\n" => '')), $email['subject'], $email['body'], $email['headers']); |
||
788 | |||
789 | // Try to stop a timeout, this would be bad... |
||
790 | @set_time_limit(300); |
||
791 | if (function_exists('apache_reset_timeout')) |
||
792 | @apache_reset_timeout(); |
||
793 | } |
||
794 | else |
||
795 | $result = smtp_mail(array($email['to']), $email['subject'], $email['body'], $email['headers']); |
||
796 | |||
797 | // Hopefully it sent? |
||
798 | if (!$result) |
||
799 | $failed_emails[] = array($email['to'], $email['body'], $email['subject'], $email['headers'], $email['send_html'], $email['time_sent'], $email['private']); |
||
800 | } |
||
801 | |||
802 | // Any emails that didn't send? |
||
803 | if (!empty($failed_emails)) |
||
804 | { |
||
805 | // Update the failed attempts check. |
||
806 | $smcFunc['db_insert']('replace', |
||
807 | '{db_prefix}settings', |
||
808 | array('variable' => 'string', 'value' => 'string'), |
||
809 | array('mail_failed_attempts', empty($modSettings['mail_failed_attempts']) ? 1 : ++$modSettings['mail_failed_attempts']), |
||
810 | array('variable') |
||
811 | ); |
||
812 | |||
813 | // If we have failed to many times, tell mail to wait a bit and try again. |
||
814 | if ($modSettings['mail_failed_attempts'] > 5) |
||
815 | $smcFunc['db_query']('', ' |
||
816 | UPDATE {db_prefix}settings |
||
817 | SET value = {string:next_mail_send} |
||
818 | WHERE variable = {literal:mail_next_send} |
||
819 | AND value = {string:last_send}', |
||
820 | array( |
||
821 | 'next_mail_send' => time() + 60, |
||
822 | 'last_send' => $modSettings['mail_next_send'], |
||
823 | ) |
||
824 | ); |
||
825 | |||
826 | // Add our email back to the queue, manually. |
||
827 | $smcFunc['db_insert']('insert', |
||
828 | '{db_prefix}mail_queue', |
||
829 | array('recipient' => 'string', 'body' => 'string', 'subject' => 'string', 'headers' => 'string', 'send_html' => 'string', 'time_sent' => 'string', 'private' => 'int'), |
||
830 | $failed_emails, |
||
831 | array('id_mail') |
||
832 | ); |
||
833 | |||
834 | return false; |
||
835 | } |
||
836 | // We where unable to send the email, clear our failed attempts. |
||
837 | elseif (!empty($modSettings['mail_failed_attempts'])) |
||
838 | $smcFunc['db_query']('', ' |
||
839 | UPDATE {db_prefix}settings |
||
840 | SET value = {string:zero} |
||
841 | WHERE variable = {string:mail_failed_attempts}', |
||
842 | array( |
||
843 | 'zero' => '0', |
||
844 | 'mail_failed_attempts' => 'mail_failed_attempts', |
||
845 | ) |
||
846 | ); |
||
847 | |||
848 | // Had something to send... |
||
849 | return true; |
||
850 | } |
||
851 | |||
852 | /** |
||
853 | * Calculate the next time the passed tasks should be triggered. |
||
854 | * |
||
855 | * @param string|array $tasks The ID of a single task or an array of tasks |
||
856 | * @param bool $forceUpdate Whether to force the tasks to run now |
||
857 | */ |
||
858 | function CalculateNextTrigger($tasks = array(), $forceUpdate = false) |
||
859 | { |
||
860 | global $modSettings, $smcFunc; |
||
861 | |||
862 | $task_query = ''; |
||
863 | if (!is_array($tasks)) |
||
864 | $tasks = array($tasks); |
||
865 | |||
866 | // Actually have something passed? |
||
867 | if (!empty($tasks)) |
||
868 | { |
||
869 | if (!isset($tasks[0]) || is_numeric($tasks[0])) |
||
870 | $task_query = ' AND id_task IN ({array_int:tasks})'; |
||
871 | else |
||
872 | $task_query = ' AND task IN ({array_string:tasks})'; |
||
873 | } |
||
874 | $nextTaskTime = empty($tasks) ? time() + 86400 : $modSettings['next_task_time']; |
||
875 | |||
876 | // Get the critical info for the tasks. |
||
877 | $request = $smcFunc['db_query']('', ' |
||
878 | SELECT id_task, next_time, time_offset, time_regularity, time_unit |
||
879 | FROM {db_prefix}scheduled_tasks |
||
880 | WHERE disabled = {int:no_disabled} |
||
881 | ' . $task_query, |
||
882 | array( |
||
883 | 'no_disabled' => 0, |
||
884 | 'tasks' => $tasks, |
||
885 | ) |
||
886 | ); |
||
887 | $tasks = array(); |
||
888 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
889 | { |
||
890 | $next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']); |
||
891 | |||
892 | // Only bother moving the task if it's out of place or we're forcing it! |
||
893 | if ($forceUpdate || $next_time < $row['next_time'] || $row['next_time'] < time()) |
||
894 | $tasks[$row['id_task']] = $next_time; |
||
895 | else |
||
896 | $next_time = $row['next_time']; |
||
897 | |||
898 | // If this is sooner than the current next task, make this the next task. |
||
899 | if ($next_time < $nextTaskTime) |
||
900 | $nextTaskTime = $next_time; |
||
901 | } |
||
902 | $smcFunc['db_free_result']($request); |
||
903 | |||
904 | // Now make the changes! |
||
905 | foreach ($tasks as $id => $time) |
||
906 | $smcFunc['db_query']('', ' |
||
907 | UPDATE {db_prefix}scheduled_tasks |
||
908 | SET next_time = {int:next_time} |
||
909 | WHERE id_task = {int:id_task}', |
||
910 | array( |
||
911 | 'next_time' => $time, |
||
912 | 'id_task' => $id, |
||
913 | ) |
||
914 | ); |
||
915 | |||
916 | // If the next task is now different update. |
||
917 | if ($modSettings['next_task_time'] != $nextTaskTime) |
||
918 | updateSettings(array('next_task_time' => $nextTaskTime)); |
||
919 | } |
||
920 | |||
921 | /** |
||
922 | * Simply returns a time stamp of the next instance of these time parameters. |
||
923 | * |
||
924 | * @param int $regularity The regularity |
||
925 | * @param string $unit What unit are we using - 'm' for minutes, 'd' for days, 'w' for weeks or anything else for seconds |
||
926 | * @param int $offset The offset |
||
927 | * @return int The timestamp for the specified time |
||
928 | */ |
||
929 | function next_time($regularity, $unit, $offset) |
||
930 | { |
||
931 | // Just in case! |
||
932 | if ($regularity == 0) |
||
933 | $regularity = 2; |
||
934 | |||
935 | $curMin = date('i', time()); |
||
936 | |||
937 | // If the unit is minutes only check regularity in minutes. |
||
938 | if ($unit == 'm') |
||
939 | { |
||
940 | $off = date('i', $offset); |
||
941 | |||
942 | // If it's now just pretend it ain't, |
||
943 | if ($off == $curMin) |
||
944 | $next_time = time() + $regularity; |
||
945 | else |
||
946 | { |
||
947 | // Make sure that the offset is always in the past. |
||
948 | $off = $off > $curMin ? $off - 60 : $off; |
||
949 | |||
950 | while ($off <= $curMin) |
||
951 | $off += $regularity; |
||
952 | |||
953 | // Now we know when the time should be! |
||
954 | $next_time = time() + 60 * ($off - $curMin); |
||
955 | } |
||
956 | } |
||
957 | // Otherwise, work out what the offset would be with today's date. |
||
958 | else |
||
959 | { |
||
960 | $next_time = mktime(date('H', $offset), date('i', $offset), 0, date('m'), date('d'), date('Y')); |
||
961 | |||
962 | // Make the time offset in the past! |
||
963 | if ($next_time > time()) |
||
964 | { |
||
965 | $next_time -= 86400; |
||
966 | } |
||
967 | |||
968 | // Default we'll jump in hours. |
||
969 | $applyOffset = 3600; |
||
970 | // 24 hours = 1 day. |
||
971 | if ($unit == 'd') |
||
972 | $applyOffset = 86400; |
||
973 | // Otherwise a week. |
||
974 | if ($unit == 'w') |
||
975 | $applyOffset = 604800; |
||
976 | |||
977 | $applyOffset *= $regularity; |
||
978 | |||
979 | // Just add on the offset. |
||
980 | while ($next_time <= time()) |
||
981 | { |
||
982 | $next_time += $applyOffset; |
||
983 | } |
||
984 | } |
||
985 | |||
986 | return $next_time; |
||
987 | } |
||
988 | |||
989 | /** |
||
990 | * This loads the bare minimum data to allow us to load language files! |
||
991 | */ |
||
992 | function loadEssentialThemeData() |
||
993 | { |
||
994 | global $settings, $modSettings, $smcFunc, $mbname, $context, $sourcedir, $txt; |
||
995 | |||
996 | // Get all the default theme variables. |
||
997 | $result = $smcFunc['db_query']('', ' |
||
998 | SELECT id_theme, variable, value |
||
999 | FROM {db_prefix}themes |
||
1000 | WHERE id_member = {int:no_member} |
||
1001 | AND id_theme IN (1, {int:theme_guests})', |
||
1002 | array( |
||
1003 | 'no_member' => 0, |
||
1004 | 'theme_guests' => !empty($modSettings['theme_guests']) ? $modSettings['theme_guests'] : 1, |
||
1005 | ) |
||
1006 | ); |
||
1007 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
1008 | { |
||
1009 | $settings[$row['variable']] = $row['value']; |
||
1010 | |||
1011 | // Is this the default theme? |
||
1012 | if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1') |
||
1013 | $settings['default_' . $row['variable']] = $row['value']; |
||
1014 | } |
||
1015 | $smcFunc['db_free_result']($result); |
||
1016 | |||
1017 | // Check we have some directories setup. |
||
1018 | if (empty($settings['template_dirs'])) |
||
1019 | { |
||
1020 | $settings['template_dirs'] = array($settings['theme_dir']); |
||
1021 | |||
1022 | // Based on theme (if there is one). |
||
1023 | if (!empty($settings['base_theme_dir'])) |
||
1024 | $settings['template_dirs'][] = $settings['base_theme_dir']; |
||
1025 | |||
1026 | // Lastly the default theme. |
||
1027 | if ($settings['theme_dir'] != $settings['default_theme_dir']) |
||
1028 | $settings['template_dirs'][] = $settings['default_theme_dir']; |
||
1029 | } |
||
1030 | |||
1031 | // Assume we want this. |
||
1032 | $context['forum_name'] = $mbname; |
||
1033 | $context['forum_name_html_safe'] = $smcFunc['htmlspecialchars']($context['forum_name']); |
||
1034 | |||
1035 | // Check loadLanguage actually exists! |
||
1036 | if (!function_exists('loadLanguage')) |
||
1037 | { |
||
1038 | require_once($sourcedir . '/Load.php'); |
||
1039 | require_once($sourcedir . '/Subs.php'); |
||
1040 | } |
||
1041 | |||
1042 | loadLanguage('index+Modifications'); |
||
1043 | |||
1044 | // Just in case it wasn't already set elsewhere. |
||
1045 | $context['character_set'] = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']; |
||
1046 | $context['utf8'] = $context['character_set'] === 'UTF-8'; |
||
1047 | $context['right_to_left'] = !empty($txt['lang_rtl']); |
||
1048 | |||
1049 | // Tell fatal_lang_error() to not reload the theme. |
||
1050 | $context['theme_loaded'] = true; |
||
1051 | } |
||
1052 | |||
1053 | /** |
||
1054 | * This retieves data (e.g. last version of SMF) from sm.org |
||
1055 | */ |
||
1056 | function scheduled_fetchSMfiles() |
||
1057 | { |
||
1058 | global $sourcedir, $txt, $language, $modSettings, $smcFunc, $context; |
||
1059 | |||
1060 | // What files do we want to get |
||
1061 | $request = $smcFunc['db_query']('', ' |
||
1062 | SELECT id_file, filename, path, parameters |
||
1063 | FROM {db_prefix}admin_info_files', |
||
1064 | array( |
||
1065 | ) |
||
1066 | ); |
||
1067 | |||
1068 | $js_files = array(); |
||
1069 | |||
1070 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1071 | { |
||
1072 | $js_files[$row['id_file']] = array( |
||
1073 | 'filename' => $row['filename'], |
||
1074 | 'path' => $row['path'], |
||
1075 | 'parameters' => sprintf($row['parameters'], $language, urlencode($modSettings['time_format']), urlencode(SMF_FULL_VERSION)), |
||
1076 | ); |
||
1077 | } |
||
1078 | |||
1079 | $smcFunc['db_free_result']($request); |
||
1080 | |||
1081 | // Just in case we run into a problem. |
||
1082 | loadEssentialThemeData(); |
||
1083 | loadLanguage('Errors', $language, false); |
||
1084 | |||
1085 | foreach ($js_files as $ID_FILE => $file) |
||
1086 | { |
||
1087 | // Create the url |
||
1088 | $server = empty($file['path']) || (substr($file['path'], 0, 7) != 'http://' && substr($file['path'], 0, 8) != 'https://') ? 'https://www.simplemachines.org' : ''; |
||
1089 | $url = $server . (!empty($file['path']) ? $file['path'] : $file['path']) . $file['filename'] . (!empty($file['parameters']) ? '?' . $file['parameters'] : ''); |
||
1090 | |||
1091 | // Get the file |
||
1092 | $file_data = fetch_web_data($url); |
||
1093 | |||
1094 | // If we got an error - give up - the site might be down. And if we should happen to be coming from elsewhere, let's also make a note of it. |
||
1095 | if ($file_data === false) |
||
1096 | { |
||
1097 | $context['scheduled_errors']['fetchSMfiles'][] = sprintf($txt['st_cannot_retrieve_file'], $url); |
||
1098 | log_error(sprintf($txt['st_cannot_retrieve_file'], $url)); |
||
1099 | return false; |
||
1100 | } |
||
1101 | |||
1102 | // Save the file to the database. |
||
1103 | $smcFunc['db_query']('substring', ' |
||
1104 | UPDATE {db_prefix}admin_info_files |
||
1105 | SET data = SUBSTRING({string:file_data}, 1, 65534) |
||
1106 | WHERE id_file = {int:id_file}', |
||
1107 | array( |
||
1108 | 'id_file' => $ID_FILE, |
||
1109 | 'file_data' => $file_data, |
||
1110 | ) |
||
1111 | ); |
||
1112 | } |
||
1113 | return true; |
||
1114 | } |
||
1115 | |||
1116 | /** |
||
1117 | * Happy birthday!! |
||
1118 | */ |
||
1119 | function scheduled_birthdayemails() |
||
1120 | { |
||
1121 | global $smcFunc; |
||
1122 | |||
1123 | $smcFunc['db_insert']('insert', '{db_prefix}background_tasks', |
||
1124 | array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'), |
||
1125 | array('$sourcedir/tasks/Birthday-Notify.php', 'Birthday_Notify_Background', '', 0), |
||
1126 | array() |
||
1127 | ); |
||
1128 | |||
1129 | return true; |
||
1130 | } |
||
1131 | |||
1132 | /** |
||
1133 | * Weekly maintenance |
||
1134 | */ |
||
1135 | function scheduled_weekly_maintenance() |
||
1136 | { |
||
1137 | global $modSettings, $smcFunc, $cache_enable, $cacheAPI; |
||
1138 | |||
1139 | // Delete some settings that needn't be set if they are otherwise empty. |
||
1140 | $emptySettings = array( |
||
1141 | 'warning_mute', 'warning_moderate', 'warning_watch', 'warning_show', 'disableCustomPerPage', 'spider_mode', 'spider_group', |
||
1142 | 'paid_currency_code', 'paid_currency_symbol', 'paid_email_to', 'paid_email', 'paid_enabled', 'paypal_email', |
||
1143 | 'search_enable_captcha', 'search_floodcontrol_time', 'show_spider_online', |
||
1144 | ); |
||
1145 | |||
1146 | $smcFunc['db_query']('', ' |
||
1147 | DELETE FROM {db_prefix}settings |
||
1148 | WHERE variable IN ({array_string:setting_list}) |
||
1149 | AND (value = {string:zero_value} OR value = {string:blank_value})', |
||
1150 | array( |
||
1151 | 'zero_value' => '0', |
||
1152 | 'blank_value' => '', |
||
1153 | 'setting_list' => $emptySettings, |
||
1154 | ) |
||
1155 | ); |
||
1156 | |||
1157 | // Some settings we never want to keep - they are just there for temporary purposes. |
||
1158 | $deleteAnywaySettings = array( |
||
1159 | 'attachment_full_notified', |
||
1160 | ); |
||
1161 | |||
1162 | $smcFunc['db_query']('', ' |
||
1163 | DELETE FROM {db_prefix}settings |
||
1164 | WHERE variable IN ({array_string:setting_list})', |
||
1165 | array( |
||
1166 | 'setting_list' => $deleteAnywaySettings, |
||
1167 | ) |
||
1168 | ); |
||
1169 | |||
1170 | // Ok should we prune the logs? |
||
1171 | if (!empty($modSettings['pruningOptions'])) |
||
1172 | { |
||
1173 | if (!empty($modSettings['pruningOptions']) && strpos($modSettings['pruningOptions'], ',') !== false) |
||
1174 | list ($modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']) = explode(',', $modSettings['pruningOptions']); |
||
1175 | |||
1176 | if (!empty($modSettings['pruneErrorLog'])) |
||
1177 | { |
||
1178 | // Figure out when our cutoff time is. 1 day = 86400 seconds. |
||
1179 | $t = time() - $modSettings['pruneErrorLog'] * 86400; |
||
1180 | |||
1181 | $smcFunc['db_query']('', ' |
||
1182 | DELETE FROM {db_prefix}log_errors |
||
1183 | WHERE log_time < {int:log_time}', |
||
1184 | array( |
||
1185 | 'log_time' => $t, |
||
1186 | ) |
||
1187 | ); |
||
1188 | } |
||
1189 | |||
1190 | if (!empty($modSettings['pruneModLog'])) |
||
1191 | { |
||
1192 | // Figure out when our cutoff time is. 1 day = 86400 seconds. |
||
1193 | $t = time() - $modSettings['pruneModLog'] * 86400; |
||
1194 | |||
1195 | $smcFunc['db_query']('', ' |
||
1196 | DELETE FROM {db_prefix}log_actions |
||
1197 | WHERE log_time < {int:log_time} |
||
1198 | AND id_log = {int:moderation_log}', |
||
1199 | array( |
||
1200 | 'log_time' => $t, |
||
1201 | 'moderation_log' => 1, |
||
1202 | ) |
||
1203 | ); |
||
1204 | } |
||
1205 | |||
1206 | if (!empty($modSettings['pruneBanLog'])) |
||
1207 | { |
||
1208 | // Figure out when our cutoff time is. 1 day = 86400 seconds. |
||
1209 | $t = time() - $modSettings['pruneBanLog'] * 86400; |
||
1210 | |||
1211 | $smcFunc['db_query']('', ' |
||
1212 | DELETE FROM {db_prefix}log_banned |
||
1213 | WHERE log_time < {int:log_time}', |
||
1214 | array( |
||
1215 | 'log_time' => $t, |
||
1216 | ) |
||
1217 | ); |
||
1218 | } |
||
1219 | |||
1220 | if (!empty($modSettings['pruneReportLog'])) |
||
1221 | { |
||
1222 | // Figure out when our cutoff time is. 1 day = 86400 seconds. |
||
1223 | $t = time() - $modSettings['pruneReportLog'] * 86400; |
||
1224 | |||
1225 | // This one is more complex then the other logs. First we need to figure out which reports are too old. |
||
1226 | $reports = array(); |
||
1227 | $result = $smcFunc['db_query']('', ' |
||
1228 | SELECT id_report |
||
1229 | FROM {db_prefix}log_reported |
||
1230 | WHERE time_started < {int:time_started} |
||
1231 | AND closed = {int:closed} |
||
1232 | AND ignore_all = {int:not_ignored}', |
||
1233 | array( |
||
1234 | 'time_started' => $t, |
||
1235 | 'closed' => 1, |
||
1236 | 'not_ignored' => 0, |
||
1237 | ) |
||
1238 | ); |
||
1239 | |||
1240 | while ($row = $smcFunc['db_fetch_row']($result)) |
||
1241 | $reports[] = $row[0]; |
||
1242 | |||
1243 | $smcFunc['db_free_result']($result); |
||
1244 | |||
1245 | if (!empty($reports)) |
||
1246 | { |
||
1247 | // Now delete the reports... |
||
1248 | $smcFunc['db_query']('', ' |
||
1249 | DELETE FROM {db_prefix}log_reported |
||
1250 | WHERE id_report IN ({array_int:report_list})', |
||
1251 | array( |
||
1252 | 'report_list' => $reports, |
||
1253 | ) |
||
1254 | ); |
||
1255 | // And delete the comments for those reports... |
||
1256 | $smcFunc['db_query']('', ' |
||
1257 | DELETE FROM {db_prefix}log_reported_comments |
||
1258 | WHERE id_report IN ({array_int:report_list})', |
||
1259 | array( |
||
1260 | 'report_list' => $reports, |
||
1261 | ) |
||
1262 | ); |
||
1263 | } |
||
1264 | } |
||
1265 | |||
1266 | if (!empty($modSettings['pruneScheduledTaskLog'])) |
||
1267 | { |
||
1268 | // Figure out when our cutoff time is. 1 day = 86400 seconds. |
||
1269 | $t = time() - $modSettings['pruneScheduledTaskLog'] * 86400; |
||
1270 | |||
1271 | $smcFunc['db_query']('', ' |
||
1272 | DELETE FROM {db_prefix}log_scheduled_tasks |
||
1273 | WHERE time_run < {int:time_run}', |
||
1274 | array( |
||
1275 | 'time_run' => $t, |
||
1276 | ) |
||
1277 | ); |
||
1278 | } |
||
1279 | |||
1280 | if (!empty($modSettings['pruneSpiderHitLog'])) |
||
1281 | { |
||
1282 | // Figure out when our cutoff time is. 1 day = 86400 seconds. |
||
1283 | $t = time() - $modSettings['pruneSpiderHitLog'] * 86400; |
||
1284 | |||
1285 | $smcFunc['db_query']('', ' |
||
1286 | DELETE FROM {db_prefix}log_spider_hits |
||
1287 | WHERE log_time < {int:log_time}', |
||
1288 | array( |
||
1289 | 'log_time' => $t, |
||
1290 | ) |
||
1291 | ); |
||
1292 | } |
||
1293 | } |
||
1294 | |||
1295 | // Get rid of any paid subscriptions that were never actioned. |
||
1296 | $smcFunc['db_query']('', ' |
||
1297 | DELETE FROM {db_prefix}log_subscribed |
||
1298 | WHERE end_time = {int:no_end_time} |
||
1299 | AND status = {int:not_active} |
||
1300 | AND start_time < {int:start_time} |
||
1301 | AND payments_pending < {int:payments_pending}', |
||
1302 | array( |
||
1303 | 'no_end_time' => 0, |
||
1304 | 'not_active' => 0, |
||
1305 | 'start_time' => time() - 60, |
||
1306 | 'payments_pending' => 1, |
||
1307 | ) |
||
1308 | ); |
||
1309 | |||
1310 | // Some OS's don't seem to clean out their sessions. |
||
1311 | $smcFunc['db_query']('', ' |
||
1312 | DELETE FROM {db_prefix}sessions |
||
1313 | WHERE last_update < {int:last_update}', |
||
1314 | array( |
||
1315 | 'last_update' => time() - 86400, |
||
1316 | ) |
||
1317 | ); |
||
1318 | |||
1319 | // Update the regex of top level domains with the IANA's latest official list |
||
1320 | $smcFunc['db_insert']('insert', '{db_prefix}background_tasks', |
||
1321 | array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'), |
||
1322 | array('$sourcedir/tasks/UpdateTldRegex.php', 'Update_TLD_Regex', '', 0), array() |
||
1323 | ); |
||
1324 | |||
1325 | // Run Cache housekeeping |
||
1326 | if (!empty($cache_enable) && !empty($cacheAPI)) |
||
1327 | $cacheAPI->housekeeping(); |
||
1328 | |||
1329 | // Prevent stale minimized CSS and JavaScript from cluttering up the theme directories |
||
1330 | deleteAllMinified(); |
||
1331 | |||
1332 | // Maybe there's more to do. |
||
1333 | call_integration_hook('integrate_weekly_maintenance'); |
||
1334 | |||
1335 | return true; |
||
1336 | } |
||
1337 | |||
1338 | /** |
||
1339 | * Perform the standard checks on expiring/near expiring subscriptions. |
||
1340 | */ |
||
1341 | function scheduled_paid_subscriptions() |
||
1342 | { |
||
1343 | global $sourcedir, $scripturl, $smcFunc, $modSettings, $language; |
||
1344 | |||
1345 | // Start off by checking for removed subscriptions. |
||
1346 | $request = $smcFunc['db_query']('', ' |
||
1347 | SELECT id_subscribe, id_member |
||
1348 | FROM {db_prefix}log_subscribed |
||
1349 | WHERE status = {int:is_active} |
||
1350 | AND end_time < {int:time_now}', |
||
1351 | array( |
||
1352 | 'is_active' => 1, |
||
1353 | 'time_now' => time(), |
||
1354 | ) |
||
1355 | ); |
||
1356 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1357 | { |
||
1358 | require_once($sourcedir . '/ManagePaid.php'); |
||
1359 | removeSubscription($row['id_subscribe'], $row['id_member']); |
||
1360 | } |
||
1361 | $smcFunc['db_free_result']($request); |
||
1362 | |||
1363 | // Get all those about to expire that have not had a reminder sent. |
||
1364 | $request = $smcFunc['db_query']('', ' |
||
1365 | SELECT ls.id_sublog, m.id_member, m.member_name, m.email_address, m.lngfile, s.name, ls.end_time |
||
1366 | FROM {db_prefix}log_subscribed AS ls |
||
1367 | JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe) |
||
1368 | JOIN {db_prefix}members AS m ON (m.id_member = ls.id_member) |
||
1369 | WHERE ls.status = {int:is_active} |
||
1370 | AND ls.reminder_sent = {int:reminder_sent} |
||
1371 | AND s.reminder > {int:reminder_wanted} |
||
1372 | AND ls.end_time < ({int:time_now} + s.reminder * 86400)', |
||
1373 | array( |
||
1374 | 'is_active' => 1, |
||
1375 | 'reminder_sent' => 0, |
||
1376 | 'reminder_wanted' => 0, |
||
1377 | 'time_now' => time(), |
||
1378 | ) |
||
1379 | ); |
||
1380 | $subs_reminded = array(); |
||
1381 | $members = array(); |
||
1382 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1383 | { |
||
1384 | // If this is the first one load the important bits. |
||
1385 | if (empty($subs_reminded)) |
||
1386 | { |
||
1387 | require_once($sourcedir . '/Subs-Post.php'); |
||
1388 | // Need the below for loadLanguage to work! |
||
1389 | loadEssentialThemeData(); |
||
1390 | } |
||
1391 | |||
1392 | $subs_reminded[] = $row['id_sublog']; |
||
1393 | $members[$row['id_member']] = $row; |
||
1394 | } |
||
1395 | $smcFunc['db_free_result']($request); |
||
1396 | |||
1397 | // Load alert preferences |
||
1398 | require_once($sourcedir . '/Subs-Notify.php'); |
||
1399 | $notifyPrefs = getNotifyPrefs(array_keys($members), 'paidsubs_expiring', true); |
||
1400 | $alert_rows = array(); |
||
1401 | foreach ($members as $row) |
||
1402 | { |
||
1403 | $replacements = array( |
||
1404 | 'PROFILE_LINK' => $scripturl . '?action=profile;area=subscriptions;u=' . $row['id_member'], |
||
1405 | 'REALNAME' => $row['member_name'], |
||
1406 | 'SUBSCRIPTION' => $row['name'], |
||
1407 | 'END_DATE' => strip_tags(timeformat($row['end_time'])), |
||
1408 | ); |
||
1409 | |||
1410 | $emaildata = loadEmailTemplate('paid_subscription_reminder', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']); |
||
1411 | |||
1412 | // Send the actual email. |
||
1413 | if ($notifyPrefs[$row['id_member']] & 0x02) |
||
1414 | sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, 'paid_sub_remind', $emaildata['is_html'], 2); |
||
1415 | |||
1416 | if ($notifyPrefs[$row['id_member']] & 0x01) |
||
1417 | { |
||
1418 | $alert_rows[] = array( |
||
1419 | 'alert_time' => time(), |
||
1420 | 'id_member' => $row['id_member'], |
||
1421 | 'id_member_started' => $row['id_member'], |
||
1422 | 'member_name' => $row['member_name'], |
||
1423 | 'content_type' => 'paidsubs', |
||
1424 | 'content_id' => $row['id_sublog'], |
||
1425 | 'content_action' => 'expiring', |
||
1426 | 'is_read' => 0, |
||
1427 | 'extra' => $smcFunc['json_encode'](array( |
||
1428 | 'subscription_name' => $row['name'], |
||
1429 | 'end_time' => $row['end_time'], |
||
1430 | )), |
||
1431 | ); |
||
1432 | updateMemberData($row['id_member'], array('alerts' => '+')); |
||
1433 | } |
||
1434 | } |
||
1435 | |||
1436 | // Insert the alerts if any |
||
1437 | if (!empty($alert_rows)) |
||
1438 | $smcFunc['db_insert']('', |
||
1439 | '{db_prefix}user_alerts', |
||
1440 | array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string', |
||
1441 | 'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'), |
||
1442 | $alert_rows, |
||
1443 | array() |
||
1444 | ); |
||
1445 | |||
1446 | // Mark the reminder as sent. |
||
1447 | if (!empty($subs_reminded)) |
||
1448 | $smcFunc['db_query']('', ' |
||
1449 | UPDATE {db_prefix}log_subscribed |
||
1450 | SET reminder_sent = {int:reminder_sent} |
||
1451 | WHERE id_sublog IN ({array_int:subscription_list})', |
||
1452 | array( |
||
1453 | 'subscription_list' => $subs_reminded, |
||
1454 | 'reminder_sent' => 1, |
||
1455 | ) |
||
1456 | ); |
||
1457 | |||
1458 | return true; |
||
1459 | } |
||
1460 | |||
1461 | /** |
||
1462 | * Check for un-posted attachments is something we can do once in a while :P |
||
1463 | * This function uses opendir cycling through all the attachments |
||
1464 | */ |
||
1465 | function scheduled_remove_temp_attachments() |
||
1466 | { |
||
1467 | global $smcFunc, $modSettings, $context, $txt; |
||
1468 | |||
1469 | // We need to know where this thing is going. |
||
1470 | if (!empty($modSettings['currentAttachmentUploadDir'])) |
||
1471 | { |
||
1472 | if (!is_array($modSettings['attachmentUploadDir'])) |
||
1473 | $modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true); |
||
1474 | |||
1475 | // Just use the current path for temp files. |
||
1476 | $attach_dirs = $modSettings['attachmentUploadDir']; |
||
1477 | } |
||
1478 | else |
||
1479 | { |
||
1480 | $attach_dirs = array($modSettings['attachmentUploadDir']); |
||
1481 | } |
||
1482 | |||
1483 | foreach ($attach_dirs as $attach_dir) |
||
1484 | { |
||
1485 | $dir = @opendir($attach_dir); |
||
1486 | if (!$dir) |
||
1487 | { |
||
1488 | loadEssentialThemeData(); |
||
1489 | loadLanguage('Post'); |
||
1490 | $context['scheduled_errors']['remove_temp_attachments'][] = $txt['cant_access_upload_path'] . ' (' . $attach_dir . ')'; |
||
1491 | log_error($txt['cant_access_upload_path'] . ' (' . $attach_dir . ')', 'critical'); |
||
1492 | return false; |
||
1493 | } |
||
1494 | |||
1495 | while ($file = readdir($dir)) |
||
1496 | { |
||
1497 | if ($file == '.' || $file == '..') |
||
1498 | continue; |
||
1499 | |||
1500 | if (strpos($file, 'post_tmp_') !== false) |
||
1501 | { |
||
1502 | // Temp file is more than 5 hours old! |
||
1503 | if (filemtime($attach_dir . '/' . $file) < time() - 18000) |
||
1504 | @unlink($attach_dir . '/' . $file); |
||
1505 | } |
||
1506 | } |
||
1507 | closedir($dir); |
||
1508 | } |
||
1509 | |||
1510 | return true; |
||
1511 | } |
||
1512 | |||
1513 | /** |
||
1514 | * Check for move topic notices that have past their best by date |
||
1515 | */ |
||
1516 | function scheduled_remove_topic_redirect() |
||
1517 | { |
||
1518 | global $smcFunc, $sourcedir; |
||
1519 | |||
1520 | // init |
||
1521 | $topics = array(); |
||
1522 | |||
1523 | // We will need this for language files |
||
1524 | loadEssentialThemeData(); |
||
1525 | |||
1526 | // Find all of the old MOVE topic notices that were set to expire |
||
1527 | $request = $smcFunc['db_query']('', ' |
||
1528 | SELECT id_topic |
||
1529 | FROM {db_prefix}topics |
||
1530 | WHERE redirect_expires <= {int:redirect_expires} |
||
1531 | AND redirect_expires <> 0', |
||
1532 | array( |
||
1533 | 'redirect_expires' => time(), |
||
1534 | ) |
||
1535 | ); |
||
1536 | |||
1537 | while ($row = $smcFunc['db_fetch_row']($request)) |
||
1538 | $topics[] = $row[0]; |
||
1539 | $smcFunc['db_free_result']($request); |
||
1540 | |||
1541 | // Zap, your gone |
||
1542 | if (count($topics) > 0) |
||
1543 | { |
||
1544 | require_once($sourcedir . '/RemoveTopic.php'); |
||
1545 | removeTopics($topics, false, true); |
||
1546 | } |
||
1547 | |||
1548 | return true; |
||
1549 | } |
||
1550 | |||
1551 | /** |
||
1552 | * Check for old drafts and remove them |
||
1553 | */ |
||
1554 | function scheduled_remove_old_drafts() |
||
1555 | { |
||
1556 | global $smcFunc, $sourcedir, $modSettings; |
||
1557 | |||
1558 | if (empty($modSettings['drafts_keep_days'])) |
||
1559 | return true; |
||
1560 | |||
1561 | // init |
||
1562 | $drafts = array(); |
||
1563 | |||
1564 | // We need this for language items |
||
1565 | loadEssentialThemeData(); |
||
1566 | |||
1567 | // Find all of the old drafts |
||
1568 | $request = $smcFunc['db_query']('', ' |
||
1569 | SELECT id_draft |
||
1570 | FROM {db_prefix}user_drafts |
||
1571 | WHERE poster_time <= {int:poster_time_old}', |
||
1572 | array( |
||
1573 | 'poster_time_old' => time() - (86400 * $modSettings['drafts_keep_days']), |
||
1574 | ) |
||
1575 | ); |
||
1576 | |||
1577 | while ($row = $smcFunc['db_fetch_row']($request)) |
||
1578 | $drafts[] = (int) $row[0]; |
||
1579 | $smcFunc['db_free_result']($request); |
||
1580 | |||
1581 | // If we have old one, remove them |
||
1582 | if (count($drafts) > 0) |
||
1583 | { |
||
1584 | require_once($sourcedir . '/Drafts.php'); |
||
1585 | DeleteDraft($drafts, false); |
||
1586 | } |
||
1587 | |||
1588 | return true; |
||
1589 | } |
||
1590 | |||
1591 | /** |
||
1592 | * Prune log_topics, log_boards & log_mark_boards_read. |
||
1593 | * For users who haven't been active in a long time, purge these records. |
||
1594 | * For users who haven't been active in a shorter time, mark boards as read, |
||
1595 | * pruning log_topics. |
||
1596 | */ |
||
1597 | function scheduled_prune_log_topics() |
||
1598 | { |
||
1599 | global $smcFunc, $sourcedir, $modSettings; |
||
1600 | |||
1601 | // If set to zero, bypass |
||
1602 | if (empty($modSettings['mark_read_max_users']) || (empty($modSettings['mark_read_beyond']) && empty($modSettings['mark_read_delete_beyond']))) |
||
1603 | return true; |
||
1604 | |||
1605 | // Convert to timestamps for comparison |
||
1606 | if (empty($modSettings['mark_read_beyond'])) |
||
1607 | $markReadCutoff = 0; |
||
1608 | else |
||
1609 | $markReadCutoff = time() - $modSettings['mark_read_beyond'] * 86400; |
||
1610 | |||
1611 | if (empty($modSettings['mark_read_delete_beyond'])) |
||
1612 | $cleanupBeyond = 0; |
||
1613 | else |
||
1614 | $cleanupBeyond = time() - $modSettings['mark_read_delete_beyond'] * 86400; |
||
1615 | |||
1616 | $maxMembers = $modSettings['mark_read_max_users']; |
||
1617 | |||
1618 | // You're basically saying to just purge, so just purge |
||
1619 | if ($markReadCutoff < $cleanupBeyond) |
||
1620 | $markReadCutoff = $cleanupBeyond; |
||
1621 | |||
1622 | // Try to prevent timeouts |
||
1623 | @set_time_limit(300); |
||
1624 | if (function_exists('apache_reset_timeout')) |
||
1625 | @apache_reset_timeout(); |
||
1626 | |||
1627 | // Start off by finding the records in log_boards, log_topics & log_mark_read |
||
1628 | // for users who haven't been around the longest... |
||
1629 | $members = array(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
1630 | $sql = 'SELECT lb.id_member, m.last_login |
||
1631 | FROM {db_prefix}members m |
||
1632 | INNER JOIN |
||
1633 | ( |
||
1634 | SELECT DISTINCT id_member |
||
1635 | FROM {db_prefix}log_boards |
||
1636 | ) lb ON m.id_member = lb.id_member |
||
1637 | WHERE m.last_login <= {int:dcutoff} |
||
1638 | UNION |
||
1639 | SELECT lmr.id_member, m.last_login |
||
1640 | FROM {db_prefix}members m |
||
1641 | INNER JOIN |
||
1642 | ( |
||
1643 | SELECT DISTINCT id_member |
||
1644 | FROM {db_prefix}log_mark_read |
||
1645 | ) lmr ON m.id_member = lmr.id_member |
||
1646 | WHERE m.last_login <= {int:dcutoff} |
||
1647 | UNION |
||
1648 | SELECT lt.id_member, m.last_login |
||
1649 | FROM {db_prefix}members m |
||
1650 | INNER JOIN |
||
1651 | ( |
||
1652 | SELECT DISTINCT id_member |
||
1653 | FROM {db_prefix}log_topics |
||
1654 | WHERE unwatched = {int:unwatched} |
||
1655 | ) lt ON m.id_member = lt.id_member |
||
1656 | WHERE m.last_login <= {int:mrcutoff} |
||
1657 | ORDER BY last_login |
||
1658 | LIMIT {int:limit}'; |
||
1659 | $result = $smcFunc['db_query']('', $sql, |
||
1660 | array( |
||
1661 | 'limit' => $maxMembers, |
||
1662 | 'dcutoff' => $cleanupBeyond, |
||
1663 | 'mrcutoff' => $markReadCutoff, |
||
1664 | 'unwatched' => 0, |
||
1665 | ) |
||
1666 | ); |
||
1667 | |||
1668 | // Move to array... |
||
1669 | $members = $smcFunc['db_fetch_all']($result); |
||
1670 | $smcFunc['db_free_result']($result); |
||
1671 | |||
1672 | // Nothing to do? |
||
1673 | if (empty($members)) |
||
1674 | return true; |
||
1675 | |||
1676 | // Determine action based on last_login... |
||
1677 | $purgeMembers = array(); |
||
1678 | $markReadMembers = array(); |
||
1679 | foreach($members as $member) |
||
1680 | { |
||
1681 | if ($member['last_login'] <= $cleanupBeyond) |
||
1682 | $purgeMembers[] = $member['id_member']; |
||
1683 | elseif ($member['last_login'] <= $markReadCutoff) |
||
1684 | $markReadMembers[] = $member['id_member']; |
||
1685 | } |
||
1686 | |||
1687 | if (!empty($purgeMembers) && !empty($modSettings['mark_read_delete_beyond'])) |
||
1688 | { |
||
1689 | // Delete rows from log_boards |
||
1690 | $sql = 'DELETE FROM {db_prefix}log_boards |
||
1691 | WHERE id_member IN ({array_int:members})'; |
||
1692 | $smcFunc['db_query']('', $sql, |
||
1693 | array( |
||
1694 | 'members' => $purgeMembers, |
||
1695 | ) |
||
1696 | ); |
||
1697 | // Delete rows from log_mark_read |
||
1698 | $sql = 'DELETE FROM {db_prefix}log_mark_read |
||
1699 | WHERE id_member IN ({array_int:members})'; |
||
1700 | $smcFunc['db_query']('', $sql, |
||
1701 | array( |
||
1702 | 'members' => $purgeMembers, |
||
1703 | ) |
||
1704 | ); |
||
1705 | // Delete rows from log_topics |
||
1706 | $sql = 'DELETE FROM {db_prefix}log_topics |
||
1707 | WHERE id_member IN ({array_int:members}) |
||
1708 | AND unwatched = {int:unwatched}'; |
||
1709 | $smcFunc['db_query']('', $sql, |
||
1710 | array( |
||
1711 | 'members' => $purgeMembers, |
||
1712 | 'unwatched' => 0, |
||
1713 | ) |
||
1714 | ); |
||
1715 | } |
||
1716 | |||
1717 | // Nothing left to do? |
||
1718 | if (empty($markReadMembers) || empty($modSettings['mark_read_beyond'])) |
||
1719 | return true; |
||
1720 | |||
1721 | // Find board inserts to perform... |
||
1722 | // Get board info for each member from log_topics. |
||
1723 | // Note this user may have read many topics on that board, |
||
1724 | // but we just want one row each, & the ID of the last message read in each board. |
||
1725 | $boards = array(); |
||
0 ignored issues
–
show
|
|||
1726 | $sql = 'SELECT lt.id_member, t.id_board, MAX(lt.id_msg) AS id_last_message |
||
1727 | FROM {db_prefix}topics t |
||
1728 | INNER JOIN |
||
1729 | ( |
||
1730 | SELECT id_member, id_topic, id_msg |
||
1731 | FROM {db_prefix}log_topics |
||
1732 | WHERE id_member IN ({array_int:members}) |
||
1733 | ) lt ON t.id_topic = lt.id_topic |
||
1734 | GROUP BY lt.id_member, t.id_board'; |
||
1735 | $result = $smcFunc['db_query']('', $sql, |
||
1736 | array( |
||
1737 | 'members' => $markReadMembers, |
||
1738 | ) |
||
1739 | ); |
||
1740 | $boards = $smcFunc['db_fetch_all']($result); |
||
1741 | $smcFunc['db_free_result']($result); |
||
1742 | |||
1743 | // Create one SQL statement for this set of inserts |
||
1744 | if (!empty($boards)) |
||
1745 | { |
||
1746 | $smcFunc['db_insert']('replace', |
||
1747 | '{db_prefix}log_mark_read', |
||
1748 | array('id_member' => 'int', 'id_board' => 'int', 'id_msg' => 'int'), |
||
1749 | $boards, |
||
1750 | array('id_member', 'id_board') |
||
1751 | ); |
||
1752 | } |
||
1753 | |||
1754 | // Finally, delete this set's rows from log_topics |
||
1755 | $sql = 'DELETE FROM {db_prefix}log_topics |
||
1756 | WHERE id_member IN ({array_int:members}) |
||
1757 | AND unwatched = {int:unwatched}'; |
||
1758 | $smcFunc['db_query']('', $sql, |
||
1759 | array( |
||
1760 | 'members' => $markReadMembers, |
||
1761 | 'unwatched' => 0, |
||
1762 | ) |
||
1763 | ); |
||
1764 | |||
1765 | return true; |
||
1766 | } |
||
1767 | |||
1768 | ?> |