Issues (1065)

Sources/ScheduledTasks.php (2 issues)

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 2025 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1.5
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, priority
716
		FROM {db_prefix}mail_queue
717
		ORDER BY priority ASC, id_mail ASC
718
		LIMIT {int:limit}',
719
		array(
720
			'limit' => ceil($number * 0.7),
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
			'priority' => $row['priority'],
738
		);
739
	}
740
	$smcFunc['db_free_result']($request);
741
742
	// Random emails from the queue..
743
	if (!empty($ids)) {
744
		$request = $smcFunc['db_query']('', '
745
			SELECT id_mail, recipient, body, subject, headers, send_html, time_sent, private, priority
746
			FROM {db_prefix}mail_queue
747
			WHERE id_mail NOT IN ({array_int:ids})
748
			ORDER BY RAND()
749
			LIMIT {int:limit}',
750
			array(
751
				'ids' => $ids,
752
				'limit' => ceil($number * 0.3),
753
			)
754
		);
755
		while ($row = $smcFunc['db_fetch_assoc']($request))
756
		{
757
			// We want to delete these from the database ASAP, so just get the data and go.
758
			$ids[] = $row['id_mail'];
759
			$emails[] = array(
760
				'to' => $row['recipient'],
761
				'body' => $row['body'],
762
				'subject' => $row['subject'],
763
				'headers' => $row['headers'],
764
				'send_html' => $row['send_html'],
765
				'time_sent' => $row['time_sent'],
766
				'private' => $row['private'],
767
				'priority' => $row['priority'],
768
			);
769
		}
770
		$smcFunc['db_free_result']($request);
771
	}
772
773
	// Delete, delete, delete!!!
774
	if (!empty($ids))
775
		$smcFunc['db_query']('', '
776
			DELETE FROM {db_prefix}mail_queue
777
			WHERE id_mail IN ({array_int:mail_list})',
778
			array(
779
				'mail_list' => $ids,
780
			)
781
		);
782
783
	// Don't believe we have any left?
784
	if (count($ids) < $number)
785
	{
786
		// Only update the setting if no-one else has beaten us to it.
787
		$smcFunc['db_query']('', '
788
			UPDATE {db_prefix}settings
789
			SET value = {string:no_send}
790
			WHERE variable = {literal:mail_next_send}
791
				AND value = {string:last_mail_send}',
792
			array(
793
				'no_send' => '0',
794
				'last_mail_send' => $modSettings['mail_next_send'],
795
			)
796
		);
797
	}
798
799
	if (empty($ids))
800
		return false;
801
802
	if (!empty($modSettings['mail_type']) && $modSettings['smtp_host'] != '')
803
		require_once($sourcedir . '/Subs-Post.php');
804
805
	// Send each email, yea!
806
	$failed_emails = array();
807
	$max_priority = 127;
808
	$smtp_expire = 259200;
809
	$priority_offset = 8;
810
	foreach ($emails as $email)
811
	{
812
		// First, figure out when the next send attempt should happen based on the current priority.
813
		$next_send_time = $email['time_sent'];
814
		if ($email['priority'] >= $priority_offset) {
815
			for ($i = 0; $i < $email['priority']; $i++) {
816
				$next_send_time += 20 * max(0, $email['priority'] - $priority_offset);
817
			}
818
		}
819
820
		// If the email is too old, discard it.
821
		if ($next_send_time > $email['time_sent'] + $smtp_expire) {
822
			continue;
823
		}
824
825
		++$email['priority'];
826
827
		// Don't send if it's too soon.
828
		if (time() < $next_send_time) {
829
			if ($email['priority'] < $max_priority) {
830
				$failed_emails[] = array($email['to'], $email['body'], $email['subject'], $email['headers'], $email['send_html'], $email['time_sent'], $email['private'], $email['priority']);
831
			}
832
833
			continue;
834
		}
835
836
		// Try to send.
837
		if (empty($modSettings['mail_type']) || $modSettings['smtp_host'] == '')
838
		{
839
			$email['subject'] = strtr($email['subject'], array("\r" => '', "\n" => ''));
840
			if (!empty($modSettings['mail_strip_carriage']))
841
			{
842
				$email['body'] = strtr($email['body'], array("\r" => ''));
843
				$email['headers'] = strtr($email['headers'], array("\r" => ''));
844
			}
845
846
			// No point logging a specific error here, as we have no language. PHP error is helpful anyway...
847
			$result = mail(strtr($email['to'], array("\r" => '', "\n" => '')), $email['subject'], $email['body'], $email['headers']);
848
849
			// Try to stop a timeout, this would be bad...
850
			@set_time_limit(300);
851
			if (function_exists('apache_reset_timeout'))
852
				@apache_reset_timeout();
853
		}
854
		else
855
			$result = smtp_mail(array($email['to']), $email['subject'], $email['body'], $email['headers']);
856
857
		// If we failed, and we haven't already hit the limit, schedule this for another attempt.
858
		if (empty($result) && $email['priority'] < $max_priority) {
859
			$failed_emails[] = array($email['to'], $email['body'], $email['subject'], $email['headers'], $email['send_html'], $email['time_sent'], $email['private'], $email['priority']);
860
		}
861
	}
862
863
	// Any emails that didn't send?
864
	if (!empty($failed_emails))
