Issues (1014)

Sources/ManageMaintenance.php (1 issue)

1
<?php
2
3
/**
4
 * Forum maintenance. Important stuff.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2022 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1.0
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Main dispatcher, the maintenance access point.
21
 * This, as usual, checks permissions, loads language files, and forwards to the actual workers.
22
 */
23
function ManageMaintenance()
24
{
25
	global $txt, $context;
26
27
	// You absolutely must be an admin by here!
28
	isAllowedTo('admin_forum');
29
30
	// Need something to talk about?
31
	loadLanguage('ManageMaintenance');
32
	loadTemplate('ManageMaintenance');
33
34
	// This uses admin tabs - as it should!
35
	$context[$context['admin_menu_name']]['tab_data'] = array(
36
		'title' => $txt['maintain_title'],
37
		'description' => $txt['maintain_info'],
38
		'tabs' => array(
39
			'routine' => array(),
40
			'database' => array(),
41
			'members' => array(),
42
			'topics' => array(),
43
		),
44
	);
45
46
	// So many things you can do - but frankly I won't let you - just these!
47
	$subActions = array(
48
		'routine' => array(
49
			'function' => 'MaintainRoutine',
50
			'template' => 'maintain_routine',
51
			'activities' => array(
52
				'version' => 'VersionDetail',
53
				'repair' => 'MaintainFindFixErrors',
54
				'recount' => 'AdminBoardRecount',
55
				'rebuild_settings' => 'RebuildSettingsFile',
56
				'logs' => 'MaintainEmptyUnimportantLogs',
57
				'cleancache' => 'MaintainCleanCache',
58
			),
59
		),
60
		'database' => array(
61
			'function' => 'MaintainDatabase',
62
			'template' => 'maintain_database',
63
			'activities' => array(
64
				'optimize' => 'OptimizeTables',
65
				'convertentities' => 'ConvertEntities',
66
				'convertmsgbody' => 'ConvertMsgBody',
67
			),
68
		),
69
		'members' => array(
70
			'function' => 'MaintainMembers',
71
			'template' => 'maintain_members',
72
			'activities' => array(
73
				'reattribute' => 'MaintainReattributePosts',
74
				'purgeinactive' => 'MaintainPurgeInactiveMembers',
75
				'recountposts' => 'MaintainRecountPosts',
76
			),
77
		),
78
		'topics' => array(
79
			'function' => 'MaintainTopics',
80
			'template' => 'maintain_topics',
81
			'activities' => array(
82
				'massmove' => 'MaintainMassMoveTopics',
83
				'pruneold' => 'MaintainRemoveOldPosts',
84
				'olddrafts' => 'MaintainRemoveOldDrafts',
85
			),
86
		),
87
		'hooks' => array(
88
			'function' => 'list_integration_hooks',
89
		),
90
		'destroy' => array(
91
			'function' => 'Destroy',
92
			'activities' => array(),
93
		),
94
	);
95
96
	call_integration_hook('integrate_manage_maintenance', array(&$subActions));
97
98
	// Yep, sub-action time!
99
	if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
100
		$subAction = $_REQUEST['sa'];
101
	else
102
		$subAction = 'routine';
103
104
	// Doing something special?
105
	if (isset($_REQUEST['activity']) && isset($subActions[$subAction]['activities'][$_REQUEST['activity']]))
106
		$activity = $_REQUEST['activity'];
107
108
	// Set a few things.
109
	$context['page_title'] = $txt['maintain_title'];
110
	$context['sub_action'] = $subAction;
111
	$context['sub_template'] = !empty($subActions[$subAction]['template']) ? $subActions[$subAction]['template'] : '';
112
113
	// Finally fall through to what we are doing.
114
	call_helper($subActions[$subAction]['function']);
115
116
	// Any special activity?
117
	if (isset($activity))
118
		call_helper($subActions[$subAction]['activities'][$activity]);
119
120
	// Create a maintenance token.  Kinda hard to do it any other way.
121
	createToken('admin-maint');
122
}
123
124
/**
125
 * Supporting function for the database maintenance area.
126
 */
127
function MaintainDatabase()
128
{
129
	global $context, $db_type, $db_character_set, $modSettings, $smcFunc, $txt;
130
131
	// Show some conversion options?
132
	$context['convert_entities'] = isset($modSettings['global_character_set']) && $modSettings['global_character_set'] === 'UTF-8';
133
134
	if ($db_type == 'mysql')
135
	{
136
		db_extend('packages');
137
138
		$colData = $smcFunc['db_list_columns']('{db_prefix}messages', true);
139
		foreach ($colData as $column)
140
			if ($column['name'] == 'body')
141
				$body_type = $column['type'];
142
143
		$context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text';
144
		$context['convert_to_suggest'] = ($body_type != 'text' && !empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] < 65536);
145
	}
146
147
	if (isset($_GET['done']) && $_GET['done'] == 'convertentities')
148
		$context['maintenance_finished'] = $txt['entity_convert_title'];
149
}
150
151
/**
152
 * Supporting function for the routine maintenance area.
153
 */
154
function MaintainRoutine()
155
{
156
	global $context, $txt;
157
158
	if (isset($_GET['done']) && in_array($_GET['done'], array('recount', 'rebuild_settings')))
159
		$context['maintenance_finished'] = $txt['maintain_' . $_GET['done']];
160
}
161
162
/**
163
 * Supporting function for the members maintenance area.
164
 */
165
function MaintainMembers()
166
{
167
	global $context, $smcFunc, $txt;
168
169
	// Get membergroups - for deleting members and the like.
170
	$result = $smcFunc['db_query']('', '
171
		SELECT id_group, group_name
172
		FROM {db_prefix}membergroups',
173
		array(
174
		)
175
	);
176
	$context['membergroups'] = array(
177
		array(
178
			'id' => 0,
179
			'name' => $txt['maintain_members_ungrouped']
180
		),
181
	);
182
	while ($row = $smcFunc['db_fetch_assoc']($result))
183
	{
184
		$context['membergroups'][] = array(
185
			'id' => $row['id_group'],
186
			'name' => $row['group_name']
187
		);
188
	}
189
	$smcFunc['db_free_result']($result);
190
191
	if (isset($_GET['done']) && $_GET['done'] == 'recountposts')
192
		$context['maintenance_finished'] = $txt['maintain_recountposts'];
193
194
	loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest');
195
}
196
197
/**
198
 * Supporting function for the topics maintenance area.
199
 */
200
function MaintainTopics()
201
{
202
	global $context, $smcFunc, $txt, $sourcedir;
203
204
	// Let's load up the boards in case they are useful.
205
	$result = $smcFunc['db_query']('order_by_board_order', '
206
		SELECT b.id_board, b.name, b.child_level, c.name AS cat_name, c.id_cat
207
		FROM {db_prefix}boards AS b
208
			LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
209
		WHERE {query_see_board}
210
			AND redirect = {string:blank_redirect}',
211
		array(
212
			'blank_redirect' => '',
213
		)
214
	);
215
	$context['categories'] = array();
216
	while ($row = $smcFunc['db_fetch_assoc']($result))
217
	{
218
		if (!isset($context['categories'][$row['id_cat']]))
219
			$context['categories'][$row['id_cat']] = array(
220
				'name' => $row['cat_name'],
221
				'boards' => array()
222
			);
223
224
		$context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array(
225
			'id' => $row['id_board'],
226
			'name' => $row['name'],
227
			'child_level' => $row['child_level']
228
		);
229
	}
230
	$smcFunc['db_free_result']($result);
231
232
	require_once($sourcedir . '/Subs-Boards.php');
233
	sortCategories($context['categories']);
234
235
	if (isset($_GET['done']) && $_GET['done'] == 'purgeold')
236
		$context['maintenance_finished'] = $txt['maintain_old'];
237
	elseif (isset($_GET['done']) && $_GET['done'] == 'massmove')
238
		$context['maintenance_finished'] = $txt['move_topics_maintenance'];
239
}
240
241
/**
242
 * Find and fix all errors on the forum.
243
 */
244
function MaintainFindFixErrors()
245
{
246
	global $sourcedir;
247
248
	// Honestly, this should be done in the sub function.
249
	validateToken('admin-maint');
250
251
	require_once($sourcedir . '/RepairBoards.php');
252
	RepairBoards();
253
}
254
255
/**
256
 * Wipes the whole cache.
257
 */
258
function MaintainCleanCache()
259
{
260
	global $context, $txt;
261
262
	checkSession();
263
	validateToken('admin-maint');
264
265
	// Just wipe the whole cache directory!
266
	clean_cache();
267
268
	$context['maintenance_finished'] = $txt['maintain_cache'];
269
}
270
271
/**
272
 * Empties all uninmportant logs
273
 */
