Passed
Push — release-2.1 ( 0c2197...207d2d )
by Jeremy
05:47
created

ScheduledTasks.php ➔ ReduceMailQueue()   F

Complexity

Conditions 34
Paths > 20000

Size

Total Lines 196

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 34
nc 57706
nop 3
dl 0
loc 196
rs 0
c 0
b 0
f 0

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
 * Function for sending out approval notices to moderators etc.
163
 */
164
function scheduled_approval_notification()
165
{
166
	global $scripturl, $txt, $sourcedir, $smcFunc;
167
168
	// Grab all the items awaiting approval and sort type then board - clear up any things that are no longer relevant.
169
	$request = $smcFunc['db_query']('', '
170
		SELECT aq.id_msg, aq.id_attach, aq.id_event, m.id_topic, m.id_board, m.subject, t.id_first_msg,
171
			b.id_profile
172
		FROM {db_prefix}approval_queue AS aq
173
			JOIN {db_prefix}messages AS m ON (m.id_msg = aq.id_msg)
174
			JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
175
			JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)',
176
		array(
177
		)
178
	);
179
	$notices = array();
180
	$profiles = array();
181
	while ($row = $smcFunc['db_fetch_assoc']($request))
182
	{
183
		// If this is no longer around we'll ignore it.
184
		if (empty($row['id_topic']))
185
			continue;
186
187
		// What type is it?
188
		if ($row['id_first_msg'] && $row['id_first_msg'] == $row['id_msg'])
189
			$type = 'topic';
190
		elseif ($row['id_attach'])
191
			$type = 'attach';
192
		else
193
			$type = 'msg';
194
195
		// Add it to the array otherwise.
196
		$notices[$row['id_board']][$type][] = array(
197
			'subject' => $row['subject'],
198
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
199
		);
200
201
		// Store the profile for a bit later.
202
		$profiles[$row['id_board']] = $row['id_profile'];
203
	}
204
	$smcFunc['db_free_result']($request);
205
206
	// Delete it all!
207
	$smcFunc['db_query']('', '
208
		TRUNCATE {db_prefix}approval_queue',
209
		array(
210
		)
211
	);
212
213
	// If nothing quit now.
214
	if (empty($notices))
215
		return true;
216
217
	// Now we need to think about finding out *who* can approve - this is hard!
218
219
	// First off, get all the groups with this permission and sort by board.
220
	$request = $smcFunc['db_query']('', '
221
		SELECT id_group, id_profile, add_deny
222
		FROM {db_prefix}board_permissions
223
		WHERE permission = {literal:approve_posts}
224
			AND id_profile IN ({array_int:profile_list})',
225
		array(
226
			'profile_list' => $profiles,
227
		)
228
	);
229
	$perms = array();
230
	$addGroups = array(1);
231
	while ($row = $smcFunc['db_fetch_assoc']($request))
232
	{
233
		// Sorry guys, but we have to ignore guests AND members - it would be too many otherwise.
234
		if ($row['id_group'] < 2)
235
			continue;
236
237
		$perms[$row['id_profile']][$row['add_deny'] ? 'add' : 'deny'][] = $row['id_group'];
238
239
		// Anyone who can access has to be considered.
240
		if ($row['add_deny'])
241
			$addGroups[] = $row['id_group'];
242
	}
243
	$smcFunc['db_free_result']($request);
244
245
	// Grab the moderators if they have permission!
246
	$mods = array();
247
	$members = array();
248
	if (in_array(2, $addGroups))
249
	{
250
		$request = $smcFunc['db_query']('', '
251
			SELECT id_member, id_board
252
			FROM {db_prefix}moderators',
253
			array(
254
			)
255
		);
256
		while ($row = $smcFunc['db_fetch_assoc']($request))
257
		{
258
			$mods[$row['id_member']][$row['id_board']] = true;
259
			// Make sure they get included in the big loop.
260
			$members[] = $row['id_member'];
261
		}
262
		$smcFunc['db_free_result']($request);
263
	}
264
265
	// Come along one and all... until we reject you ;)
266
	$request = $smcFunc['db_query']('', '
267
		SELECT id_member, real_name, email_address, lngfile, id_group, additional_groups, mod_prefs
268
		FROM {db_prefix}members
269
		WHERE id_group IN ({array_int:additional_group_list})
270
			OR FIND_IN_SET({raw:additional_group_list_implode}, additional_groups) != 0' . (empty($members) ? '' : '
271
			OR id_member IN ({array_int:member_list})') . '
272
		ORDER BY lngfile',
273
		array(
274
			'additional_group_list' => $addGroups,
275
			'member_list' => $members,
276
			'additional_group_list_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $addGroups),
277
		)
278
	);
279
	$members = array();
280
	while ($row = $smcFunc['db_fetch_assoc']($request))
281
	{
282
		// Check whether they are interested.
283
		if (!empty($row['mod_prefs']))
284
		{
285
			list(,, $pref_binary) = explode('|', $row['mod_prefs']);
286
			if (!($pref_binary & 4))
287
				continue;
288
		}
289
290
		$members[$row['id_member']] = array(
291
			'id' => $row['id_member'],
292
			'groups' => array_merge(explode(',', $row['additional_groups']), array($row['id_group'])),
293
			'language' => $row['lngfile'],
294
			'email' => $row['email_address'],
295
			'name' => $row['real_name'],
296
		);
297
	}
298
	$smcFunc['db_free_result']($request);
299
300
	// Get the mailing stuff.
301
	require_once($sourcedir . '/Subs-Post.php');
302
	// Need the below for loadLanguage to work!
303
	loadEssentialThemeData();
304
305
	$current_language = '';
306
	// Finally, loop through each member, work out what they can do, and send it.
307
	foreach ($members as $id => $member)
308
	{
309
		$emailbody = '';
310
311
		// Load the language file as required.
312
		if (empty($current_language) || $current_language != $member['language'])
313
			$current_language = loadLanguage('EmailTemplates', $member['language'], false);
314
315
		// Loop through each notice...
316
		foreach ($notices as $board => $notice)
317
		{
318
			$access = false;
319
320
			// Can they mod in this board?
321
			if (isset($mods[$id][$board]))
322
				$access = true;
323
324
			// Do the group check...
325
			if (!$access && isset($perms[$profiles[$board]]['add']))
326
			{
327
				// They can access?!
328
				if (array_intersect($perms[$profiles[$board]]['add'], $member['groups']))
329
					$access = true;
330
331
				// If they have deny rights don't consider them!
332
				if (isset($perms[$profiles[$board]]['deny']))
333
					if (array_intersect($perms[$profiles[$board]]['deny'], $member['groups']))
334
						$access = false;
335
			}
336
337
			// Finally, fix it for admins!
338
			if (in_array(1, $member['groups']))
339
				$access = true;
340
341
			// If they can't access it then give it a break!
342
			if (!$access)
343
				continue;
344
345
			foreach ($notice as $type => $items)
346
			{
347
				// Build up the top of this section.
348
				$emailbody .= $txt['scheduled_approval_email_' . $type] . "\n" .
349
					'------------------------------------------------------' . "\n";
350
351
				foreach ($items as $item)
352
					$emailbody .= $item['subject'] . ' - ' . $item['href'] . "\n";
353
354
				$emailbody .= "\n";
355
			}
356
		}
357
358
		if ($emailbody == '')
359
			continue;
360
361
		$replacements = array(
362
			'REALNAME' => $member['name'],
363
			'BODY' => $emailbody,
364
		);
365
366
		$emaildata = loadEmailTemplate('scheduled_approval', $replacements, $current_language);
367
368
		// Send the actual email.
369
		sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'schedapp', $emaildata['is_html'], 2);