865
	{
866
		// Update the failed attempts check.
867
		$smcFunc['db_insert']('replace',
868
			'{db_prefix}settings',
869
			array('variable' => 'string', 'value' => 'string'),
870
			array('mail_failed_attempts', empty($modSettings['mail_failed_attempts']) ? 1 : ++$modSettings['mail_failed_attempts']),
871
			array('variable')
872
		);
873
874
		// If we have failed to many times, tell mail to wait a bit and try again.
875
		if ($modSettings['mail_failed_attempts'] > 5)
876
			$smcFunc['db_query']('', '
877
				UPDATE {db_prefix}settings
878
				SET value = {string:next_mail_send}
879
				WHERE variable = {literal:mail_next_send}
880
					AND value = {string:last_send}',
881
				array(
882
					'next_mail_send' => time() + 60,
883
					'last_send' => $modSettings['mail_next_send'],
884
				)
885
			);
886
887
		// Add our email back to the queue, manually.
888
		$smcFunc['db_insert']('insert',
889
			'{db_prefix}mail_queue',
890
			array('recipient' => 'string', 'body' => 'string', 'subject' => 'string', 'headers' => 'string', 'send_html' => 'string', 'time_sent' => 'string', 'private' => 'int', 'priority' => 'int'),
891
			$failed_emails,
892
			array('id_mail')
893
		);
894
895
		return false;
896
	}
897
	// We where unable to send the email, clear our failed attempts.
898
	elseif (!empty($modSettings['mail_failed_attempts']))
899
		$smcFunc['db_query']('', '
900
			UPDATE {db_prefix}settings
901
			SET value = {string:zero}
902
			WHERE variable = {string:mail_failed_attempts}',
903
			array(
904
				'zero' => '0',
905
				'mail_failed_attempts' => 'mail_failed_attempts',
906
			)
907
		);
908
909
	// Had something to send...
910
	return true;
911
}
912
913
/**
914
 * Calculate the next time the passed tasks should be triggered.
915
 *
916
 * @param string|array $tasks The ID of a single task or an array of tasks
917
 * @param bool $forceUpdate Whether to force the tasks to run now
918
 */
919
function CalculateNextTrigger($tasks = array(), $forceUpdate = false)
920
{
921
	global $modSettings, $smcFunc;
922
923
	$task_query = '';
924
	if (!is_array($tasks))
925
		$tasks = array($tasks);
926
927
	// Actually have something passed?
928
	if (!empty($tasks))
929
	{
930
		if (!isset($tasks[0]) || is_numeric($tasks[0]))
931
			$task_query = ' AND id_task IN ({array_int:tasks})';
932
		else
933
			$task_query = ' AND task IN ({array_string:tasks})';
934
	}
935
	$nextTaskTime = empty($tasks) ? time() + 86400 : $modSettings['next_task_time'];
936
937
	// Get the critical info for the tasks.
938
	$request = $smcFunc['db_query']('', '
939
		SELECT id_task, next_time, time_offset, time_regularity, time_unit
940
		FROM {db_prefix}scheduled_tasks
941
		WHERE disabled = {int:no_disabled}
942
			' . $task_query,
943
		array(
944
			'no_disabled' => 0,
945
			'tasks' => $tasks,
946
		)
947
	);
948
	$tasks = array();
949
	while ($row = $smcFunc['db_fetch_assoc']($request))
950
	{
951
		$next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']);
952
953
		// Only bother moving the task if it's out of place or we're forcing it!
954
		if ($forceUpdate || $next_time < $row['next_time'] || $row['next_time'] < time())
955
			$tasks[$row['id_task']] = $next_time;
956
		else
957
			$next_time = $row['next_time'];
958
959
		// If this is sooner than the current next task, make this the next task.
960
		if ($next_time < $nextTaskTime)
961
			$nextTaskTime = $next_time;
962
	}
963
	$smcFunc['db_free_result']($request);
964
965
	// Now make the changes!
966
	foreach ($tasks as $id => $time)
967
		$smcFunc['db_query']('', '
968
			UPDATE {db_prefix}scheduled_tasks
969
			SET next_time = {int:next_time}
970
			WHERE id_task = {int:id_task}',
971
			array(
972
				'next_time' => $time,
973
				'id_task' => $id,
974
			)
975
		);
976
977
	// If the next task is now different update.
978
	if ($modSettings['next_task_time'] != $nextTaskTime)
979
		updateSettings(array('next_task_time' => $nextTaskTime));
980
}
981
982
/**
983
 * Simply returns a time stamp of the next instance of these time parameters.
984
 *
985
 * @param int $regularity The regularity
986
 * @param string $unit What unit are we using - 'm' for minutes, 'd' for days, 'w' for weeks or anything else for seconds
987
 * @param int $offset The offset
988
 * @return int The timestamp for the specified time
989
 */