274
function MaintainEmptyUnimportantLogs()
275
{
276
	global $context, $smcFunc, $txt;
277
278
	checkSession();
279
	validateToken('admin-maint');
280
281
	// No one's online now.... MUHAHAHAHA :P.
282
	$smcFunc['db_query']('', '
283
		DELETE FROM {db_prefix}log_online');
284
285
	// Dump the banning logs.
286
	$smcFunc['db_query']('', '
287
		DELETE FROM {db_prefix}log_banned');
288
289
	// Start id_error back at 0 and dump the error log.
290
	$smcFunc['db_query']('truncate_table', '
291
		TRUNCATE {db_prefix}log_errors');
292
293
	// Clear out the spam log.
294
	$smcFunc['db_query']('', '
295
		DELETE FROM {db_prefix}log_floodcontrol');
296
297
	// Last but not least, the search logs!
298
	$smcFunc['db_query']('truncate_table', '
299
		TRUNCATE {db_prefix}log_search_topics');
300
301
	$smcFunc['db_query']('truncate_table', '
302
		TRUNCATE {db_prefix}log_search_messages');
303
304
	$smcFunc['db_query']('truncate_table', '
305
		TRUNCATE {db_prefix}log_search_results');
306
307
	updateSettings(array('search_pointer' => 0));
308
309
	$context['maintenance_finished'] = $txt['maintain_logs'];
310
}
311
312
/**
313
 * Oh noes! I'd document this but that would give it away
314
 */
315
function Destroy()
316
{
317
	global $context;
318
319
	echo '<!DOCTYPE html>
320
		<html', $context['right_to_left'] ? ' dir="rtl"' : '', '><head><title>', $context['forum_name_html_safe'], ' deleted!</title></head>
321
		<body style="background-color: orange; font-family: arial, sans-serif; text-align: center;">
322
		<div style="margin-top: 8%; font-size: 400%; color: black;">Oh my, you killed ', $context['forum_name_html_safe'], '!</div>
323
		<div style="margin-top: 7%; font-size: 500%; color: red;"><strong>You lazy bum!</strong></div>
324
		</body></html>';
325
	obExit(false);
326
}
327
328
/**
329
 * Convert the column "body" of the table {db_prefix}messages from TEXT to MEDIUMTEXT and vice versa.
330
 * It requires the admin_forum permission.
331
 * This is needed only for MySQL.
332
 * During the conversion from MEDIUMTEXT to TEXT it check if any of the posts exceed the TEXT length and if so it aborts.
333
 * This action is linked from the maintenance screen (if it's applicable).
334
 * Accessed by ?action=admin;area=maintain;sa=database;activity=convertmsgbody.
335
 *
336
 * @uses template_convert_msgbody()
337
 */
338
function ConvertMsgBody()
339
{
340
	global $scripturl, $context, $txt, $db_type;
341
	global $modSettings, $smcFunc;
342
343
	// Show me your badge!
344
	isAllowedTo('admin_forum');
345
346
	if ($db_type != 'mysql')
347
		return;
348
349
	db_extend('packages');
350
351
	$colData = $smcFunc['db_list_columns']('{db_prefix}messages', true);
352
	foreach ($colData as $column)
353
		if ($column['name'] == 'body')
354
			$body_type = $column['type'];
355
356
	$context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text';
357
358
	if ($body_type == 'text' || ($body_type != 'text' && isset($_POST['do_conversion'])))
359
	{
360
		checkSession();
361
		validateToken('admin-maint');
362
363
		// Make it longer so we can do their limit.
364
		if ($body_type == 'text')
365
			$smcFunc['db_change_column']('{db_prefix}messages', 'body', array('type' => 'mediumtext'));
366
		// Shorten the column so we can have a bit (literally per record) less space occupied
367
		else
368
			$smcFunc['db_change_column']('{db_prefix}messages', 'body', array('type' => 'text'));
369
370
		// 3rd party integrations may be interested in knowning about this.
371
		call_integration_hook('integrate_convert_msgbody', array($body_type));
372
373
		$colData = $smcFunc['db_list_columns']('{db_prefix}messages', true);
374
		foreach ($colData as $column)
375
			if ($column['name'] == 'body')
376
				$body_type = $column['type'];
377
378
		$context['maintenance_finished'] = $txt[$context['convert_to'] . '_title'];
379
		$context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text';
380
		$context['convert_to_suggest'] = ($body_type != 'text' && !empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] < 65536);
381
382
		return;
383
	}
384
	elseif ($body_type != 'text' && (!isset($_POST['do_conversion']) || isset($_POST['cont'])))
385
	{
386
		checkSession();
387
		if (empty($_REQUEST['start']))
388
			validateToken('admin-maint');
389
		else
390
			validateToken('admin-convertMsg');
391
392
		$context['page_title'] = $txt['not_done_title'];
393
		$context['continue_post_data'] = '';
394
		$context['continue_countdown'] = 3;
395
		$context['sub_template'] = 'not_done';
396
		$increment = 500;
397
		$id_msg_exceeding = isset($_POST['id_msg_exceeding']) ? explode(',', $_POST['id_msg_exceeding']) : array();
398
399
		$request = $smcFunc['db_query']('', '
400
			SELECT COUNT(*) as count
401
			FROM {db_prefix}messages',
402
			array()
403
		);
404
		list($max_msgs) = $smcFunc['db_fetch_row']($request);
405
		$smcFunc['db_free_result']($request);
406
407
		// Try for as much time as possible.
408
		@set_time_limit(600);
409
410
		while ($_REQUEST['start'] < $max_msgs)
411
		{
412
			$request = $smcFunc['db_query']('', '
413
				SELECT id_msg
414
				FROM {db_prefix}messages
415
				WHERE id_msg BETWEEN {int:start} AND {int:start} + {int:increment}
416
					AND LENGTH(body) > 65535',
417
				array(
418
					'start' => $_REQUEST['start'],
419
					'increment' => $increment - 1,
420
				)
421
			);
422
			while ($row = $smcFunc['db_fetch_assoc']($request))
423
				$id_msg_exceeding[] = $row['id_msg'];
424
			$smcFunc['db_free_result']($request);
425
426
			$_REQUEST['start'] += $increment;
427
428
			if (microtime(true) - TIME_START > 3)
429
			{
430
				createToken('admin-convertMsg');
431
				$context['continue_post_data'] = '
432
					<input type="hidden" name="' . $context['admin-convertMsg_token_var'] . '" value="' . $context['admin-convertMsg_token'] . '">
433
					<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '">
434
					<input type="hidden" name="id_msg_exceeding" value="' . implode(',', $id_msg_exceeding) . '">';
435
436
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=database;activity=convertmsgbody;start=' . $_REQUEST['start'];
437
				$context['continue_percent'] = round(100 * $_REQUEST['start'] / $max_msgs);
438
439
				return;
440
			}
441
		}
442
		createToken('admin-maint');
443
		$context['page_title'] = $txt[$context['convert_to'] . '_title'];
444
		$context['sub_template'] = 'convert_msgbody';
445
446
		if (!empty($id_msg_exceeding))
447
		{
448
			if (count($id_msg_exceeding) > 100)
449
			{
450
				$query_msg = array_slice($id_msg_exceeding, 0, 100);
451
				$context['exceeding_messages_morethan'] = sprintf($txt['exceeding_messages_morethan'], count($id_msg_exceeding));
452
			}
453
			else
454
				$query_msg = $id_msg_exceeding;
455
456
			$context['exceeding_messages'] = array();
457
			$request = $smcFunc['db_query']('', '
458
				SELECT id_msg, id_topic, subject
459
				FROM {db_prefix}messages
460
				WHERE id_msg IN ({array_int:messages})',
461
				array(
462
					'messages' => $query_msg,
463
				)
464
			);
465
			while ($row = $smcFunc['db_fetch_assoc']($request))
466
				$context['exceeding_messages'][] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>';
467
			$smcFunc['db_free_result']($request);
468
		}
469
	}
470
}
471
472
/**
473
 * Converts HTML-entities to their UTF-8 character equivalents.
474
 * This requires the admin_forum permission.
475
 * Pre-condition: UTF-8 has been set as database and global character set.
476
 *
477
 * It is divided in steps of 10 seconds.
478
 * This action is linked from the maintenance screen (if applicable).
479
 * It is accessed by ?action=admin;area=maintain;sa=database;activity=convertentities.
480
 *
481
 * @uses template_convert_entities()
482
 */
483
function ConvertEntities()
484
{
485
	global $db_character_set, $modSettings, $context, $smcFunc, $db_type, $db_prefix;
486
487
	isAllowedTo('admin_forum');
488
489
	// Check to see if UTF-8 is currently the default character set.
490
	if ($modSettings['global_character_set'] !== 'UTF-8')
491
		fatal_lang_error('entity_convert_only_utf8');
492
493
	// Some starting values.
494
	$context['table'] = empty($_REQUEST['table']) ? 0 : (int) $_REQUEST['table'];
495
	$context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
496
497
	$context['start_time'] = time();
498
499
	$context['first_step'] = !isset($_REQUEST[$context['session_var']]);
500
	$context['last_step'] = false;
501
502
	// The first step is just a text screen with some explanation.
503
	if ($context['first_step'])
504
	{
505
		validateToken('admin-maint');
506
		createToken('admin-maint');
507
508
		$context['sub_template'] = 'convert_entities';
509
		return;
510
	}
511
	// Otherwise use the generic "not done" template.
512
	$context['sub_template'] = 'not_done';
513
	$context['continue_post_data'] = '';
514
	$context['continue_countdown'] = 3;
515
516
	// Now we're actually going to convert...
517
	checkSession('request');
518
	validateToken('admin-maint');
519
	createToken('admin-maint');
520
	$context['not_done_token'] = 'admin-maint';
521
522
	// A list of tables ready for conversion.
523
	$tables = array(
524
		'ban_groups',
525
		'ban_items',
526
		'boards',
527
		'calendar',
528
		'calendar_holidays',
529
		'categories',
530
		'log_errors',
531
		'log_search_subjects',
532
		'membergroups',
533
		'members',
534
		'message_icons',
535
		'messages',
536
		'package_servers',
537
		'personal_messages',
538
		'pm_recipients',
539
		'polls',
540
		'poll_choices',
541
		'smileys',
542
		'themes',
543
	);
544
	$context['num_tables'] = count($tables);
545
546
	// Loop through all tables that need converting.
547
	for (; $context['table'] < $context['num_tables']; $context['table']++)
548
	{
549
		$cur_table = $tables[$context['table']];
550
		$primary_key = '';
551
		// Make sure we keep stuff unique!
552
		$primary_keys = array();
553
554
		if (function_exists('apache_reset_timeout'))
555
			@apache_reset_timeout();
556
557
		// Get a list of text columns.
558
		$columns = array();
559
		if ($db_type == 'postgresql')
560
			$request = $smcFunc['db_query']('', '
561
				SELECT column_name "Field", data_type "Type"
562
				FROM information_schema.columns
563
				WHERE table_name = {string:cur_table}
564
					AND (data_type = \'character varying\' or data_type = \'text\')',
565
				array(
566
					'cur_table' => $db_prefix . $cur_table,
567
				)
568
			);
569
		else
570
			$request = $smcFunc['db_query']('', '
571
				SHOW FULL COLUMNS
572
				FROM {db_prefix}{raw:cur_table}',
573
				array(
574
					'cur_table' => $cur_table,
575
				)
576
			);
577
		while ($column_info = $smcFunc['db_fetch_assoc']($request))
578
			if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false)
579
				$columns[] = strtolower($column_info['Field']);
580
581
		// Get the column with the (first) primary key.
582
		if ($db_type == 'postgresql')
583
			$request = $smcFunc['db_query']('', '
584
				SELECT a.attname "Column_name", \'PRIMARY\' "Key_name", attnum "Seq_in_index"
585
				FROM   pg_index i
586
				JOIN   pg_attribute a ON a.attrelid = i.indrelid
587
					AND a.attnum = ANY(i.indkey)
588
				WHERE  i.indrelid = {string:cur_table}::regclass
589
					AND    i.indisprimary',
590
				array(
591
					'cur_table' => $db_prefix . $cur_table,
592
				)
593
			);
594
		else
595
			$request = $smcFunc['db_query']('', '
596
				SHOW KEYS
597
				FROM {db_prefix}{raw:cur_table}',
598
				array(
599
					'cur_table' => $cur_table,
600
				)
601
			);
602
		while ($row = $smcFunc['db_fetch_assoc']($request))
603
		{
604
			if ($row['Key_name'] === 'PRIMARY')
605
			{
606
				if ((empty($primary_key) || $row['Seq_in_index'] == 1) && !in_array(strtolower($row['Column_name']), $columns))
607
					$primary_key = $row['Column_name'];
608
609
				$primary_keys[] = $row['Column_name'];
610
			}
611
		}
612
		$smcFunc['db_free_result']($request);
613
614
		// No primary key, no glory.
615
		// Same for columns. Just to be sure we've work to do!
616
		if (empty($primary_key) || empty($columns))
617
			continue;
618
619
		// Get the maximum value for the primary key.
620
		$request = $smcFunc['db_query']('', '
621
			SELECT MAX({identifier:key})
622
			FROM {db_prefix}{raw:cur_table}',
623
			array(
624
				'key' => $primary_key,
625
				'cur_table' => $cur_table,
626
			)
627
		);
628
		list($max_value) = $smcFunc['db_fetch_row']($request);
629
		$smcFunc['db_free_result']($request);
630
631
		if (empty($max_value))
632
			continue;
633
634
		while ($context['start'] <= $max_value)
635
		{
636
			// Retrieve a list of rows that has at least one entity to convert.
637
			$request = $smcFunc['db_query']('', '
638
				SELECT {raw:primary_keys}, {raw:columns}
639
				FROM {db_prefix}{raw:cur_table}
640
				WHERE {raw:primary_key} BETWEEN {int:start} AND {int:start} + 499
641
					AND {raw:like_compare}
642
				LIMIT 500',
643
				array(
644
					'primary_keys' => implode(', ', $primary_keys),
645
					'columns' => implode(', ', $columns),
646
					'cur_table' => $cur_table,
647
					'primary_key' => $primary_key,
648
					'start' => $context['start'],
649
					'like_compare' => '(' . implode(' LIKE \'%&#%\' OR ', $columns) . ' LIKE \'%&#%\')',
650
				)
651
			);
652
			while ($row = $smcFunc['db_fetch_assoc']($request))
653
			{
654
				$insertion_variables = array();
655
				$changes = array();
656
				foreach ($row as $column_name => $column_value)
657
					if ($column_name !== $primary_key && strpos($column_value, '&#') !== false)
658
					{
659
						$changes[] = $column_name . ' = {string:changes_' . $column_name . '}';
660
						$insertion_variables['changes_' . $column_name] = preg_replace_callback('~&#(\d{1,5}|x[0-9a-fA-F]{1,4});~', 'fixchardb__callback', $column_value);
661
					}
662
663
				$where = array();
664
				foreach ($primary_keys as $key)
665
				{
666
					$where[] = $key . ' = {string:where_' . $key . '}';
667
					$insertion_variables['where_' . $key] = $row[$key];
668
				}
669
670
				// Update the row.
671
				if (!empty($changes))
672
					$smcFunc['db_query']('', '
673
						UPDATE {db_prefix}' . $cur_table . '
674
						SET
675
							' . implode(',
676
							', $changes) . '
677
						WHERE ' . implode(' AND ', $where),
678
						$insertion_variables
679
					);
680
			}
681
			$smcFunc['db_free_result']($request);
682
			$context['start'] += 500;
683
684
			// After ten seconds interrupt.
685
			if (time() - $context['start_time'] > 10)
686
			{
687
				// Calculate an approximation of the percentage done.
688
				$context['continue_percent'] = round(100 * ($context['table'] + ($context['start'] / $max_value)) / $context['num_tables'], 1);
689
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=database;activity=convertentities;table=' . $context['table'] . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
690
				return;
691
			}
692
		}
693
		$context['start'] = 0;
694
	}
695
696
	// If we're here, we must be done.
697
	$context['continue_percent'] = 100;
698
	$context['continue_get_data'] = '?action=admin;area=maintain;sa=database;done=convertentities';
699
	$context['last_step'] = true;
700
	$context['continue_countdown'] = 3;
701
}
702
703
/**
704
 * Optimizes all tables in the database and lists how much was saved.
705
 * It requires the admin_forum permission.
706
 * It shows as the maintain_forum admin area.
707
 * It is accessed from ?action=admin;area=maintain;sa=database;activity=optimize.
708
 * It also updates the optimize scheduled task such that the tables are not automatically optimized again too soon.
709
 *
710
 * @uses template_optimize()
711
 */
712
function OptimizeTables()
713
{
714
	global $db_prefix, $txt, $context, $smcFunc;
715
716
	isAllowedTo('admin_forum');
717
718
	checkSession('request');
719
720
	if (!isset($_SESSION['optimized_tables']))
721
		validateToken('admin-maint');
722
	else
723
		validateToken('admin-optimize', 'post', false);
724
725
	ignore_user_abort(true);
726
	db_extend();
727
728
	$context['page_title'] = $txt['database_optimize'];
729
	$context['sub_template'] = 'optimize';
730
	$context['continue_post_data'] = '';
731
	$context['continue_countdown'] = 3;
732
733
	// Only optimize the tables related to this smf install, not all the tables in the db
734
	$real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
735
736
	// Get a list of tables, as well as how many there are.
737
	$temp_tables = $smcFunc['db_list_tables'](false, $real_prefix . '%');
738
	$tables = array();
739
	foreach ($temp_tables as $table)
740
		$tables[] = array('table_name' => $table);
741
742
	// If there aren't any tables then I believe that would mean the world has exploded...
743
	$context['num_tables'] = count($tables);
744
	if ($context['num_tables'] == 0)
745
		fatal_error('You appear to be running SMF in a flat file mode... fantastic!', false);
746
747
	$_REQUEST['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
748
749
	// Try for extra time due to large tables.
750
	@set_time_limit(100);
751
752
	// For each table....
753
	$_SESSION['optimized_tables'] = !empty($_SESSION['optimized_tables']) ? $_SESSION['optimized_tables'] : array();
754
	for ($key = $_REQUEST['start']; $context['num_tables'] - 1; $key++)
755
	{
756
		if (empty($tables[$key]))
757
			break;
758
759
		// Continue?
760
		if (microtime(true) - TIME_START > 10)
761
		{
762
			$_REQUEST['start'] = $key;
763
			$context['continue_get_data'] = '?action=admin;area=maintain;sa=database;activity=optimize;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
764
			$context['continue_percent'] = round(100 * $_REQUEST['start'] / $context['num_tables']);
765
			$context['sub_template'] = 'not_done';
766
			$context['page_title'] = $txt['not_done_title'];
767
768
			createToken('admin-optimize');
769
			$context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-optimize_token_var'] . '" value="' . $context['admin-optimize_token'] . '">';
770
771
			if (function_exists('apache_reset_timeout'))
772
				apache_reset_timeout();
773
774
			return;
775
		}
776
777
		// Optimize the table!  We use backticks here because it might be a custom table.
778
		$data_freed = $smcFunc['db_optimize_table']($tables[$key]['table_name']);
779
780
		if ($data_freed > 0)
781
			$_SESSION['optimized_tables'][] = array(
782
				'name' => $tables[$key]['table_name'],
783
				'data_freed' => $data_freed,
784
			);
785
	}
786
787
	// Number of tables, etc...
788
	$txt['database_numb_tables'] = sprintf($txt['database_numb_tables'], $context['num_tables']);
789
	$context['num_tables_optimized'] = count($_SESSION['optimized_tables']);
790
	$context['optimized_tables'] = $_SESSION['optimized_tables'];
791
	unset($_SESSION['optimized_tables']);
792
}
793
794
/**
795
 * Recount many forum totals that can be recounted automatically without harm.
796
 * it requires the admin_forum permission.
797
 * It shows the maintain_forum admin area.
798
 *
799
 * Totals recounted:
800
 * - fixes for topics with wrong num_replies.
801
 * - updates for num_posts and num_topics of all boards.
802
 * - recounts instant_messages but not unread_messages.
803
 * - repairs messages pointing to boards with topics pointing to other boards.
804
 * - updates the last message posted in boards and children.
805
 * - updates member count, latest member, topic count, and message count.
806
 *
807
 * The function redirects back to ?action=admin;area=maintain when complete.
808
 * It is accessed via ?action=admin;area=maintain;sa=database;activity=recount.
809
 */
810
function AdminBoardRecount()
811
{
812
	global $txt, $context, $modSettings, $sourcedir, $smcFunc;
813
814
	isAllowedTo('admin_forum');
815
	checkSession('request');
816
817
	// validate the request or the loop
818
	validateToken(!isset($_REQUEST['step']) ? 'admin-maint' : 'admin-boardrecount');
819
	$context['not_done_token'] = 'admin-boardrecount';
820
	createToken($context['not_done_token']);
821
	
822
	$context['page_title'] = $txt['not_done_title'];
823
	$context['continue_post_data'] = '';
824
	$context['continue_countdown'] = 3;
825
	$context['sub_template'] = 'not_done';
826
827
	// Try for as much time as possible.
828
	@set_time_limit(600);
829
830
	// Step the number of topics at a time so things don't time out...
831
	$request = $smcFunc['db_query']('', '
832
		SELECT MAX(id_topic)
833
		FROM {db_prefix}topics',
834
		array(
835
		)
836
	);
837
	list ($max_topics) = $smcFunc['db_fetch_row']($request);
838
	$smcFunc['db_free_result']($request);
839
840
	$increment = min(max(50, ceil($max_topics / 4)), 2000);
841
	if (empty($_REQUEST['start']))
842
		$_REQUEST['start'] = 0;
843
844
	$total_steps = 8;
845
846
	// Get each topic with a wrong reply count and fix it - let's just do some at a time, though.
847
	if (empty($_REQUEST['step']))
848
	{
849
		$_REQUEST['step'] = 0;
850
851
		while ($_REQUEST['start'] < $max_topics)
852
		{
853
			// Recount approved messages
854
			$request = $smcFunc['db_query']('', '
855
				SELECT t.id_topic, MAX(t.num_replies) AS num_replies,
856
					GREATEST(COUNT(ma.id_msg) - 1, 0) AS real_num_replies
857
				FROM {db_prefix}topics AS t
858
					LEFT JOIN {db_prefix}messages AS ma ON (ma.id_topic = t.id_topic AND ma.approved = {int:is_approved})
859
				WHERE t.id_topic > {int:start}
860
					AND t.id_topic <= {int:max_id}
861
				GROUP BY t.id_topic
862
				HAVING GREATEST(COUNT(ma.id_msg) - 1, 0) != MAX(t.num_replies)',
863
				array(
864
					'is_approved' => 1,
865
					'start' => $_REQUEST['start'],
866
					'max_id' => $_REQUEST['start'] + $increment,
867
				)
868
			);
869
			while ($row = $smcFunc['db_fetch_assoc']($request))
870
				$smcFunc['db_query']('', '
871
					UPDATE {db_prefix}topics
872
					SET num_replies = {int:num_replies}
873
					WHERE id_topic = {int:id_topic}',
874
					array(
875
						'num_replies' => $row['real_num_replies'],
876
						'id_topic' => $row['id_topic'],
877
					)
878
				);
879
			$smcFunc['db_free_result']($request);
880
881
			// Recount unapproved messages
882
			$request = $smcFunc['db_query']('', '
883
				SELECT t.id_topic, MAX(t.unapproved_posts) AS unapproved_posts,
884
					COUNT(mu.id_msg) AS real_unapproved_posts
885
				FROM {db_prefix}topics AS t
886
					LEFT JOIN {db_prefix}messages AS mu ON (mu.id_topic = t.id_topic AND mu.approved = {int:not_approved})
887
				WHERE t.id_topic > {int:start}
888
					AND t.id_topic <= {int:max_id}
889
				GROUP BY t.id_topic
890
				HAVING COUNT(mu.id_msg) != MAX(t.unapproved_posts)',
891
				array(
892
					'not_approved' => 0,
893
					'start' => $_REQUEST['start'],
894
					'max_id' => $_REQUEST['start'] + $increment,
895
				)
896
			);
897
			while ($row = $smcFunc['db_fetch_assoc']($request))
898
				$smcFunc['db_query']('', '
899
					UPDATE {db_prefix}topics
900
					SET unapproved_posts = {int:unapproved_posts}
901
					WHERE id_topic = {int:id_topic}',
902
					array(
903
						'unapproved_posts' => $row['real_unapproved_posts'],
904
						'id_topic' => $row['id_topic'],
905
					)
906
				);
907
			$smcFunc['db_free_result']($request);
908
909
			$_REQUEST['start'] += $increment;
910
911
			if (microtime(true) - TIME_START > 3)
912
			{
913
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=0;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
914
				$context['continue_percent'] = round((100 * $_REQUEST['start'] / $max_topics) / $total_steps);
915
916
				return;
917
			}
918
		}
919
920
		$_REQUEST['start'] = 0;
921
	}
922
923
	// Update the post count of each board.
924
	if ($_REQUEST['step'] <= 1)
925
	{
926
		if (empty($_REQUEST['start']))
927
			$smcFunc['db_query']('', '
928
				UPDATE {db_prefix}boards
929
				SET num_posts = {int:num_posts}
930
				WHERE redirect = {string:redirect}',
931
				array(
932
					'num_posts' => 0,
933
					'redirect' => '',
934
				)
935
			);
936
937
		while ($_REQUEST['start'] < $max_topics)
938
		{
939
			$request = $smcFunc['db_query']('', '
940
				SELECT m.id_board, COUNT(*) AS real_num_posts
941
				FROM {db_prefix}messages AS m
942
				WHERE m.id_topic > {int:id_topic_min}
943
					AND m.id_topic <= {int:id_topic_max}
944
					AND m.approved = {int:is_approved}
945
				GROUP BY m.id_board',
946
				array(
947
					'id_topic_min' => $_REQUEST['start'],
948
					'id_topic_max' => $_REQUEST['start'] + $increment,
949
					'is_approved' => 1,
950
				)
951
			);
952
			while ($row = $smcFunc['db_fetch_assoc']($request))
953
				$smcFunc['db_query']('', '
954
					UPDATE {db_prefix}boards
955
					SET num_posts = num_posts + {int:real_num_posts}
956
					WHERE id_board = {int:id_board}',
957
					array(
958
						'id_board' => $row['id_board'],
959
						'real_num_posts' => $row['real_num_posts'],
960
					)
961
				);
962
			$smcFunc['db_free_result']($request);
963
964
			$_REQUEST['start'] += $increment;
965
966
			if (microtime(true) - TIME_START > 3)
967
			{
968
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=1;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
969
				$context['continue_percent'] = round((200 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
970
971
				return;
972
			}
973
		}
974
975
		$_REQUEST['start'] = 0;
976
	}
977
978
	// Update the topic count of each board.
979
	if ($_REQUEST['step'] <= 2)
980
	{
981
		if (empty($_REQUEST['start']))
982
			$smcFunc['db_query']('', '
983
				UPDATE {db_prefix}boards
984
				SET num_topics = {int:num_topics}',
985
				array(
986
					'num_topics' => 0,
987
				)
988
			);
989
990
		while ($_REQUEST['start'] < $max_topics)
991
		{
992
			$request = $smcFunc['db_query']('', '
993
				SELECT t.id_board, COUNT(*) AS real_num_topics
994
				FROM {db_prefix}topics AS t
995
				WHERE t.approved = {int:is_approved}
996
					AND t.id_topic > {int:id_topic_min}
997
					AND t.id_topic <= {int:id_topic_max}
998
				GROUP BY t.id_board',
999
				array(
1000
					'is_approved' => 1,
1001
					'id_topic_min' => $_REQUEST['start'],
1002
					'id_topic_max' => $_REQUEST['start'] + $increment,
1003
				)
1004
			);
1005
			while ($row = $smcFunc['db_fetch_assoc']($request))
1006
				$smcFunc['db_query']('', '
1007
					UPDATE {db_prefix}boards
1008
					SET num_topics = num_topics + {int:real_num_topics}
1009
					WHERE id_board = {int:id_board}',
1010
					array(
1011
						'id_board' => $row['id_board'],
1012
						'real_num_topics' => $row['real_num_topics'],
1013
					)
1014
				);
1015
			$smcFunc['db_free_result']($request);
1016
1017
			$_REQUEST['start'] += $increment;
1018
1019
			if (microtime(true) - TIME_START > 3)
1020
			{
1021
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=2;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1022
				$context['continue_percent'] = round((300 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
1023
1024
				return;
1025
			}
1026
		}
1027
1028
		$_REQUEST['start'] = 0;
1029
	}
1030
1031
	// Update the unapproved post count of each board.
1032
	if ($_REQUEST['step'] <= 3)
1033
	{
1034
		if (empty($_REQUEST['start']))
1035
			$smcFunc['db_query']('', '
1036
				UPDATE {db_prefix}boards
1037
				SET unapproved_posts = {int:unapproved_posts}',
1038
				array(
1039
					'unapproved_posts' => 0,
1040
				)
1041
			);
1042
1043
		while ($_REQUEST['start'] < $max_topics)
1044
		{
1045
			$request = $smcFunc['db_query']('', '
1046
				SELECT m.id_board, COUNT(*) AS real_unapproved_posts
1047
				FROM {db_prefix}messages AS m
1048
				WHERE m.id_topic > {int:id_topic_min}
1049
					AND m.id_topic <= {int:id_topic_max}
1050
					AND m.approved = {int:is_approved}
1051
				GROUP BY m.id_board',
1052
				array(
1053
					'id_topic_min' => $_REQUEST['start'],
1054
					'id_topic_max' => $_REQUEST['start'] + $increment,
1055
					'is_approved' => 0,
1056
				)
1057
			);
1058
			while ($row = $smcFunc['db_fetch_assoc']($request))
1059
				$smcFunc['db_query']('', '
1060
					UPDATE {db_prefix}boards
1061
					SET unapproved_posts = unapproved_posts + {int:unapproved_posts}
1062
					WHERE id_board = {int:id_board}',
1063
					array(
1064
						'id_board' => $row['id_board'],
1065
						'unapproved_posts' => $row['real_unapproved_posts'],
1066
					)
1067
				);
1068
			$smcFunc['db_free_result']($request);
1069
1070
			$_REQUEST['start'] += $increment;
1071
1072
			if (microtime(true) - TIME_START > 3)
1073
			{
1074
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=3;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1075
				$context['continue_percent'] = round((400 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
1076
1077
				return;
1078
			}
1079
		}
1080
1081
		$_REQUEST['start'] = 0;
1082
	}
1083
1084
	// Update the unapproved topic count of each board.
1085
	if ($_REQUEST['step'] <= 4)
1086
	{
1087
		if (empty($_REQUEST['start']))
1088
			$smcFunc['db_query']('', '
1089
				UPDATE {db_prefix}boards
1090
				SET unapproved_topics = {int:unapproved_topics}',
1091
				array(
1092
					'unapproved_topics' => 0,
1093
				)
1094
			);
1095
1096
		while ($_REQUEST['start'] < $max_topics)
1097
		{
1098
			$request = $smcFunc['db_query']('', '
1099
				SELECT t.id_board, COUNT(*) AS real_unapproved_topics
1100
				FROM {db_prefix}topics AS t
1101
				WHERE t.approved = {int:is_approved}
1102
					AND t.id_topic > {int:id_topic_min}
1103
					AND t.id_topic <= {int:id_topic_max}
1104
				GROUP BY t.id_board',
1105
				array(
1106
					'is_approved' => 0,
1107
					'id_topic_min' => $_REQUEST['start'],
1108
					'id_topic_max' => $_REQUEST['start'] + $increment,
1109
				)
1110
			);
1111
			while ($row = $smcFunc['db_fetch_assoc']($request))
1112
				$smcFunc['db_query']('', '
1113
					UPDATE {db_prefix}boards
1114
					SET unapproved_topics = unapproved_topics + {int:real_unapproved_topics}
1115
					WHERE id_board = {int:id_board}',
1116
					array(
1117
						'id_board' => $row['id_board'],
1118
						'real_unapproved_topics' => $row['real_unapproved_topics'],
1119
					)
1120
				);
1121
			$smcFunc['db_free_result']($request);
1122
1123
			$_REQUEST['start'] += $increment;
1124
1125
			if (microtime(true) - TIME_START > 3)
1126
			{
1127
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=4;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1128
				$context['continue_percent'] = round((500 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
1129
1130
				return;
1131
			}
1132
		}
1133
1134
		$_REQUEST['start'] = 0;
1135
	}
1136
1137
	// Get all members with wrong number of personal messages.
1138
	if ($_REQUEST['step'] <= 5)
1139
	{
1140
		$request = $smcFunc['db_query']('', '
1141
			SELECT mem.id_member, COUNT(pmr.id_pm) AS real_num,
1142
				MAX(mem.instant_messages) AS instant_messages
1143
			FROM {db_prefix}members AS mem
1144
				LEFT JOIN {db_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = {int:is_not_deleted})
1145
			GROUP BY mem.id_member
1146
			HAVING COUNT(pmr.id_pm) != MAX(mem.instant_messages)',
1147
			array(
1148
				'is_not_deleted' => 0,
1149
			)
1150
		);
1151
		while ($row = $smcFunc['db_fetch_assoc']($request))
1152
			updateMemberData($row['id_member'], array('instant_messages' => $row['real_num']));
1153
		$smcFunc['db_free_result']($request);
1154
1155
		$request = $smcFunc['db_query']('', '
1156
			SELECT mem.id_member, COUNT(pmr.id_pm) AS real_num,
1157
				MAX(mem.unread_messages) AS unread_messages
1158
			FROM {db_prefix}members AS mem
1159
				LEFT JOIN {db_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = {int:is_not_deleted} AND pmr.is_read = {int:is_not_read})
1160
			GROUP BY mem.id_member
1161
			HAVING COUNT(pmr.id_pm) != MAX(mem.unread_messages)',
1162
			array(
1163
				'is_not_deleted' => 0,
1164
				'is_not_read' => 0,
1165
			)
1166
		);
1167
		while ($row = $smcFunc['db_fetch_assoc']($request))
1168
			updateMemberData($row['id_member'], array('unread_messages' => $row['real_num']));
1169
		$smcFunc['db_free_result']($request);
1170
1171
		if (microtime(true) - TIME_START > 3)
1172
		{
1173
			$context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=0;' . $context['session_var'] . '=' . $context['session_id'];
1174
			$context['continue_percent'] = round(700 / $total_steps);
1175
1176
			return;
1177
		}
1178
	}
1179
1180
	// Any messages pointing to the wrong board?
1181
	if ($_REQUEST['step'] <= 6)
1182
	{
1183
		while ($_REQUEST['start'] < $modSettings['maxMsgID'])
1184
		{
1185
			$request = $smcFunc['db_query']('', '
1186
				SELECT t.id_board, m.id_msg
1187
				FROM {db_prefix}messages AS m
1188
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_board != m.id_board)
1189
				WHERE m.id_msg > {int:id_msg_min}
1190
					AND m.id_msg <= {int:id_msg_max}',
1191
				array(
1192
					'id_msg_min' => $_REQUEST['start'],
1193
					'id_msg_max' => $_REQUEST['start'] + $increment,
1194
				)
1195
			);
1196
			$boards = array();
1197
			while ($row = $smcFunc['db_fetch_assoc']($request))
1198
				$boards[$row['id_board']][] = $row['id_msg'];
1199
1200
			$smcFunc['db_free_result']($request);
1201
1202
			foreach ($boards as $board_id => $messages)
1203
				$smcFunc['db_query']('', '
1204
					UPDATE {db_prefix}messages
1205
					SET id_board = {int:id_board}
1206
					WHERE id_msg IN ({array_int:id_msg_array})',
1207
					array(
1208
						'id_msg_array' => $messages,
1209
						'id_board' => $board_id,
1210
					)
1211
				);
1212
1213
			$_REQUEST['start'] += $increment;
1214
1215
			if (microtime(true) - TIME_START > 3)
1216
			{
1217
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1218
				$context['continue_percent'] = round((700 + 100 * $_REQUEST['start'] / $modSettings['maxMsgID']) / $total_steps);
1219
1220
				return;
1221
			}
1222
		}
1223
1224
		$_REQUEST['start'] = 0;
1225
	}
1226
1227
	// Update the latest message of each board.
1228
	$request = $smcFunc['db_query']('', '
1229
		SELECT m.id_board, MAX(m.id_msg) AS local_last_msg
1230
		FROM {db_prefix}messages AS m
1231
		WHERE m.approved = {int:is_approved}
1232
		GROUP BY m.id_board',
1233
		array(
1234
			'is_approved' => 1,
1235
		)
1236
	);
1237
	$realBoardCounts = array();
1238
	while ($row = $smcFunc['db_fetch_assoc']($request))
1239
		$realBoardCounts[$row['id_board']] = $row['local_last_msg'];
1240
	$smcFunc['db_free_result']($request);
1241
1242
	$request = $smcFunc['db_query']('', '
1243
		SELECT id_board, id_parent, id_last_msg, child_level, id_msg_updated
1244
		FROM {db_prefix}boards',
1245
		array(
1246
		)
1247
	);
1248
	$resort_me = array();
1249
	while ($row = $smcFunc['db_fetch_assoc']($request))
1250
	{
1251
		$row['local_last_msg'] = isset($realBoardCounts[$row['id_board']]) ? $realBoardCounts[$row['id_board']] : 0;
1252
		$resort_me[$row['child_level']][] = $row;
1253
	}
1254
	$smcFunc['db_free_result']($request);
1255
1256
	krsort($resort_me);
1257
1258
	$lastModifiedMsg = array();
1259
	foreach ($resort_me as $rows)
1260
		foreach ($rows as $row)
1261
		{
1262
			// The latest message is the latest of the current board and its children.
1263
			if (isset($lastModifiedMsg[$row['id_board']]))
1264
				$curLastModifiedMsg = max($row['local_last_msg'], $lastModifiedMsg[$row['id_board']]);
1265
			else
1266
				$curLastModifiedMsg = $row['local_last_msg'];
1267
1268
			// If what is and what should be the latest message differ, an update is necessary.
1269
			if ($row['local_last_msg'] != $row['id_last_msg'] || $curLastModifiedMsg != $row['id_msg_updated'])
1270
				$smcFunc['db_query']('', '
1271
					UPDATE {db_prefix}boards
1272
					SET id_last_msg = {int:id_last_msg}, id_msg_updated = {int:id_msg_updated}
1273
					WHERE id_board = {int:id_board}',
1274
					array(
1275
						'id_last_msg' => $row['local_last_msg'],
1276
						'id_msg_updated' => $curLastModifiedMsg,
1277
						'id_board' => $row['id_board'],
1278
					)
1279
				);
1280
1281
			// Parent boards inherit the latest modified message of their children.
1282
			if (isset($lastModifiedMsg[$row['id_parent']]))
1283
				$lastModifiedMsg[$row['id_parent']] = max($row['local_last_msg'], $lastModifiedMsg[$row['id_parent']]);
1284
			else
1285
				$lastModifiedMsg[$row['id_parent']] = $row['local_last_msg'];
1286
		}
1287
1288
	// Update all the basic statistics.
1289
	updateStats('member');
1290
	updateStats('message');
1291
	updateStats('topic');
1292
1293
	// Finally, update the latest event times.
1294
	require_once($sourcedir . '/ScheduledTasks.php');
1295
	CalculateNextTrigger();
1296
1297
	redirectexit('action=admin;area=maintain;sa=routine;done=recount');
1298
}
1299
1300
/**
1301
 * Perform a detailed version check.  A very good thing ;).
1302
 * The function parses the comment headers in all files for their version information,
1303
 * and outputs that for some javascript to check with simplemachines.org.
1304
 * It does not connect directly with simplemachines.org, but rather expects the client to.
1305
 *
1306
 * It requires the admin_forum permission.
1307
 * Uses the view_versions admin area.
1308
 * Accessed through ?action=admin;area=maintain;sa=routine;activity=version.
1309
 *
1310
 * @uses template_view_versions()
1311
 */
1312
function VersionDetail()
1313
{
1314
	global $txt, $sourcedir, $context;
1315
1316
	isAllowedTo('admin_forum');
1317
1318
	// Call the function that'll get all the version info we need.
1319
	require_once($sourcedir . '/Subs-Admin.php');
1320
	$versionOptions = array(
1321
		'include_ssi' => true,
1322
		'include_subscriptions' => true,
1323
		'include_tasks' => true,
1324
		'sort_results' => true,
1325
	);
1326
	$version_info = getFileVersions($versionOptions);
1327
1328
	// Add the new info to the template context.
1329
	$context += array(
1330
		'file_versions' => $version_info['file_versions'],
1331
		'default_template_versions' => $version_info['default_template_versions'],
1332
		'template_versions' => $version_info['template_versions'],
1333
		'default_language_versions' => $version_info['default_language_versions'],
1334
		'default_known_languages' => array_keys($version_info['default_language_versions']),
1335
		'tasks_versions' => $version_info['tasks_versions'],
1336
	);
1337
1338
	// Make it easier to manage for the template.
1339
	$context['forum_version'] = SMF_FULL_VERSION;
1340
1341
	$context['sub_template'] = 'view_versions';
1342
	$context['page_title'] = $txt['admin_version_check'];
1343
}
1344
1345
/**
1346
 * Re-attribute posts.
1347
 */
1348
function MaintainReattributePosts()
1349
{
1350
	global $sourcedir, $context, $txt;
1351
1352
	checkSession();
1353
1354
	// Find the member.
1355
	require_once($sourcedir . '/Subs-Auth.php');
1356
	$members = findMembers($_POST['to']);
1357
1358
	if (empty($members))
1359
		fatal_lang_error('reattribute_cannot_find_member');
1360
1361
	$memID = array_shift($members);
1362
	$memID = $memID['id'];
1363
1364
	$email = $_POST['type'] == 'email' ? $_POST['from_email'] : '';
1365
	$membername = $_POST['type'] == 'name' ? $_POST['from_name'] : '';
1366
1367
	// Now call the reattribute function.
1368
	require_once($sourcedir . '/Subs-Members.php');
1369
	reattributePosts($memID, $email, $membername, !empty($_POST['posts']));
1370
1371
	$context['maintenance_finished'] = $txt['maintain_reattribute_posts'];
1372
}
1373
1374
/**
1375
 * Removing old members. Done and out!
1376
 *
1377
 * @todo refactor
1378
 */
1379
function MaintainPurgeInactiveMembers()
1380
{
1381
	global $sourcedir, $context, $smcFunc, $txt;
1382
1383
	$_POST['maxdays'] = empty($_POST['maxdays']) ? 0 : (int) $_POST['maxdays'];
1384
	if (!empty($_POST['groups']) && $_POST['maxdays'] > 0)
1385
	{
1386
		checkSession();
1387
		validateToken('admin-maint');
1388
1389
		$groups = array();
1390
		foreach ($_POST['groups'] as $id => $dummy)
1391
			$groups[] = (int) $id;
1392
		$time_limit = (time() - ($_POST['maxdays'] * 24 * 3600));
1393
		$where_vars = array(
1394
			'time_limit' => $time_limit,
1395
		);
1396
		if ($_POST['del_type'] == 'activated')
1397
		{
1398
			$where = 'mem.date_registered < {int:time_limit} AND mem.is_activated = {int:is_activated}';
1399
			$where_vars['is_activated'] = 0;
1400
		}
1401
		else
1402
			$where = 'mem.last_login < {int:time_limit} AND (mem.last_login != 0 OR mem.date_registered < {int:time_limit})';
1403
1404
		// Need to get *all* groups then work out which (if any) we avoid.
1405
		$request = $smcFunc['db_query']('', '
1406
			SELECT id_group, group_name, min_posts
1407
			FROM {db_prefix}membergroups',
1408
			array(
1409
			)
1410
		);
1411
		while ($row = $smcFunc['db_fetch_assoc']($request))
1412
		{
1413
			// Avoid this one?
1414
			if (!in_array($row['id_group'], $groups))
1415
			{
1416
				// Post group?
1417
				if ($row['min_posts'] != -1)
1418
				{
1419
					$where .= ' AND mem.id_post_group != {int:id_post_group_' . $row['id_group'] . '}';
1420
					$where_vars['id_post_group_' . $row['id_group']] = $row['id_group'];
1421
				}
1422
				else
1423
				{
1424
					$where .= ' AND mem.id_group != {int:id_group_' . $row['id_group'] . '} AND FIND_IN_SET({int:id_group_' . $row['id_group'] . '}, mem.additional_groups) = 0';
1425
					$where_vars['id_group_' . $row['id_group']] = $row['id_group'];
1426
				}
1427
			}
1428
		}
1429
		$smcFunc['db_free_result']($request);
1430
1431
		// If we have ungrouped unselected we need to avoid those guys.
1432
		if (!in_array(0, $groups))
1433
		{
1434
			$where .= ' AND (mem.id_group != 0 OR mem.additional_groups != {string:blank_add_groups})';
1435
			$where_vars['blank_add_groups'] = '';
1436
		}
1437
1438
		// Select all the members we're about to murder/remove...
1439
		$request = $smcFunc['db_query']('', '
1440
			SELECT mem.id_member, COALESCE(m.id_member, 0) AS is_mod
1441
			FROM {db_prefix}members AS mem
1442
				LEFT JOIN {db_prefix}moderators AS m ON (m.id_member = mem.id_member)
1443
			WHERE ' . $where,
1444
			$where_vars
1445
		);
1446
		$members = array();
1447
		while ($row = $smcFunc['db_fetch_assoc']($request))
1448
		{
1449
			if (!$row['is_mod'] || !in_array(3, $groups))
1450
				$members[] = $row['id_member'];
1451
		}
1452
		$smcFunc['db_free_result']($request);
1453
1454
		require_once($sourcedir . '/Subs-Members.php');
1455
		deleteMembers($members);
1456
	}
1457
1458
	$context['maintenance_finished'] = $txt['maintain_members'];
1459
	createToken('admin-maint');
1460
}
1461
1462
/**
1463
 * Removing old posts doesn't take much as we really pass through.
1464
 */
1465
function MaintainRemoveOldPosts()
1466
{
1467
	global $sourcedir;
1468
1469
	validateToken('admin-maint');
1470
1471
	// Actually do what we're told!
1472
	require_once($sourcedir . '/RemoveTopic.php');
1473
	RemoveOldTopics2();
1474
}
1475
1476
/**
1477
 * Removing old drafts
1478
 */
1479
function MaintainRemoveOldDrafts()
1480
{
1481
	global $sourcedir, $smcFunc;
1482
1483
	validateToken('admin-maint');
1484
1485
	$drafts = array();
1486
1487
	// Find all of the old drafts
1488
	$request = $smcFunc['db_query']('', '
1489
		SELECT id_draft
1490
		FROM {db_prefix}user_drafts
1491
		WHERE poster_time <= {int:poster_time_old}',
1492
		array(
1493
			'poster_time_old' => time() - (86400 * $_POST['draftdays']),
1494
		)
1495
	);
1496
1497
	while ($row = $smcFunc['db_fetch_row']($request))
1498
		$drafts[] = (int) $row[0];
1499
	$smcFunc['db_free_result']($request);
1500
1501
	// If we have old drafts, remove them
1502
	if (count($drafts) > 0)
1503
	{
1504
		require_once($sourcedir . '/Drafts.php');
1505
		DeleteDraft($drafts, false);
1506
	}
1507
}
1508
1509
/**
1510
 * Moves topics from one board to another.
1511
 *
1512
 * @uses template_not_done() to pause the process.
1513
 */
1514
function MaintainMassMoveTopics()
1515
{
1516
	global $smcFunc, $sourcedir, $context, $txt;
1517
1518
	// Only admins.
1519
	isAllowedTo('admin_forum');
1520
1521
	checkSession('request');
1522
	validateToken('admin-maint');
1523
1524
	// Set up to the context.
1525
	$context['page_title'] = $txt['not_done_title'];
1526
	$context['continue_countdown'] = 3;
1527
	$context['continue_post_data'] = '';
1528
	$context['continue_get_data'] = '';
1529
	$context['sub_template'] = 'not_done';
1530
	$context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
1531
	$context['start_time'] = time();
1532
1533
	// First time we do this?
1534
	$id_board_from = isset($_REQUEST['id_board_from']) ? (int) $_REQUEST['id_board_from'] : 0;
1535
	$id_board_to = isset($_REQUEST['id_board_to']) ? (int) $_REQUEST['id_board_to'] : 0;
1536
	$max_days = isset($_REQUEST['maxdays']) ? (int) $_REQUEST['maxdays'] : 0;
1537
	$locked = isset($_POST['move_type_locked']) || isset($_GET['locked']);
1538
	$sticky = isset($_POST['move_type_sticky']) || isset($_GET['sticky']);
1539
1540
	// No boards then this is your stop.
1541
	if (empty($id_board_from) || empty($id_board_to))
1542
		return;
1543
1544
	// The big WHERE clause
1545
	$conditions = 'WHERE t.id_board = {int:id_board_from}
1546
		AND m.icon != {string:moved}';
1547
1548
	// DB parameters
1549
	$params = array(
1550
		'id_board_from' => $id_board_from,
1551
		'moved' => 'moved',
1552
	);
1553
1554
	// Only moving topics not posted in for x days?
1555
	if (!empty($max_days))
1556
	{
1557
		$conditions .= '
1558
			AND m.poster_time < {int:poster_time}';
1559
		$params['poster_time'] = time() - 3600 * 24 * $max_days;
1560
	}
1561
1562
	// Moving locked topics?
1563
	if ($locked)
1564
	{
1565
		$conditions .= '
1566
			AND t.locked = {int:locked}';
1567
		$params['locked'] = 1;
1568
	}
1569
1570
	// What about sticky topics?
1571
	if ($sticky)
1572
	{
1573
		$conditions .= '
1574
			AND t.is_sticky = {int:sticky}';
1575
		$params['sticky'] = 1;
1576
	}
1577
1578
	// How many topics are we converting?
1579
	if (!isset($_REQUEST['totaltopics']))
1580
	{
1581
		$request = $smcFunc['db_query']('', '
1582
			SELECT COUNT(*)
1583
			FROM {db_prefix}topics AS t
1584
				INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)' .
1585
			$conditions,
1586
			$params
1587
		);
1588
		list ($total_topics) = $smcFunc['db_fetch_row']($request);
1589
		$smcFunc['db_free_result']($request);
1590
	}
1591
	else
1592
		$total_topics = (int) $_REQUEST['totaltopics'];
1593
1594
	// Seems like we need this here.
1595
	$context['continue_get_data'] = '?action=admin;area=maintain;sa=topics;activity=massmove;id_board_from=' . $id_board_from . ';id_board_to=' . $id_board_to . ';totaltopics=' . $total_topics . ';max_days=' . $max_days;
1596
1597
	if ($locked)
1598
		$context['continue_get_data'] .= ';locked';
1599
1600
	if ($sticky)
1601
		$context['continue_get_data'] .= ';sticky';
1602
1603
	$context['continue_get_data'] .= ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1604
1605
	// We have topics to move so start the process.
1606
	if (!empty($total_topics))
1607
	{
1608
		while ($context['start'] <= $total_topics)
1609
		{
1610
			// Lets get the topics.
1611
			$request = $smcFunc['db_query']('', '
1612
				SELECT t.id_topic
1613
				FROM {db_prefix}topics AS t
1614
					INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
1615
				' . $conditions . '
1616
				LIMIT 10',
1617
				$params
1618
			);
1619
1620
			// Get the ids.
1621
			$topics = array();
1622
			while ($row = $smcFunc['db_fetch_assoc']($request))
1623
				$topics[] = $row['id_topic'];
1624
1625
			// Just return if we don't have any topics left to move.
1626
			if (empty($topics))
1627
			{
1628
				cache_put_data('board-' . $id_board_from, null, 120);
1629
				cache_put_data('board-' . $id_board_to, null, 120);
1630
				redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
1631
			}
1632
1633
			// Lets move them.
1634
			require_once($sourcedir . '/MoveTopic.php');
1635
			moveTopics($topics, $id_board_to);
1636
1637
			// We've done at least ten more topics.
1638
			$context['start'] += 10;
1639
1640
			// Lets wait a while.
1641
			if (time() - $context['start_time'] > 3)
1642
			{
1643
				// What's the percent?
1644
				$context['continue_percent'] = round(100 * ($context['start'] / $total_topics), 1);
1645
				$context['continue_get_data'] = '?action=admin;area=maintain;sa=topics;activity=massmove;id_board_from=' . $id_board_from . ';id_board_to=' . $id_board_to . ';totaltopics=' . $total_topics . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1646
1647
				// Let the template system do it's thang.
1648
				return;
1649
			}
1650
		}
1651
	}
1652
1653
	// Don't confuse admins by having an out of date cache.
1654
	cache_put_data('board-' . $id_board_from, null, 120);
1655
	cache_put_data('board-' . $id_board_to, null, 120);
1656
1657
	redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
1658
}
1659
1660
/**
1661
 * Recalculate all members post counts
1662
 * it requires the admin_forum permission.
1663
 *
1664
 * - recounts all posts for members found in the message table
1665
 * - updates the members post count record in the members table
1666
 * - honors the boards post count flag
1667
 * - does not count posts in the recycle bin
1668
 * - zeros post counts for all members with no posts in the message table
1669
 * - runs as a delayed loop to avoid server overload
1670
 * - uses the not_done template in Admin.template
1671
 *
1672
 * The function redirects back to action=admin;area=maintain;sa=members when complete.
1673
 * It is accessed via ?action=admin;area=maintain;sa=members;activity=recountposts
1674
 */
1675
function MaintainRecountPosts()
1676
{
1677
	global $txt, $context, $modSettings, $smcFunc;
1678
1679
	// You have to be allowed in here
1680
	isAllowedTo('admin_forum');
1681
	checkSession('request');
1682
1683
	// Set up to the context.
1684
	$context['page_title'] = $txt['not_done_title'];
1685
	$context['continue_countdown'] = 3;
1686
	$context['continue_get_data'] = '';
1687
	$context['sub_template'] = 'not_done';
1688
1689
	// init
1690
	$increment = 200;
1691
	$_REQUEST['start'] = !isset($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
1692
1693
	// Ask for some extra time, on big boards this may take a bit
1694
	@set_time_limit(600);
1695
1696
	// Only run this query if we don't have the total number of members that have posted
1697
	if (!isset($_SESSION['total_members']))
1698
	{
1699
		validateToken('admin-maint');
1700
1701
		$request = $smcFunc['db_query']('', '
1702
			SELECT COUNT(DISTINCT m.id_member)
1703
			FROM {db_prefix}messages AS m
1704
			JOIN {db_prefix}boards AS b on m.id_board = b.id_board
1705
			WHERE m.id_member != 0
1706
				AND b.count_posts = 0',
1707
			array(
1708
			)
1709
		);
1710
1711
		// save it so we don't do this again for this task
1712
		list ($_SESSION['total_members']) = $smcFunc['db_fetch_row']($request);
1713
		$smcFunc['db_free_result']($request);
1714
	}
1715
	else
1716
		validateToken('admin-recountposts');
1717
1718
	// Lets get a group of members and determine their post count (from the boards that have post count enabled of course).
1719
	$request = $smcFunc['db_query']('', '
1720
		SELECT m.id_member, COUNT(*) AS posts
1721
		FROM {db_prefix}messages AS m
1722
			INNER JOIN {db_prefix}boards AS b ON m.id_board = b.id_board
1723
		WHERE m.id_member != {int:zero}
1724
			AND b.count_posts = {int:zero}
1725
			' . (!empty($modSettings['recycle_enable']) ? ' AND b.id_board != {int:recycle}' : '') . '
1726
		GROUP BY m.id_member
1727
		LIMIT {int:start}, {int:number}',
1728
		array(
1729
			'start' => $_REQUEST['start'],
1730
			'number' => $increment,
1731
			'recycle' => $modSettings['recycle_board'],
1732
			'zero' => 0,
1733
		)
1734
	);
1735
	$total_rows = $smcFunc['db_num_rows']($request);
1736
1737
	// Update the post count for this group
1738
	while ($row = $smcFunc['db_fetch_assoc']($request))
1739
	{
1740
		$smcFunc['db_query']('', '
1741
			UPDATE {db_prefix}members
1742
			SET posts = {int:posts}
1743
			WHERE id_member = {int:row}',
1744
			array(
1745
				'row' => $row['id_member'],
1746
				'posts' => $row['posts'],
1747
			)
1748
		);
1749
	}
1750
	$smcFunc['db_free_result']($request);
1751
1752
	// Continue?
1753
	if ($total_rows == $increment)
1754
	{
1755
		$_REQUEST['start'] += $increment;
1756
		$context['continue_get_data'] = '?action=admin;area=maintain;sa=members;activity=recountposts;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
1757
		$context['continue_percent'] = round(100 * $_REQUEST['start'] / $_SESSION['total_members']);
1758
1759
		createToken('admin-recountposts');
1760
		$context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-recountposts_token_var'] . '" value="' . $context['admin-recountposts_token'] . '">';
1761
1762
		if (function_exists('apache_reset_timeout'))
1763
			apache_reset_timeout();
1764
		return;
1765
	}
1766
1767
	// final steps ... made more difficult since we don't yet support sub-selects on joins
1768
	// place all members who have posts in the message table in a temp table
1769
	$createTemporary = $smcFunc['db_query']('', '
1770
		CREATE TEMPORARY TABLE {db_prefix}tmp_maint_recountposts (
1771
			id_member mediumint(8) unsigned NOT NULL default {string:string_zero},
1772
			PRIMARY KEY (id_member)
1773
		)
1774
		SELECT m.id_member
1775
		FROM {db_prefix}messages AS m
1776
			INNER JOIN {db_prefix}boards AS b ON m.id_board = b.id_board
1777
		WHERE m.id_member != {int:zero}
1778
			AND b.count_posts = {int:zero}
1779
			' . (!empty($modSettings['recycle_enable']) ? ' AND b.id_board != {int:recycle}' : '') . '
1780
		GROUP BY m.id_member',
1781
		array(
1782
			'zero' => 0,
1783
			'string_zero' => '0',
1784
			'db_error_skip' => true,
1785
			'recycle' => !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0,
1786
		)
1787
	) !== false;
1788
1789
	if ($createTemporary)
1790
	{
1791
		// outer join the members table on the temporary table finding the members that have a post count but no posts in the message table
1792
		$request = $smcFunc['db_query']('', '
1793
			SELECT mem.id_member, mem.posts
1794
			FROM {db_prefix}members AS mem
1795
				LEFT OUTER JOIN {db_prefix}tmp_maint_recountposts AS res
1796
				ON res.id_member = mem.id_member
1797
			WHERE res.id_member IS null
1798
				AND mem.posts != {int:zero}',
1799
			array(
1800
				'zero' => 0,
1801
			)
1802
		);
1803
1804
		// set the post count to zero for any delinquents we may have found
1805
		while ($row = $smcFunc['db_fetch_assoc']($request))
1806
		{
1807
			$smcFunc['db_query']('', '
1808
				UPDATE {db_prefix}members
1809
				SET posts = {int:zero}
1810
				WHERE id_member = {int:row}',
1811
				array(
1812
					'row' => $row['id_member'],
1813
					'zero' => 0,
1814
				)
1815
			);
1816
		}
1817
		$smcFunc['db_free_result']($request);
1818
	}
1819
1820
	// all done
1821
	unset($_SESSION['total_members']);
1822
	$context['maintenance_finished'] = $txt['maintain_recountposts'];
1823
	redirectexit('action=admin;area=maintain;sa=members;done=recountposts');
1824
}
1825
1826
function RebuildSettingsFile()
1827
{
1828
	global $sourcedir;
1829
1830
	isAllowedTo('admin_forum');
1831
1832
	require_once($sourcedir . '/Subs-Admin.php');
1833
	updateSettingsFile(array(), false, true);
1834
1835
	redirectexit('action=admin;area=maintain;sa=routine;done=rebuild_settings');
1836
}
1837
1838
/**
1839
 * Generates a list of integration hooks for display
1840
 * Accessed through ?action=admin;area=maintain;sa=hooks;
1841
 * Allows for removal or disabling of selected hooks
1842
 */
1843
function list_integration_hooks()
1844
{
1845
	global $boarddir, $sourcedir, $scripturl, $context, $txt;
1846
1847
	$filter_url = '';
1848
	$current_filter = '';
1849
	$hooks = get_integration_hooks();
1850
	$hooks_filters = array();
1851
1852
	if (isset($_GET['filter'], $hooks[$_GET['filter']]))
1853
	{
1854
		$filter_url = ';filter=' . $_GET['filter'];
1855
		$current_filter = $_GET['filter'];
1856
	}
1857
	$filtered_hooks = array_filter(
1858
		$hooks,
1859
		function($hook) use ($current_filter)
1860
		{
1861
			return $current_filter == '' || $current_filter == $hook;
1862
		},
1863
		ARRAY_FILTER_USE_KEY
1864
	);
1865
	ksort($hooks);
1866
1867
	foreach ($hooks as $hook => $functions)
1868
		$hooks_filters[] = '<option' . ($current_filter == $hook ? ' selected ' : '') . ' value="' . $hook . '">' . $hook . '</option>';
1869
1870
	if (!empty($hooks_filters))
1871
		$context['insert_after_template'] .= '
1872
		<script>
1873
			var hook_name_header = document.getElementById(\'header_list_integration_hooks_hook_name\');
1874
			hook_name_header.innerHTML += ' . JavaScriptEscape('<select style="margin-left:15px;" onchange="window.location=(\'' . $scripturl . '?action=admin;area=maintain;sa=hooks\' + (this.value ? \';filter=\' + this.value : \'\'));"><option value="">' . $txt['hooks_reset_filter'] . '</option>' . implode('', $hooks_filters) . '</select>') . ';
1875
		</script>';
1876
1877
	if (!empty($_REQUEST['do']) && isset($_REQUEST['hook']) && isset($_REQUEST['function']))
1878
	{
1879
		checkSession('request');
1880
		validateToken('admin-hook', 'request');
1881
1882
		if ($_REQUEST['do'] == 'remove')
1883
			remove_integration_function($_REQUEST['hook'], urldecode($_REQUEST['function']));
1884
1885
		else
1886
		{
1887
			$function_remove = urldecode($_REQUEST['function']) . (($_REQUEST['do'] == 'disable') ? '' : '!');
1888
			$function_add = urldecode($_REQUEST['function']) . (($_REQUEST['do'] == 'disable') ? '!' : '');
1889
1890
			remove_integration_function($_REQUEST['hook'], $function_remove);
1891
			add_integration_function($_REQUEST['hook'], $function_add);
1892
		}
1893
1894
		redirectexit('action=admin;area=maintain;sa=hooks' . $filter_url);
1895
	}
1896
1897
	createToken('admin-hook', 'request');
1898
1899
	$list_options = array(
1900
		'id' => 'list_integration_hooks',
1901
		'title' => $txt['hooks_title_list'],
1902
		'items_per_page' => 20,
1903
		'base_href' => $scripturl . '?action=admin;area=maintain;sa=hooks' . $filter_url . ';' . $context['session_var'] . '=' . $context['session_id'],
1904
		'default_sort_col' => 'hook_name',
1905
		'get_items' => array(
1906
			'function' => 'get_integration_hooks_data',
1907
			'params' => array(
1908
				$filtered_hooks,
1909
				strtr($boarddir, '\\', '/'),
1910
				strtr($sourcedir, '\\', '/'),
1911
			),
1912
		),
1913
		'get_count' => array(
1914
			'value' => array_reduce(
1915
				$filtered_hooks,
1916
				function($accumulator, $functions)
1917
				{
1918
					return $accumulator + count($functions);
1919
				},
1920
				0
1921
			),
1922
		),
1923
		'no_items_label' => $txt['hooks_no_hooks'],
1924
		'columns' => array(
1925
			'hook_name' => array(
1926
				'header' => array(
1927
					'value' => $txt['hooks_field_hook_name'],
1928
				),
1929
				'data' => array(
1930
					'db' => 'hook_name',
1931
				),
1932
				'sort' => array(
1933
					'default' => 'hook_name',
1934
					'reverse' => 'hook_name DESC',
1935
				),
1936
			),
1937
			'function_name' => array(
1938
				'header' => array(
1939
					'value' => $txt['hooks_field_function_name'],
1940
				),
1941
				'data' => array(
1942
					'function' => function($data) use ($txt)
1943
					{
1944
						// Show a nice icon to indicate this is an instance.
1945
						$instance = (!empty($data['instance']) ? '<span class="main_icons news" title="' . $txt['hooks_field_function_method'] . '"></span> ' : '');
1946
1947
						if (!empty($data['included_file']) && !empty($data['real_function']))
1948
							return $instance . $txt['hooks_field_function'] . ': ' . $data['real_function'] . '<br>' . $txt['hooks_field_included_file'] . ': ' . $data['included_file'];
1949
1950
						else
1951
							return $instance . $data['real_function'];
1952
					},
1953
				),
1954
				'sort' => array(
1955
					'default' => 'function_name',
1956
					'reverse' => 'function_name DESC',
1957
				),
1958
			),
1959
			'file_name' => array(
1960
				'header' => array(
1961
					'value' => $txt['hooks_field_file_name'],
1962
				),
1963
				'data' => array(
1964
					'db' => 'file_name',
1965
				),
1966
				'sort' => array(
1967
					'default' => 'file_name',
1968
					'reverse' => 'file_name DESC',
1969
				),
1970
			),
1971
			'status' => array(
1972
				'header' => array(
1973
					'value' => $txt['hooks_field_hook_exists'],
1974
					'style' => 'width:3%;',
1975
				),
1976
				'data' => array(
1977
					'function' => function($data) use ($txt, $scripturl, $context, $filter_url)
1978
					{
1979
						$change_status = array('before' => '', 'after' => '');
1980
1981
						if ($data['can_disable'])
1982
						{
1983
							$change_status['before'] = '<a href="' . $scripturl . '?action=admin;area=maintain;sa=hooks;do=' . ($data['enabled'] ? 'disable' : 'enable') . ';hook=' . $data['hook_name'] . ';function=' . urlencode($data['real_function']) . $filter_url . ';' . $context['admin-hook_token_var'] . '=' . $context['admin-hook_token'] . ';' . $context['session_var'] . '=' . $context['session_id'] . '" data-confirm="' . $txt['quickmod_confirm'] . '" class="you_sure">';
1984
							$change_status['after'] = '</a>';
1985
						}
1986
1987
						return $change_status['before'] . '<span class="main_icons post_moderation_' . $data['status'] . '" title="' . $data['img_text'] . '"></span>' . $change_status['after'];
1988
					},
1989
					'class' => 'centertext',
1990
				),
1991
				'sort' => array(
1992
					'default' => 'status',
1993
					'reverse' => 'status DESC',
1994
				),
1995
			),
1996
		),
1997
		'additional_rows' => array(
1998
			array(
1999
				'position' => 'after_title',
2000
				'value' => $txt['hooks_disable_instructions'] . '<br>
2001
					' . $txt['hooks_disable_legend'] . ':
2002
				<ul style="list-style: none;">
2003
					<li><span class="main_icons post_moderation_allow"></span> ' . $txt['hooks_disable_legend_exists'] . '</li>
2004
					<li><span class="main_icons post_moderation_moderate"></span> ' . $txt['hooks_disable_legend_disabled'] . '</li>
2005
					<li><span class="main_icons post_moderation_deny"></span> ' . $txt['hooks_disable_legend_missing'] . '</li>
2006
				</ul>'
2007
			),
2008
		),
2009
	);
2010
2011
	$list_options['columns']['remove'] = array(
2012
		'header' => array(
2013
			'value' => $txt['hooks_button_remove'],
2014
			'style' => 'width:3%',
2015
		),
2016
		'data' => array(
2017
			'function' => function($data) use ($txt, $scripturl, $context, $filter_url)
2018
			{
2019
				if (!$data['hook_exists'])
2020
					return '
2021
					<a href="' . $scripturl . '?action=admin;area=maintain;sa=hooks;do=remove;hook=' . $data['hook_name'] . ';function=' . urlencode($data['function_name']) . $filter_url . ';' . $context['admin-hook_token_var'] . '=' . $context['admin-hook_token'] . ';' . $context['session_var'] . '=' . $context['session_id'] . '" data-confirm="' . $txt['quickmod_confirm'] . '" class="you_sure">
2022
						<span class="main_icons delete" title="' . $txt['hooks_button_remove'] . '"></span>
2023
					</a>';
2024
			},
2025
			'class' => 'centertext',
2026
		),
2027
	);
2028
	$list_options['form'] = array(
2029
		'href' => $scripturl . '?action=admin;area=maintain;sa=hooks' . $filter_url . ';' . $context['session_var'] . '=' . $context['session_id'],
2030
		'name' => 'list_integration_hooks',
2031
	);
2032
2033
	require_once($sourcedir . '/Subs-List.php');
2034
	createList($list_options);
2035
2036
	$context['page_title'] = $txt['hooks_title_list'];
2037
	$context['sub_template'] = 'show_list';
2038
	$context['default_list'] = 'list_integration_hooks';
2039
}
2040
2041
/**
2042
 * Gets all of the files in a directory and its children directories
2043
 *
2044
 * @param string $dirname The path to the directory
2045
 * @return array An array containing information about the files found in the specified directory and its children
2046
 */
2047
function get_files_recursive(string $dirname): array
2048
{
2049
	return iterator_to_array(
2050
		new RecursiveIteratorIterator(
2051
			new RecursiveCallbackFilterIterator(
2052
				new RecursiveDirectoryIterator($dirname, FilesystemIterator::UNIX_PATHS),
2053
				function ($fileInfo, $currentFile, $iterator)
2054
				{
2055
					// Allow recursion
2056
					if ($iterator->hasChildren())
2057
						return true;
2058
					return $fileInfo->getExtension() == 'php';
2059
				}
2060
			)
2061
		)
2062
	);
2063
}
2064
2065
/**
2066
 * Callback function for the integration hooks list (list_integration_hooks)
2067
 * Gets all of the hooks in the system and their status
2068
 *
2069
 * @param int $start The item to start with (for pagination purposes)
2070
 * @param int $per_page How many items to display on each page
2071
 * @param string $sort A string indicating how to sort things
2072
 * @return array An array of information about the integration hooks
2073
 */
2074
function get_integration_hooks_data($start, $per_page, $sort, $filtered_hooks, $normalized_boarddir, $normalized_sourcedir)
2075
{
2076
	global $settings, $txt, $context, $scripturl;
2077
2078
	$function_list = $sort_array = $temp_data = array();
2079
	$files = get_files_recursive($normalized_sourcedir);
2080
	foreach ($files as $currentFile => $fileInfo)
2081
		$function_list += get_defined_functions_in_file($currentFile);
2082
2083
	$sort_types = array(
2084
		'hook_name' => array('hook_name', SORT_ASC),
2085
		'hook_name DESC' => array('hook_name', SORT_DESC),
2086
		'function_name' => array('function_name', SORT_ASC),
2087
		'function_name DESC' => array('function_name', SORT_DESC),
2088
		'file_name' => array('file_name', SORT_ASC),
2089
		'file_name DESC' => array('file_name', SORT_DESC),
2090
		'status' => array('status', SORT_ASC),
2091
		'status DESC' => array('status', SORT_DESC),
2092
	);
2093
2094
	foreach ($filtered_hooks as $hook => $functions)
2095
		foreach ($functions as $rawFunc)
2096
		{
2097
			$hookParsedData = parse_integration_hook($hook, $rawFunc);
2098
2099
			// Handle hooks pointing outside the sources directory.
2100
			if ($hookParsedData['absPath'] != '' && !isset($files[$hookParsedData['absPath']]) && file_exists($hookParsedData['absPath']))
2101
				$function_list += get_defined_functions_in_file($hookParsedData['absPath']);
2102
2103
			$hook_exists = isset($function_list[$hookParsedData['call']]) || (substr($hook, -8) === '_include' && isset($files[$hookParsedData['absPath']]));
2104
			$temp = array(
2105
				'hook_name' => $hook,
2106
				'function_name' => $hookParsedData['rawData'],
2107
				'real_function' => $hookParsedData['call'],
2108
				'included_file' => $hookParsedData['hookFile'],
2109
				'file_name' => strtr($hookParsedData['absPath'] ?: ($function_list[$hookParsedData['call']] ?? ''), [$normalized_boarddir => '.']),
2110
				'instance' => $hookParsedData['object'],
2111
				'hook_exists' => $hook_exists,
2112
				'status' => $hook_exists ? ($hookParsedData['enabled'] ? 'allow' : 'moderate') : 'deny',
2113
				'img_text' => $txt['hooks_' . ($hook_exists ? ($hookParsedData['enabled'] ? 'active' : 'disabled') : 'missing')],
2114
				'enabled' => $hookParsedData['enabled'],
2115
				'can_disable' => $hookParsedData['call'] != '',
2116
			);
2117
			$sort_array[] = $temp[$sort_types[$sort][0]];
2118
			$temp_data[] = $temp;
2119
		}
2120
2121
	array_multisort($sort_array, $sort_types[$sort][1], $temp_data);
2122
2123
	return array_slice($temp_data, $start, $per_page, true);
2124
}
2125
2126
/**
2127
 * Parses modSettings to create integration hook array
2128
 *
2129
 * @return array An array of information about the integration hooks
2130
 */
2131
function get_integration_hooks()
2132
{
2133
	global $modSettings;
2134
	static $integration_hooks;
2135
2136
	if (!isset($integration_hooks))
2137
	{
2138
		$integration_hooks = array();
2139
		foreach ($modSettings as $key => $value)
2140
		{
2141
			if (!empty($value) && substr($key, 0, 10) === 'integrate_')
2142
				$integration_hooks[$key] = explode(',', $value);
2143
		}
2144
	}
2145
2146
	return $integration_hooks;
2147
}
2148
2149
/**
2150
 * Parses each hook data and returns an array.
2151
 *
2152
 * @param string $hook
2153
 * @param string $rawData A string as it was saved to the DB.
2154
 * @return array everything found in the string itself
2155
 */
2156
function parse_integration_hook(string $hook, string $rawData)
2157
{
2158
	global $boarddir, $settings, $sourcedir;
2159
2160
	// A single string can hold tons of info!
2161
	$hookData = array(
2162
		'object' => false,
2163
		'enabled' => true,
2164
		'absPath' => '',
2165
		'hookFile' => '',
2166
		'pureFunc' => '',
2167
		'method' => '',
2168
		'class' => '',
2169
		'call' => '',
2170
		'rawData' => $rawData,
2171
	);
2172
2173
	// Meh...
2174
	if (empty($rawData))
2175
		return $hookData;
2176
2177
	$modFunc = $rawData;
2178
2179
	// Any files?
2180
	if (substr($hook, -8) === '_include')
2181
		$modFunc = $modFunc . '|';
2182
	if (strpos($modFunc, '|') !== false)
2183
	{
2184
		list ($hookData['hookFile'], $modFunc) = explode('|', $modFunc);
2185
		$hookData['absPath'] = strtr(strtr(trim($hookData['hookFile']), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir'] ?? '')), '\\', '/');
2186
	}
2187
2188
	// Hook is an instance.
2189
	if (strpos($modFunc, '#') !== false)
2190
	{
2191
		$modFunc = str_replace('#', '', $modFunc);
2192
		$hookData['object'] = true;
2193
	}
2194
2195
	// Hook is "disabled"
2196
	if (strpos($modFunc, '!') !== false)
2197
	{
2198
		$modFunc = str_replace('!', '', $modFunc);
2199
		$hookData['enabled'] = false;
2200
	}
2201
2202
	// Handling methods?
2203
	if (strpos($modFunc, '::') !== false)
2204
	{
2205
		list ($hookData['class'], $hookData['method']) = explode('::', $modFunc);
2206
		$hookData['pureFunc'] = $hookData['method'];
2207
		$hookData['call'] = $modFunc;
2208
	}
2209
2210
	else
2211
		$hookData['call'] = $hookData['pureFunc'] = $modFunc;
2212
2213
	return $hookData;
2214
}
2215
2216
function get_defined_functions_in_file(string $file): array
2217
{
2218
	$source = file_get_contents($file);
2219
	// token_get_all() is too slow so use a nice little regex instead.
2220
	preg_match_all('/\bnamespace\s++((?P>label)(?:\\\(?P>label))*+)\s*+;|\bclass\s++((?P>label))[\w\s]*+{|\bfunction\s++((?P>label))\s*+\(.*\)[:\|\w\s]*+{(?(DEFINE)(?<label>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*+))/i', $source, $matches, PREG_SET_ORDER);
2221
2222
	$functions = array();
2223
	$namespace = '';
2224
	$class = '';
2225
2226
	foreach ($matches as $match)
2227
	{
2228
		if (!empty($match[1]))
2229
			$namespace = $match[1] . '\\';
2230
		elseif (!empty($match[2]))
2231
			$class = $namespace . $match[2] . '::';
2232
		elseif (!empty($match[3]))
2233
			$functions[$class . $match[3]] = $file;
2234
	}
2235
2236
	return $functions;
2237
}
2238
2239
/**
2240
 * Converts html entities to utf8 equivalents
2241
 * special db wrapper for mysql based on the limitation of mysql/mb3
2242
 *
2243
 * Callback function for preg_replace_callback
2244
 * Uses capture group 1 in the supplied array
2245
 * Does basic checks to keep characters inside a viewable range.
2246
 *
2247
 * @param array $matches An array of matches (relevant info should be the 2nd item in the array)
2248
 * @return string The fixed string or return the old when limitation of mysql is hit
2249
 */
2250
function fixchardb__callback($matches)
2251
{
2252
	global $smcFunc;
2253
	if (!isset($matches[1]))
2254
		return '';
2255
2256
	$num = $matches[1][0] === 'x' ? hexdec(substr($matches[1], 1)) : (int) $matches[1];
0 ignored issues
show
$matches[1] of type array is incompatible with the type string expected by parameter $string of substr(). ( Ignorable by Annotation )

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

2256
	$num = $matches[1][0] === 'x' ? hexdec(substr(/** @scrutinizer ignore-type */ $matches[1], 1)) : (int) $matches[1];
Loading history...
2257
2258
	// it's to big for mb3?
2259
	if ($num > 0xFFFF && !$smcFunc['db_mb4'])
2260
		return $matches[0];
2261
	else
2262
		return fixchar__callback($matches);
2263
}
2264
2265
?>