370
	}
371
372
	// All went well!
373
	return true;
374
}
375
376
/**
377
 * Do some daily cleaning up.
378
 */
379
function scheduled_daily_maintenance()
380
{
381
	global $smcFunc, $modSettings, $sourcedir, $boarddir, $db_type, $image_proxy_enabled;
382
383
	// First clean out the cache.
384
	clean_cache();
385
386
	// If warning decrement is enabled and we have people who have not had a new warning in 24 hours, lower their warning level.
387
	list (, , $modSettings['warning_decrement']) = explode(',', $modSettings['warning_settings']);
388
	if ($modSettings['warning_decrement'])
389
	{
390
		// Find every member who has a warning level...
391
		$request = $smcFunc['db_query']('', '
392
			SELECT id_member, warning
393
			FROM {db_prefix}members
394
			WHERE warning > {int:no_warning}',
395
			array(
396
				'no_warning' => 0,
397
			)
398
		);
399
		$members = array();
400
		while ($row = $smcFunc['db_fetch_assoc']($request))
401
			$members[$row['id_member']] = $row['warning'];
402
		$smcFunc['db_free_result']($request);
403
404
		// Have some members to check?
405
		if (!empty($members))
406
		{
407
			// Find out when they were last warned.
408
			$request = $smcFunc['db_query']('', '
409
				SELECT id_recipient, MAX(log_time) AS last_warning
410
				FROM {db_prefix}log_comments
411
				WHERE id_recipient IN ({array_int:member_list})
412
					AND comment_type = {string:warning}
413
				GROUP BY id_recipient',
414
				array(
415
					'member_list' => array_keys($members),
416
					'warning' => 'warning',
417
				)
418
			);
419
			$member_changes = array();
420
			while ($row = $smcFunc['db_fetch_assoc']($request))
421
			{
422
				// More than 24 hours ago?
423
				if ($row['last_warning'] <= time() - 86400)
424
					$member_changes[] = array(
425
						'id' => $row['id_recipient'],
426
						'warning' => $members[$row['id_recipient']] >= $modSettings['warning_decrement'] ? $members[$row['id_recipient']] - $modSettings['warning_decrement'] : 0,
427
					);
428
			}
429
			$smcFunc['db_free_result']($request);
430
431
			// Have some members to change?
432
			if (!empty($member_changes))
433
				foreach ($member_changes as $change)
434
					$smcFunc['db_query']('', '
435
						UPDATE {db_prefix}members
436
						SET warning = {int:warning}
437
						WHERE id_member = {int:id_member}',
438
						array(
439
							'warning' => $change['warning'],
440
							'id_member' => $change['id'],
441
						)
442
					);
443
		}
444
	}
445
446
	// Do any spider stuff.
447
	if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1)
448
	{
449
		require_once($sourcedir . '/ManageSearchEngines.php');
450
		consolidateSpiderStats();
451
	}
452
453
	// Clean up some old login history information.
454
	$smcFunc['db_query']('', '
455
		DELETE FROM {db_prefix}member_logins
456
		WHERE time < {int:oldLogins}',
457
		array(
458
			'oldLogins' => time() - (!empty($modSettings['loginHistoryDays']) ? 60 * 60 * 24 * $modSettings['loginHistoryDays'] : 2592000),
459
	));
460
461
	// Run Imageproxy housekeeping
462
	if (!empty($image_proxy_enabled))
463
	{
464
		global $proxyhousekeeping;
465
		$proxyhousekeeping = true;
466
467
		require_once($boarddir . '/proxy.php');
468
		$proxy = new ProxyServer();
469
		$proxy->housekeeping();
470
471
		unset($proxyhousekeeping);
472
	}
473
474
	// Log we've done it...
475
	return true;
476
}
477
478
/**
479
 * Send out a daily email of all subscribed topics.
480
 */
481
function scheduled_daily_digest()
482
{
483
	global $is_weekly, $txt, $mbname, $scripturl, $sourcedir, $smcFunc, $context, $modSettings;
484
485
	// We'll want this...
486
	require_once($sourcedir . '/Subs-Post.php');
487
	loadEssentialThemeData();
488
489
	$is_weekly = !empty($is_weekly) ? 1 : 0;
490
491
	// Right - get all the notification data FIRST.
492
	$request = $smcFunc['db_query']('', '
493
		SELECT ln.id_topic, COALESCE(t.id_board, ln.id_board) AS id_board, mem.email_address, mem.member_name,
494
			mem.lngfile, mem.id_member
495
		FROM {db_prefix}log_notify AS ln
496
			JOIN {db_prefix}members AS mem ON (mem.id_member = ln.id_member)
497
			LEFT JOIN {db_prefix}topics AS t ON (ln.id_topic != {int:empty_topic} AND t.id_topic = ln.id_topic)
498
		WHERE mem.is_activated = {int:is_activated}',
499
		array(
500
			'empty_topic' => 0,
501
			'is_activated' => 1,
502
		)
503
	);
504
	$members = array();
505
	$langs = array();
506
	$notify = array();
507
	while ($row = $smcFunc['db_fetch_assoc']($request))
508
	{
509
		if (!isset($members[$row['id_member']]))
510
		{
511
			$members[$row['id_member']] = array(
512
				'email' => $row['email_address'],
513
				'name' => $row['member_name'],
514
				'id' => $row['id_member'],
515
				'lang' => $row['lngfile'],
516
			);
517
			$langs[$row['lngfile']] = $row['lngfile'];
518
		}
519
520
		// Store this useful data!
521
		$boards[$row['id_board']] = $row['id_board'];
522
		if ($row['id_topic'])
523
			$notify['topics'][$row['id_topic']][] = $row['id_member'];
524
		else
525
			$notify['boards'][$row['id_board']][] = $row['id_member'];
526
	}
527
	$smcFunc['db_free_result']($request);
528
529
	if (empty($boards))
530
		return true;
531
532
	// Just get the board names.
533
	$request = $smcFunc['db_query']('', '
534
		SELECT id_board, name
535
		FROM {db_prefix}boards
536
		WHERE id_board IN ({array_int:board_list})',
537
		array(
538
			'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...
539
		)
540
	);
541
	$boards = array();
542
	while ($row = $smcFunc['db_fetch_assoc']($request))
543
		$boards[$row['id_board']] = $row['name'];
544
	$smcFunc['db_free_result']($request);
545
546
	if (empty($boards))
547
		return true;
548
549
	// Get the actual topics...
550
	$request = $smcFunc['db_query']('', '
551
		SELECT ld.note_type, t.id_topic, t.id_board, t.id_member_started, m.id_msg, m.subject,
552
			b.name AS board_name
553
		FROM {db_prefix}log_digest AS ld
554
			JOIN {db_prefix}topics AS t ON (t.id_topic = ld.id_topic
555
				AND t.id_board IN ({array_int:board_list}))
556
			JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
557
			JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
558
		WHERE ' . ($is_weekly ? 'ld.daily != {int:daily_value}' : 'ld.daily IN (0, 2)'),
559
		array(
560
			'board_list' => array_keys($boards),
561
			'daily_value' => 2,
562
		)
563
	);
564
	$types = array();
565
	while ($row = $smcFunc['db_fetch_assoc']($request))
566
	{
567
		if (!isset($types[$row['note_type']][$row['id_board']]))
568
			$types[$row['note_type']][$row['id_board']] = array(
569
				'lines' => array(),
570
				'name' => $row['board_name'],
571
				'id' => $row['id_board'],
572
			);
573
574
		if ($row['note_type'] == 'reply')
575
		{
576
			if (isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]))
577
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['count']++;
578
			else
579
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array(
580
					'id' => $row['id_topic'],
581
					'subject' => un_htmlspecialchars($row['subject']),
582
					'count' => 1,
583
				);
584
		}