990
function next_time($regularity, $unit, $offset)
991
{
992
	// Just in case!
993
	if ($regularity == 0)
994
		$regularity = 2;
995
996
	$curMin = date('i', time());
997
998
	// If the unit is minutes only check regularity in minutes.
999
	if ($unit == 'm')
1000
	{
1001
		$off = date('i', $offset);
1002
1003
		// If it's now just pretend it ain't,
1004
		if ($off == $curMin)
1005
			$next_time = time() + $regularity;
1006
		else
1007
		{
1008
			// Make sure that the offset is always in the past.
1009
			$off = $off > $curMin ? $off - 60 : $off;
1010
1011
			while ($off <= $curMin)
1012
				$off += $regularity;
1013
1014
			// Now we know when the time should be!
1015
			$next_time = time() + 60 * ($off - $curMin);
1016
		}
1017
	}
1018
	// Otherwise, work out what the offset would be with today's date.
1019
	else
1020
	{
1021
		$next_time = mktime(date('H', $offset), date('i', $offset), 0, date('m'), date('d'), date('Y'));
1022
1023
		// Make the time offset in the past!
1024
		if ($next_time > time())
1025
		{
1026
			$next_time -= 86400;
1027
		}
1028
1029
		// Default we'll jump in hours.
1030
		$applyOffset = 3600;
1031
		// 24 hours = 1 day.
1032
		if ($unit == 'd')
1033
			$applyOffset = 86400;
1034
		// Otherwise a week.
1035
		if ($unit == 'w')
1036
			$applyOffset = 604800;
1037
1038
		$applyOffset *= $regularity;
1039
1040
		// Just add on the offset.
1041
		while ($next_time <= time())
1042
		{
1043
			$next_time += $applyOffset;
1044
		}
1045
	}
1046
1047
	return $next_time;
1048
}
1049
1050
/**
1051
 * This loads the bare minimum data to allow us to load language files!
1052
 */
1053
function loadEssentialThemeData()
1054
{
1055
	global $settings, $modSettings, $smcFunc, $mbname, $context, $sourcedir, $txt;
1056
1057
	// Get all the default theme variables.
1058
	$result = $smcFunc['db_query']('', '
1059
		SELECT id_theme, variable, value
1060
		FROM {db_prefix}themes
1061
		WHERE id_member = {int:no_member}
1062
			AND id_theme IN (1, {int:theme_guests})',
1063
		array(
1064
			'no_member' => 0,
1065
			'theme_guests' => !empty($modSettings['theme_guests']) ? $modSettings['theme_guests'] : 1,
1066
		)
1067
	);
1068
	while ($row = $smcFunc['db_fetch_assoc']($result))
1069
	{
1070
		$settings[$row['variable']] = $row['value'];
1071
1072
		// Is this the default theme?
1073
		if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1')
1074
			$settings['default_' . $row['variable']] = $row['value'];
1075
	}
1076
	$smcFunc['db_free_result']($result);
1077
1078
	// Check we have some directories setup.
1079
	if (empty($settings['template_dirs']))
1080
	{
1081
		$settings['template_dirs'] = array($settings['theme_dir']);
1082
1083
		// Based on theme (if there is one).
1084
		if (!empty($settings['base_theme_dir']))
1085
			$settings['template_dirs'][] = $settings['base_theme_dir'];
1086
1087
		// Lastly the default theme.
1088
		if ($settings['theme_dir'] != $settings['default_theme_dir'])
1089
			$settings['template_dirs'][] = $settings['default_theme_dir'];
1090
	}
1091
1092
	// Assume we want this.
1093
	$context['forum_name'] = $mbname;
1094
	$context['forum_name_html_safe'] = $smcFunc['htmlspecialchars']($context['forum_name']);
1095
1096
	// Check loadLanguage actually exists!
1097
	if (!function_exists('loadLanguage'))
1098
	{
1099
		require_once($sourcedir . '/Load.php');
1100
		require_once($sourcedir . '/Subs.php');
1101
	}
1102
1103
	loadLanguage('index+Modifications');
1104
1105
	// Just in case it wasn't already set elsewhere.
1106
	$context['character_set'] = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set'];
1107
	$context['utf8'] = $context['character_set'] === 'UTF-8';
1108
	$context['right_to_left'] = !empty($txt['lang_rtl']);
1109
1110
	// Tell fatal_lang_error() to not reload the theme.
1111
	$context['theme_loaded'] = true;
1112
}
1113
1114
/**
1115
 * This retieves data (e.g. last version of SMF) from sm.org
1116
 */
