Passed
Pull Request — release-2.1 (#5113)
by John
04:45
created

scheduled_approval_notification()   F

Complexity

Conditions 32
Paths > 20000

Size

Total Lines 210
Code Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 32
eloc 100
c 0
b 0
f 0
nop 0
dl 0
loc 210
rs 0
nc 54965

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 http://www.simplemachines.org
10
 * @copyright 2018 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 Beta 4
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 $time_start, $smcFunc;
25
26
	// Special case for doing the mail queue.
27
	if (isset($_GET['scheduled']) && $_GET['scheduled'] == 'mailq')
28
		ReduceMailQueue();
29
	else
30
	{
31
		$task_string = '';
32
33
		// Select the next task to do.
34
		$request = $smcFunc['db_query']('', '
35
			SELECT id_task, task, next_time, time_offset, time_regularity, time_unit, callable
36
			FROM {db_prefix}scheduled_tasks
37
			WHERE disabled = {int:not_disabled}
38
				AND next_time <= {int:current_time}
39
			ORDER BY next_time ASC
40
			LIMIT 1',
41
			array(
42
				'not_disabled' => 0,
43
				'current_time' => time(),
44
			)
45
		);
46
		if ($smcFunc['db_num_rows']($request) != 0)
47
		{
48
			// The two important things really...
49
			$row = $smcFunc['db_fetch_assoc']($request);
50
51
			// When should this next be run?
52
			$next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']);
53
54
			// How long in seconds it the gap?
55
			$duration = $row['time_regularity'];
56
			if ($row['time_unit'] == 'm')
57
				$duration *= 60;
58
			elseif ($row['time_unit'] == 'h')
59
				$duration *= 3600;
60
			elseif ($row['time_unit'] == 'd')
61
				$duration *= 86400;
62
			elseif ($row['time_unit'] == 'w')
63
				$duration *= 604800;
64
65
			// If we were really late running this task actually skip the next one.
66
			if (time() + ($duration / 2) > $next_time)
67
				$next_time += $duration;
68
69
			// Update it now, so no others run this!
70
			$smcFunc['db_query']('', '
71
				UPDATE {db_prefix}scheduled_tasks
72
				SET next_time = {int:next_time}
73
				WHERE id_task = {int:id_task}
74
					AND next_time = {int:current_next_time}',
75
				array(
76
					'next_time' => $next_time,
77
					'id_task' => $row['id_task'],
78
					'current_next_time' => $row['next_time'],
79
				)
80
			);
81
			$affected_rows = $smcFunc['db_affected_rows']();
82
83
			// What kind of task are we handling?
84
			if (!empty($row['callable']))
85
				$task_string = $row['callable'];
86
87
			// Default SMF task or old mods?
88
			elseif (function_exists('scheduled_' . $row['task']))
89
				$task_string = 'scheduled_' . $row['task'];
90
91
			// One last resource, the task name.
92
			elseif (!empty($row['task']))
93
				$task_string = $row['task'];
94
95
			// The function must exist or we are wasting our time, plus do some timestamp checking, and database check!
96
			if (!empty($task_string) && (!isset($_GET['ts']) || $_GET['ts'] == $row['next_time']) && $affected_rows)
97
			{
98
				ignore_user_abort(true);
99
100
				// Get the callable.
101
				$callable_task = call_helper($task_string, true);
102
103
				// Perform the task.
104
				if (!empty($callable_task))
105
					$completed = call_user_func($callable_task);
0 ignored issues
show
Bug introduced by
It seems like $callable_task can also be of type boolean; however, parameter $function of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
					$completed = call_user_func(/** @scrutinizer ignore-type */ $callable_task);
Loading history...
106
107
				else
108
					$completed = false;
109
110
				// Log that we did it ;)
111
				if ($completed)
112
				{
113
					$total_time = round(microtime(true) - $time_start, 3);
114
					$smcFunc['db_insert']('',
115
						'{db_prefix}log_scheduled_tasks',
116
						array(
117
							'id_task' => 'int', 'time_run' => 'int', 'time_taken' => 'float',
118
						),
119
						array(
120
							$row['id_task'], time(), (int) $total_time,
121
						),
122
						array()
123
					);
124
				}
125
			}
126
		}
127
		$smcFunc['db_free_result']($request);
128
129
		// Get the next timestamp right.
130
		$request = $smcFunc['db_query']('', '
131
			SELECT next_time
132
			FROM {db_prefix}scheduled_tasks
133
			WHERE disabled = {int:not_disabled}
134
			ORDER BY next_time ASC
135
			LIMIT 1',
136
			array(
137
				'not_disabled' => 0,
138
			)
139
		);
140
		// No new task scheduled yet?
141
		if ($smcFunc['db_num_rows']($request) === 0)
142
			$nextEvent = time() + 86400;
143
		else
144
			list ($nextEvent) = $smcFunc['db_fetch_row']($request);
145
		$smcFunc['db_free_result']($request);
146
147
		updateSettings(array('next_task_time' => $nextEvent));
148
	}
149
150
	// Shall we return?
151
	if (!isset($_GET['scheduled']))
152
		return true;
153
154
	// Finally, send some stuff...
155
	header('expires: Mon, 26 Jul 1997 05:00:00 GMT');
156
	header('last-modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
157
	header('content-type: image/gif');
158
	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");
159
}
160
161
/**
162
 * Do some daily cleaning up.
163
 */
164
function scheduled_daily_maintenance()
165
{
166
	global $smcFunc, $modSettings, $sourcedir, $boarddir, $db_type, $image_proxy_enabled;
167
168
	// First clean out the cache.
169
	clean_cache();
170
171
	// If warning decrement is enabled and we have people who have not had a new warning in 24 hours, lower their warning level.
172
	list (, , $modSettings['warning_decrement']) = explode(',', $modSettings['warning_settings']);
173
	if ($modSettings['warning_decrement'])
174
	{
175
		// Find every member who has a warning level...
176
		$request = $smcFunc['db_query']('', '
177
			SELECT id_member, warning
178
			FROM {db_prefix}members
179
			WHERE warning > {int:no_warning}',
180
			array(
181
				'no_warning' => 0,
182
			)
183
		);
184
		$members = array();
185
		while ($row = $smcFunc['db_fetch_assoc']($request))
186
			$members[$row['id_member']] = $row['warning'];
187
		$smcFunc['db_free_result']($request);
188
189
		// Have some members to check?
190
		if (!empty($members))
191
		{
192
			// Find out when they were last warned.
193
			$request = $smcFunc['db_query']('', '
194
				SELECT id_recipient, MAX(log_time) AS last_warning
195
				FROM {db_prefix}log_comments
196
				WHERE id_recipient IN ({array_int:member_list})
197
					AND comment_type = {string:warning}
198
				GROUP BY id_recipient',
199
				array(
200
					'member_list' => array_keys($members),
201
					'warning' => 'warning',
202
				)
203
			);
204
			$member_changes = array();
205
			while ($row = $smcFunc['db_fetch_assoc']($request))
206
			{
207
				// More than 24 hours ago?
208
				if ($row['last_warning'] <= time() - 86400)
209
					$member_changes[] = array(
210
						'id' => $row['id_recipient'],
211
						'warning' => $members[$row['id_recipient']] >= $modSettings['warning_decrement'] ? $members[$row['id_recipient']] - $modSettings['warning_decrement'] : 0,
212
					);
213
			}
214
			$smcFunc['db_free_result']($request);
215
216
			// Have some members to change?
217
			if (!empty($member_changes))
218
				foreach ($member_changes as $change)
219
					$smcFunc['db_query']('', '
220
						UPDATE {db_prefix}members
221
						SET warning = {int:warning}
222
						WHERE id_member = {int:id_member}',
223
						array(
224
							'warning' => $change['warning'],
225
							'id_member' => $change['id'],
226
						)
227
					);
228
		}
229
	}
230
231
	// Do any spider stuff.
232
	if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1)
233
	{
234
		require_once($sourcedir . '/ManageSearchEngines.php');
235
		consolidateSpiderStats();
236
	}
237
238
	// Clean up some old login history information.
239
	$smcFunc['db_query']('', '
240
		DELETE FROM {db_prefix}member_logins
241
		WHERE time < {int:oldLogins}',
242
		array(
243
			'oldLogins' => time() - (!empty($modSettings['loginHistoryDays']) ? 60 * 60 * 24 * $modSettings['loginHistoryDays'] : 2592000),
244
	));
245
246
	// Run Imageproxy housekeeping
247
	if (!empty($image_proxy_enabled))
248
	{
249
		global $proxyhousekeeping;
250
		$proxyhousekeeping = true;
251
252
		require_once($boarddir . '/proxy.php');
253
		$proxy = new ProxyServer();
254
		$proxy->housekeeping();
255
256
		unset($proxyhousekeeping);
257
	}
258
259
	// Log we've done it...
260
	return true;
261
}
262
263
/**
264
 * Send out a daily email of all subscribed topics.
265
 */