585
		elseif ($row['note_type'] == 'topic')
586
		{
587
			if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]))
588
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array(
589
					'id' => $row['id_topic'],
590
					'subject' => un_htmlspecialchars($row['subject']),
591
				);
592
		}
593
		else
594
		{
595
			if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]))
596
				$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array(
597
					'id' => $row['id_topic'],
598
					'subject' => un_htmlspecialchars($row['subject']),
599
					'starter' => $row['id_member_started'],
600
				);
601
		}
602
603
		$types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array();
604
		if (!empty($notify['topics'][$row['id_topic']]))
605
			$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']]);
606
		if (!empty($notify['boards'][$row['id_board']]))
607
			$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']]);
608
	}
609
	$smcFunc['db_free_result']($request);
610
611
	if (empty($types))
612
		return true;
613
614
	// Let's load all the languages into a cache thingy.
615
	$langtxt = array();
616
	foreach ($langs as $lang)
617
	{
618
		loadLanguage('Post', $lang);
619
		loadLanguage('index', $lang);
620
		loadLanguage('EmailTemplates', $lang);
621
		$langtxt[$lang] = array(
622
			'subject' => $txt['digest_subject_' . ($is_weekly ? 'weekly' : 'daily')],
623
			'char_set' => $txt['lang_character_set'],
624
			'intro' => sprintf($txt['digest_intro_' . ($is_weekly ? 'weekly' : 'daily')], $mbname),
625
			'new_topics' => $txt['digest_new_topics'],
626
			'topic_lines' => $txt['digest_new_topics_line'],
627
			'new_replies' => $txt['digest_new_replies'],
628
			'mod_actions' => $txt['digest_mod_actions'],
629
			'replies_one' => $txt['digest_new_replies_one'],
630
			'replies_many' => $txt['digest_new_replies_many'],
631
			'sticky' => $txt['digest_mod_act_sticky'],
632
			'lock' => $txt['digest_mod_act_lock'],
633
			'unlock' => $txt['digest_mod_act_unlock'],
634
			'remove' => $txt['digest_mod_act_remove'],
635
			'move' => $txt['digest_mod_act_move'],
636
			'merge' => $txt['digest_mod_act_merge'],
637
			'split' => $txt['digest_mod_act_split'],
638
			'bye' => $txt['regards_team'],
639
		);
640
	}
641
642
	// The preferred way...
643
	require_once($sourcedir . '/Subs-Notify.php');
644
	$prefs = getNotifyPrefs(array_keys($members), array('msg_notify_type', 'msg_notify_pref'), true);
645
646
	// Right - send out the silly things - this will take quite some space!
647
	$members_sent = array();
648
	foreach ($members as $mid => $member)
649
	{
650
		$frequency = isset($prefs[$mid]['msg_notify_pref']) ? $prefs[$mid]['msg_notify_pref'] : 0;
651
		$notify_types = !empty($prefs[$mid]['msg_notify_type']) ? $prefs[$mid]['msg_notify_type'] : 1;
652
653
		// Did they not elect to choose this?
654
		if ($frequency < 3 || $frequency == 4 && !$is_weekly || $frequency == 3 && $is_weekly || $notify_types == 4)
655
			continue;
656
657
		// Right character set!
658
		$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 616. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
659
660
		// Do the start stuff!
661
		$email = array(
662
			'subject' => $mbname . ' - ' . $langtxt[$lang]['subject'],
663
			'body' => $member['name'] . ',' . "\n\n" . $langtxt[$lang]['intro'] . "\n" . $scripturl . '?action=profile;area=notification;u=' . $member['id'] . "\n",
664
			'email' => $member['email'],
665
		);
666
667
		// All new topics?
668
		if (isset($types['topic']))
669
		{
670
			$titled = false;
671
			foreach ($types['topic'] as $id => $board)
672
				foreach ($board['lines'] as $topic)
673
					if (in_array($mid, $topic['members']))
674
					{
675
						if (!$titled)
676
						{
677
							$email['body'] .= "\n" . $langtxt[$lang]['new_topics'] . ':' . "\n" . '-----------------------------------------------';
678
							$titled = true;
679
						}
680
						$email['body'] .= "\n" . sprintf($langtxt[$lang]['topic_lines'], $topic['subject'], $board['name']);
681
					}
682
			if ($titled)
683
				$email['body'] .= "\n";
684
		}
685
686
		// What about replies?
687
		if (isset($types['reply']))
688
		{
689
			$titled = false;
690
			foreach ($types['reply'] as $id => $board)
691
				foreach ($board['lines'] as $topic)
692
					if (in_array($mid, $topic['members']))
693
					{
694
						if (!$titled)
695
						{
696
							$email['body'] .= "\n" . $langtxt[$lang]['new_replies'] . ':' . "\n" . '-----------------------------------------------';
697
							$titled = true;
698
						}
699
						$email['body'] .= "\n" . ($topic['count'] == 1 ? sprintf($langtxt[$lang]['replies_one'], $topic['subject']) : sprintf($langtxt[$lang]['replies_many'], $topic['count'], $topic['subject']));
700
					}
701
702
			if ($titled)
703
				$email['body'] .= "\n";
704
		}
705
706
		// Finally, moderation actions!
707
		if ($notify_types < 3)
708
		{
709
			$titled = false;
710
			foreach ($types as $note_type => $type)
711
			{
712
				if ($note_type == 'topic' || $note_type == 'reply')
713
					continue;
714
715
				foreach ($type as $id => $board)
716
					foreach ($board['lines'] as $topic)
717
						if (in_array($mid, $topic['members']))
718
						{
719
							if (!$titled)
720
							{
721
								$email['body'] .= "\n" . $langtxt[$lang]['mod_actions'] . ':' . "\n" . '-----------------------------------------------';
722
								$titled = true;
723
							}
724
							$email['body'] .= "\n" . sprintf($langtxt[$lang][$note_type], $topic['subject']);
725
						}
726
			}
727
		}
728
		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...
729
			$email['body'] .= "\n";
730
731
		// Then just say our goodbyes!
732
		$email['body'] .= "\n\n" . $txt['regards_team'];
733
734
		// Send it - low priority!
735
		sendmail($email['email'], $email['subject'], $email['body'], null, 'digest', false, 4);
736
737
		$members_sent[] = $mid;
738
	}
739
740
	// Clean up...
741
	if ($is_weekly)
742
	{
743
		$smcFunc['db_query']('', '
744
			DELETE FROM {db_prefix}log_digest
745
			WHERE daily != {int:not_daily}',
746
			array(
747
				'not_daily' => 0,
748
			)
749
		);
750
		$smcFunc['db_query']('', '
751
			UPDATE {db_prefix}log_digest
752
			SET daily = {int:daily_value}
753
			WHERE daily = {int:not_daily}',
754
			array(
755
				'daily_value' => 2,
756
				'not_daily' => 0,
757
			)
758
		);
759
	}