1117
function scheduled_fetchSMfiles()
1118
{
1119
	global $sourcedir, $txt, $language, $modSettings, $smcFunc, $context;
1120
1121
	// What files do we want to get
1122
	$request = $smcFunc['db_query']('', '
1123
		SELECT id_file, filename, path, parameters
1124
		FROM {db_prefix}admin_info_files',
1125
		array(
1126
		)
1127
	);
1128
1129
	$js_files = array();
1130
1131
	while ($row = $smcFunc['db_fetch_assoc']($request))
1132
	{
1133
		$js_files[$row['id_file']] = array(
1134
			'filename' => $row['filename'],
1135
			'path' => $row['path'],
1136
			'parameters' => sprintf($row['parameters'], $language, urlencode($modSettings['time_format']), urlencode(SMF_FULL_VERSION)),
1137
		);
1138
	}
1139
1140
	$smcFunc['db_free_result']($request);
1141
1142
	// Just in case we run into a problem.
1143
	loadEssentialThemeData();
1144
	loadLanguage('Errors', $language, false);
1145
1146
	foreach ($js_files as $ID_FILE => $file)
1147
	{
1148
		// Create the url
1149
		$server = empty($file['path']) || (substr($file['path'], 0, 7) != 'http://' && substr($file['path'], 0, 8) != 'https://') ? 'https://www.simplemachines.org' : '';
1150
		$url = $server . (!empty($file['path']) ? $file['path'] : $file['path']) . $file['filename'] . (!empty($file['parameters']) ? '?' . $file['parameters'] : '');
1151
1152
		// Get the file
1153
		$file_data = fetch_web_data($url);
1154
1155
		// 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.
1156
		if ($file_data === false)
1157
		{
1158
			$context['scheduled_errors']['fetchSMfiles'][] = sprintf($txt['st_cannot_retrieve_file'], $url);
1159
			log_error(sprintf($txt['st_cannot_retrieve_file'], $url));
1160
			return false;
1161
		}
1162
1163
		// Save the file to the database.
1164
		$smcFunc['db_query']('substring', '
1165
			UPDATE {db_prefix}admin_info_files
1166
			SET data = SUBSTRING({string:file_data}, 1, 65534)
1167
			WHERE id_file = {int:id_file}',
1168
			array(
1169
				'id_file' => $ID_FILE,
1170
				'file_data' => $file_data,
1171
			)
1172
		);
1173
	}
1174
	return true;
1175
}
1176
1177
/**
1178
 * Happy birthday!!
1179
 */
1180
function scheduled_birthdayemails()
1181
{
1182
	global $smcFunc;
1183
1184
	$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
1185
		array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
1186
		array('$sourcedir/tasks/Birthday-Notify.php', 'Birthday_Notify_Background', '', 0),
1187
		array()
1188
	);
1189
1190
	return true;
1191
}
1192
1193
/**
1194
 * Weekly maintenance
1195
 */