266
function scheduled_daily_digest()
267
{
268
	global $is_weekly, $txt, $mbname, $scripturl, $sourcedir, $smcFunc, $context, $modSettings;
269
270
	// We'll want this...
271
	require_once($sourcedir . '/Subs-Post.php');
272
	loadEssentialThemeData();
273
274
	$is_weekly = !empty($is_weekly) ? 1 : 0;
275
276
	// Right - get all the notification data FIRST.
277
	$request = $smcFunc['db_query']('', '
278
		SELECT ln.id_topic, COALESCE(t.id_board, ln.id_board) AS id_board, mem.email_address, mem.member_name,
279
			mem.lngfile, mem.id_member
280
		FROM {db_prefix}log_notify AS ln
281
			JOIN {db_prefix}members AS mem ON (mem.id_member = ln.id_member)
282
			LEFT JOIN {db_prefix}topics AS t ON (ln.id_topic != {int:empty_topic} AND t.id_topic = ln.id_topic)
283
		WHERE mem.is_activated = {int:is_activated}',
284
		array(
285
			'empty_topic' => 0,
286
			'is_activated' => 1,
287
		)
288
	);
289
	$members = array();
290
	$langs = array();
291
	$notify = array();
292
	while ($row = $smcFunc['db_fetch_assoc']($request))
293
	{
294
		if (!isset($members[$row['id_member']]))
295
		{
296
			$members[$row['id_member']] = array(
297
				'email' => $row['email_address'],
298
				'name' => $row['member_name'],
299
				'id' => $row['id_member'],
300
				'lang' => $row['lngfile'],
301
			);
302
			$langs[$row['lngfile']] = $row['lngfile'];
303
		}
304
305
		// Store this useful data!
306
		$boards[$row['id_board']] = $row['id_board'];
307
		if ($row['id_topic'])
308
			$notify['topics'][$row['id_topic']][] = $row['id_member'];
309
		else
310
			$notify['boards'][$row['id_board']][] = $row['id_member'];
311
	}
312
	$smcFunc['db_free_result']($request);
313
314
	if (empty($boards))
315
		return true;
316
317
	// Just get the board names.
318
	$request = $smcFunc['db_query']('', '
319
		SELECT id_board, name
320
		FROM {db_prefix}boards
321
		WHERE id_board IN ({array_int:board_list})',
322
		array(
323
			'board_list' => $boards,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $boards does not seem to be defined for all execution paths leading up to this point.
Loading history...
324
		)
325
	);
326
	$boards = array();
327
	while ($row = $smcFunc['db_fetch_assoc']($request))
328
		$boards[$row['id_board']] = $row['name'];
329
	$smcFunc['db_free_result']($request);
330
331
	if (empty($boards))
332
		return true;
333
334
	// Get the actual topics...
335
	$request = $smcFunc['db_query']('', '
336
		SELECT ld.note_type, t.id_topic, t.id_board, t.id_member_started, m.id_msg, m.subject,
337
			b.name AS board_name
338
		FROM {db_prefix}log_digest AS ld
339
			JOIN {db_prefix}topics AS t ON (t.id_topic = ld.id_topic
340
				AND t.id_board IN ({array_int:board_list}))
341
			JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
342
			JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
343
		WHERE ' . ($is_weekly ? 'ld.daily != {int:daily_value}' : 'ld.daily IN (0, 2)'),
344
		array(
345
			'board_list' => array_keys($boards),
346
			'daily_value' => 2,
347
		)
348
	);
349
	$types = array();
350
	while ($row = $smcFunc['db_fetch_assoc']($request))
351
	{
352
		if (!isset($types[$row['note_type']][$row['id_board']]))
353
			$types[$row['note_type']][$row['id_board']] = array(
354
				'lines' => array(),
355
				'name' => $row['board_name'],
356
				'id' => $row['id_board'],
357
			);
358
359
		if ($row['note_type'] == 'reply')
360
		{
361
			if (isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]))
362
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['count']++;
363
			else
364
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array(
365
					'id' => $row['id_topic'],
366
					'subject' => un_htmlspecialchars($row['subject']),
367
					'count' => 1,
368
				);
369
		}
370
		elseif ($row['note_type'] == 'topic')
371
		{
372
			if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]))
373
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array(
374
					'id' => $row['id_topic'],
375
					'subject' => un_htmlspecialchars($row['subject']),
376
				);
377
		}
378
		else
379
		{
380
			if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]))
381
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array(
382
					'id' => $row['id_topic'],
383
					'subject' => un_htmlspecialchars($row['subject']),
384
					'starter' => $row['id_member_started'],
385
				);
386
		}
387
388
		$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array();
389
		if (!empty($notify['topics'][$row['id_topic']]))
390
			$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']]);
391
		if (!empty($notify['boards'][$row['id_board']]))
392
			$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']]);
393
	}
394
	$smcFunc['db_free_result']($request);
395
396
	if (empty($types))
397
		return true;
398
399
	// Let's load all the languages into a cache thingy.
400
	$langtxt = array();
401
	foreach ($langs as $lang)