760
	else
761
	{
762
		// Clear any only weekly ones, and stop us from sending daily again.
763
		$smcFunc['db_query']('', '
764
			DELETE FROM {db_prefix}log_digest
765
			WHERE daily = {int:daily_value}',
766
			array(
767
				'daily_value' => 2,
768
			)
769
		);
770
		$smcFunc['db_query']('', '
771
			UPDATE {db_prefix}log_digest
772
			SET daily = {int:both_value}
773
			WHERE daily = {int:no_value}',
774
			array(
775
				'both_value' => 1,
776
				'no_value' => 0,
777
			)
778
		);
779
	}
780
781
	// Just in case the member changes their settings mark this as sent.
782
	if (!empty($members_sent))
783
	{
784
		$smcFunc['db_query']('', '
785
			UPDATE {db_prefix}log_notify
786
			SET sent = {int:is_sent}
787
			WHERE id_member IN ({array_int:member_list})',
788
			array(
789
				'member_list' => $members_sent,
790
				'is_sent' => 1,
791
			)
792
		);
793
	}
794
795
	// Log we've done it...
796
	return true;
797
}
798
799
/**
800
 * Like the daily stuff - just seven times less regular ;)
801
 */
802
function scheduled_weekly_digest()
803
{
804
	global $is_weekly;
805
806
	// We just pass through to the daily function - avoid duplication!
807
	$is_weekly = true;
808
	return scheduled_daily_digest();
809
}
810
811
/**
812
 * Send a group of emails from the mail queue.
813
 *
814
 * @param bool|int $number The number to send each loop through or false to use the standard limits
815
 * @param bool $override_limit Whether to bypass the limit
816
 * @param bool $force_send Whether to forcibly send the messages now (useful when using cron jobs)
817
 * @return bool Whether things were sent
818
 */