1196
function scheduled_weekly_maintenance()
1197
{
1198
	global $modSettings, $smcFunc, $cache_enable, $cacheAPI;
1199
1200
	// Delete some settings that needn't be set if they are otherwise empty.
1201
	$emptySettings = array(
1202
		'warning_mute', 'warning_moderate', 'warning_watch', 'warning_show', 'disableCustomPerPage', 'spider_mode', 'spider_group',
1203
		'paid_currency_code', 'paid_currency_symbol', 'paid_email_to', 'paid_email', 'paid_enabled', 'paypal_email',
1204
		'search_enable_captcha', 'search_floodcontrol_time', 'show_spider_online',
1205
	);
1206
1207
	$smcFunc['db_query']('', '
1208
		DELETE FROM {db_prefix}settings
1209
		WHERE variable IN ({array_string:setting_list})
1210
			AND (value = {string:zero_value} OR value = {string:blank_value})',
1211
		array(
1212
			'zero_value' => '0',
1213
			'blank_value' => '',
1214
			'setting_list' => $emptySettings,
1215
		)
1216
	);
1217
1218
	// Some settings we never want to keep - they are just there for temporary purposes.
1219
	$deleteAnywaySettings = array(
1220
		'attachment_full_notified',
1221
	);
1222
1223
	$smcFunc['db_query']('', '
1224
		DELETE FROM {db_prefix}settings
1225
		WHERE variable IN ({array_string:setting_list})',
1226
		array(
1227
			'setting_list' => $deleteAnywaySettings,
1228
		)
1229
	);
1230
1231
	// Ok should we prune the logs?
1232
	if (!empty($modSettings['pruningOptions']))
1233
	{
1234
		if (!empty($modSettings['pruningOptions']) && strpos($modSettings['pruningOptions'], ',') !== false)
1235
			list ($modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']) = explode(',', $modSettings['pruningOptions']);
1236
1237
		if (!empty($modSettings['pruneErrorLog']))
1238
		{
1239
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1240
			$t = time() - $modSettings['pruneErrorLog'] * 86400;
1241
1242
			$smcFunc['db_query']('', '
1243
				DELETE FROM {db_prefix}log_errors
1244
				WHERE log_time < {int:log_time}',
1245
				array(
1246
					'log_time' => $t,
1247
				)
1248
			);
1249
		}
1250
1251
		if (!empty($modSettings['pruneModLog']))
1252
		{
1253
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1254
			$t = time() - $modSettings['pruneModLog'] * 86400;
1255
1256
			$smcFunc['db_query']('', '
1257
				DELETE FROM {db_prefix}log_actions
1258
				WHERE log_time < {int:log_time}
1259
					AND id_log = {int:moderation_log}',
1260
				array(
1261
					'log_time' => $t,
1262
					'moderation_log' => 1,
1263
				)
1264
			);
1265
		}
1266
1267
		if (!empty($modSettings['pruneBanLog']))
1268
		{
1269
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1270
			$t = time() - $modSettings['pruneBanLog'] * 86400;
1271
1272
			$smcFunc['db_query']('', '
1273
				DELETE FROM {db_prefix}log_banned
1274
				WHERE log_time < {int:log_time}',
1275
				array(
1276
					'log_time' => $t,
1277
				)
1278
			);
1279
		}
1280
1281
		if (!empty($modSettings['pruneReportLog']))
1282
		{
1283
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1284
			$t = time() - $modSettings['pruneReportLog'] * 86400;
1285
1286
			// This one is more complex then the other logs.  First we need to figure out which reports are too old.
1287
			$reports = array();
1288
			$result = $smcFunc['db_query']('', '
1289
				SELECT id_report
1290
				FROM {db_prefix}log_reported
1291
				WHERE time_started < {int:time_started}
1292
					AND closed = {int:closed}
1293
					AND ignore_all = {int:not_ignored}',
1294
				array(
1295
					'time_started' => $t,
1296
					'closed' => 1,
1297
					'not_ignored' => 0,
1298
				)
1299
			);
1300
1301
			while ($row = $smcFunc['db_fetch_row']($result))
1302
				$reports[] = $row[0];
1303
1304
			$smcFunc['db_free_result']($result);
1305
1306
			if (!empty($reports))
1307
			{
1308
				// Now delete the reports...
1309
				$smcFunc['db_query']('', '
1310
					DELETE FROM {db_prefix}log_reported
1311
					WHERE id_report IN ({array_int:report_list})',
1312
					array(
1313
						'report_list' => $reports,
1314
					)
1315
				);
1316
				// And delete the comments for those reports...
1317
				$smcFunc['db_query']('', '
1318
					DELETE FROM {db_prefix}log_reported_comments
1319
					WHERE id_report IN ({array_int:report_list})',
1320
					array(
1321
						'report_list' => $reports,
1322
					)
1323
				);
1324
			}
1325
		}
1326
1327
		if (!empty($modSettings['pruneScheduledTaskLog']))
1328
		{
1329
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1330
			$t = time() - $modSettings['pruneScheduledTaskLog'] * 86400;
1331
1332
			$smcFunc['db_query']('', '
1333
				DELETE FROM {db_prefix}log_scheduled_tasks
1334
				WHERE time_run < {int:time_run}',
1335
				array(
1336
					'time_run' => $t,
1337
				)
1338
			);
1339
		}
1340
1341
		if (!empty($modSettings['pruneSpiderHitLog']))
1342
		{
1343
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1344
			$t = time() - $modSettings['pruneSpiderHitLog'] * 86400;
1345
1346
			$smcFunc['db_query']('', '
1347
				DELETE FROM {db_prefix}log_spider_hits
1348
				WHERE log_time < {int:log_time}',
1349
				array(
1350
					'log_time' => $t,
1351
				)
1352
			);
1353
		}
1354
	}
1355
1356
	// Get rid of any paid subscriptions that were never actioned.
1357
	$smcFunc['db_query']('', '
1358
		DELETE FROM {db_prefix}log_subscribed
1359
		WHERE end_time = {int:no_end_time}
1360
			AND status = {int:not_active}
1361
			AND start_time < {int:start_time}
1362
			AND payments_pending < {int:payments_pending}',
1363
		array(
1364
			'no_end_time' => 0,
1365
			'not_active' => 0,
1366
			'start_time' => time() - 60,
1367
			'payments_pending' => 1,
1368
		)
1369
	);
1370
1371
	// Some OS's don't seem to clean out their sessions.
1372
	$smcFunc['db_query']('', '
1373
		DELETE FROM {db_prefix}sessions
1374
		WHERE last_update < {int:last_update}',
1375
		array(
1376
			'last_update' => time() - 86400,
1377
		)
1378
	);
1379
1380
	// Update the regex of top level domains with the IANA's latest official list
1381
	$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
1382
		array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
1383
		array('$sourcedir/tasks/UpdateTldRegex.php', 'Update_TLD_Regex', '', 0), array()
1384
	);
1385
1386
	// Ensure Unicode data files are up to date
1387
	$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
1388
		array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
1389
		array('$sourcedir/tasks/UpdateUnicode.php', 'Update_Unicode', '', 0), array()
1390
	);
1391
1392
	// Run Cache housekeeping
1393
	if (!empty($cache_enable) && !empty($cacheAPI))
1394
		$cacheAPI->housekeeping();
1395
1396
	// Prevent stale minimized CSS and JavaScript from cluttering up the theme directories
1397
	deleteAllMinified();
1398
1399
	// Maybe there's more to do.
1400
	call_integration_hook('integrate_weekly_maintenance');
1401
1402
	return true;
1403
}
1404
1405
/**
1406
 * Perform the standard checks on expiring/near expiring subscriptions.
1407
 */
1408
function scheduled_paid_subscriptions()
1409
{
1410
	global $sourcedir, $scripturl, $smcFunc, $modSettings, $language;
1411
1412
	// Start off by checking for removed subscriptions.
1413
	$request = $smcFunc['db_query']('', '
1414
		SELECT id_subscribe, id_member
1415
		FROM {db_prefix}log_subscribed
1416
		WHERE status = {int:is_active}
1417
			AND end_time < {int:time_now}',
1418
		array(
1419
			'is_active' => 1,
1420
			'time_now' => time(),
1421
		)
1422
	);
1423
	while ($row = $smcFunc['db_fetch_assoc']($request))
1424
	{
1425
		require_once($sourcedir . '/ManagePaid.php');
1426
		removeSubscription($row['id_subscribe'], $row['id_member']);
1427
	}
1428
	$smcFunc['db_free_result']($request);
1429
1430
	// Get all those about to expire that have not had a reminder sent.
1431
	$request = $smcFunc['db_query']('', '
1432
		SELECT ls.id_sublog, m.id_member, m.member_name, m.email_address, m.lngfile, s.name, ls.end_time
1433
		FROM {db_prefix}log_subscribed AS ls
1434
			JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe)
1435
			JOIN {db_prefix}members AS m ON (m.id_member = ls.id_member)
1436
		WHERE ls.status = {int:is_active}
1437
			AND ls.reminder_sent = {int:reminder_sent}
1438
			AND s.reminder > {int:reminder_wanted}
1439
			AND ls.end_time < ({int:time_now} + s.reminder * 86400)',
1440
		array(
1441
			'is_active' => 1,
1442
			'reminder_sent' => 0,
1443
			'reminder_wanted' => 0,
1444
			'time_now' => time(),
1445
		)
1446
	);