402
	{
403
		loadLanguage('Post', $lang);
404
		loadLanguage('index', $lang);
405
		loadLanguage('EmailTemplates', $lang);
406
		$langtxt[$lang] = array(
407
			'subject' => $txt['digest_subject_' . ($is_weekly ? 'weekly' : 'daily')],
408
			'char_set' => $txt['lang_character_set'],
409
			'intro' => sprintf($txt['digest_intro_' . ($is_weekly ? 'weekly' : 'daily')], $mbname),
410
			'new_topics' => $txt['digest_new_topics'],
411
			'topic_lines' => $txt['digest_new_topics_line'],
412
			'new_replies' => $txt['digest_new_replies'],
413
			'mod_actions' => $txt['digest_mod_actions'],
414
			'replies_one' => $txt['digest_new_replies_one'],
415
			'replies_many' => $txt['digest_new_replies_many'],
416
			'sticky' => $txt['digest_mod_act_sticky'],
417
			'lock' => $txt['digest_mod_act_lock'],
418
			'unlock' => $txt['digest_mod_act_unlock'],
419
			'remove' => $txt['digest_mod_act_remove'],
420
			'move' => $txt['digest_mod_act_move'],
421
			'merge' => $txt['digest_mod_act_merge'],
422
			'split' => $txt['digest_mod_act_split'],
423
			'bye' => $txt['regards_team'],
424
		);
425
	}
426
427
	// The preferred way...
428
	require_once($sourcedir . '/Subs-Notify.php');
429
	$prefs = getNotifyPrefs(array_keys($members), array('msg_notify_type', 'msg_notify_pref'), true);
430
431
	// Right - send out the silly things - this will take quite some space!
432
	$members_sent = array();
433
	foreach ($members as $mid => $member)
434
	{
435
		$frequency = isset($prefs[$mid]['msg_notify_pref']) ? $prefs[$mid]['msg_notify_pref'] : 0;
436
		$notify_types = !empty($prefs[$mid]['msg_notify_type']) ? $prefs[$mid]['msg_notify_type'] : 1;
437
438
		// Did they not elect to choose this?
439
		if ($frequency < 3 || $frequency == 4 && !$is_weekly || $frequency == 3 && $is_weekly || $notify_types == 4)
440
			continue;
441
442
		// Right character set!
443
		$context['character_set'] = empty($modSettings['global_character_set']) ? $langtxt[$lang]['char_set'] : $modSettings['global_character_set'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $lang seems to be defined by a foreach iteration on line 401. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
444
445
		// Do the start stuff!
446
		$email = array(
447
			'subject' => $mbname . ' - ' . $langtxt[$lang]['subject'],
448
			'body' => $member['name'] . ',' . "\n\n" . $langtxt[$lang]['intro'] . "\n" . $scripturl . '?action=profile;area=notification;u=' . $member['id'] . "\n",
449
			'email' => $member['email'],
450
		);
451
452
		// All new topics?
453
		if (isset($types['topic']))
454
		{
455
			$titled = false;
456
			foreach ($types['topic'] as $id => $board)
457
				foreach ($board['lines'] as $topic)
458
					if (in_array($mid, $topic['members']))
459
					{
460
						if (!$titled)
461
						{
462
							$email['body'] .= "\n" . $langtxt[$lang]['new_topics'] . ':' . "\n" . '-----------------------------------------------';
463
							$titled = true;
464
						}
465
						$email['body'] .= "\n" . sprintf($langtxt[$lang]['topic_lines'], $topic['subject'], $board['name']);
466
					}
467
			if ($titled)
468
				$email['body'] .= "\n";
469
		}
470
471
		// What about replies?
472
		if (isset($types['reply']))
473
		{
474
			$titled = false;
475
			foreach ($types['reply'] as $id => $board)
476
				foreach ($board['lines'] as $topic)
477
					if (in_array($mid, $topic['members']))
478
					{
479
						if (!$titled)
480
						{
481
							$email['body'] .= "\n" . $langtxt[$lang]['new_replies'] . ':' . "\n" . '-----------------------------------------------';
482
							$titled = true;
483
						}
484
						$email['body'] .= "\n" . ($topic['count'] == 1 ? sprintf($langtxt[$lang]['replies_one'], $topic['subject']) : sprintf($langtxt[$lang]['replies_many'], $topic['count'], $topic['subject']));
485
					}
486
487
			if ($titled)
488
				$email['body'] .= "\n";
489
		}
490
491
		// Finally, moderation actions!
492
		if ($notify_types < 3)
493
		{
494
			$titled = false;
495
			foreach ($types as $note_type => $type)
496
			{
497
				if ($note_type == 'topic' || $note_type == 'reply')
498
					continue;
499
500
				foreach ($type as $id => $board)
501
					foreach ($board['lines'] as $topic)
502
						if (in_array($mid, $topic['members']))
503
						{
504
							if (!$titled)
505
							{
506
								$email['body'] .= "\n" . $langtxt[$lang]['mod_actions'] . ':' . "\n" . '-----------------------------------------------';
507
								$titled = true;
508
							}
509
							$email['body'] .= "\n" . sprintf($langtxt[$lang][$note_type], $topic['subject']);
510
						}
511
			}
512
		}
513
		if ($titled)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $titled does not seem to be defined for all execution paths leading up to this point.
Loading history...
514
			$email['body'] .= "\n";
515
516
		// Then just say our goodbyes!
517
		$email['body'] .= "\n\n" . $txt['regards_team'];
518
519
		// Send it - low priority!
520
		sendmail($email['email'], $email['subject'], $email['body'], null, 'digest', false, 4);
521
522
		$members_sent[] = $mid;
523
	}
524
525
	// Clean up...
526
	if ($is_weekly)
527
	{
528
		$smcFunc['db_query']('', '
529
			DELETE FROM {db_prefix}log_digest
530
			WHERE daily != {int:not_daily}',
531
			array(
532
				'not_daily' => 0,
533
			)
534
		);
535
		$smcFunc['db_query']('', '
536
			UPDATE {db_prefix}log_digest
537
			SET daily = {int:daily_value}
538
			WHERE daily = {int:not_daily}',
539
			array(
540
				'daily_value' => 2,
541
				'not_daily' => 0,
542
			)
543
		);
544
	}
545
	else
546
	{
547
		// Clear any only weekly ones, and stop us from sending daily again.
548
		$smcFunc['db_query']('', '
549
			DELETE FROM {db_prefix}log_digest
550
			WHERE daily = {int:daily_value}',
551
			array(
552
				'daily_value' => 2,
553
			)
554
		);
555
		$smcFunc['db_query']('', '
556
			UPDATE {db_prefix}log_digest
557
			SET daily = {int:both_value}
558
			WHERE daily = {int:no_value}',
559
			array(
560
				'both_value' => 1,
561
				'no_value' => 0,
562
			)
563
		);
564
	}
565
566
	// Just in case the member changes their settings mark this as sent.
567
	if (!empty($members_sent))
568
	{
569
		$smcFunc['db_query']('', '
570
			UPDATE {db_prefix}log_notify
571
			SET sent = {int:is_sent}
572
			WHERE id_member IN ({array_int:member_list})',
573
			array(
574
				'member_list' => $members_sent,
575
				'is_sent' => 1,
576
			)
577
		);
578
	}
579
580
	// Log we've done it...
581
	return true;
582
}
583
584
/**
585
 * Like the daily stuff - just seven times less regular ;)
586
 */
587
function scheduled_weekly_digest()
588
{
589
	global $is_weekly;
590
591
	// We just pass through to the daily function - avoid duplication!
592
	$is_weekly = true;
593
	return scheduled_daily_digest();
594
}
595
596
/**
597
 * Send a group of emails from the mail queue.
598
 *
599
 * @param bool|int $number The number to send each loop through or false to use the standard limits
600
 * @param bool $override_limit Whether to bypass the limit
601
 * @param bool $force_send Whether to forcibly send the messages now (useful when using cron jobs)
602
 * @return bool Whether things were sent
603
 */