819
function ReduceMailQueue($number = false, $override_limit = false, $force_send = false)
820
{
821
	global $modSettings, $smcFunc, $sourcedir;
822
823
	// Are we intending another script to be sending out the queue?
824
	if (!empty($modSettings['mail_queue_use_cron']) && empty($force_send))
825
		return false;
826
827
	// By default send 5 at once.
828
	if (!$number)
829
		$number = empty($modSettings['mail_quantity']) ? 5 : $modSettings['mail_quantity'];
830
831
	// If we came with a timestamp, and that doesn't match the next event, then someone else has beaten us.
832
	if (isset($_GET['ts']) && $_GET['ts'] != $modSettings['mail_next_send'] && empty($force_send))
833
		return false;
834
835
	// By default move the next sending on by 10 seconds, and require an affected row.
836
	if (!$override_limit)
837
	{
838
		$delay = !empty($modSettings['mail_queue_delay']) ? $modSettings['mail_queue_delay'] : (!empty($modSettings['mail_limit']) && $modSettings['mail_limit'] < 5 ? 10 : 5);
839
840
		$smcFunc['db_query']('', '
841
			UPDATE {db_prefix}settings
842
			SET value = {string:next_mail_send}
843
			WHERE variable = {literal:mail_next_send}
844
				AND value = {string:last_send}',
845
			array(
846
				'next_mail_send' => time() + $delay,
847
				'last_send' => $modSettings['mail_next_send'],
848
			)
849
		);
850
		if ($smcFunc['db_affected_rows']() == 0)
851
			return false;
852
		$modSettings['mail_next_send'] = time() + $delay;
853
	}
854
855
	// If we're not overriding how many are we allow to send?
856
	if (!$override_limit && !empty($modSettings['mail_limit']))
857
	{
858
		list ($mt, $mn) = @explode('|', $modSettings['mail_recent']);
859
860
		// Nothing worth noting...
861
		if (empty($mn) || $mt < time() - 60)
862
		{
863
			$mt = time();
864
			$mn = $number;
865
		}
866
		// Otherwise we have a few more we can spend?
867
		elseif ($mn < $modSettings['mail_limit'])
868
		{
869
			$mn += $number;
870
		}
871
		// No more I'm afraid, return!
872
		else
873
			return false;
874
875
		// Reflect that we're about to send some, do it now to be safe.
876
		updateSettings(array('mail_recent' => $mt . '|' . $mn));
877
	}
878
879
	// Now we know how many we're sending, let's send them.
880
	$request = $smcFunc['db_query']('', '
881
		SELECT /*!40001 SQL_NO_CACHE */ id_mail, recipient, body, subject, headers, send_html, time_sent, private
882
		FROM {db_prefix}mail_queue
883
		ORDER BY priority ASC, id_mail ASC
884
		LIMIT {int:limit}',
885
		array(
886
			'limit' => $number,
887
		)
888
	);
889
	$ids = array();
890
	$emails = array();
891
	while ($row = $smcFunc['db_fetch_assoc']($request))
892
	{
893
		// We want to delete these from the database ASAP, so just get the data and go.
894
		$ids[] = $row['id_mail'];
895
		$emails[] = array(
896
			'to' => $row['recipient'],
897
			'body' => $row['body'],
898
			'subject' => $row['subject'],
899
			'headers' => $row['headers'],
900
			'send_html' => $row['send_html'],
901
			'time_sent' => $row['time_sent'],
902
			'private' => $row['private'],
903
		);
904
	}
905
	$smcFunc['db_free_result']($request);
906
907
	// Delete, delete, delete!!!
908
	if (!empty($ids))
909
		$smcFunc['db_query']('', '
910
			DELETE FROM {db_prefix}mail_queue
911
			WHERE id_mail IN ({array_int:mail_list})',
912
			array(
913
				'mail_list' => $ids,
914
			)
915
		);
916
917
	// Don't believe we have any left?
918
	if (count($ids) < $number)
919
	{
920
		// Only update the setting if no-one else has beaten us to it.
921
		$smcFunc['db_query']('', '
922
			UPDATE {db_prefix}settings
923
			SET value = {string:no_send}
924
			WHERE variable = {literal:mail_next_send}
925
				AND value = {string:last_mail_send}',
926
			array(
927
				'no_send' => '0',
928
				'last_mail_send' => $modSettings['mail_next_send'],
929
			)
930
		);
931
	}
932
933
	if (empty($ids))
934
		return false;
935
936
	if (!empty($modSettings['mail_type']) && $modSettings['smtp_host'] != '')
937
		require_once($sourcedir . '/Subs-Post.php');
938
939
	// Send each email, yea!
940
	$failed_emails = array();
941
	foreach ($emails as $email)
942
	{
943
		if (empty($modSettings['mail_type']) || $modSettings['smtp_host'] == '')
944
		{
945
			$email['subject'] = strtr($email['subject'], array("\r" => '', "\n" => ''));
946
			if (!empty($modSettings['mail_strip_carriage']))
947
			{
948
				$email['body'] = strtr($email['body'], array("\r" => ''));
949
				$email['headers'] = strtr($email['headers'], array("\r" => ''));
950
			}
951
952
			// No point logging a specific error here, as we have no language. PHP error is helpful anyway...
953
			$result = mail(strtr($email['to'], array("\r" => '', "\n" => '')), $email['subject'], $email['body'], $email['headers']);
954
955
			// Try to stop a timeout, this would be bad...
956
			@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

956
			/** @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...
957
			if (function_exists('apache_reset_timeout'))
958
				@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

958
				/** @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...
959
		}
960
		else
961
			$result = smtp_mail(array($email['to']), $email['subject'], $email['body'], $email['headers']);
962
963
		// Hopefully it sent?
964
		if (!$result)
965
			$failed_emails[] = array($email['to'], $email['body'], $email['subject'], $email['headers'], $email['send_html'], $email['time_sent'], $email['private']);
966
	}
967
968
	// Any emails that didn't send?
969
	if (!empty($failed_emails))
970
	{
971
		// Update the failed attempts check.
972
		$smcFunc['db_insert']('replace',
973
			'{db_prefix}settings',
974
			array('variable' => 'string', 'value' => 'string'),
975
			array('mail_failed_attempts', empty($modSettings['mail_failed_attempts']) ? 1 : ++$modSettings['mail_failed_attempts']),
976
			array('variable')
977
		);
978
979
		// If we have failed to many times, tell mail to wait a bit and try again.
980
		if ($modSettings['mail_failed_attempts'] > 5)
981
			$smcFunc['db_query']('', '
982
				UPDATE {db_prefix}settings
983
				SET value = {string:next_mail_send}
984
				WHERE variable = {literal:mail_next_send}
985
					AND value = {string:last_send}',
986
				array(
987
					'next_mail_send' => time() + 60,
988
					'last_send' => $modSettings['mail_next_send'],
989
			));
990
991
		// Add our email back to the queue, manually.
992
		$smcFunc['db_insert']('insert',
993
			'{db_prefix}mail_queue',
994
			array('recipient' => 'string', 'body' => 'string', 'subject' => 'string', 'headers' => 'string', 'send_html' => 'string', 'time_sent' => 'string', 'private' => 'int'),
995
			$failed_emails,
996
			array('id_mail')
997
		);
998
999
		return false;
1000
	}
1001
	// We where unable to send the email, clear our failed attempts.
1002
	elseif (!empty($modSettings['mail_failed_attempts']))
1003
		$smcFunc['db_query']('', '
1004
			UPDATE {db_prefix}settings
1005
			SET value = {string:zero}
1006
			WHERE variable = {string:mail_failed_attempts}',
1007
			array(
1008
				'zero' => '0',
1009
				'mail_failed_attempts' => 'mail_failed_attempts',
1010
		));
1011
1012
	// Had something to send...
1013
	return true;
1014
}
1015
1016
/**
1017
 * Calculate the next time the passed tasks should be triggered.
1018
 *
1019
 * @param string|array $tasks The ID of a single task or an array of tasks
1020
 * @param bool $forceUpdate Whether to force the tasks to run now
1021
 */
1022
function CalculateNextTrigger($tasks = array(), $forceUpdate = false)
1023
{
1024
	global $modSettings, $smcFunc;
1025
1026
	$task_query = '';
1027
	if (!is_array($tasks))
1028
		$tasks = array($tasks);
1029
1030
	// Actually have something passed?
1031
	if (!empty($tasks))
1032
	{
1033
		if (!isset($tasks[0]) || is_numeric($tasks[0]))
1034
			$task_query = ' AND id_task IN ({array_int:tasks})';
1035
		else
1036
			$task_query = ' AND task IN ({array_string:tasks})';
1037
	}
1038
	$nextTaskTime = empty($tasks) ? time() + 86400 : $modSettings['next_task_time'];
1039
1040
	// Get the critical info for the tasks.
1041
	$request = $smcFunc['db_query']('', '
1042
		SELECT id_task, next_time, time_offset, time_regularity, time_unit
1043
		FROM {db_prefix}scheduled_tasks
1044
		WHERE disabled = {int:no_disabled}
1045
			' . $task_query,
1046
		array(
1047
			'no_disabled' => 0,
1048
			'tasks' => $tasks,
1049
		)
1050
	);
1051
	$tasks = array();
1052
	while ($row = $smcFunc['db_fetch_assoc']($request))
1053
	{
1054
		$next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']);
1055
1056
		// Only bother moving the task if it's out of place or we're forcing it!
1057
		if ($forceUpdate || $next_time < $row['next_time'] || $row['next_time'] < time())
1058
			$tasks[$row['id_task']] = $next_time;
1059
		else
1060
			$next_time = $row['next_time'];
1061
1062
		// If this is sooner than the current next task, make this the next task.
1063
		if ($next_time < $nextTaskTime)
1064
			$nextTaskTime = $next_time;
1065
	}
1066
	$smcFunc['db_free_result']($request);
1067
1068
	// Now make the changes!
1069
	foreach ($tasks as $id => $time)
1070
		$smcFunc['db_query']('', '
1071
			UPDATE {db_prefix}scheduled_tasks
1072
			SET next_time = {int:next_time}
1073
			WHERE id_task = {int:id_task}',
1074
			array(
1075
				'next_time' => $time,
1076
				'id_task' => $id,
1077
			)
1078
		);
1079
1080
	// If the next task is now different update.
1081
	if ($modSettings['next_task_time'] != $nextTaskTime)
1082
		updateSettings(array('next_task_time' => $nextTaskTime));
1083
}
1084
1085
/**
1086
 * Simply returns a time stamp of the next instance of these time parameters.
1087
 *
1088
 * @param int $regularity The regularity
1089
 * @param string $unit What unit are we using - 'm' for minutes, 'd' for days, 'w' for weeks or anything else for seconds
1090
 * @param int $offset The offset
1091
 * @return int The timestamp for the specified time
1092
 */
1093
function next_time($regularity, $unit, $offset)
1094
{
1095
	// Just in case!
1096
	if ($regularity == 0)
1097
		$regularity = 2;
1098
1099
	$curMin = date('i', time());
1100
1101
	// If the unit is minutes only check regularity in minutes.
1102
	if ($unit == 'm')
1103
	{
1104
		$off = date('i', $offset);
1105
1106
		// If it's now just pretend it ain't,
1107
		if ($off == $curMin)
1108
			$next_time = time() + $regularity;
1109
		else
1110
		{
1111
			// Make sure that the offset is always in the past.
1112
			$off = $off > $curMin ? $off - 60 : $off;
1113
1114
			while ($off <= $curMin)
1115
				$off += $regularity;
1116
1117
			// Now we know when the time should be!
1118
			$next_time = time() + 60 * ($off - $curMin);
1119
		}
1120
	}
1121
	// Otherwise, work out what the offset would be with today's date.
1122
	else
1123
	{
1124
		$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

1124
		$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('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

1124
		$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('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

1124
		$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('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

1124
		$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('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

1124
		$next_time = mktime(date('H', $offset), date('i', $offset), 0, /** @scrutinizer ignore-type */ date('m'), date('d'), date('Y'));
Loading history...
1125
1126
		// Make the time offset in the past!
1127
		if ($next_time > time())
1128
		{
1129
			$next_time -= 86400;
1130
		}
1131
1132
		// Default we'll jump in hours.
1133
		$applyOffset = 3600;
1134
		// 24 hours = 1 day.
1135
		if ($unit == 'd')
1136
			$applyOffset = 86400;
1137
		// Otherwise a week.
1138
		if ($unit == 'w')
1139
			$applyOffset = 604800;
1140
1141
		$applyOffset *= $regularity;
1142
1143
		// Just add on the offset.
1144
		while ($next_time <= time())
1145
		{
1146
			$next_time += $applyOffset;
1147
		}
1148
	}
1149
1150
	return $next_time;
1151
}
1152
1153
/**
1154
 * This loads the bare minimum data to allow us to load language files!
1155
 */
1156
function loadEssentialThemeData()
1157
{
1158
	global $settings, $modSettings, $smcFunc, $mbname, $context, $sourcedir, $txt;
1159
1160
	// Get all the default theme variables.
1161
	$result = $smcFunc['db_query']('', '
1162
		SELECT id_theme, variable, value
1163
		FROM {db_prefix}themes
1164
		WHERE id_member = {int:no_member}
1165
			AND id_theme IN (1, {int:theme_guests})',
1166
		array(
1167
			'no_member' => 0,
1168
			'theme_guests' => !empty($modSettings['theme_guests']) ? $modSettings['theme_guests'] : 1,
1169
		)
1170
	);
1171
	while ($row = $smcFunc['db_fetch_assoc']($result))
1172
	{
1173
		$settings[$row['variable']] = $row['value'];
1174
1175
		// Is this the default theme?
1176
		if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1')
1177
			$settings['default_' . $row['variable']] = $row['value'];
1178
	}
1179
	$smcFunc['db_free_result']($result);
1180
1181
	// Check we have some directories setup.
1182
	if (empty($settings['template_dirs']))
1183
	{
1184
		$settings['template_dirs'] = array($settings['theme_dir']);
1185
1186
		// Based on theme (if there is one).
1187
		if (!empty($settings['base_theme_dir']))
1188
			$settings['template_dirs'][] = $settings['base_theme_dir'];
1189
1190
		// Lastly the default theme.
1191
		if ($settings['theme_dir'] != $settings['default_theme_dir'])
1192
			$settings['template_dirs'][] = $settings['default_theme_dir'];
1193
	}
1194
1195
	// Assume we want this.
1196
	$context['forum_name'] = $mbname;
1197
1198
	// Check loadLanguage actually exists!
1199
	if (!function_exists('loadLanguage'))
1200
	{
1201
		require_once($sourcedir . '/Load.php');
1202
		require_once($sourcedir . '/Subs.php');
1203
	}
1204
1205
	loadLanguage('index+Modifications');
1206
1207
	// Just in case it wasn't already set elsewhere.
1208
	$context['character_set'] = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set'];
1209
	$context['utf8'] = $context['character_set'] === 'UTF-8';
1210
	$context['right_to_left'] = !empty($txt['lang_rtl']);
1211
1212
	// Tell fatal_lang_error() to not reload the theme.
1213
	$context['theme_loaded'] = true;
1214
}
1215
1216
/**
1217
 * This retieves data (e.g. last version of SMF) from sm.org
1218
 */
1219
function scheduled_fetchSMfiles()
1220
{
1221
	global $sourcedir, $txt, $language, $forum_version, $modSettings, $smcFunc, $context;
1222
1223
	// What files do we want to get
1224
	$request = $smcFunc['db_query']('', '
1225
		SELECT id_file, filename, path, parameters
1226
		FROM {db_prefix}admin_info_files',
1227
		array(
1228
		)
1229
	);
1230
1231
	$js_files = array();
1232
1233
	while ($row = $smcFunc['db_fetch_assoc']($request))
1234
	{
1235
		$js_files[$row['id_file']] = array(
1236
			'filename' => $row['filename'],
1237
			'path' => $row['path'],
1238
			'parameters' => sprintf($row['parameters'], $language, urlencode($modSettings['time_format']), urlencode($forum_version)),
1239
		);
1240
	}
1241
1242
	$smcFunc['db_free_result']($request);
1243
1244
	// Just in case we run into a problem.
1245
	loadEssentialThemeData();
1246
	loadLanguage('Errors', $language, false);
1247
1248
	foreach ($js_files as $ID_FILE => $file)
1249
	{
1250
		// Create the url
1251
		$server = empty($file['path']) || (substr($file['path'], 0, 7) != 'http://' && substr($file['path'], 0, 8) != 'https://') ? 'https://www.simplemachines.org' : '';
1252
		$url = $server . (!empty($file['path']) ? $file['path'] : $file['path']) . $file['filename'] . (!empty($file['parameters']) ? '?' . $file['parameters'] : '');
1253
1254
		// Get the file
1255
		$file_data = fetch_web_data($url);
1256
1257
		// 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.
1258
		if ($file_data === false)
1259
		{
1260
			$context['scheduled_errors']['fetchSMfiles'][] = sprintf($txt['st_cannot_retrieve_file'], $url);
1261
			log_error(sprintf($txt['st_cannot_retrieve_file'], $url));
1262
			return false;
1263
		}
1264
1265
		// Save the file to the database.
1266
		$smcFunc['db_query']('substring', '
1267
			UPDATE {db_prefix}admin_info_files
1268
			SET data = SUBSTRING({string:file_data}, 1, 65534)
1269
			WHERE id_file = {int:id_file}',
1270
			array(
1271
				'id_file' => $ID_FILE,
1272
				'file_data' => $file_data,
1273
			)
1274
		);
1275
	}
1276
	return true;
1277
}
1278
1279
/**
1280
 * Happy birthday!!
1281
 */
1282
function scheduled_birthdayemails()
1283
{
1284
	global $smcFunc;
1285
1286
	$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
1287
		array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
1288
		array('$sourcedir/tasks/Birthday-Notify.php', 'Birthday_Notify_Background', '', 0),
1289
		array()
1290
	);
1291
1292
	return true;
1293
}
1294
1295
/**
1296
 * Weekly maintenance
1297
 */
1298
function scheduled_weekly_maintenance()
1299
{
1300
	global $modSettings, $smcFunc, $cache_enable, $cacheAPI;
1301
1302
	// Delete some settings that needn't be set if they are otherwise empty.
1303
	$emptySettings = array(
1304
		'warning_mute', 'warning_moderate', 'warning_watch', 'warning_show', 'disableCustomPerPage', 'spider_mode', 'spider_group',
1305
		'paid_currency_code', 'paid_currency_symbol', 'paid_email_to', 'paid_email', 'paid_enabled', 'paypal_email',
1306
		'search_enable_captcha', 'search_floodcontrol_time', 'show_spider_online',
1307
	);
1308
1309
	$smcFunc['db_query']('', '
1310
		DELETE FROM {db_prefix}settings
1311
		WHERE variable IN ({array_string:setting_list})
1312
			AND (value = {string:zero_value} OR value = {string:blank_value})',
1313
		array(
1314
			'zero_value' => '0',
1315
			'blank_value' => '',
1316
			'setting_list' => $emptySettings,
1317
		)
1318
	);
1319
1320
	// Some settings we never want to keep - they are just there for temporary purposes.
1321
	$deleteAnywaySettings = array(
1322
		'attachment_full_notified',
1323
	);
1324
1325
	$smcFunc['db_query']('', '
1326
		DELETE FROM {db_prefix}settings
1327
		WHERE variable IN ({array_string:setting_list})',
1328
		array(
1329
			'setting_list' => $deleteAnywaySettings,
1330
		)
1331
	);
1332
1333
	// Ok should we prune the logs?
1334
	if (!empty($modSettings['pruningOptions']))
1335
	{
1336
		if (!empty($modSettings['pruningOptions']) && strpos($modSettings['pruningOptions'], ',') !== false)
1337
			list ($modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']) = explode(',', $modSettings['pruningOptions']);
1338
1339
		if (!empty($modSettings['pruneErrorLog']))
1340
		{
1341
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1342
			$t = time() - $modSettings['pruneErrorLog'] * 86400;
1343
1344
			$smcFunc['db_query']('', '
1345
				DELETE FROM {db_prefix}log_errors
1346
				WHERE log_time < {int:log_time}',
1347
				array(
1348
					'log_time' => $t,
1349
				)
1350
			);
1351
		}
1352
1353
		if (!empty($modSettings['pruneModLog']))
1354
		{
1355
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1356
			$t = time() - $modSettings['pruneModLog'] * 86400;
1357
1358
			$smcFunc['db_query']('', '
1359
				DELETE FROM {db_prefix}log_actions
1360
				WHERE log_time < {int:log_time}
1361
					AND id_log = {int:moderation_log}',
1362
				array(
1363
					'log_time' => $t,
1364
					'moderation_log' => 1,
1365
				)
1366
			);
1367
		}
1368
1369
		if (!empty($modSettings['pruneBanLog']))
1370
		{
1371
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1372
			$t = time() - $modSettings['pruneBanLog'] * 86400;
1373
1374
			$smcFunc['db_query']('', '
1375
				DELETE FROM {db_prefix}log_banned
1376
				WHERE log_time < {int:log_time}',
1377
				array(
1378
					'log_time' => $t,
1379
				)
1380
			);
1381
		}
1382
1383
		if (!empty($modSettings['pruneReportLog']))
1384
		{
1385
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1386
			$t = time() - $modSettings['pruneReportLog'] * 86400;
1387
1388
			// This one is more complex then the other logs.  First we need to figure out which reports are too old.
1389
			$reports = array();
1390
			$result = $smcFunc['db_query']('', '
1391
				SELECT id_report
1392
				FROM {db_prefix}log_reported
1393
				WHERE time_started < {int:time_started}
1394
					AND closed = {int:closed}
1395
					AND ignore_all = {int:not_ignored}',
1396
				array(
1397
					'time_started' => $t,
1398
					'closed' => 1,
1399
					'not_ignored' => 0,
1400
				)
1401
			);
1402
1403
			while ($row = $smcFunc['db_fetch_row']($result))
1404
				$reports[] = $row[0];
1405
1406
			$smcFunc['db_free_result']($result);
1407
1408
			if (!empty($reports))
1409
			{
1410
				// Now delete the reports...
1411
				$smcFunc['db_query']('', '
1412
					DELETE FROM {db_prefix}log_reported
1413
					WHERE id_report IN ({array_int:report_list})',
1414
					array(
1415
						'report_list' => $reports,
1416
					)
1417
				);
1418
				// And delete the comments for those reports...
1419
				$smcFunc['db_query']('', '
1420
					DELETE FROM {db_prefix}log_reported_comments
1421
					WHERE id_report IN ({array_int:report_list})',
1422
					array(
1423
						'report_list' => $reports,
1424
					)
1425
				);
1426
			}
1427
		}
1428
1429
		if (!empty($modSettings['pruneScheduledTaskLog']))
1430
		{
1431
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1432
			$t = time() - $modSettings['pruneScheduledTaskLog'] * 86400;
1433
1434
			$smcFunc['db_query']('', '
1435
				DELETE FROM {db_prefix}log_scheduled_tasks
1436
				WHERE time_run < {int:time_run}',
1437
				array(
1438
					'time_run' => $t,
1439
				)
1440
			);
1441
		}
1442
1443
		if (!empty($modSettings['pruneSpiderHitLog']))
1444
		{
1445
			// Figure out when our cutoff time is.  1 day = 86400 seconds.
1446
			$t = time() - $modSettings['pruneSpiderHitLog'] * 86400;
1447
1448
			$smcFunc['db_query']('', '
1449
				DELETE FROM {db_prefix}log_spider_hits
1450
				WHERE log_time < {int:log_time}',
1451
				array(
1452
					'log_time' => $t,
1453
				)
1454
			);
1455
		}
1456
	}
1457
1458
	// Get rid of any paid subscriptions that were never actioned.
1459
	$smcFunc['db_query']('', '
1460
		DELETE FROM {db_prefix}log_subscribed
1461
		WHERE end_time = {int:no_end_time}
1462
			AND status = {int:not_active}
1463
			AND start_time < {int:start_time}
1464
			AND payments_pending < {int:payments_pending}',
1465
		array(
1466
			'no_end_time' => 0,
1467
			'not_active' => 0,
1468
			'start_time' => time() - 60,
1469
			'payments_pending' => 1,
1470
		)
1471
	);
1472
1473
	// Some OS's don't seem to clean out their sessions.
1474
	$smcFunc['db_query']('', '
1475
		DELETE FROM {db_prefix}sessions
1476
		WHERE last_update < {int:last_update}',
1477
		array(
1478
			'last_update' => time() - 86400,
1479
		)
1480
	);
1481
1482
	// Update the regex of top level domains with the IANA's latest official list
1483
	$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
1484
		array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
1485
		array('$sourcedir/tasks/UpdateTldRegex.php', 'Update_TLD_Regex', '', 0), array()
1486
	);
1487
1488
	// Run Cache housekeeping
1489
	if (!empty($cache_enable) && !empty($cacheAPI))
1490
		$cacheAPI->housekeeping();
1491
1492
	// Prevent stale minimized CSS and JavaScript from cluttering up the theme directories
1493
	deleteAllMinified();
1494
1495
	return true;
1496
}
1497
1498
/**
1499
 * Perform the standard checks on expiring/near expiring subscriptions.
1500
 */
1501
function scheduled_paid_subscriptions()
1502
{
1503
	global $sourcedir, $scripturl, $smcFunc, $modSettings, $language;
1504
1505
	// Start off by checking for removed subscriptions.
1506
	$request = $smcFunc['db_query']('', '
1507
		SELECT id_subscribe, id_member
1508
		FROM {db_prefix}log_subscribed
1509
		WHERE status = {int:is_active}
1510
			AND end_time < {int:time_now}',
1511
		array(
1512
			'is_active' => 1,
1513
			'time_now' => time(),
1514
		)
1515
	);
1516
	while ($row = $smcFunc['db_fetch_assoc']($request))
1517
	{
1518
		require_once($sourcedir . '/ManagePaid.php');
1519
		removeSubscription($row['id_subscribe'], $row['id_member']);
1520
	}
1521
	$smcFunc['db_free_result']($request);
1522
1523
	// Get all those about to expire that have not had a reminder sent.
1524
	$request = $smcFunc['db_query']('', '
1525
		SELECT ls.id_sublog, m.id_member, m.member_name, m.email_address, m.lngfile, s.name, ls.end_time
1526
		FROM {db_prefix}log_subscribed AS ls
1527
			JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe)
1528
			JOIN {db_prefix}members AS m ON (m.id_member = ls.id_member)
1529
		WHERE ls.status = {int:is_active}
1530
			AND ls.reminder_sent = {int:reminder_sent}
1531
			AND s.reminder > {int:reminder_wanted}
1532
			AND ls.end_time < ({int:time_now} + s.reminder * 86400)',
1533
		array(
1534
			'is_active' => 1,
1535
			'reminder_sent' => 0,
1536
			'reminder_wanted' => 0,
1537
			'time_now' => time(),
1538
		)
1539
	);
1540
	$subs_reminded = array();
1541
	$members = array();
1542
	while ($row = $smcFunc['db_fetch_assoc']($request))
1543
	{
1544
		// If this is the first one load the important bits.
1545
		if (empty($subs_reminded))
1546
		{
1547
			require_once($sourcedir . '/Subs-Post.php');
1548
			// Need the below for loadLanguage to work!
1549
			loadEssentialThemeData();
1550
		}
1551
1552
		$subs_reminded[] = $row['id_sublog'];
1553
		$members[$row['id_member']] = $row;
1554
	}
1555
	$smcFunc['db_free_result']($request);
1556
1557
	// Load alert preferences
1558
	require_once($sourcedir . '/Subs-Notify.php');
1559
	$notifyPrefs = getNotifyPrefs(array_keys($members), 'paidsubs_expiring', true);
1560
	$alert_rows = array();
1561
	foreach ($members as $row)
1562
	{
1563
		$replacements = array(
1564
			'PROFILE_LINK' => $scripturl . '?action=profile;area=subscriptions;u=' . $row['id_member'],
1565
			'REALNAME' => $row['member_name'],
1566
			'SUBSCRIPTION' => $row['name'],
1567
			'END_DATE' => strip_tags(timeformat($row['end_time'])),
1568
		);
1569
1570
		$emaildata = loadEmailTemplate('paid_subscription_reminder', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
1571
1572
		// Send the actual email.
1573
		if ($notifyPrefs[$row['id_member']] & 0x02)
1574
			sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, 'paid_sub_remind', $emaildata['is_html'], 2);
1575
1576
		if ($notifyPrefs[$row['id_member']] & 0x01)
1577
		{
1578
			$alert_rows[] = array(
1579
				'alert_time' => time(),
1580
				'id_member' => $row['id_member'],
1581
				'id_member_started' => $row['id_member'],
1582
				'member_name' => $row['member_name'],
1583
				'content_type' => 'paidsubs',
1584
				'content_id' => $row['id_sublog'],
1585
				'content_action' => 'expiring',
1586
				'is_read' => 0,
1587
				'extra' => $smcFunc['json_encode'](array(
1588
					'subscription_name' => $row['name'],
1589
					'end_time' => strip_tags(timeformat($row['end_time'])),
1590
				)),
1591
			);
1592
			updateMemberData($row['id_member'], array('alerts' => '+'));
1593
		}
1594
	}
1595
1596
	// Insert the alerts if any
1597
	if (!empty($alert_rows))
1598
		$smcFunc['db_insert']('',
1599
			'{db_prefix}user_alerts',
1600
			array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
1601
				'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
1602
			$alert_rows,
1603
			array()
1604
		);
1605
1606
	// Mark the reminder as sent.
1607
	if (!empty($subs_reminded))
1608
		$smcFunc['db_query']('', '
1609
			UPDATE {db_prefix}log_subscribed
1610
			SET reminder_sent = {int:reminder_sent}
1611
			WHERE id_sublog IN ({array_int:subscription_list})',
1612
			array(
1613
				'subscription_list' => $subs_reminded,
1614
				'reminder_sent' => 1,
1615
			)
1616
		);
1617
1618
	return true;
1619
}
1620
1621
/**
1622
 * Check for un-posted attachments is something we can do once in a while :P
1623
 * This function uses opendir cycling through all the attachments
1624
 */
1625
function scheduled_remove_temp_attachments()
1626
{
1627
	global $smcFunc, $modSettings, $context, $txt;
1628
1629
	// We need to know where this thing is going.
1630
	if (!empty($modSettings['currentAttachmentUploadDir']))
1631
	{
1632
		if (!is_array($modSettings['attachmentUploadDir']))
1633
			$modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true);
1634
1635
		// Just use the current path for temp files.
1636
		$attach_dirs = $modSettings['attachmentUploadDir'];
1637
	}
1638
	else
1639
	{
1640
		$attach_dirs = array($modSettings['attachmentUploadDir']);
1641
	}
1642
1643
	foreach ($attach_dirs as $attach_dir)
1644
	{
1645
		$dir = @opendir($attach_dir);
1646
		if (!$dir)
1647
		{
1648
			loadEssentialThemeData();
1649
			loadLanguage('Post');
1650
			$context['scheduled_errors']['remove_temp_attachments'][] = $txt['cant_access_upload_path'] . ' (' . $attach_dir . ')';
1651
			log_error($txt['cant_access_upload_path'] . ' (' . $attach_dir . ')', 'critical');
1652
			return false;
1653
		}
1654
1655
		while ($file = readdir($dir))
1656
		{
1657
			if ($file == '.' || $file == '..')
1658
				continue;
1659
1660
			if (strpos($file, 'post_tmp_') !== false)
1661
			{
1662
				// Temp file is more than 5 hours old!
1663
				if (filemtime($attach_dir . '/' . $file) < time() - 18000)
1664
					@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

1664
					/** @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...
1665
			}
1666
		}
1667
		closedir($dir);
1668
	}
1669
1670
	return true;
1671
}
1672
1673
/**
1674
 * Check for move topic notices that have past their best by date
1675
 */
1676
function scheduled_remove_topic_redirect()
1677
{
1678
	global $smcFunc, $sourcedir;
1679
1680
	// init
1681
	$topics = array();
1682
1683
	// We will need this for language files
1684
	loadEssentialThemeData();
1685
1686
	// Find all of the old MOVE topic notices that were set to expire
1687
	$request = $smcFunc['db_query']('', '
1688
		SELECT id_topic
1689
		FROM {db_prefix}topics
1690
		WHERE redirect_expires <= {int:redirect_expires}
1691
			AND redirect_expires <> 0',
1692
		array(
1693
			'redirect_expires' => time(),
1694
		)
1695
	);
1696
1697
	while ($row = $smcFunc['db_fetch_row']($request))
1698
		$topics[] = $row[0];
1699
	$smcFunc['db_free_result']($request);
1700
1701
	// Zap, your gone
1702
	if (count($topics) > 0)
1703
	{
1704
		require_once($sourcedir . '/RemoveTopic.php');
1705
		removeTopics($topics, false, true);
1706
	}
1707
1708
	return true;
1709
}
1710
1711
/**
1712
 * Check for old drafts and remove them
1713
 */
1714
function scheduled_remove_old_drafts()
1715
{
1716
	global $smcFunc, $sourcedir, $modSettings;
1717
1718
	if (empty($modSettings['drafts_keep_days']))
1719
		return true;
1720
1721
	// init
1722
	$drafts = array();
1723
1724
	// We need this for language items
1725
	loadEssentialThemeData();
1726
1727
	// Find all of the old drafts
1728
	$request = $smcFunc['db_query']('', '
1729
		SELECT id_draft
1730
		FROM {db_prefix}user_drafts
1731
		WHERE poster_time <= {int:poster_time_old}',
1732
		array(
1733
			'poster_time_old' => time() - (86400 * $modSettings['drafts_keep_days']),
1734
		)
1735
	);
1736
1737
	while ($row = $smcFunc['db_fetch_row']($request))
1738
		$drafts[] = (int) $row[0];
1739
	$smcFunc['db_free_result']($request);
1740
1741
	// If we have old one, remove them
1742
	if (count($drafts) > 0)
1743
	{
1744
		require_once($sourcedir . '/Drafts.php');
1745
		DeleteDraft($drafts, false);
0 ignored issues
show
Bug introduced by
$drafts of type integer[]|array 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

1745
		DeleteDraft(/** @scrutinizer ignore-type */ $drafts, false);
Loading history...
1746
	}
1747
1748
	return true;
1749
}
1750
1751
?>