1447
	$subs_reminded = array();
1448
	$members = array();
1449
	while ($row = $smcFunc['db_fetch_assoc']($request))
1450
	{
1451
		// If this is the first one load the important bits.
1452
		if (empty($subs_reminded))
1453
		{
1454
			require_once($sourcedir . '/Subs-Post.php');
1455
			// Need the below for loadLanguage to work!
1456
			loadEssentialThemeData();
1457
		}
1458
1459
		$subs_reminded[] = $row['id_sublog'];
1460
		$members[$row['id_member']] = $row;
1461
	}
1462
	$smcFunc['db_free_result']($request);
1463
1464
	// Load alert preferences
1465
	require_once($sourcedir . '/Subs-Notify.php');
1466
	$notifyPrefs = getNotifyPrefs(array_keys($members), 'paidsubs_expiring', true);
1467
	$alert_rows = array();
1468
	foreach ($members as $row)
1469
	{
1470
		$replacements = array(
1471
			'PROFILE_LINK' => $scripturl . '?action=profile;area=subscriptions;u=' . $row['id_member'],
1472
			'REALNAME' => $row['member_name'],
1473
			'SUBSCRIPTION' => $row['name'],
1474
			'END_DATE' => strip_tags(timeformat($row['end_time'])),
1475
		);
1476
1477
		$emaildata = loadEmailTemplate('paid_subscription_reminder', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
1478
1479
		// Check notification prefs.
1480
		$subs_notify = isset($notifyPrefs[$row['id_member']]['paidsubs_expiring']) ? $notifyPrefs[$row['id_member']]['paidsubs_expiring'] : 0;
1481
1482
		// Send the actual email.
1483
		if ($subs_notify & 0x02)
1484
			sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, 'paid_sub_remind', $emaildata['is_html'], 2);
1485
1486
		if ($subs_notify & 0x01)
1487
		{
1488
			$alert_rows[] = array(
1489
				'alert_time' => time(),
1490
				'id_member' => $row['id_member'],
1491
				'id_member_started' => $row['id_member'],
1492
				'member_name' => $row['member_name'],
1493
				'content_type' => 'paidsubs',
1494
				'content_id' => $row['id_sublog'],
1495
				'content_action' => 'expiring',
1496
				'is_read' => 0,
1497
				'extra' => $smcFunc['json_encode'](array(
1498
					'subscription_name' => $row['name'],
1499
					'end_time' => $row['end_time'],
1500
				)),
1501
			);
1502
			updateMemberData($row['id_member'], array('alerts' => '+'));
1503
		}
1504
	}
1505
1506
	// Insert the alerts if any
1507
	if (!empty($alert_rows))
1508
		$smcFunc['db_insert']('',
1509
			'{db_prefix}user_alerts',
1510
			array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
1511
				'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
1512
			$alert_rows,
1513
			array()
1514
		);
1515
1516
	// Mark the reminder as sent.
1517
	if (!empty($subs_reminded))
1518
		$smcFunc['db_query']('', '
1519
			UPDATE {db_prefix}log_subscribed
1520
			SET reminder_sent = {int:reminder_sent}
1521
			WHERE id_sublog IN ({array_int:subscription_list})',
1522
			array(
1523
				'subscription_list' => $subs_reminded,
1524
				'reminder_sent' => 1,
1525
			)
1526
		);
1527
1528
	return true;
1529
}
1530
1531
/**
1532
 * Check for un-posted attachments is something we can do once in a while :P
1533
 * This function uses opendir cycling through all the attachments
1534
 */
1535
function scheduled_remove_temp_attachments()
1536
{
1537
	global $smcFunc, $modSettings, $context, $txt;
1538
1539
	// We need to know where this thing is going.
1540
	if (!empty($modSettings['currentAttachmentUploadDir']))
1541
	{
1542
		if (!is_array($modSettings['attachmentUploadDir']))
1543
			$modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true);
1544
1545
		// Just use the current path for temp files.
1546
		$attach_dirs = $modSettings['attachmentUploadDir'];
1547
	}
1548
	else
1549
	{
1550
		$attach_dirs = array($modSettings['attachmentUploadDir']);
1551
	}
1552
1553
	foreach ($attach_dirs as $attach_dir)