604
function ReduceMailQueue($number = false, $override_limit = false, $force_send = false)
605
{
606
	global $modSettings, $smcFunc, $sourcedir;
607
608
	// Are we intending another script to be sending out the queue?
609
	if (!empty($modSettings['mail_queue_use_cron']) && empty($force_send))
610
		return false;
611
612
	// By default send 5 at once.
613
	if (!$number)
614
		$number = empty($modSettings['mail_quantity']) ? 5 : $modSettings['mail_quantity'];
615
616
	// If we came with a timestamp, and that doesn't match the next event, then someone else has beaten us.
617
	if (isset($_GET['ts']) && $_GET['ts'] != $modSettings['mail_next_send'] && empty($force_send))
618
		return false;
619
620
	// By default move the next sending on by 10 seconds, and require an affected row.
621
	if (!$override_limit)
622
	{
623
		$delay = !empty($modSettings['mail_queue_delay']) ? $modSettings['mail_queue_delay'] : (!empty($modSettings['mail_limit']) && $modSettings['mail_limit'] < 5 ? 10 : 5);
624
625
		$smcFunc['db_query']('', '
626
			UPDATE {db_prefix}settings
627
			SET value = {string:next_mail_send}
628
			WHERE variable = {literal:mail_next_send}
629
				AND value = {string:last_send}',
630
			array(
631
				'next_mail_send' => time() + $delay,
632
				'last_send' => $modSettings['mail_next_send'],
633
			)
634
		);
635
		if ($smcFunc['db_affected_rows']() == 0)
636
			return false;
637
		$modSettings['mail_next_send'] = time() + $delay;
638
	}
639
640
	// If we're not overriding how many are we allow to send?
641
	if (!$override_limit && !empty($modSettings['mail_limit']))
642
	{
643
		list ($mt, $mn) = @explode('|', $modSettings['mail_recent']);
644
645
		// Nothing worth noting...
646
		if (empty($mn) || $mt < time() - 60)
647
		{
648
			$mt = time();
649
			$mn = $number;
650
		}
651
		// Otherwise we have a few more we can spend?
652
		elseif ($mn < $modSettings['mail_limit'])
653
		{
654
			$mn += $number;
655
		}
656
		// No more I'm afraid, return!
657
		else
658
			return false;
659
660
		// Reflect that we're about to send some, do it now to be safe.
661
		updateSettings(array('mail_recent' => $mt . '|' . $mn));
662
	}
663
664
	// Now we know how many we're sending, let's send them.
665
	$request = $smcFunc['db_query']('', '
666
		SELECT /*!40001 SQL_NO_CACHE */ id_mail, recipient, body, subject, headers, send_html, time_sent, private
667
		FROM {db_prefix}mail_queue
668
		ORDER BY priority ASC, id_mail ASC
669
		LIMIT {int:limit}',
670
		array(
671
			'limit' => $number,
672
		)
673
	);
674
	$ids = array();
675
	$emails = array();
676
	while ($row = $smcFunc['db_fetch_assoc']($request))
677
	{
678
		// We want to delete these from the database ASAP, so just get the data and go.
679
		$ids[] = $row['id_mail'];
680
		$emails[] = array(
681
			'to' => $row['recipient'],
682
			'body' => $row['body'],
683
			'subject' => $row['subject'],
684
			'headers' => $row['headers'],
685
			'send_html' => $row['send_html'],
686
			'time_sent' => $row['time_sent'],
687
			'private' => $row['private'],
688
		);
689
	}
690
	$smcFunc['db_free_result']($request);
691
692
	// Delete, delete, delete!!!
693
	if (!empty($ids))
694
		$smcFunc['db_query']('', '
695
			DELETE FROM {db_prefix}mail_queue
696
			WHERE id_mail IN ({array_int:mail_list})',
697
			array(
698
				'mail_list' => $ids,
699
			)
700
		);
701
702
	// Don't believe we have any left?
703
	if (count($ids) < $number)
704
	{
705
		// Only update the setting if no-one else has beaten us to it.
706
		$smcFunc['db_query']('', '
707
			UPDATE {db_prefix}settings
708
			SET value = {string:no_send}
709
			WHERE variable = {literal:mail_next_send}
710
				AND value = {string:last_mail_send}',
711
			array(
712
				'no_send' => '0',
713
				'last_mail_send' => $modSettings['mail_next_send'],
714
			)
715
		);
716
	}
717
718
	if (empty($ids))
719
		return false;
720
721
	if (!empty($modSettings['mail_type']) && $modSettings['smtp_host'] != '')
722
		require_once($sourcedir . '/Subs-Post.php');
723
724
	// Send each email, yea!
725
	$failed_emails = array();
726
	foreach ($emails as $email)
727
	{
728
		if (empty($modSettings['mail_type']) || $modSettings['smtp_host'] == '')
729
		{
730
			$email['subject'] = strtr($email['subject'], array("\r" => '', "\n" => ''));
731
			if (!empty($modSettings['mail_strip_carriage']))
732
			{
733
				$email['body'] = strtr($email['body'], array("\r" => ''));
734
				$email['headers'] = strtr($email['headers'], array("\r" => ''));
735
			}
736
737
			// No point logging a specific error here, as we have no language. PHP error is helpful anyway...
738
			$result = mail(strtr($email['to'], array("\r" => '', "\n" => '')), $email['subject'], $email['body'], $email['headers']);
739
740
			// Try to stop a timeout, this would be bad...
741
			@set_time_limit(300);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for set_time_limit(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

741
			/** @scrutinizer ignore-unhandled */ @set_time_limit(300);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
742
			if (function_exists('apache_reset_timeout'))
743
				@apache_reset_timeout();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for apache_reset_timeout(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

743
				/** @scrutinizer ignore-unhandled */ @apache_reset_timeout();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
744
		}
745
		else
746
			$result = smtp_mail(array($email['to']), $email['subject'], $email['body'], $email['headers']);
747
748
		// Hopefully it sent?
749
		if (!$result)
750
			$failed_emails[] = array($email['to'], $email['body'], $email['subject'], $email['headers'], $email['send_html'], $email['time_sent'], $email['private']);
751
	}
752
753
	// Any emails that didn't send?
754
	if (!empty($failed_emails))
755
	{
756
		// Update the failed attempts check.
757
		$smcFunc['db_insert']('replace',
758
			'{db_prefix}settings',
759
			array('variable' => 'string', 'value' => 'string'),
760
			array('mail_failed_attempts', empty($modSettings['mail_failed_attempts']) ? 1 : ++$modSettings['mail_failed_attempts']),
761
			array('variable')
762
		);
763
764
		// If we have failed to many times, tell mail to wait a bit and try again.
765
		if ($modSettings['mail_failed_attempts'] > 5)
766
			$smcFunc['db_query']('', '
767
				UPDATE {db_prefix}settings
768
				SET value = {string:next_mail_send}
769
				WHERE variable = {literal:mail_next_send}
770
					AND value = {string:last_send}',
771
				array(
772
					'next_mail_send' => time() + 60,
773
					'last_send' => $modSettings['mail_next_send'],
774
			));
775
776
		// Add our email back to the queue, manually.
777
		$smcFunc['db_insert']('insert',
778
			'{db_prefix}mail_queue',
779
			array('recipient' => 'string', 'body' => 'string', 'subject' => 'string', 'headers' => 'string', 'send_html' => 'string', 'time_sent' => 'string', 'private' => 'int'),
780
			$failed_emails,
781
			array('id_mail')
782
		);
783
784
		return false;
785
	}
786
	// We where unable to send the email, clear our failed attempts.
787
	elseif (!empty($modSettings['mail_failed_attempts']))
788
		$smcFunc['db_query']('', '
789
			UPDATE {db_prefix}settings
790
			SET value = {string:zero}
791
			WHERE variable = {string:mail_failed_attempts}',
792
			array(
793
				'zero' => '0',
794
				'mail_failed_attempts' => 'mail_failed_attempts',
795
		));
796
797
	// Had something to send...
798
	return true;
799
}
800
801
/**
802
 * Calculate the next time the passed tasks should be triggered.
803
 *
804
 * @param string|array $tasks The ID of a single task or an array of tasks
805
 * @param bool $forceUpdate Whether to force the tasks to run now
806
 */
807
function CalculateNextTrigger($tasks = array(), $forceUpdate = false)
808
{
809
	global $modSettings, $smcFunc;
810
811
	$task_query = '';
812
	if (!is_array($tasks))
813
		$tasks = array($tasks);
814
815
	// Actually have something passed?
816
	if (!empty($tasks))
817
	{
818
		if (!isset($tasks[0]) || is_numeric($tasks[0]))
819
			$task_query = ' AND id_task IN ({array_int:tasks})';
820
		else
821
			$task_query = ' AND task IN ({array_string:tasks})';
822
	}
823
	$nextTaskTime = empty($tasks) ? time() + 86400 : $modSettings['next_task_time'];
824
825
	// Get the critical info for the tasks.
826
	$request = $smcFunc['db_query']('', '
827
		SELECT id_task, next_time, time_offset, time_regularity, time_unit
828
		FROM {db_prefix}scheduled_tasks
829
		WHERE disabled = {int:no_disabled}
830
			' . $task_query,
831
		array(
832
			'no_disabled' => 0,
833
			'tasks' => $tasks,
834
		)
835
	);
836
	$tasks = array();
837
	while ($row = $smcFunc['db_fetch_assoc']($request))
838
	{
839
		$next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']);
840
841
		// Only bother moving the task if it's out of place or we're forcing it!
842
		if ($forceUpdate || $next_time < $row['next_time'] || $row['next_time'] < time())
843
			$tasks[$row['id_task']] = $next_time;
844
		else
845
			$next_time = $row['next_time'];
846
847
		// If this is sooner than the current next task, make this the next task.
848
		if ($next_time < $nextTaskTime)
849
			$nextTaskTime = $next_time;
850
	}
851
	$smcFunc['db_free_result']($request);
852
853
	// Now make the changes!
854
	foreach ($tasks as $id => $time)
855
		$smcFunc['db_query']('', '
856
			UPDATE {db_prefix}scheduled_tasks
857
			SET next_time = {int:next_time}
858
			WHERE id_task = {int:id_task}',
859
			array(
860
				'next_time' => $time,
861
				'id_task' => $id,
862
			)
863
		);
864
865
	// If the next task is now different update.
866
	if ($modSettings['next_task_time'] != $nextTaskTime)
867
		updateSettings(array('next_task_time' => $nextTaskTime));
868
}
869
870
/**
871
 * Simply returns a time stamp of the next instance of these time parameters.
872
 *
873
 * @param int $regularity The regularity
874
 * @param string $unit What unit are we using - 'm' for minutes, 'd' for days, 'w' for weeks or anything else for seconds
875
 * @param int $offset The offset
876
 * @return int The timestamp for the specified time
877
 */
878
function next_time($regularity, $unit, $offset)
879
{
880
	// Just in case!
881
	if ($regularity == 0)
882
		$regularity = 2;
883
884
	$curMin = date('i', time());
885
886
	// If the unit is minutes only check regularity in minutes.
887
	if ($unit == 'm')
888
	{
889
		$off = date('i', $offset);
890
891
		// If it's now just pretend it ain't,
892
		if ($off == $curMin)
893
			$next_time = time() + $regularity;
894
		else
895
		{
896
			// Make sure that the offset is always in the past.
897
			$off = $off > $curMin ? $off - 60 : $off;
898
899
			while ($off <= $curMin)
900
				$off += $regularity;
901
902
			// Now we know when the time should be!
903
			$next_time = time() + 60 * ($off - $curMin);
904
		}
905
	}
906
	// Otherwise, work out what the offset would be with today's date.
907
	else
908
	{
909
		$next_time = mktime(date('H', $offset), date('i', $offset), 0, date('m'), date('d'), date('Y'));
0 ignored issues
show
Bug introduced by
date('H', $offset) of type string is incompatible with the type integer expected by parameter $hour of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

909
		$next_time = mktime(/** @scrutinizer ignore-type */ date('H', $offset), date('i', $offset), 0, date('m'), date('d'), date('Y'));
Loading history...
Bug introduced by
date('i', $offset) of type string is incompatible with the type integer expected by parameter $minute of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

909
		$next_time = mktime(date('H', $offset), /** @scrutinizer ignore-type */ date('i', $offset), 0, date('m'), date('d'), date('Y'));
Loading history...
Bug introduced by
date('d') of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

909
		$next_time = mktime(date('H', $offset), date('i', $offset), 0, date('m'), /** @scrutinizer ignore-type */ date('d'), date('Y'));
Loading history...
Bug introduced by
date('Y') of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

909
		$next_time = mktime(date('H', $offset), date('i', $offset), 0, date('m'), date('d'), /** @scrutinizer ignore-type */ date('Y'));
Loading history...
Bug introduced by
date('m') of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

909
		$next_time = mktime(date('H', $offset), date('i', $offset), 0, /** @scrutinizer ignore-type */ date('m'), date('d'), date('Y'));
Loading history...
910
911
		// Make the time offset in the past!
912
		if ($next_time > time())
913
		{
914
			$next_time -= 86400;
915
		}
916
917
		// Default we'll jump in hours.
918
		$applyOffset = 3600;
919
		// 24 hours = 1 day.
920
		if ($unit == 'd')
921
			$applyOffset = 86400;
922
		// Otherwise a week.
923
		if ($unit == 'w')
924
			$applyOffset = 604800;
925
926
		$applyOffset *= $regularity;
927
928
		// Just add on the offset.
929
		while ($next_time <= time())
930
		{
931
			$next_time += $applyOffset;
932
		}
933
	}
934
935
	return $next_time;
936
}
937
938
/**
939
 * This loads the bare minimum data to allow us to load language files!
940
 */
941
function loadEssentialThemeData()
942
{
943
	global $settings, $modSettings, $smcFunc, $mbname, $context, $sourcedir, $txt;
944
945
	// Get all the default theme variables.
946
	$result = $smcFunc['db_query']('', '
947
		SELECT id_theme, variable, value
948
		FROM {db_prefix}themes
949
		WHERE id_member = {int:no_member}
950
			AND id_theme IN (1, {int:theme_guests})',
951
		array(
952
			'no_member' => 0,
953
			'theme_guests' => !empty($modSettings['theme_guests']) ? $modSettings['theme_guests'] : 1,
954
		)
955
	);
956
	while ($row = $smcFunc['db_fetch_assoc']($result))
957
	{
958
		$settings[$row['variable']] = $row['value'];
959
960
		// Is this the default theme?
961
		if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1')
962
			$settings['default_' . $row['variable']] = $row['value'];
963
	}
964
	$smcFunc['db_free_result']($result);
965
966
	// Check we have some directories setup.
967
	if (empty($settings['template_dirs']))
968
	{
969
		$settings['template_dirs'] = array($settings['theme_dir']);
970
971
		// Based on theme (if there is one).
972
		if (!empty($settings['base_theme_dir']))
973
			$settings['template_dirs'][] = $settings['base_theme_dir'];
974
975
		// Lastly the default theme.
976
		if ($settings['theme_dir'] != $settings['default_theme_dir'])
977
			$settings['template_dirs'][] = $settings['default_theme_dir'];
978
	}
979
980
	// Assume we want this.
981
	$context['forum_name'] = $mbname;
982
983
	// Check loadLanguage actually exists!
984
	if (!function_exists('loadLanguage'))
985
	{
986
		require_once($sourcedir . '/Load.php');
987
		require_once($sourcedir . '/Subs.php');
988
	}
989
990
	loadLanguage('index+Modifications');
991
992
	// Just in case it wasn't already set elsewhere.
993
	$context['character_set'] = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set'];
994
	$context['utf8'] = $context['character_set'] === 'UTF-8';
995
	$context['right_to_left'] = !empty($txt['lang_rtl']);
996
997
	// Tell fatal_lang_error() to not reload the theme.
998
	$context['theme_loaded'] = true;
999
}
1000
1001
/**
1002
 * This retieves data (e.g. last version of SMF) from sm.org
1003
 */
1004
function scheduled_fetchSMfiles()
1005
{
1006
	global $sourcedir, $txt, $language, $forum_version, $modSettings, $smcFunc, $context;
1007
1008
	// What files do we want to get
1009
	$request = $smcFunc['db_query']('', '
1010
		SELECT id_file, filename, path, parameters
1011
		FROM {db_prefix}admin_info_files',
1012
		array(
1013
		)
1014
	);
1015
1016
	$js_files = array();
1017
1018
	while ($row = $smcFunc['db_fetch_assoc']($request))
1019
	{
1020
		$js_files[$row['id_file']] = array(
1021
			'filename' => $row['filename'],
1022
			'path' => $row['path'],
1023
			'parameters' => sprintf($row['parameters'], $language, urlencode($modSettings['time_format']), urlencode($forum_version)),
1024
		);
1025
	}
1026
1027
	$smcFunc['db_free_result']($request);
1028
1029
	// Just in case we run into a problem.
1030
	loadEssentialThemeData();
1031
	loadLanguage('Errors', $language, false);
1032
1033
	foreach ($js_files as $ID_FILE => $file)
1034
	{
1035
		// Create the url
1036
		$server = empty($file['path']) || (substr($file['path'], 0, 7) != 'http://' && substr($file['path'], 0, 8) != 'https://') ? 'https://www.simplemachines.org' : '';
1037
		$url = $server . (!empty($file['path']) ? $file['path'] : $file['path']) . $file['filename'] . (!empty($file['parameters']) ? '?' . $file['parameters'] : '');
1038
1039
		// Get the file
1040
		$file_data = fetch_web_data($url);
1041
1042
		// 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.
1043
		if ($file_data === false)
1044
		{
1045
			$context['scheduled_errors']['fetchSMfiles'][] = sprintf($txt['st_cannot_retrieve_file'], $url);
1046
			log_error(sprintf($txt['st_cannot_retrieve_file'], $url));
1047
			return false;
1048
		}
1049
1050
		// Save the file to the database.
1051
		$smcFunc['db_query']('substring', '
1052
			UPDATE {db_prefix}admin_info_files
1053
			SET data = SUBSTRING({string:file_data}, 1, 65534)
1054
			WHERE id_file = {int:id_file}',
1055
			array(
1056
				'id_file' => $ID_FILE,
1057
				'file_data' => $file_data,
1058
			)
1059
		);
1060
	}
1061
	return true;
1062
}
1063
1064
/**
1065
 * Happy birthday!!
1066
 */
1067
function scheduled_birthdayemails()
1068
{
1069
	global $smcFunc;
1070
1071
	$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
1072
		array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
1073
		array('$sourcedir/tasks/Birthday-Notify.php', 'Birthday_Notify_Background', '', 0),
1074
		array()
1075
	);
1076
1077
	return true;
1078
}
1079
1080
/**
1081
 * Weekly maintenance
1082
 */
1083
function scheduled_weekly_maintenance()
1084
{
1085
	global $modSettings, $smcFunc, $cache_enable, $cacheAPI;
1086
1087
	// Delete some settings that needn't be set if they are otherwise empty.
1088
	$emptySettings = array(
1089
		'warning_mute', 'warning_moderate', 'warning_watch', 'warning_show', 'disableCustomPerPage', 'spider_mode', 'spider_group',
1090
		'paid_currency_code', 'paid_currency_symbol', 'paid_email_to', 'paid_email', 'paid_enabled', 'paypal_email',
1091
		'search_enable_captcha', 'search_floodcontrol_time', 'show_spider_online',
1092
	);
1093
1094
	$smcFunc['db_query']('', '
1095
		DELETE FROM {db_prefix}settings
1096
		WHERE variable IN ({array_string:setting_list})
1097
			AND (value = {string:zero_value} OR value = {string:blank_value})',
1098
		array(
1099
			'zero_value' => '0',
1100
			'blank_value' => '',
1101
			'setting_list' => $emptySettings,
1102
		)
1103
	);
1104
1105
	// Some settings we never want to keep - they are just there for temporary purposes.
1106
	$deleteAnywaySettings = array(
1107
		'attachment_full_notified',
1108
	);
1109
1110
	$smcFunc['db_query']('', '
1111
		DELETE FROM {db_prefix}settings
1112
		WHERE variable IN ({array_string:setting_list})',
1113
		array(
1114
			'setting_list' => $deleteAnywaySettings,
1115
		)
1116
	);
1117
1118
	// Ok should we prune the logs?
1119
	if (!empty($modSettings['pruningOptions']))
1120
	{
1121
		if (!empty($modSettings['pruningOptions']) && strpos($modSettings['pruningOptions'], ',') !== false)
1122
			list ($modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']) = explode(',', $modSettings['pruningOptions']);
1123
1124
		if (!empty($modSettings['pruneErrorLog']))
1125
		{
1126
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1127
			$t = time() - $modSettings['pruneErrorLog'] * 86400;
1128
1129
			$smcFunc['db_query']('', '
1130
				DELETE FROM {db_prefix}log_errors
1131
				WHERE log_time < {int:log_time}',
1132
				array(
1133
					'log_time' => $t,
1134
				)
1135
			);
1136
		}
1137
1138
		if (!empty($modSettings['pruneModLog']))
1139
		{
1140
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1141
			$t = time() - $modSettings['pruneModLog'] * 86400;
1142
1143
			$smcFunc['db_query']('', '
1144
				DELETE FROM {db_prefix}log_actions
1145
				WHERE log_time < {int:log_time}
1146
					AND id_log = {int:moderation_log}',
1147
				array(
1148
					'log_time' => $t,
1149
					'moderation_log' => 1,
1150
				)
1151
			);
1152
		}
1153
1154
		if (!empty($modSettings['pruneBanLog']))
1155
		{
1156
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1157
			$t = time() - $modSettings['pruneBanLog'] * 86400;
1158
1159
			$smcFunc['db_query']('', '
1160
				DELETE FROM {db_prefix}log_banned
1161
				WHERE log_time < {int:log_time}',
1162
				array(
1163
					'log_time' => $t,
1164
				)
1165
			);
1166
		}
1167
1168
		if (!empty($modSettings['pruneReportLog']))
1169
		{
1170
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1171
			$t = time() - $modSettings['pruneReportLog'] * 86400;
1172
1173
			// This one is more complex then the other logs.  First we need to figure out which reports are too old.
1174
			$reports = array();
1175
			$result = $smcFunc['db_query']('', '
1176
				SELECT id_report
1177
				FROM {db_prefix}log_reported
1178
				WHERE time_started < {int:time_started}
1179
					AND closed = {int:closed}
1180
					AND ignore_all = {int:not_ignored}',
1181
				array(
1182
					'time_started' => $t,
1183
					'closed' => 1,
1184
					'not_ignored' => 0,
1185
				)
1186
			);
1187
1188
			while ($row = $smcFunc['db_fetch_row']($result))
1189
				$reports[] = $row[0];
1190
1191
			$smcFunc['db_free_result']($result);
1192
1193
			if (!empty($reports))
1194
			{
1195
				// Now delete the reports...
1196
				$smcFunc['db_query']('', '
1197
					DELETE FROM {db_prefix}log_reported
1198
					WHERE id_report IN ({array_int:report_list})',
1199
					array(
1200
						'report_list' => $reports,
1201
					)
1202
				);
1203
				// And delete the comments for those reports...
1204
				$smcFunc['db_query']('', '
1205
					DELETE FROM {db_prefix}log_reported_comments
1206
					WHERE id_report IN ({array_int:report_list})',
1207
					array(
1208
						'report_list' => $reports,
1209
					)
1210
				);
1211
			}
1212
		}
1213
1214
		if (!empty($modSettings['pruneScheduledTaskLog']))
1215
		{
1216
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1217
			$t = time() - $modSettings['pruneScheduledTaskLog'] * 86400;
1218
1219
			$smcFunc['db_query']('', '
1220
				DELETE FROM {db_prefix}log_scheduled_tasks
1221
				WHERE time_run < {int:time_run}',
1222
				array(
1223
					'time_run' => $t,
1224
				)
1225
			);
1226
		}
1227
1228
		if (!empty($modSettings['pruneSpiderHitLog']))
1229
		{
1230
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1231
			$t = time() - $modSettings['pruneSpiderHitLog'] * 86400;
1232
1233
			$smcFunc['db_query']('', '
1234
				DELETE FROM {db_prefix}log_spider_hits
1235
				WHERE log_time < {int:log_time}',
1236
				array(
1237
					'log_time' => $t,
1238
				)
1239
			);
1240
		}
1241
	}
1242
1243
	// Get rid of any paid subscriptions that were never actioned.
1244
	$smcFunc['db_query']('', '
1245
		DELETE FROM {db_prefix}log_subscribed
1246
		WHERE end_time = {int:no_end_time}
1247
			AND status = {int:not_active}
1248
			AND start_time < {int:start_time}
1249
			AND payments_pending < {int:payments_pending}',
1250
		array(
1251
			'no_end_time' => 0,
1252
			'not_active' => 0,
1253
			'start_time' => time() - 60,
1254
			'payments_pending' => 1,
1255
		)
1256
	);
1257
1258
	// Some OS's don't seem to clean out their sessions.
1259
	$smcFunc['db_query']('', '
1260
		DELETE FROM {db_prefix}sessions
1261
		WHERE last_update < {int:last_update}',
1262
		array(
1263
			'last_update' => time() - 86400,
1264
		)
1265
	);
1266
1267
	// Update the regex of top level domains with the IANA's latest official list
1268
	$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
1269
		array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
1270
		array('$sourcedir/tasks/UpdateTldRegex.php', 'Update_TLD_Regex', '', 0), array()
1271
	);
1272
1273
	// Run Cache housekeeping
1274
	if (!empty($cache_enable) && !empty($cacheAPI))
1275
		$cacheAPI->housekeeping();
1276
1277
	// Prevent stale minimized CSS and JavaScript from cluttering up the theme directories
1278
	deleteAllMinified();
1279
1280
	return true;
1281
}
1282
1283
/**
1284
 * Perform the standard checks on expiring/near expiring subscriptions.
1285
 */
1286
function scheduled_paid_subscriptions()
1287
{
1288
	global $sourcedir, $scripturl, $smcFunc, $modSettings, $language;
1289
1290
	// Start off by checking for removed subscriptions.
1291
	$request = $smcFunc['db_query']('', '
1292
		SELECT id_subscribe, id_member
1293
		FROM {db_prefix}log_subscribed
1294
		WHERE status = {int:is_active}
1295
			AND end_time < {int:time_now}',
1296
		array(
1297
			'is_active' => 1,
1298
			'time_now' => time(),
1299
		)
1300
	);
1301
	while ($row = $smcFunc['db_fetch_assoc']($request))
1302
	{
1303
		require_once($sourcedir . '/ManagePaid.php');
1304
		removeSubscription($row['id_subscribe'], $row['id_member']);
1305
	}
1306
	$smcFunc['db_free_result']($request);
1307
1308
	// Get all those about to expire that have not had a reminder sent.
1309
	$request = $smcFunc['db_query']('', '
1310
		SELECT ls.id_sublog, m.id_member, m.member_name, m.email_address, m.lngfile, s.name, ls.end_time
1311
		FROM {db_prefix}log_subscribed AS ls
1312
			JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe)
1313
			JOIN {db_prefix}members AS m ON (m.id_member = ls.id_member)
1314
		WHERE ls.status = {int:is_active}
1315
			AND ls.reminder_sent = {int:reminder_sent}
1316
			AND s.reminder > {int:reminder_wanted}
1317
			AND ls.end_time < ({int:time_now} + s.reminder * 86400)',
1318
		array(
1319
			'is_active' => 1,
1320
			'reminder_sent' => 0,
1321
			'reminder_wanted' => 0,
1322
			'time_now' => time(),
1323
		)
1324
	);
1325
	$subs_reminded = array();
1326
	$members = array();
1327
	while ($row = $smcFunc['db_fetch_assoc']($request))
1328
	{
1329
		// If this is the first one load the important bits.
1330
		if (empty($subs_reminded))
1331
		{
1332
			require_once($sourcedir . '/Subs-Post.php');
1333
			// Need the below for loadLanguage to work!
1334
			loadEssentialThemeData();
1335
		}
1336
1337
		$subs_reminded[] = $row['id_sublog'];
1338
		$members[$row['id_member']] = $row;
1339
	}
1340
	$smcFunc['db_free_result']($request);
1341
1342
	// Load alert preferences
1343
	require_once($sourcedir . '/Subs-Notify.php');
1344
	$notifyPrefs = getNotifyPrefs(array_keys($members), 'paidsubs_expiring', true);
1345
	$alert_rows = array();
1346
	foreach ($members as $row)
1347
	{
1348
		$replacements = array(
1349
			'PROFILE_LINK' => $scripturl . '?action=profile;area=subscriptions;u=' . $row['id_member'],
1350
			'REALNAME' => $row['member_name'],
1351
			'SUBSCRIPTION' => $row['name'],
1352
			'END_DATE' => strip_tags(timeformat($row['end_time'])),
1353
		);
1354
1355
		$emaildata = loadEmailTemplate('paid_subscription_reminder', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
1356
1357
		// Send the actual email.
1358
		if ($notifyPrefs[$row['id_member']] & 0x02)
1359
			sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, 'paid_sub_remind', $emaildata['is_html'], 2);
1360
1361
		if ($notifyPrefs[$row['id_member']] & 0x01)
1362
		{
1363
			$alert_rows[] = array(
1364
				'alert_time' => time(),
1365
				'id_member' => $row['id_member'],
1366
				'id_member_started' => $row['id_member'],
1367
				'member_name' => $row['member_name'],
1368
				'content_type' => 'paidsubs',
1369
				'content_id' => $row['id_sublog'],
1370
				'content_action' => 'expiring',
1371
				'is_read' => 0,
1372
				'extra' => $smcFunc['json_encode'](array(
1373
					'subscription_name' => $row['name'],
1374
					'end_time' => strip_tags(timeformat($row['end_time'])),
1375
				)),
1376
			);
1377
			updateMemberData($row['id_member'], array('alerts' => '+'));
1378
		}
1379
	}
1380
1381
	// Insert the alerts if any
1382
	if (!empty($alert_rows))
1383
		$smcFunc['db_insert']('',
1384
			'{db_prefix}user_alerts',
1385
			array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
1386
				'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
1387
			$alert_rows,
1388
			array()
1389
		);
1390
1391
	// Mark the reminder as sent.
1392
	if (!empty($subs_reminded))
1393
		$smcFunc['db_query']('', '
1394
			UPDATE {db_prefix}log_subscribed
1395
			SET reminder_sent = {int:reminder_sent}
1396
			WHERE id_sublog IN ({array_int:subscription_list})',
1397
			array(
1398
				'subscription_list' => $subs_reminded,
1399
				'reminder_sent' => 1,
1400
			)
1401
		);
1402
1403
	return true;
1404
}
1405
1406
/**
1407
 * Check for un-posted attachments is something we can do once in a while :P
1408
 * This function uses opendir cycling through all the attachments
1409
 */
1410
function scheduled_remove_temp_attachments()
1411
{
1412
	global $smcFunc, $modSettings, $context, $txt;
1413
1414
	// We need to know where this thing is going.
1415
	if (!empty($modSettings['currentAttachmentUploadDir']))
1416
	{
1417
		if (!is_array($modSettings['attachmentUploadDir']))
1418
			$modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true);
1419
1420
		// Just use the current path for temp files.
1421
		$attach_dirs = $modSettings['attachmentUploadDir'];
1422
	}
1423
	else
1424
	{
1425
		$attach_dirs = array($modSettings['attachmentUploadDir']);
1426
	}
1427
1428
	foreach ($attach_dirs as $attach_dir)
1429
	{
1430
		$dir = @opendir($attach_dir);
1431
		if (!$dir)
1432
		{
1433
			loadEssentialThemeData();
1434
			loadLanguage('Post');
1435
			$context['scheduled_errors']['remove_temp_attachments'][] = $txt['cant_access_upload_path'] . ' (' . $attach_dir . ')';
1436
			log_error($txt['cant_access_upload_path'] . ' (' . $attach_dir . ')', 'critical');
1437
			return false;
1438
		}
1439
1440
		while ($file = readdir($dir))
1441
		{
1442
			if ($file == '.' || $file == '..')
1443
				continue;
1444
1445
			if (strpos($file, 'post_tmp_') !== false)
1446
			{
1447
				// Temp file is more than 5 hours old!
1448
				if (filemtime($attach_dir . '/' . $file) < time() - 18000)
1449
					@unlink($attach_dir . '/' . $file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

1449
					/** @scrutinizer ignore-unhandled */ @unlink($attach_dir . '/' . $file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1450
			}
1451
		}
1452
		closedir($dir);
1453
	}
1454
1455
	return true;
1456
}
1457
1458
/**
1459
 * Check for move topic notices that have past their best by date
1460
 */
1461
function scheduled_remove_topic_redirect()
1462
{
1463
	global $smcFunc, $sourcedir;
1464
1465
	// init
1466
	$topics = array();
1467
1468
	// We will need this for language files
1469
	loadEssentialThemeData();
1470
1471
	// Find all of the old MOVE topic notices that were set to expire
1472
	$request = $smcFunc['db_query']('', '
1473
		SELECT id_topic
1474
		FROM {db_prefix}topics
1475
		WHERE redirect_expires <= {int:redirect_expires}
1476
			AND redirect_expires <> 0',
1477
		array(
1478
			'redirect_expires' => time(),
1479
		)
1480
	);
1481
1482
	while ($row = $smcFunc['db_fetch_row']($request))
1483
		$topics[] = $row[0];
1484
	$smcFunc['db_free_result']($request);
1485
1486
	// Zap, your gone
1487
	if (count($topics) > 0)
1488
	{
1489
		require_once($sourcedir . '/RemoveTopic.php');
1490
		removeTopics($topics, false, true);
1491
	}
1492
1493
	return true;
1494
}
1495
1496
/**
1497
 * Check for old drafts and remove them
1498
 */
1499
function scheduled_remove_old_drafts()
1500
{
1501
	global $smcFunc, $sourcedir, $modSettings;
1502
1503
	if (empty($modSettings['drafts_keep_days']))
1504
		return true;
1505
1506
	// init
1507
	$drafts = array();
1508
1509
	// We need this for language items
1510
	loadEssentialThemeData();
1511
1512
	// Find all of the old drafts
1513
	$request = $smcFunc['db_query']('', '
1514
		SELECT id_draft
1515
		FROM {db_prefix}user_drafts
1516
		WHERE poster_time <= {int:poster_time_old}',
1517
		array(
1518
			'poster_time_old' => time() - (86400 * $modSettings['drafts_keep_days']),
1519
		)
1520
	);
1521
1522
	while ($row = $smcFunc['db_fetch_row']($request))
1523
		$drafts[] = (int) $row[0];
1524
	$smcFunc['db_free_result']($request);
1525
1526
	// If we have old one, remove them
1527
	if (count($drafts) > 0)
1528
	{
1529
		require_once($sourcedir . '/Drafts.php');
1530
		DeleteDraft($drafts, false);
0 ignored issues
show
Bug introduced by
$drafts of type array|integer[] is incompatible with the type integer expected by parameter $id_draft of DeleteDraft(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1530
		DeleteDraft(/** @scrutinizer ignore-type */ $drafts, false);
Loading history...
1531
	}
1532
1533
	return true;
1534
}
1535
1536
?>