1554
	{
1555
		$dir = @opendir($attach_dir);
1556
		if (!$dir)
1557
		{
1558
			loadEssentialThemeData();
1559
			loadLanguage('Post');
1560
			$context['scheduled_errors']['remove_temp_attachments'][] = $txt['cant_access_upload_path'] . ' (' . $attach_dir . ')';
1561
			log_error($txt['cant_access_upload_path'] . ' (' . $attach_dir . ')', 'critical');
1562
			return false;
1563
		}
1564
1565
		while ($file = readdir($dir))
1566
		{
1567
			if ($file == '.' || $file == '..')
1568
				continue;
1569
1570
			if (strpos($file, 'post_tmp_') !== false)
1571
			{
1572
				// Temp file is more than 5 hours old!
1573
				if (filemtime($attach_dir . '/' . $file) < time() - 18000)
1574
					@unlink($attach_dir . '/' . $file);
1575
			}
1576
		}
1577
		closedir($dir);
1578
	}
1579
1580
	return true;
1581
}
1582
1583
/**
1584
 * Check for move topic notices that have past their best by date
1585
 */
1586
function scheduled_remove_topic_redirect()
1587
{
1588
	global $smcFunc, $sourcedir;
1589
1590
	// init
1591
	$topics = array();
1592
1593
	// We will need this for language files
1594
	loadEssentialThemeData();
1595
1596
	// Find all of the old MOVE topic notices that were set to expire
1597
	$request = $smcFunc['db_query']('', '
1598
		SELECT id_topic
1599
		FROM {db_prefix}topics
1600
		WHERE redirect_expires <= {int:redirect_expires}
1601
			AND redirect_expires <> 0',
1602
		array(
1603
			'redirect_expires' => time(),
1604
		)
1605
	);
1606
1607
	while ($row = $smcFunc['db_fetch_row']($request))
1608
		$topics[] = $row[0];
1609
	$smcFunc['db_free_result']($request);
1610
1611
	// Zap, your gone
1612
	if (count($topics) > 0)
1613
	{
1614
		require_once($sourcedir . '/RemoveTopic.php');
1615
		removeTopics($topics, false, true);
1616
	}
1617
1618
	return true;
1619
}
1620
1621
/**
1622
 * Check for old drafts and remove them
1623
 */
1624
function scheduled_remove_old_drafts()
1625
{
1626
	global $smcFunc, $sourcedir, $modSettings;
1627
1628
	if (empty($modSettings['drafts_keep_days']))
1629
		return true;
1630
1631
	// init
1632
	$drafts = array();
1633
1634
	// We need this for language items
1635
	loadEssentialThemeData();
1636
1637
	// Find all of the old drafts
1638
	$request = $smcFunc['db_query']('', '
1639
		SELECT id_draft
1640
		FROM {db_prefix}user_drafts
1641
		WHERE poster_time <= {int:poster_time_old}',
1642
		array(
1643
			'poster_time_old' => time() - (86400 * $modSettings['drafts_keep_days']),
1644
		)
1645
	);
1646
1647
	while ($row = $smcFunc['db_fetch_row']($request))
1648
		$drafts[] = (int) $row[0];
1649
	$smcFunc['db_free_result']($request);
1650
1651
	// If we have old one, remove them
1652
	if (count($drafts) > 0)
1653
	{
1654
		require_once($sourcedir . '/Drafts.php');
1655
		DeleteDraft($drafts, false);
1656
	}
1657
1658
	return true;
1659
}
1660
1661
/**
1662
 * Prune log_topics, log_boards & log_mark_boards_read.
1663
 * For users who haven't been active in a long time, purge these records.
1664
 * For users who haven't been active in a shorter time, mark boards as read,
1665
 * pruning log_topics.
1666
 */
1667
function scheduled_prune_log_topics()
1668
{
1669
	global $smcFunc, $sourcedir, $modSettings;
1670
1671
	// If set to zero, bypass
1672
	if (empty($modSettings['mark_read_max_users']) || (empty($modSettings['mark_read_beyond']) && empty($modSettings['mark_read_delete_beyond'])))
1673
		return true;
1674
1675
	// Convert to timestamps for comparison
1676
	if (empty($modSettings['mark_read_beyond']))
1677
		$markReadCutoff = 0;
1678
	else
1679
		$markReadCutoff = time() - $modSettings['mark_read_beyond'] * 86400;
1680
1681
	if (empty($modSettings['mark_read_delete_beyond']))
1682
		$cleanupBeyond = 0;
1683
	else
1684
		$cleanupBeyond = time() - $modSettings['mark_read_delete_beyond'] * 86400;
1685
1686
	$maxMembers = $modSettings['mark_read_max_users'];
1687
1688
	// You're basically saying to just purge, so just purge
1689
	if ($markReadCutoff < $cleanupBeyond)
1690
		$markReadCutoff = $cleanupBeyond;
1691
1692
	// Try to prevent timeouts
1693
	@set_time_limit(300);
1694
	if (function_exists('apache_reset_timeout'))
1695
		@apache_reset_timeout();
1696
1697
	// Start off by finding the records in log_boards, log_topics & log_mark_read
1698
	// for users who haven't been around the longest...
1699
	$members = array();
0 ignored issues
show
The assignment to $members is dead and can be removed.
Loading history...
1700
	$sql = 'SELECT lb.id_member, m.last_login
1701
			FROM {db_prefix}members m
1702
			INNER JOIN
1703
			(
1704
				SELECT DISTINCT id_member
1705
				FROM {db_prefix}log_boards
1706
			) lb ON m.id_member = lb.id_member
1707
			WHERE m.last_login <= {int:dcutoff}
1708
		UNION
1709
		SELECT lmr.id_member, m.last_login
1710
			FROM {db_prefix}members m
1711
			INNER JOIN
1712
			(
1713
				SELECT DISTINCT id_member
1714
				FROM {db_prefix}log_mark_read
1715
			) lmr ON m.id_member = lmr.id_member
1716
			WHERE m.last_login <= {int:dcutoff}
1717
		UNION
1718
		SELECT lt.id_member, m.last_login
1719
			FROM {db_prefix}members m
1720
			INNER JOIN
1721
			(
1722
				SELECT DISTINCT id_member
1723
				FROM {db_prefix}log_topics
1724
				WHERE unwatched = {int:unwatched}
1725
			) lt ON m.id_member = lt.id_member
1726
			WHERE m.last_login <= {int:mrcutoff}
1727
		ORDER BY last_login
1728
		LIMIT {int:limit}';
1729
	$result = $smcFunc['db_query']('', $sql,
1730
		array(
1731
			'limit' => $maxMembers,
1732
			'dcutoff' => $cleanupBeyond,
1733
			'mrcutoff' => $markReadCutoff,
1734
			'unwatched' => 0,
1735
		)
1736
	);
1737
1738
	// Move to array...
1739
	$members = $smcFunc['db_fetch_all']($result);
1740
	$smcFunc['db_free_result']($result);
1741
1742
	// Nothing to do?
1743
	if (empty($members))
1744
		return true;
1745
1746
	// Determine action based on last_login...
1747
	$purgeMembers = array();
1748
	$markReadMembers = array();
1749
	foreach($members as $member)
1750
	{
1751
		if ($member['last_login'] <= $cleanupBeyond)
1752
			$purgeMembers[] = $member['id_member'];
1753
		elseif ($member['last_login'] <= $markReadCutoff)
1754
			$markReadMembers[] = $member['id_member'];
1755
	}
1756
1757
	if (!empty($purgeMembers) && !empty($modSettings['mark_read_delete_beyond']))
1758
	{
1759
		// Delete rows from log_boards
1760
		$sql = 'DELETE FROM {db_prefix}log_boards
1761
			WHERE id_member IN ({array_int:members})';
1762
		$smcFunc['db_query']('', $sql,
1763
			array(
1764
				'members' => $purgeMembers,
1765
			)
1766
		);
1767
		// Delete rows from log_mark_read
1768
		$sql = 'DELETE FROM {db_prefix}log_mark_read
1769
			WHERE id_member IN ({array_int:members})';
1770
		$smcFunc['db_query']('', $sql,
1771
			array(
1772
				'members' => $purgeMembers,
1773
			)
1774
		);
1775
		// Delete rows from log_topics
1776
		$sql = 'DELETE FROM {db_prefix}log_topics
1777
			WHERE id_member IN ({array_int:members})
1778
				AND unwatched = {int:unwatched}';
1779
		$smcFunc['db_query']('', $sql,
1780
			array(
1781
				'members' => $purgeMembers,
1782
				'unwatched' => 0,
1783
			)
1784
		);
1785
	}
1786
1787
	// Nothing left to do?
1788
	if (empty($markReadMembers) || empty($modSettings['mark_read_beyond']))
1789
		return true;
1790
1791
	// Find board inserts to perform...
1792
	// Get board info for each member from log_topics.
1793
	// Note this user may have read many topics on that board,
1794
	// but we just want one row each, & the ID of the last message read in each board.
1795
	$boards = array();
0 ignored issues
show
The assignment to $boards is dead and can be removed.
Loading history...
1796
	$sql = 'SELECT lt.id_member, t.id_board, MAX(lt.id_msg) AS id_last_message
1797
		FROM {db_prefix}topics t
1798
		INNER JOIN
1799
		(
1800
			SELECT id_member, id_topic, id_msg
1801
			FROM {db_prefix}log_topics
1802
			WHERE id_member IN ({array_int:members})
1803
		) lt ON t.id_topic = lt.id_topic
1804
		GROUP BY lt.id_member, t.id_board';
1805
	$result = $smcFunc['db_query']('', $sql,
1806
		array(
1807
			'members' => $markReadMembers,
1808
		)
1809
	);
1810
	$boards = $smcFunc['db_fetch_all']($result);
1811
	$smcFunc['db_free_result']($result);
1812
1813
	// Create one SQL statement for this set of inserts
1814
	if (!empty($boards))
1815
	{
1816
		$smcFunc['db_insert']('replace',
1817
			'{db_prefix}log_mark_read',
1818
			array('id_member' => 'int', 'id_board' => 'int', 'id_msg' => 'int'),
1819
			$boards,
1820
			array('id_member', 'id_board')
1821
		);
1822
	}
1823
1824
	// Finally, delete this set's rows from log_topics
1825
	$sql = 'DELETE FROM {db_prefix}log_topics
1826
		WHERE id_member IN ({array_int:members})
1827
			AND unwatched = {int:unwatched}';
1828
	$smcFunc['db_query']('', $sql,
1829
		array(
1830
			'members' => $markReadMembers,
1831
			'unwatched' => 0,
1832
		)
1833
	);
1834
1835
	return true;
1836
}
1837
1838
?>