Failed Conditions
Push — release-2.1 ( 8d1977...8da17b )
by Rick
06:19
created

Sources/Subs.php (6 issues)

1
<?php
2
3
/**
4
 * This file has all the main functions in it that relate to, well, everything.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2020 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Update some basic statistics.
21
 *
22
 * 'member' statistic updates the latest member, the total member
23
 *  count, and the number of unapproved members.
24
 * 'member' also only counts approved members when approval is on, but
25
 *  is much more efficient with it off.
26
 *
27
 * 'message' changes the total number of messages, and the
28
 *  highest message id by id_msg - which can be parameters 1 and 2,
29
 *  respectively.
30
 *
31
 * 'topic' updates the total number of topics, or if parameter1 is true
32
 *  simply increments them.
33
 *
34
 * 'subject' updates the log_search_subjects in the event of a topic being
35
 *  moved, removed or split.  parameter1 is the topicid, parameter2 is the new subject
36
 *
37
 * 'postgroups' case updates those members who match condition's
38
 *  post-based membergroups in the database (restricted by parameter1).
39
 *
40
 * @param string $type Stat type - can be 'member', 'message', 'topic', 'subject' or 'postgroups'
41
 * @param mixed $parameter1 A parameter for updating the stats
42
 * @param mixed $parameter2 A 2nd parameter for updating the stats
43
 */
44
function updateStats($type, $parameter1 = null, $parameter2 = null)
45
{
46
	global $modSettings, $smcFunc;
47
48
	switch ($type)
49
	{
50
		case 'member':
51
			$changes = array(
52
				'memberlist_updated' => time(),
53
			);
54
55
			// #1 latest member ID, #2 the real name for a new registration.
56
			if (is_numeric($parameter1))
57
			{
58
				$changes['latestMember'] = $parameter1;
59
				$changes['latestRealName'] = $parameter2;
60
61
				updateSettings(array('totalMembers' => true), true);
62
			}
63
64
			// We need to calculate the totals.
65
			else
66
			{
67
				// Update the latest activated member (highest id_member) and count.
68
				$result = $smcFunc['db_query']('', '
69
					SELECT COUNT(*), MAX(id_member)
70
					FROM {db_prefix}members
71
					WHERE is_activated = {int:is_activated}',
72
					array(
73
						'is_activated' => 1,
74
					)
75
				);
76
				list ($changes['totalMembers'], $changes['latestMember']) = $smcFunc['db_fetch_row']($result);
77
				$smcFunc['db_free_result']($result);
78
79
				// Get the latest activated member's display name.
80
				$result = $smcFunc['db_query']('', '
81
					SELECT real_name
82
					FROM {db_prefix}members
83
					WHERE id_member = {int:id_member}
84
					LIMIT 1',
85
					array(
86
						'id_member' => (int) $changes['latestMember'],
87
					)
88
				);
89
				list ($changes['latestRealName']) = $smcFunc['db_fetch_row']($result);
90
				$smcFunc['db_free_result']($result);
91
92
				// Update the amount of members awaiting approval
93
				$result = $smcFunc['db_query']('', '
94
					SELECT COUNT(*)
95
					FROM {db_prefix}members
96
					WHERE is_activated IN ({array_int:activation_status})',
97
					array(
98
						'activation_status' => array(3, 4, 5),
99
					)
100
				);
101
102
				list ($changes['unapprovedMembers']) = $smcFunc['db_fetch_row']($result);
103
				$smcFunc['db_free_result']($result);
104
			}
105
			updateSettings($changes);
106
			break;
107
108
		case 'message':
109
			if ($parameter1 === true && $parameter2 !== null)
110
				updateSettings(array('totalMessages' => true, 'maxMsgID' => $parameter2), true);
111
			else
112
			{
113
				// SUM and MAX on a smaller table is better for InnoDB tables.
114
				$result = $smcFunc['db_query']('', '
115
					SELECT SUM(num_posts + unapproved_posts) AS total_messages, MAX(id_last_msg) AS max_msg_id
116
					FROM {db_prefix}boards
117
					WHERE redirect = {string:blank_redirect}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
118
						AND id_board != {int:recycle_board}' : ''),
119
					array(
120
						'recycle_board' => isset($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0,
121
						'blank_redirect' => '',
122
					)
123
				);
124
				$row = $smcFunc['db_fetch_assoc']($result);
125
				$smcFunc['db_free_result']($result);
126
127
				updateSettings(array(
128
					'totalMessages' => $row['total_messages'] === null ? 0 : $row['total_messages'],
129
					'maxMsgID' => $row['max_msg_id'] === null ? 0 : $row['max_msg_id']
130
				));
131
			}
132
			break;
133
134
		case 'subject':
135
			// Remove the previous subject (if any).
136
			$smcFunc['db_query']('', '
137
				DELETE FROM {db_prefix}log_search_subjects
138
				WHERE id_topic = {int:id_topic}',
139
				array(
140
					'id_topic' => (int) $parameter1,
141
				)
142
			);
143
144
			// Insert the new subject.
145
			if ($parameter2 !== null)
146
			{
147
				$parameter1 = (int) $parameter1;
148
				$parameter2 = text2words($parameter2);
149
150
				$inserts = array();
151
				foreach ($parameter2 as $word)
152
					$inserts[] = array($word, $parameter1);
153
154
				if (!empty($inserts))
155
					$smcFunc['db_insert']('ignore',
156
						'{db_prefix}log_search_subjects',
157
						array('word' => 'string', 'id_topic' => 'int'),
158
						$inserts,
159
						array('word', 'id_topic')
160
					);
161
			}
162
			break;
163
164
		case 'topic':
165
			if ($parameter1 === true)
166
				updateSettings(array('totalTopics' => true), true);
167
168
			else
169
			{
170
				// Get the number of topics - a SUM is better for InnoDB tables.
171
				// We also ignore the recycle bin here because there will probably be a bunch of one-post topics there.
172
				$result = $smcFunc['db_query']('', '
173
					SELECT SUM(num_topics + unapproved_topics) AS total_topics
174
					FROM {db_prefix}boards' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
175
					WHERE id_board != {int:recycle_board}' : ''),
176
					array(
177
						'recycle_board' => !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0,
178
					)
179
				);
180
				$row = $smcFunc['db_fetch_assoc']($result);
181
				$smcFunc['db_free_result']($result);
182
183
				updateSettings(array('totalTopics' => $row['total_topics'] === null ? 0 : $row['total_topics']));
184
			}
185
			break;
186
187
		case 'postgroups':
188
			// Parameter two is the updated columns: we should check to see if we base groups off any of these.
189
			if ($parameter2 !== null && !in_array('posts', $parameter2))
190
				return;
191
192
			$postgroups = cache_get_data('updateStats:postgroups', 360);
193
			if ($postgroups == null || $parameter1 == null)
0 ignored issues
show
It seems like you are loosely comparing $postgroups of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
194
			{
195
				// Fetch the postgroups!
196
				$request = $smcFunc['db_query']('', '
197
					SELECT id_group, min_posts
198
					FROM {db_prefix}membergroups
199
					WHERE min_posts != {int:min_posts}',
200
					array(
201
						'min_posts' => -1,
202
					)
203
				);
204
				$postgroups = array();
205
				while ($row = $smcFunc['db_fetch_assoc']($request))
206
					$postgroups[$row['id_group']] = $row['min_posts'];
207
208
				$smcFunc['db_free_result']($request);
209
210
				// Sort them this way because if it's done with MySQL it causes a filesort :(.
211
				arsort($postgroups);
212
213
				cache_put_data('updateStats:postgroups', $postgroups, 360);
214
			}
215
216
			// Oh great, they've screwed their post groups.
217
			if (empty($postgroups))
218
				return;
219
220
			// Set all membergroups from most posts to least posts.
221
			$conditions = '';
222
			$lastMin = 0;
223
			foreach ($postgroups as $id => $min_posts)
224
			{
225
				$conditions .= '
226
					WHEN posts >= ' . $min_posts . (!empty($lastMin) ? ' AND posts <= ' . $lastMin : '') . ' THEN ' . $id;
227
228
				$lastMin = $min_posts;
229
			}
230
231
			// A big fat CASE WHEN... END is faster than a zillion UPDATE's ;).
232
			$smcFunc['db_query']('', '
233
				UPDATE {db_prefix}members
234
				SET id_post_group = CASE ' . $conditions . '
235
				ELSE 0
236
				END' . ($parameter1 != null ? '
237
				WHERE ' . (is_array($parameter1) ? 'id_member IN ({array_int:members})' : 'id_member = {int:members}') : ''),
238
				array(
239
					'members' => $parameter1,
240
				)
241
			);
242
			break;
243
244
		default:
245
			trigger_error('updateStats(): Invalid statistic type \'' . $type . '\'', E_USER_NOTICE);
246
	}
247
}
248
249
/**
250
 * Updates the columns in the members table.
251
 * Assumes the data has been htmlspecialchar'd.
252
 * this function should be used whenever member data needs to be
253
 * updated in place of an UPDATE query.
254
 *
255
 * id_member is either an int or an array of ints to be updated.
256
 *
257
 * data is an associative array of the columns to be updated and their respective values.
258
 * any string values updated should be quoted and slashed.
259
 *
260
 * the value of any column can be '+' or '-', which mean 'increment'
261
 * and decrement, respectively.
262
 *
263
 * if the member's post number is updated, updates their post groups.
264
 *
265
 * @param mixed $members An array of member IDs, the ID of a single member, or null to update this for all members
266
 * @param array $data The info to update for the members
267
 */
268
function updateMemberData($members, $data)
269
{
270
	global $modSettings, $user_info, $smcFunc, $sourcedir, $cache_enable;
271
272
	// An empty array means there's nobody to update.
273
	if ($members === array())
274
		return;
275
276
	$parameters = array();
277
	if (is_array($members))
278
	{
279
		$condition = 'id_member IN ({array_int:members})';
280
		$parameters['members'] = $members;
281
	}
282
283
	elseif ($members === null)
284
		$condition = '1=1';
285
286
	else
287
	{
288
		$condition = 'id_member = {int:member}';
289
		$parameters['member'] = $members;
290
	}
291
292
	// Everything is assumed to be a string unless it's in the below.
293
	$knownInts = array(
294
		'date_registered', 'posts', 'id_group', 'last_login', 'instant_messages', 'unread_messages',
295
		'new_pm', 'pm_prefs', 'gender', 'show_online', 'pm_receive_from', 'alerts',
296
		'id_theme', 'is_activated', 'id_msg_last_visit', 'id_post_group', 'total_time_logged_in', 'warning',
297
	);
298
	$knownFloats = array(
299
		'time_offset',
300
	);
301
302
	if (!empty($modSettings['integrate_change_member_data']))
303
	{
304
		// Only a few member variables are really interesting for integration.
305
		$integration_vars = array(
306
			'member_name',
307
			'real_name',
308
			'email_address',
309
			'id_group',
310
			'gender',
311
			'birthdate',
312
			'website_title',
313
			'website_url',
314
			'location',
315
			'time_format',
316
			'time_offset',
317
			'avatar',
318
			'lngfile',
319
		);
320
		$vars_to_integrate = array_intersect($integration_vars, array_keys($data));
321
322
		// Only proceed if there are any variables left to call the integration function.
323
		if (count($vars_to_integrate) != 0)
324
		{
325
			// Fetch a list of member_names if necessary
326
			if ((!is_array($members) && $members === $user_info['id']) || (is_array($members) && count($members) == 1 && in_array($user_info['id'], $members)))
327
				$member_names = array($user_info['username']);
328
			else
329
			{
330
				$member_names = array();
331
				$request = $smcFunc['db_query']('', '
332
					SELECT member_name
333
					FROM {db_prefix}members
334
					WHERE ' . $condition,
335
					$parameters
336
				);
337
				while ($row = $smcFunc['db_fetch_assoc']($request))
338
					$member_names[] = $row['member_name'];
339
				$smcFunc['db_free_result']($request);
340
			}
341
342
			if (!empty($member_names))
343
				foreach ($vars_to_integrate as $var)
344
					call_integration_hook('integrate_change_member_data', array($member_names, $var, &$data[$var], &$knownInts, &$knownFloats));
345
		}
346
	}
347
348
	$setString = '';
349
	foreach ($data as $var => $val)
350
	{
351
		switch ($var)
352
		{
353
			case  'birthdate':
354
				$type = 'date';
355
				break;
356
357
			case 'member_ip':
358
			case 'member_ip2':
359
				$type = 'inet';
360
				break;
361
362
			default:
363
				$type = 'string';
364
		}
365
366
		if (in_array($var, $knownInts))
367
			$type = 'int';
368
369
		elseif (in_array($var, $knownFloats))
370
			$type = 'float';
371
372
		// Doing an increment?
373
		if ($var == 'alerts' && ($val === '+' || $val === '-'))
374
		{
375
			include_once($sourcedir . '/Profile-Modify.php');
376
			if (is_array($members))
377
			{
378
				$val = 'CASE ';
379
				foreach ($members as $k => $v)
380
					$val .= 'WHEN id_member = ' . $v . ' THEN '. alert_count($v, true) . ' ';
381
382
				$val = $val . ' END';
383
				$type = 'raw';
384
			}
385
386
			else
387
				$val = alert_count($members, true);
388
		}
389
390
		elseif ($type == 'int' && ($val === '+' || $val === '-'))
391
		{
392
			$val = $var . ' ' . $val . ' 1';
393
			$type = 'raw';
394
		}
395
396
		// Ensure posts, instant_messages, and unread_messages don't overflow or underflow.
397
		if (in_array($var, array('posts', 'instant_messages', 'unread_messages')))
398
		{
399
			if (preg_match('~^' . $var . ' (\+ |- |\+ -)([\d]+)~', $val, $match))
400
			{
401
				if ($match[1] != '+ ')
402
					$val = 'CASE WHEN ' . $var . ' <= ' . abs($match[2]) . ' THEN 0 ELSE ' . $val . ' END';
403
404
				$type = 'raw';
405
			}
406
		}
407
408
		$setString .= ' ' . $var . ' = {' . $type . ':p_' . $var . '},';
409
		$parameters['p_' . $var] = $val;
410
	}
411
412
	$smcFunc['db_query']('', '
413
		UPDATE {db_prefix}members
414
		SET' . substr($setString, 0, -1) . '
415
		WHERE ' . $condition,
416
		$parameters
417
	);
418
419
	updateStats('postgroups', $members, array_keys($data));
420
421
	// Clear any caching?
422
	if (!empty($cache_enable) && $cache_enable >= 2 && !empty($members))
423
	{
424
		if (!is_array($members))
425
			$members = array($members);
426
427
		foreach ($members as $member)
428
		{
429
			if ($cache_enable >= 3)
430
			{
431
				cache_put_data('member_data-profile-' . $member, null, 120);
432
				cache_put_data('member_data-normal-' . $member, null, 120);
433
				cache_put_data('member_data-minimal-' . $member, null, 120);
434
			}
435
			cache_put_data('user_settings-' . $member, null, 60);
436
		}
437
	}
438
}
439
440
/**
441
 * Updates the settings table as well as $modSettings... only does one at a time if $update is true.
442
 *
443
 * - updates both the settings table and $modSettings array.
444
 * - all of changeArray's indexes and values are assumed to have escaped apostrophes (')!
445
 * - if a variable is already set to what you want to change it to, that
446
 *   variable will be skipped over; it would be unnecessary to reset.
447
 * - When use_update is true, UPDATEs will be used instead of REPLACE.
448
 * - when use_update is true, the value can be true or false to increment
449
 *  or decrement it, respectively.
450
 *
451
 * @param array $changeArray An array of info about what we're changing in 'setting' => 'value' format
452
 * @param bool $update Whether to use an UPDATE query instead of a REPLACE query
453
 */
454
function updateSettings($changeArray, $update = false)
455
{
456
	global $modSettings, $smcFunc;
457
458
	if (empty($changeArray) || !is_array($changeArray))
459
		return;
460
461
	$toRemove = array();
462
463
	// Go check if there is any setting to be removed.
464
	foreach ($changeArray as $k => $v)
465
		if ($v === null)
466
		{
467
			// Found some, remove them from the original array and add them to ours.
468
			unset($changeArray[$k]);
469
			$toRemove[] = $k;
470
		}
471
472
	// Proceed with the deletion.
473
	if (!empty($toRemove))
474
		$smcFunc['db_query']('', '
475
			DELETE FROM {db_prefix}settings
476
			WHERE variable IN ({array_string:remove})',
477
			array(
478
				'remove' => $toRemove,
479
			)
480
		);
481
482
	// In some cases, this may be better and faster, but for large sets we don't want so many UPDATEs.
483
	if ($update)
484
	{
485
		foreach ($changeArray as $variable => $value)
486
		{
487
			$smcFunc['db_query']('', '
488
				UPDATE {db_prefix}settings
489
				SET value = {' . ($value === false || $value === true ? 'raw' : 'string') . ':value}
490
				WHERE variable = {string:variable}',
491
				array(
492
					'value' => $value === true ? 'value + 1' : ($value === false ? 'value - 1' : $value),
493
					'variable' => $variable,
494
				)
495
			);
496
			$modSettings[$variable] = $value === true ? $modSettings[$variable] + 1 : ($value === false ? $modSettings[$variable] - 1 : $value);
497
		}
498
499
		// Clean out the cache and make sure the cobwebs are gone too.
500
		cache_put_data('modSettings', null, 90);
501
502
		return;
503
	}
504
505
	$replaceArray = array();
506
	foreach ($changeArray as $variable => $value)
507
	{
508
		// Don't bother if it's already like that ;).
509
		if (isset($modSettings[$variable]) && $modSettings[$variable] == $value)
510
			continue;
511
		// If the variable isn't set, but would only be set to nothing'ness, then don't bother setting it.
512
		elseif (!isset($modSettings[$variable]) && empty($value))
513
			continue;
514
515
		$replaceArray[] = array($variable, $value);
516
517
		$modSettings[$variable] = $value;
518
	}
519
520
	if (empty($replaceArray))
521
		return;
522
523
	$smcFunc['db_insert']('replace',
524
		'{db_prefix}settings',
525
		array('variable' => 'string-255', 'value' => 'string-65534'),
526
		$replaceArray,
527
		array('variable')
528
	);
529
530
	// Kill the cache - it needs redoing now, but we won't bother ourselves with that here.
531
	cache_put_data('modSettings', null, 90);
532
}
533
534
/**
535
 * Constructs a page list.
536
 *
537
 * - builds the page list, e.g. 1 ... 6 7 [8] 9 10 ... 15.
538
 * - flexible_start causes it to use "url.page" instead of "url;start=page".
539
 * - very importantly, cleans up the start value passed, and forces it to
540
 *   be a multiple of num_per_page.
541
 * - checks that start is not more than max_value.
542
 * - base_url should be the URL without any start parameter on it.
543
 * - uses the compactTopicPagesEnable and compactTopicPagesContiguous
544
 *   settings to decide how to display the menu.
545
 *
546
 * an example is available near the function definition.
547
 * $pageindex = constructPageIndex($scripturl . '?board=' . $board, $_REQUEST['start'], $num_messages, $maxindex, true);
548
 *
549
 * @param string $base_url The basic URL to be used for each link.
550
 * @param int &$start The start position, by reference. If this is not a multiple of the number of items per page, it is sanitized to be so and the value will persist upon the function's return.
551
 * @param int $max_value The total number of items you are paginating for.
552
 * @param int $num_per_page The number of items to be displayed on a given page. $start will be forced to be a multiple of this value.
553
 * @param bool $flexible_start Whether a ;start=x component should be introduced into the URL automatically (see above)
554
 * @param bool $show_prevnext Whether the Previous and Next links should be shown (should be on only when navigating the list)
555
 *
556
 * @return string The complete HTML of the page index that was requested, formatted by the template.
557
 */
558
function constructPageIndex($base_url, &$start, $max_value, $num_per_page, $flexible_start = false, $show_prevnext = true)
559
{
560
	global $modSettings, $context, $smcFunc, $settings, $txt;
561
562
	// Save whether $start was less than 0 or not.
563
	$start = (int) $start;
564
	$start_invalid = $start < 0;
565
566
	// Make sure $start is a proper variable - not less than 0.
567
	if ($start_invalid)
568
		$start = 0;
569
	// Not greater than the upper bound.
570
	elseif ($start >= $max_value)
571
		$start = max(0, (int) $max_value - (((int) $max_value % (int) $num_per_page) == 0 ? $num_per_page : ((int) $max_value % (int) $num_per_page)));
572
	// And it has to be a multiple of $num_per_page!
573
	else
574
		$start = max(0, (int) $start - ((int) $start % (int) $num_per_page));
575
576
	$context['current_page'] = $start / $num_per_page;
577
578
	// Define some default page index settings if we don't already have it...
579
	if (!isset($settings['page_index']))
580
	{
581
		// This defines the formatting for the page indexes used throughout the forum.
582
		$settings['page_index'] = array(
583
			'extra_before' => '<span class="pages">' . $txt['pages'] . '</span>',
584
			'previous_page' => '<span class="main_icons previous_page"></span>',
585
			'current_page' => '<span class="current_page">%1$d</span> ',
586
			'page' => '<a class="nav_page" href="{URL}">%2$s</a> ',
587
			'expand_pages' => '<span class="expand_pages" onclick="expandPages(this, {LINK}, {FIRST_PAGE}, {LAST_PAGE}, {PER_PAGE});"> ... </span>',
588
			'next_page' => '<span class="main_icons next_page"></span>',
589
			'extra_after' => '',
590
		);
591
	}
592
593
	$base_link = strtr($settings['page_index']['page'], array('{URL}' => $flexible_start ? $base_url : strtr($base_url, array('%' => '%%')) . ';start=%1$d'));
594
	$pageindex = $settings['page_index']['extra_before'];
595
596
	// Compact pages is off or on?
597
	if (empty($modSettings['compactTopicPagesEnable']))
598
	{
599
		// Show the left arrow.
600
		$pageindex .= $start == 0 ? ' ' : sprintf($base_link, $start - $num_per_page, $settings['page_index']['previous_page']);
601
602
		// Show all the pages.
603
		$display_page = 1;
604
		for ($counter = 0; $counter < $max_value; $counter += $num_per_page)
605
			$pageindex .= $start == $counter && !$start_invalid ? sprintf($settings['page_index']['current_page'], $display_page++) : sprintf($base_link, $counter, $display_page++);
606
607
		// Show the right arrow.
608
		$display_page = ($start + $num_per_page) > $max_value ? $max_value : ($start + $num_per_page);
609
		if ($start != $counter - $max_value && !$start_invalid)
610
			$pageindex .= $display_page > $counter - $num_per_page ? ' ' : sprintf($base_link, $display_page, $settings['page_index']['next_page']);
611
	}
612
	else
613
	{
614
		// If they didn't enter an odd value, pretend they did.
615
		$PageContiguous = (int) ($modSettings['compactTopicPagesContiguous'] - ($modSettings['compactTopicPagesContiguous'] % 2)) / 2;
616
617
		// Show the "prev page" link. (>prev page< 1 ... 6 7 [8] 9 10 ... 15 next page)
618
		if (!empty($start) && $show_prevnext)
619
			$pageindex .= sprintf($base_link, $start - $num_per_page, $settings['page_index']['previous_page']);
620
		else
621
			$pageindex .= '';
622
623
		// Show the first page. (prev page >1< ... 6 7 [8] 9 10 ... 15)
624
		if ($start > $num_per_page * $PageContiguous)
625
			$pageindex .= sprintf($base_link, 0, '1');
626
627
		// Show the ... after the first page.  (prev page 1 >...< 6 7 [8] 9 10 ... 15 next page)
628
		if ($start > $num_per_page * ($PageContiguous + 1))
629
			$pageindex .= strtr($settings['page_index']['expand_pages'], array(
630
				'{LINK}' => JavaScriptEscape($smcFunc['htmlspecialchars']($base_link)),
631
				'{FIRST_PAGE}' => $num_per_page,
632
				'{LAST_PAGE}' => $start - $num_per_page * $PageContiguous,
633
				'{PER_PAGE}' => $num_per_page,
634
			));
635
636
		// Show the pages before the current one. (prev page 1 ... >6 7< [8] 9 10 ... 15 next page)
637
		for ($nCont = $PageContiguous; $nCont >= 1; $nCont--)
638
			if ($start >= $num_per_page * $nCont)
639
			{
640
				$tmpStart = $start - $num_per_page * $nCont;
641
				$pageindex .= sprintf($base_link, $tmpStart, $tmpStart / $num_per_page + 1);
642
			}
643
644
		// Show the current page. (prev page 1 ... 6 7 >[8]< 9 10 ... 15 next page)
645
		if (!$start_invalid)
646
			$pageindex .= sprintf($settings['page_index']['current_page'], $start / $num_per_page + 1);
647
		else
648
			$pageindex .= sprintf($base_link, $start, $start / $num_per_page + 1);
649
650
		// Show the pages after the current one... (prev page 1 ... 6 7 [8] >9 10< ... 15 next page)
651
		$tmpMaxPages = (int) (($max_value - 1) / $num_per_page) * $num_per_page;
652
		for ($nCont = 1; $nCont <= $PageContiguous; $nCont++)
653
			if ($start + $num_per_page * $nCont <= $tmpMaxPages)
654
			{
655
				$tmpStart = $start + $num_per_page * $nCont;
656
				$pageindex .= sprintf($base_link, $tmpStart, $tmpStart / $num_per_page + 1);
657
			}
658
659
		// Show the '...' part near the end. (prev page 1 ... 6 7 [8] 9 10 >...< 15 next page)
660
		if ($start + $num_per_page * ($PageContiguous + 1) < $tmpMaxPages)
661
			$pageindex .= strtr($settings['page_index']['expand_pages'], array(
662
				'{LINK}' => JavaScriptEscape($smcFunc['htmlspecialchars']($base_link)),
663
				'{FIRST_PAGE}' => $start + $num_per_page * ($PageContiguous + 1),
664
				'{LAST_PAGE}' => $tmpMaxPages,
665
				'{PER_PAGE}' => $num_per_page,
666
			));
667
668
		// Show the last number in the list. (prev page 1 ... 6 7 [8] 9 10 ... >15<  next page)
669
		if ($start + $num_per_page * $PageContiguous < $tmpMaxPages)
670
			$pageindex .= sprintf($base_link, $tmpMaxPages, $tmpMaxPages / $num_per_page + 1);
671
672
		// Show the "next page" link. (prev page 1 ... 6 7 [8] 9 10 ... 15 >next page<)
673
		if ($start != $tmpMaxPages && $show_prevnext)
674
			$pageindex .= sprintf($base_link, $start + $num_per_page, $settings['page_index']['next_page']);
675
	}
676
	$pageindex .= $settings['page_index']['extra_after'];
677
678
	return $pageindex;
679
}
680
681
/**
682
 * - Formats a number.
683
 * - uses the format of number_format to decide how to format the number.
684
 *   for example, it might display "1 234,50".
685
 * - caches the formatting data from the setting for optimization.
686
 *
687
 * @param float $number A number
688
 * @param bool|int $override_decimal_count If set, will use the specified number of decimal places. Otherwise it's automatically determined
689
 * @return string A formatted number
690
 */
691
function comma_format($number, $override_decimal_count = false)
692
{
693
	global $txt;
694
	static $thousands_separator = null, $decimal_separator = null, $decimal_count = null;
695
696
	// Cache these values...
697
	if ($decimal_separator === null)
698
	{
699
		// Not set for whatever reason?
700
		if (empty($txt['number_format']) || preg_match('~^1([^\d]*)?234([^\d]*)(0*?)$~', $txt['number_format'], $matches) != 1)
701
			return $number;
702
703
		// Cache these each load...
704
		$thousands_separator = $matches[1];
705
		$decimal_separator = $matches[2];
706
		$decimal_count = strlen($matches[3]);
707
	}
708
709
	// Format the string with our friend, number_format.
710
	return number_format($number, (float) $number === $number ? ($override_decimal_count === false ? $decimal_count : $override_decimal_count) : 0, $decimal_separator, $thousands_separator);
711
}
712
713
/**
714
 * Format a time to make it look purdy.
715
 *
716
 * - returns a pretty formatted version of time based on the user's format in $user_info['time_format'].
717
 * - applies all necessary time offsets to the timestamp, unless offset_type is set.
718
 * - if todayMod is set and show_today was not not specified or true, an
719
 *   alternate format string is used to show the date with something to show it is "today" or "yesterday".
720
 * - performs localization (more than just strftime would do alone.)
721
 *
722
 * @param int $log_time A timestamp
723
 * @param bool $show_today Whether to show "Today"/"Yesterday" or just a date
724
 * @param bool|string $offset_type If false, uses both user time offset and forum offset. If 'forum', uses only the forum offset. Otherwise no offset is applied.
725
 * @param bool $process_safe Activate setlocale check for changes at runtime. Slower, but safer.
726
 * @return string A formatted timestamp
727
 */
728
function timeformat($log_time, $show_today = true, $offset_type = false, $process_safe = false)
729
{
730
	global $context, $user_info, $txt, $modSettings;
731
	static $non_twelve_hour, $locale, $now;
732
	static $unsupportedFormats, $finalizedFormats;
733
734
	$unsupportedFormatsWindows = array('z', 'Z');
735
736
	// Ensure required values are set
737
	$user_info['time_offset'] = !empty($user_info['time_offset']) ? $user_info['time_offset'] : 0;
738
	$modSettings['time_offset'] = !empty($modSettings['time_offset']) ? $modSettings['time_offset'] : 0;
739
	$user_info['time_format'] = !empty($user_info['time_format']) ? $user_info['time_format'] : (!empty($modSettings['time_format']) ? $modSettings['time_format'] : '%F %H:%M');
740
741
	// Offset the time.
742
	if (!$offset_type)
743
		$log_time = $log_time + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600;
744
	// Just the forum offset?
745
	elseif ($offset_type == 'forum')
746
		$log_time = $log_time + $modSettings['time_offset'] * 3600;
747
748
	// We can't have a negative date (on Windows, at least.)
749
	if ($log_time < 0)
750
		$log_time = 0;
751
752
	// Today and Yesterday?
753
	$prefix = '';
754
	if ($modSettings['todayMod'] >= 1 && $show_today === true)
755
	{
756
		$now_time = forum_time();
757
758
		if ($now_time - $log_time < (86400 * $modSettings['todayMod']))
759
		{
760
			$then = @getdate($log_time);
761
			$now = (!empty($now) ? $now : @getdate($now_time));
762
763
			// Same day of the year, same year.... Today!
764
			if ($then['yday'] == $now['yday'] && $then['year'] == $now['year'])
765
			{
766
				$prefix = $txt['today'];
767
			}
768
			// Day-of-year is one less and same year, or it's the first of the year and that's the last of the year...
769
			elseif ($modSettings['todayMod'] == '2' && (($then['yday'] == $now['yday'] - 1 && $then['year'] == $now['year']) || ($now['yday'] == 0 && $then['year'] == $now['year'] - 1) && $then['mon'] == 12 && $then['mday'] == 31))
770
			{
771
				$prefix = $txt['yesterday'];
772
			}
773
		}
774
	}
775
776
	$str = !is_bool($show_today) ? $show_today : $user_info['time_format'];
777
778
	// Use the cached formats if available
779
	if (is_null($finalizedFormats))
780
		$finalizedFormats = (array) cache_get_data('timeformatstrings', 86400);
781
782
	if (!isset($finalizedFormats[$str]) || !is_array($finalizedFormats[$str]))
783
		$finalizedFormats[$str] = array();
784
785
	// Make a supported version for this format if we don't already have one
786
	$format_type = !empty($prefix) ? 'time_only' : 'normal';
787
	if (empty($finalizedFormats[$str][$format_type]))
788
	{
789
		$timeformat = $format_type == 'time_only' ? get_date_or_time_format('time', $str) : $str;
790
791
		// Not all systems support all formats, and Windows fails altogether if unsupported ones are
792
		// used, so let's prevent that. Some substitutions go to the nearest reasonable fallback, some
793
		// turn into static strings, some (i.e. %a, %A, %b, %B, %p) have special handling below.
794
		$strftimeFormatSubstitutions = array(
795
			// Day
796
			'a' => '#txt_days_short_%w#', 'A' => '#txt_days_%w#', 'e' => '%d', 'd' => '&#37;d', 'j' => '&#37;j', 'u' => '%w', 'w' => '&#37;w',
797
			// Week
798
			'U' => '&#37;U', 'V' => '%U', 'W' => '%U',
799
			// Month
800
			'b' => '#txt_months_short_%m#', 'B' => '#txt_months_%m#', 'h' => '%b', 'm' => '&#37;m',
801
			// Year
802
			'C' => '&#37;C', 'g' => '%y', 'G' => '%Y', 'y' => '&#37;y', 'Y' => '&#37;Y',
803
			// Time
804
			'H' => '&#37;H', 'k' => '%H', 'I' => '%H', 'l' => '%I', 'M' => '&#37;M', 'p' => '&#37;p', 'P' => '%p',
805
			'r' => '%I:%M:%S %p', 'R' => '%H:%M', 'S' => '&#37;S', 'T' => '%H:%M:%S', 'X' => '%T', 'z' => '&#37;z', 'Z' => '&#37;Z',
806
			// Time and Date Stamps
807
			'c' => '%F %T', 'D' => '%m/%d/%y', 'F' => '%Y-%m-%d', 's' => '&#37;s', 'x' => '%F',
808
			// Miscellaneous
809
			'n' => "\n", 't' => "\t", '%' => '&#37;',
810
		);
811
812
		// No need to do this part again if we already did it once
813
		if (is_null($unsupportedFormats))
814
			$unsupportedFormats = (array) cache_get_data('unsupportedtimeformats', 86400);
815
		if (empty($unsupportedFormats))
816
		{
817
			foreach ($strftimeFormatSubstitutions as $format => $substitution)
818
			{
819
				// Avoid a crashing bug with PHP 7 on certain versions of Windows
820
				if ($context['server']['is_windows'] && in_array($format, $unsupportedFormatsWindows))
821
				{
822
					$unsupportedFormats[] = $format;
823
					continue;
824
				}
825
826
				$value = @strftime('%' . $format);
827
828
				// Windows will return false for unsupported formats
829
				// Other operating systems return the format string as a literal
830
				if ($value === false || $value === $format)
831
					$unsupportedFormats[] = $format;
832
			}
833
			cache_put_data('unsupportedtimeformats', $unsupportedFormats, 86400);
834
		}
835
836
		// Windows needs extra help if $timeformat contains something completely invalid, e.g. '%Q'
837
		if (DIRECTORY_SEPARATOR === '\\')
838
			$timeformat = preg_replace('~%(?!' . implode('|', array_keys($strftimeFormatSubstitutions)) . ')~', '&#37;', $timeformat);
839
840
		// Substitute unsupported formats with supported ones
841
		if (!empty($unsupportedFormats))
842
			while (preg_match('~%(' . implode('|', $unsupportedFormats) . ')~', $timeformat, $matches))
843
				$timeformat = str_replace($matches[0], $strftimeFormatSubstitutions[$matches[1]], $timeformat);
844
845
		// Remember this so we don't need to do it again
846
		$finalizedFormats[$str][$format_type] = $timeformat;
847
		cache_put_data('timeformatstrings', $finalizedFormats, 86400);
848
	}
849
850
	$timeformat = $finalizedFormats[$str][$format_type];
851
852
	// Make sure we are using the correct locale.
853
	if (!isset($locale) || ($process_safe === true && setlocale(LC_TIME, '0') != $locale))
854
		$locale = setlocale(LC_TIME, array($txt['lang_locale'] . '.' . $modSettings['global_character_set'], $txt['lang_locale'] . '.' . $txt['lang_character_set'], $txt['lang_locale']));
855
856
	// If the current locale is unsupported, we'll have to localize the hard way.
857
	if ($locale === false)
858
	{
859
		$timeformat = strtr($timeformat, array(
860
			'%a' => '#txt_days_short_%w#',
861
			'%A' => '#txt_days_%w#',
862
			'%b' => '#txt_months_short_%m#',
863
			'%B' => '#txt_months_%m#',
864
			'%p' => '&#37;p',
865
			'%P' => '&#37;p'
866
		));
867
	}
868
	// Just in case the locale doesn't support '%p' properly.
869
	// @todo Is this even necessary?
870
	else
871
	{
872
		if (!isset($non_twelve_hour) && strpos($timeformat, '%p') !== false)
873
			$non_twelve_hour = trim(strftime('%p')) === '';
874
875
		if (!empty($non_twelve_hour))
876
			$timeformat = strtr($timeformat, array(
877
				'%p' => '&#37;p',
878
				'%P' => '&#37;p'
879
			));
880
	}
881
882
	// And now, the moment we've all be waiting for...
883
	$timestring = strftime($timeformat, $log_time);
884
885
	// Do-it-yourself time localization.  Fun.
886
	if (strpos($timestring, '&#37;p') !== false)
887
		$timestring = str_replace('&#37;p', (strftime('%H', $log_time) < 12 ? $txt['time_am'] : $txt['time_pm']), $timestring);
888
	if (strpos($timestring, '#txt_') !== false)
889
	{
890
		if (strpos($timestring, '#txt_days_short_') !== false)
891
			$timestring = strtr($timestring, array(
892
				'#txt_days_short_0#' => $txt['days_short'][0],
893
				'#txt_days_short_1#' => $txt['days_short'][1],
894
				'#txt_days_short_2#' => $txt['days_short'][2],
895
				'#txt_days_short_3#' => $txt['days_short'][3],
896
				'#txt_days_short_4#' => $txt['days_short'][4],
897
				'#txt_days_short_5#' => $txt['days_short'][5],
898
				'#txt_days_short_6#' => $txt['days_short'][6],
899
			));
900
901
		if (strpos($timestring, '#txt_days_') !== false)
902
			$timestring = strtr($timestring, array(
903
				'#txt_days_0#' => $txt['days'][0],
904
				'#txt_days_1#' => $txt['days'][1],
905
				'#txt_days_2#' => $txt['days'][2],
906
				'#txt_days_3#' => $txt['days'][3],
907
				'#txt_days_4#' => $txt['days'][4],
908
				'#txt_days_5#' => $txt['days'][5],
909
				'#txt_days_6#' => $txt['days'][6],
910
			));
911
912
		if (strpos($timestring, '#txt_months_short_') !== false)
913
			$timestring = strtr($timestring, array(
914
				'#txt_months_short_01#' => $txt['months_short'][1],
915
				'#txt_months_short_02#' => $txt['months_short'][2],
916
				'#txt_months_short_03#' => $txt['months_short'][3],
917
				'#txt_months_short_04#' => $txt['months_short'][4],
918
				'#txt_months_short_05#' => $txt['months_short'][5],
919
				'#txt_months_short_06#' => $txt['months_short'][6],
920
				'#txt_months_short_07#' => $txt['months_short'][7],
921
				'#txt_months_short_08#' => $txt['months_short'][8],
922
				'#txt_months_short_09#' => $txt['months_short'][9],
923
				'#txt_months_short_10#' => $txt['months_short'][10],
924
				'#txt_months_short_11#' => $txt['months_short'][11],
925
				'#txt_months_short_12#' => $txt['months_short'][12],
926
			));
927
928
		if (strpos($timestring, '#txt_months_') !== false)
929
			$timestring = strtr($timestring, array(
930
				'#txt_months_01#' => $txt['months'][1],
931
				'#txt_months_02#' => $txt['months'][2],
932
				'#txt_months_03#' => $txt['months'][3],
933
				'#txt_months_04#' => $txt['months'][4],
934
				'#txt_months_05#' => $txt['months'][5],
935
				'#txt_months_06#' => $txt['months'][6],
936
				'#txt_months_07#' => $txt['months'][7],
937
				'#txt_months_08#' => $txt['months'][8],
938
				'#txt_months_09#' => $txt['months'][9],
939
				'#txt_months_10#' => $txt['months'][10],
940
				'#txt_months_11#' => $txt['months'][11],
941
				'#txt_months_12#' => $txt['months'][12],
942
			));
943
	}
944
945
	// Restore any literal percent characters, add the prefix, and we're done.
946
	return $prefix . str_replace('&#37;', '%', $timestring);
947
}
948
949
/**
950
 * Gets a version of a strftime() format that only shows the date or time components
951
 *
952
 * @param string $type Either 'date' or 'time'.
953
 * @param string $format A strftime() format to process. Defaults to $user_info['time_format'].
954
 * @return string A strftime() format string
955
 */
956
function get_date_or_time_format($type = '', $format = '')
957
{
958
	global $user_info, $modSettings;
959
	static $formats;
960
961
	// If the format is invalid, fall back to defaults.
962
	if (strpos($format, '%') === false)
963
		$format = !empty($user_info['time_format']) ? $user_info['time_format'] : (!empty($modSettings['time_format']) ? $modSettings['time_format'] : '%F %k:%M');
964
965
	$orig_format = $format;
966
967
	// Have we already done this?
968
	if (isset($formats[$orig_format][$type]))
969
		return $formats[$orig_format][$type];
970
971
	if ($type === 'date')
972
	{
973
		$specifications = array(
974
			// Day
975
			'%a' => '%a', '%A' => '%A', '%e' => '%e', '%d' => '%d', '%j' => '%j', '%u' => '%u', '%w' => '%w',
976
			// Week
977
			'%U' => '%U', '%V' => '%V', '%W' => '%W',
978
			// Month
979
			'%b' => '%b', '%B' => '%B', '%h' => '%h', '%m' => '%m',
980
			// Year
981
			'%C' => '%C', '%g' => '%g', '%G' => '%G', '%y' => '%y', '%Y' => '%Y',
982
			// Time
983
			'%H' => '', '%k' => '', '%I' => '', '%l' => '', '%M' => '', '%p' => '', '%P' => '',
984
			'%r' => '', '%R' => '', '%S' => '', '%T' => '', '%X' => '', '%z' => '', '%Z' => '',
985
			// Time and Date Stamps
986
			'%c' => '%x', '%D' => '%D', '%F' => '%F', '%s' => '%s', '%x' => '%x',
987
			// Miscellaneous
988
			'%n' => '', '%t' => '', '%%' => '%%',
989
		);
990
991
		$default_format = '%F';
992
	}
993
	elseif ($type === 'time')
994
	{
995
		$specifications = array(
996
			// Day
997
			'%a' => '', '%A' => '', '%e' => '', '%d' => '', '%j' => '', '%u' => '', '%w' => '',
998
			// Week
999
			'%U' => '', '%V' => '', '%W' => '',
1000
			// Month
1001
			'%b' => '', '%B' => '', '%h' => '', '%m' => '',
1002
			// Year
1003
			'%C' => '', '%g' => '', '%G' => '', '%y' => '', '%Y' => '',
1004
			// Time
1005
			'%H' => '%H', '%k' => '%k', '%I' => '%I', '%l' => '%l', '%M' => '%M', '%p' => '%p', '%P' => '%P',
1006
			'%r' => '%r', '%R' => '%R', '%S' => '%S', '%T' => '%T', '%X' => '%X', '%z' => '%z', '%Z' => '%Z',
1007
			// Time and Date Stamps
1008
			'%c' => '%X', '%D' => '', '%F' => '', '%s' => '%s', '%x' => '',
1009
			// Miscellaneous
1010
			'%n' => '', '%t' => '', '%%' => '%%',
1011
		);
1012
1013
		$default_format = '%k:%M';
1014
	}
1015
	// Invalid type requests just get the full format string.
1016
	else
1017
		return $format;
1018
1019
	// Separate the specifications we want from the ones we don't.
1020
	$wanted = array_filter($specifications);
1021
	$unwanted = array_diff(array_keys($specifications), $wanted);
1022
1023
	// First, make any necessary substitutions in the format.
1024
	$format = strtr($format, $wanted);
1025
1026
	// Next, strip out any specifications and literal text that we don't want.
1027
	$format_parts = preg_split('~%[' . (strtr(implode('', $unwanted), array('%' => ''))) . ']~u', $format);
1028
1029
	foreach ($format_parts as $p => $f)
1030
	{
1031
		if (strpos($f, '%') === false)
1032
			unset($format_parts[$p]);
1033
	}
1034
1035
	$format = implode('', $format_parts);
1036
1037
	// Finally, strip out any unwanted leftovers.
1038
	// For info on the charcter classes used here, see https://www.php.net/manual/en/regexp.reference.unicode.php and https://www.regular-expressions.info/unicode.html
1039
	$format = preg_replace(
1040
		array(
1041
			// Anything that isn't a specification, punctuation mark, or whitespace.
1042
			'~(?<!%)\p{L}|[^\p{L}\p{P}\s]~u',
1043
			// A series of punctuation marks (except %), possibly separated by whitespace.
1044
			'~([^%\P{P}])(\s*)(?'.'>(\1|[^%\P{Po}])\s*(?!$))*~u',
1045
			// Unwanted trailing punctuation and whitespace.
1046
			'~(?'.'>([\p{Pd}\p{Ps}\p{Pi}\p{Pc}]|[^%\P{Po}])\s*)*$~u',
1047
			// Unwanted opening punctuation and whitespace.
1048
			'~^\s*(?'.'>([\p{Pd}\p{Pe}\p{Pf}\p{Pc}]|[^%\P{Po}])\s*)*~u',
1049
		),
1050
		array(
1051
			'',
1052
			'$1$2',
1053
			'',
1054
			'',
1055
		),
1056
		$format
1057
	);
1058
1059
	// Gotta have something...
1060
	if (empty($format))
1061
		$format = $default_format;
1062
1063
	// Remember what we've done.
1064
	$formats[$orig_format][$type] = trim($format);
1065
1066
	return $formats[$orig_format][$type];
1067
}
1068
1069
/**
1070
 * Replaces special entities in strings with the real characters.
1071
 *
1072
 * Functionally equivalent to htmlspecialchars_decode(), except that this also
1073
 * replaces '&nbsp;' with a simple space character.
1074
 *
1075
 * @param string $string A string
1076
 * @return string The string without entities
1077
 */
1078
function un_htmlspecialchars($string)
1079
{
1080
	global $context;
1081
	static $translation = array();
1082
1083
	// Determine the character set... Default to UTF-8
1084
	if (empty($context['character_set']))
1085
		$charset = 'UTF-8';
1086
	// Use ISO-8859-1 in place of non-supported ISO-8859 charsets...
1087
	elseif (strpos($context['character_set'], 'ISO-8859-') !== false && !in_array($context['character_set'], array('ISO-8859-5', 'ISO-8859-15')))
1088
		$charset = 'ISO-8859-1';
1089
	else
1090
		$charset = $context['character_set'];
1091
1092
	if (empty($translation))
1093
		$translation = array_flip(get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES, $charset)) + array('&#039;' => '\'', '&#39;' => '\'', '&nbsp;' => ' ');
1094
1095
	return strtr($string, $translation);
1096
}
1097
1098
/**
1099
 * Shorten a subject + internationalization concerns.
1100
 *
1101
 * - shortens a subject so that it is either shorter than length, or that length plus an ellipsis.
1102
 * - respects internationalization characters and entities as one character.
1103
 * - avoids trailing entities.
1104
 * - returns the shortened string.
1105
 *
1106
 * @param string $subject The subject
1107
 * @param int $len How many characters to limit it to
1108
 * @return string The shortened subject - either the entire subject (if it's <= $len) or the subject shortened to $len characters with "..." appended
1109
 */
1110
function shorten_subject($subject, $len)
1111
{
1112
	global $smcFunc;
1113
1114
	// It was already short enough!
1115
	if ($smcFunc['strlen']($subject) <= $len)
1116
		return $subject;
1117
1118
	// Shorten it by the length it was too long, and strip off junk from the end.
1119
	return $smcFunc['substr']($subject, 0, $len) . '...';
1120
}
1121
1122
/**
1123
 * Gets the current time with offset.
1124
 *
1125
 * - always applies the offset in the time_offset setting.
1126
 *
1127
 * @param bool $use_user_offset Whether to apply the user's offset as well
1128
 * @param int $timestamp A timestamp (null to use current time)
1129
 * @return int Seconds since the unix epoch, with forum time offset and (optionally) user time offset applied
1130
 */
1131
function forum_time($use_user_offset = true, $timestamp = null)
1132
{
1133
	global $user_info, $modSettings;
1134
1135
	if ($timestamp === null)
1136
		$timestamp = time();
1137
	elseif ($timestamp == 0)
1138
		return 0;
1139
1140
	return $timestamp + ($modSettings['time_offset'] + ($use_user_offset ? $user_info['time_offset'] : 0)) * 3600;
1141
}
1142
1143
/**
1144
 * Calculates all the possible permutations (orders) of array.
1145
 * should not be called on huge arrays (bigger than like 10 elements.)
1146
 * returns an array containing each permutation.
1147
 *
1148
 * @deprecated since 2.1
1149
 * @param array $array An array
1150
 * @return array An array containing each permutation
1151
 */
1152
function permute($array)
1153
{
1154
	$orders = array($array);
1155
1156
	$n = count($array);
1157
	$p = range(0, $n);
1158
	for ($i = 1; $i < $n; null)
1159
	{
1160
		$p[$i]--;
1161
		$j = $i % 2 != 0 ? $p[$i] : 0;
1162
1163
		$temp = $array[$i];
1164
		$array[$i] = $array[$j];
1165
		$array[$j] = $temp;
1166
1167
		for ($i = 1; $p[$i] == 0; $i++)
1168
			$p[$i] = 1;
1169
1170
		$orders[] = $array;
1171
	}
1172
1173
	return $orders;
1174
}
1175
1176
/**
1177
 * Parse bulletin board code in a string, as well as smileys optionally.
1178
 *
1179
 * - only parses bbc tags which are not disabled in disabledBBC.
1180
 * - handles basic HTML, if enablePostHTML is on.
1181
 * - caches the from/to replace regular expressions so as not to reload them every time a string is parsed.
1182
 * - only parses smileys if smileys is true.
1183
 * - does nothing if the enableBBC setting is off.
1184
 * - uses the cache_id as a unique identifier to facilitate any caching it may do.
1185
 * - returns the modified message.
1186
 *
1187
 * @param string|bool $message The message.
1188
 *		When a empty string, nothing is done.
1189
 *		When false we provide a list of BBC codes available.
1190
 *		When a string, the message is parsed and bbc handled.
1191
 * @param bool $smileys Whether to parse smileys as well
1192
 * @param string $cache_id The cache ID
1193
 * @param array $parse_tags If set, only parses these tags rather than all of them
1194
 * @return string The parsed message
1195
 */
1196
function parse_bbc($message, $smileys = true, $cache_id = '', $parse_tags = array())
1197
{
1198
	global $smcFunc, $txt, $scripturl, $context, $modSettings, $user_info, $sourcedir, $cache_enable;
1199
	static $bbc_lang_locales = array(), $itemcodes = array(), $no_autolink_tags = array();
1200
	static $disabled, $alltags_regex = '', $param_regexes = array();
1201
1202
	// Don't waste cycles
1203
	if ($message === '')
1204
		return '';
1205
1206
	// Just in case it wasn't determined yet whether UTF-8 is enabled.
1207
	if (!isset($context['utf8']))
1208
		$context['utf8'] = (empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8';
1209
1210
	// Clean up any cut/paste issues we may have
1211
	$message = sanitizeMSCutPaste($message);
1212
1213
	// If the load average is too high, don't parse the BBC.
1214
	if (!empty($context['load_average']) && !empty($modSettings['bbc']) && $context['load_average'] >= $modSettings['bbc'])
1215
	{
1216
		$context['disabled_parse_bbc'] = true;
1217
		return $message;
1218
	}
1219
1220
	if ($smileys !== null && ($smileys == '1' || $smileys == '0'))
1221
		$smileys = (bool) $smileys;
1222
1223
	if (empty($modSettings['enableBBC']) && $message !== false)
1224
	{
1225
		if ($smileys === true)
1226
			parsesmileys($message);
1227
1228
		return $message;
1229
	}
1230
1231
	// If we already have a version of the BBCodes for the current language, use that. Otherwise, make one.
1232
	if (!empty($bbc_lang_locales[$txt['lang_locale']]))
1233
		$bbc_codes = $bbc_lang_locales[$txt['lang_locale']];
1234
	else
1235
		$bbc_codes = array();
1236
1237
	// If we are not doing every tag then we don't cache this run.
1238
	if (!empty($parse_tags))
1239
		$bbc_codes = array();
1240
1241
	// Ensure $modSettings['tld_regex'] contains a valid regex for the autolinker
1242
	if (!empty($modSettings['autoLinkUrls']))
1243
		set_tld_regex();
1244
1245
	// Allow mods access before entering the main parse_bbc loop
1246
	call_integration_hook('integrate_pre_parsebbc', array(&$message, &$smileys, &$cache_id, &$parse_tags));
1247
1248
	// Sift out the bbc for a performance improvement.
1249
	if (empty($bbc_codes) || $message === false || !empty($parse_tags))
1250
	{
1251
		if (!empty($modSettings['disabledBBC']))
1252
		{
1253
			$disabled = array();
1254
1255
			$temp = explode(',', strtolower($modSettings['disabledBBC']));
1256
1257
			foreach ($temp as $tag)
1258
				$disabled[trim($tag)] = true;
1259
1260
			if (in_array('color', $disabled))
1261
				$disabled = array_merge($disabled, array(
1262
					'black' => true,
1263
					'white' => true,
1264
					'red' => true,
1265
					'green' => true,
1266
					'blue' => true,
1267
					)
1268
				);
1269
		}
1270
1271
		// The YouTube bbc needs this for its origin parameter
1272
		$scripturl_parts = parse_url($scripturl);
1273
		$hosturl = $scripturl_parts['scheme'] . '://' . $scripturl_parts['host'];
1274
1275
		/* The following bbc are formatted as an array, with keys as follows:
1276
1277
			tag: the tag's name - should be lowercase!
1278
1279
			type: one of...
1280
				- (missing): [tag]parsed content[/tag]
1281
				- unparsed_equals: [tag=xyz]parsed content[/tag]
1282
				- parsed_equals: [tag=parsed data]parsed content[/tag]
1283
				- unparsed_content: [tag]unparsed content[/tag]
1284
				- closed: [tag], [tag/], [tag /]
1285
				- unparsed_commas: [tag=1,2,3]parsed content[/tag]
1286
				- unparsed_commas_content: [tag=1,2,3]unparsed content[/tag]
1287
				- unparsed_equals_content: [tag=...]unparsed content[/tag]
1288
1289
			parameters: an optional array of parameters, for the form
1290
			  [tag abc=123]content[/tag].  The array is an associative array
1291
			  where the keys are the parameter names, and the values are an
1292
			  array which may contain the following:
1293
				- match: a regular expression to validate and match the value.
1294
				- quoted: true if the value should be quoted.
1295
				- validate: callback to evaluate on the data, which is $data.
1296
				- value: a string in which to replace $1 with the data.
1297
					Either value or validate may be used, not both.
1298
				- optional: true if the parameter is optional.
1299
				- default: a default value for missing optional parameters.
1300
1301
			test: a regular expression to test immediately after the tag's
1302
			  '=', ' ' or ']'.  Typically, should have a \] at the end.
1303
			  Optional.
1304
1305
			content: only available for unparsed_content, closed,
1306
			  unparsed_commas_content, and unparsed_equals_content.
1307
			  $1 is replaced with the content of the tag.  Parameters
1308
			  are replaced in the form {param}.  For unparsed_commas_content,
1309
			  $2, $3, ..., $n are replaced.
1310
1311
			before: only when content is not used, to go before any
1312
			  content.  For unparsed_equals, $1 is replaced with the value.
1313
			  For unparsed_commas, $1, $2, ..., $n are replaced.
1314
1315
			after: similar to before in every way, except that it is used
1316
			  when the tag is closed.
1317
1318
			disabled_content: used in place of content when the tag is
1319
			  disabled.  For closed, default is '', otherwise it is '$1' if
1320
			  block_level is false, '<div>$1</div>' elsewise.
1321
1322
			disabled_before: used in place of before when disabled.  Defaults
1323
			  to '<div>' if block_level, '' if not.
1324
1325
			disabled_after: used in place of after when disabled.  Defaults
1326
			  to '</div>' if block_level, '' if not.
1327
1328
			block_level: set to true the tag is a "block level" tag, similar
1329
			  to HTML.  Block level tags cannot be nested inside tags that are
1330
			  not block level, and will not be implicitly closed as easily.
1331
			  One break following a block level tag may also be removed.
1332
1333
			trim: if set, and 'inside' whitespace after the begin tag will be
1334
			  removed.  If set to 'outside', whitespace after the end tag will
1335
			  meet the same fate.
1336
1337
			validate: except when type is missing or 'closed', a callback to
1338
			  validate the data as $data.  Depending on the tag's type, $data
1339
			  may be a string or an array of strings (corresponding to the
1340
			  replacement.)
1341
1342
			quoted: when type is 'unparsed_equals' or 'parsed_equals' only,
1343
			  may be not set, 'optional', or 'required' corresponding to if
1344
			  the content may be quoted.  This allows the parser to read
1345
			  [tag="abc]def[esdf]"] properly.
1346
1347
			require_parents: an array of tag names, or not set.  If set, the
1348
			  enclosing tag *must* be one of the listed tags, or parsing won't
1349
			  occur.
1350
1351
			require_children: similar to require_parents, if set children
1352
			  won't be parsed if they are not in the list.
1353
1354
			disallow_children: similar to, but very different from,
1355
			  require_children, if it is set the listed tags will not be
1356
			  parsed inside the tag.
1357
1358
			parsed_tags_allowed: an array restricting what BBC can be in the
1359
			  parsed_equals parameter, if desired.
1360
		*/
1361
1362
		$codes = array(
1363
			array(
1364
				'tag' => 'abbr',
1365
				'type' => 'unparsed_equals',
1366
				'before' => '<abbr title="$1">',
1367
				'after' => '</abbr>',
1368
				'quoted' => 'optional',
1369
				'disabled_after' => ' ($1)',
1370
			),
1371
			// Legacy (and just an alias for [abbr] even when enabled)
1372
			array(
1373
				'tag' => 'acronym',
1374
				'type' => 'unparsed_equals',
1375
				'before' => '<abbr title="$1">',
1376
				'after' => '</abbr>',
1377
				'quoted' => 'optional',
1378
				'disabled_after' => ' ($1)',
1379
			),
1380
			array(
1381
				'tag' => 'anchor',
1382
				'type' => 'unparsed_equals',
1383
				'test' => '[#]?([A-Za-z][A-Za-z0-9_\-]*)\]',
1384
				'before' => '<span id="post_$1">',
1385
				'after' => '</span>',
1386
			),
1387
			array(
1388
				'tag' => 'attach',
1389
				'type' => 'unparsed_content',
1390
				'parameters' => array(
1391
					'id' => array('match' => '(\d+)'),
1392
					'alt' => array('optional' => true),
1393
					'width' => array('optional' => true, 'match' => '(\d+)'),
1394
					'height' => array('optional' => true, 'match' => '(\d+)'),
1395
					'display' => array('optional' => true, 'match' => '(link|embed)'),
1396
				),
1397
				'content' => '$1',
1398
				'validate' => function(&$tag, &$data, $disabled, $params) use ($modSettings, $context, $sourcedir, $txt, $smcFunc)
1399
				{
1400
					$returnContext = '';
1401
1402
					// BBC or the entire attachments feature is disabled
1403
					if (empty($modSettings['attachmentEnable']) || !empty($disabled['attach']))
1404
						return $data;
1405
1406
					// Save the attach ID.
1407
					$attachID = $params['{id}'];
1408
1409
					// Kinda need this.
1410
					require_once($sourcedir . '/Subs-Attachments.php');
1411
1412
					$currentAttachment = parseAttachBBC($attachID);
1413
1414
					// parseAttachBBC will return a string ($txt key) rather than dying with a fatal_error. Up to you to decide what to do.
1415
					if (is_string($currentAttachment))
1416
						return $data = !empty($txt[$currentAttachment]) ? $txt[$currentAttachment] : $currentAttachment;
1417
1418
					// We need a display mode.
1419
					if (empty($params['{display}']))
1420
					{
1421
						// Images, video, and audio are embedded by default.
1422
						if (!empty($currentAttachment['is_image']) || strpos($currentAttachment['mime_type'], 'video/') === 0 || strpos($currentAttachment['mime_type'], 'audio/') === 0)
1423
							$params['{display}'] = 'embed';
1424
						// Anything else shows a link by default.
1425
						else
1426
							$params['{display}'] = 'link';
1427
					}
1428
1429
					// Embedded file.
1430
					if ($params['{display}'] == 'embed')
1431
					{
1432
						$alt = ' alt="' . (!empty($params['{alt}']) ? $params['{alt}'] : $currentAttachment['name']) . '"';
1433
						$title = !empty($data) ? ' title="' . $smcFunc['htmlspecialchars']($data) . '"' : '';
1434
1435
						$width = !empty($params['{width}']) ? $params['{width}'] : (!empty($currentAttachment['width']) ? $currentAttachment['width'] : '');
1436
						$height = !empty($params['{height}']) ? $params['{height}'] : (!empty($currentAttachment['height']) ? $currentAttachment['height'] : '');
1437
1438
						// Image.
1439
						if (!empty($currentAttachment['is_image']))
1440
						{
1441
							$width = !empty($width) ? ' width="' . $width . '"' : '';
1442
							$height = !empty($height) ? ' height="' . $height . '"' : '';
1443
1444
							if ($currentAttachment['thumbnail']['has_thumb'] && empty($params['{width}']) && empty($params['{height}']))
1445
								$returnContext .= '<a href="' . $currentAttachment['href'] . ';image" id="link_' . $currentAttachment['id'] . '" onclick="' . $currentAttachment['thumbnail']['javascript'] . '"><img src="' . $currentAttachment['thumbnail']['href'] . '"' . $alt . $title . ' id="thumb_' . $currentAttachment['id'] . '" class="atc_img"></a>';
1446
							else
1447
								$returnContext .= '<img src="' . $currentAttachment['href'] . ';image"' . $alt . $title . $width . $height . ' class="bbc_img"/>';
1448
						}
1449
						// Video.
1450
						elseif (strpos($currentAttachment['mime_type'], 'video/') === 0)
1451
						{
1452
							$width = !empty($width) ? ' width="' . $width . '"' : '';
1453
							$height = !empty($height) ? ' height="' . $height . '"' : '';
1454
1455
							$returnContext .= '<div class="videocontainer"><div><video controls preload="none" src="'. $currentAttachment['href'] . '" playsinline' . $width . $height . ' style="object-fit:contain;"><a href="' . $currentAttachment['href'] . '" class="bbc_link">' . $smcFunc['htmlspecialchars'](!empty($data) ? $data : $currentAttachment['name']) . '</a></video></div></div>' . (!empty($data) && $data != $currentAttachment['name'] ? '<div class="smalltext">' . $data . '</div>' : '');
1456
						}
1457
						// Audio.
1458
						elseif (strpos($currentAttachment['mime_type'], 'audio/') === 0)
1459
						{
1460
							$width = 'max-width:100%; width: ' . (!empty($width) ? $width : '400') . 'px;';
1461
							$height = !empty($height) ? 'height: ' . $height . 'px;' : '';
1462
1463
							$returnContext .= (!empty($data) && $data != $currentAttachment['name'] ? $data . ' ' : '') . '<audio controls preload="none" src="'. $currentAttachment['href'] . '" class="bbc_audio" style="vertical-align:middle;' . $width . $height . '"><a href="' . $currentAttachment['href'] . '" class="bbc_link">' . $smcFunc['htmlspecialchars'](!empty($data) ? $data : $currentAttachment['name']) . '</a></audio>';
1464
						}
1465
						// Anything else.
1466
						else
1467
						{
1468
							$width = !empty($width) ? ' width="' . $width . '"' : '';
1469
							$height = !empty($height) ? ' height="' . $height . '"' : '';
1470
1471
							$returnContext .= '<object type="' . $currentAttachment['mime_type'] . '" data="' . $currentAttachment['href'] . '"' . $width . $height . ' typemustmatch><a href="' . $currentAttachment['href'] . '" class="bbc_link">' . $smcFunc['htmlspecialchars'](!empty($data) ? $data : $currentAttachment['name']) . '</a></object>';
1472
						}
1473
					}
1474
1475
					// No image. Show a link.
1476
					else
1477
						$returnContext .= '<a href="' . $currentAttachment['href'] . '" class="bbc_link">' . $smcFunc['htmlspecialchars'](!empty($data) ? $data : $currentAttachment['name']) . '</a>';
1478
1479
					// Gotta append what we just did.
1480
					$data = $returnContext;
1481
				},
1482
			),
1483
			array(
1484
				'tag' => 'b',
1485
				'before' => '<b>',
1486
				'after' => '</b>',
1487
			),
1488
			// Legacy (equivalent to [ltr] or [rtl])
1489
			array(
1490
				'tag' => 'bdo',
1491
				'type' => 'unparsed_equals',
1492
				'before' => '<bdo dir="$1">',
1493
				'after' => '</bdo>',
1494
				'test' => '(rtl|ltr)\]',
1495
				'block_level' => true,
1496
			),
1497
			// Legacy (alias of [color=black])
1498
			array(
1499
				'tag' => 'black',
1500
				'before' => '<span style="color: black;" class="bbc_color">',
1501
				'after' => '</span>',
1502
			),
1503
			// Legacy (alias of [color=blue])
1504
			array(
1505
				'tag' => 'blue',
1506
				'before' => '<span style="color: blue;" class="bbc_color">',
1507
				'after' => '</span>',
1508
			),
1509
			array(
1510
				'tag' => 'br',
1511
				'type' => 'closed',
1512
				'content' => '<br>',
1513
			),
1514
			array(
1515
				'tag' => 'center',
1516
				'before' => '<div class="centertext">',
1517
				'after' => '</div>',
1518
				'block_level' => true,
1519
			),
1520
			array(
1521
				'tag' => 'code',
1522
				'type' => 'unparsed_content',
1523
				'content' => '<div class="codeheader"><span class="code floatleft">' . $txt['code'] . '</span> <a class="codeoperation smf_select_text">' . $txt['code_select'] . '</a> <a class="codeoperation smf_expand_code hidden" data-shrink-txt="' . $txt['code_shrink'] . '" data-expand-txt="' . $txt['code_expand'] . '">' . $txt['code_expand'] . '</a></div><code class="bbc_code">$1</code>',
1524
				// @todo Maybe this can be simplified?
1525
				'validate' => isset($disabled['code']) ? null : function(&$tag, &$data, $disabled) use ($context)
1526
				{
1527
					if (!isset($disabled['code']))
1528
					{
1529
						$php_parts = preg_split('~(&lt;\?php|\?&gt;)~', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
1530
1531
						for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
1532
						{
1533
							// Do PHP code coloring?
1534
							if ($php_parts[$php_i] != '&lt;?php')
1535
								continue;
1536
1537
							$php_string = '';
1538
							while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != '?&gt;')
1539
							{
1540
								$php_string .= $php_parts[$php_i];
1541
								$php_parts[$php_i++] = '';
1542
							}
1543
							$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
1544
						}
1545
1546
						// Fix the PHP code stuff...
1547
						$data = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode('', $php_parts));
1548
						$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
1549
1550
						// Recent Opera bug requiring temporary fix. &nsbp; is needed before </code> to avoid broken selection.
1551
						if (!empty($context['browser']['is_opera']))
1552
							$data .= '&nbsp;';
1553
					}
1554
				},
1555
				'block_level' => true,
1556
			),
1557
			array(
1558
				'tag' => 'code',
1559
				'type' => 'unparsed_equals_content',
1560
				'content' => '<div class="codeheader"><span class="code floatleft">' . $txt['code'] . '</span> ($2) <a class="codeoperation smf_select_text">' . $txt['code_select'] . '</a> <a class="codeoperation smf_expand_code hidden" data-shrink-txt="' . $txt['code_shrink'] . '" data-expand-txt="' . $txt['code_expand'] . '">' . $txt['code_expand'] . '</a></div><code class="bbc_code">$1</code>',
1561
				// @todo Maybe this can be simplified?
1562
				'validate' => isset($disabled['code']) ? null : function(&$tag, &$data, $disabled) use ($context)
1563
				{
1564
					if (!isset($disabled['code']))
1565
					{
1566
						$php_parts = preg_split('~(&lt;\?php|\?&gt;)~', $data[0], -1, PREG_SPLIT_DELIM_CAPTURE);
1567
1568
						for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
1569
						{
1570
							// Do PHP code coloring?
1571
							if ($php_parts[$php_i] != '&lt;?php')
1572
								continue;
1573
1574
							$php_string = '';
1575
							while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != '?&gt;')
1576
							{
1577
								$php_string .= $php_parts[$php_i];
1578
								$php_parts[$php_i++] = '';
1579
							}
1580
							$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
1581
						}
1582
1583
						// Fix the PHP code stuff...
1584
						$data[0] = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode('', $php_parts));
1585
						$data[0] = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data[0]);
1586
1587
						// Recent Opera bug requiring temporary fix. &nsbp; is needed before </code> to avoid broken selection.
1588
						if (!empty($context['browser']['is_opera']))
1589
							$data[0] .= '&nbsp;';
1590
					}
1591
				},
1592
				'block_level' => true,
1593
			),
1594
			array(
1595
				'tag' => 'color',
1596
				'type' => 'unparsed_equals',
1597
				'test' => '(#[\da-fA-F]{3}|#[\da-fA-F]{6}|[A-Za-z]{1,20}|rgb\((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s?,\s?){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\))\]',
1598
				'before' => '<span style="color: $1;" class="bbc_color">',
1599
				'after' => '</span>',
1600
			),
1601
			array(
1602
				'tag' => 'email',
1603
				'type' => 'unparsed_content',
1604
				'content' => '<a href="mailto:$1" class="bbc_email">$1</a>',
1605
				// @todo Should this respect guest_hideContacts?
1606
				'validate' => function(&$tag, &$data, $disabled)
1607
				{
1608
					$data = strtr($data, array('<br>' => ''));
1609
				},
1610
			),
1611
			array(
1612
				'tag' => 'email',
1613
				'type' => 'unparsed_equals',
1614
				'before' => '<a href="mailto:$1" class="bbc_email">',
1615
				'after' => '</a>',
1616
				// @todo Should this respect guest_hideContacts?
1617
				'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
1618
				'disabled_after' => ' ($1)',
1619
			),
1620
			// Legacy (and just a link even when not disabled)
1621
			array(
1622
				'tag' => 'flash',
1623
				'type' => 'unparsed_commas_content',
1624
				'test' => '\d+,\d+\]',
1625
				'content' => '<a href="$1" target="_blank" rel="noopener">$1</a>',
1626
				'validate' => function (&$tag, &$data, $disabled)
1627
				{
1628
					$scheme = parse_url($data[0], PHP_URL_SCHEME);
1629
					if (empty($scheme))
1630
						$data[0] = '//' . ltrim($data[0], ':/');
1631
				},
1632
			),
1633
			array(
1634
				'tag' => 'float',
1635
				'type' => 'unparsed_equals',
1636
				'test' => '(left|right)(\s+max=\d+(?:%|px|em|rem|ex|pt|pc|ch|vw|vh|vmin|vmax|cm|mm|in)?)?\]',
1637
				'before' => '<div $1>',
1638
				'after' => '</div>',
1639
				'validate' => function(&$tag, &$data, $disabled)
1640
				{
1641
					$class = 'class="bbc_float float' . (strpos($data, 'left') === 0 ? 'left' : 'right') . '"';
1642
1643
					if (preg_match('~\bmax=(\d+(?:%|px|em|rem|ex|pt|pc|ch|vw|vh|vmin|vmax|cm|mm|in)?)~', $data, $matches))
1644
						$css = ' style="max-width:' . $matches[1] . (is_numeric($matches[1]) ? 'px' : '') . '"';
1645
					else
1646
						$css = '';
1647
1648
					$data = $class . $css;
1649
				},
1650
				'trim' => 'outside',
1651
				'block_level' => true,
1652
			),
1653
			// Legacy (alias of [url] with an FTP URL)
1654
			array(
1655
				'tag' => 'ftp',
1656
				'type' => 'unparsed_content',
1657
				'content' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">$1</a>',
1658
				'validate' => function(&$tag, &$data, $disabled)
1659
				{
1660
					$data = strtr($data, array('<br>' => ''));
1661
					$scheme = parse_url($data, PHP_URL_SCHEME);
1662
					if (empty($scheme))
1663
						$data = 'ftp://' . ltrim($data, ':/');
1664
				},
1665
			),
1666
			// Legacy (alias of [url] with an FTP URL)
1667
			array(
1668
				'tag' => 'ftp',
1669
				'type' => 'unparsed_equals',
1670
				'before' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">',
1671
				'after' => '</a>',
1672
				'validate' => function(&$tag, &$data, $disabled)
1673
				{
1674
					$scheme = parse_url($data, PHP_URL_SCHEME);
1675
					if (empty($scheme))
1676
						$data = 'ftp://' . ltrim($data, ':/');
1677
				},
1678
				'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
1679
				'disabled_after' => ' ($1)',
1680
			),
1681
			array(
1682
				'tag' => 'font',
1683
				'type' => 'unparsed_equals',
1684
				'test' => '[A-Za-z0-9_,\-\s]+?\]',
1685
				'before' => '<span style="font-family: $1;" class="bbc_font">',
1686
				'after' => '</span>',
1687
			),
1688
			// Legacy (one of those things that should not be done)
1689
			array(
1690
				'tag' => 'glow',
1691
				'type' => 'unparsed_commas',
1692
				'test' => '[#0-9a-zA-Z\-]{3,12},([012]\d{1,2}|\d{1,2})(,[^]]+)?\]',
1693
				'before' => '<span style="text-shadow: $1 1px 1px 1px">',
1694
				'after' => '</span>',
1695
			),
1696
			// Legacy (alias of [color=green])
1697
			array(
1698
				'tag' => 'green',
1699
				'before' => '<span style="color: green;" class="bbc_color">',
1700
				'after' => '</span>',
1701
			),
1702
			array(
1703
				'tag' => 'html',
1704
				'type' => 'unparsed_content',
1705
				'content' => '<div>$1</div>',
1706
				'block_level' => true,
1707
				'disabled_content' => '$1',
1708
			),
1709
			array(
1710
				'tag' => 'hr',
1711
				'type' => 'closed',
1712
				'content' => '<hr>',
1713
				'block_level' => true,
1714
			),
1715
			array(
1716
				'tag' => 'i',
1717
				'before' => '<i>',
1718
				'after' => '</i>',
1719
			),
1720
			array(
1721
				'tag' => 'img',
1722
				'type' => 'unparsed_content',
1723
				'parameters' => array(
1724
					'alt' => array('optional' => true),
1725
					'title' => array('optional' => true),
1726
					'width' => array('optional' => true, 'value' => ' width="$1"', 'match' => '(\d+)'),
1727
					'height' => array('optional' => true, 'value' => ' height="$1"', 'match' => '(\d+)'),
1728
				),
1729
				'content' => '<img src="$1" alt="{alt}" title="{title}"{width}{height} class="bbc_img resized">',
1730
				'validate' => function(&$tag, &$data, $disabled)
1731
				{
1732
					$data = strtr($data, array('<br>' => ''));
1733
1734
					if (parse_url($data, PHP_URL_SCHEME) === null)
1735
						$data = '//' . ltrim($data, ':/');
1736
					else
1737
						$data = get_proxied_url($data);
1738
				},
1739
				'disabled_content' => '($1)',
1740
			),
1741
			array(
1742
				'tag' => 'img',
1743
				'type' => 'unparsed_content',
1744
				'content' => '<img src="$1" alt="" class="bbc_img">',
1745
				'validate' => function(&$tag, &$data, $disabled)
1746
				{
1747
					$data = strtr($data, array('<br>' => ''));
1748
1749
					if (parse_url($data, PHP_URL_SCHEME) === null)
1750
						$data = '//' . ltrim($data, ':/');
1751
					else
1752
						$data = get_proxied_url($data);
1753
				},
1754
				'disabled_content' => '($1)',
1755
			),
1756
			array(
1757
				'tag' => 'iurl',
1758
				'type' => 'unparsed_content',
1759
				'content' => '<a href="$1" class="bbc_link">$1</a>',
1760
				'validate' => function(&$tag, &$data, $disabled)
1761
				{
1762
					$data = strtr($data, array('<br>' => ''));
1763
					$scheme = parse_url($data, PHP_URL_SCHEME);
1764
					if (empty($scheme))
1765
						$data = '//' . ltrim($data, ':/');
1766
				},
1767
			),
1768
			array(
1769
				'tag' => 'iurl',
1770
				'type' => 'unparsed_equals',
1771
				'quoted' => 'optional',
1772
				'before' => '<a href="$1" class="bbc_link">',
1773
				'after' => '</a>',
1774
				'validate' => function(&$tag, &$data, $disabled)
1775
				{
1776
					if (substr($data, 0, 1) == '#')
1777
						$data = '#post_' . substr($data, 1);
1778
					else
1779
					{
1780
						$scheme = parse_url($data, PHP_URL_SCHEME);
1781
						if (empty($scheme))
1782
							$data = '//' . ltrim($data, ':/');
1783
					}
1784
				},
1785
				'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
1786
				'disabled_after' => ' ($1)',
1787
			),
1788
			array(
1789
				'tag' => 'justify',
1790
				'before' => '<div style="text-align: justify;">',
1791
				'after' => '</div>',
1792
				'block_level' => true,
1793
			),
1794
			array(
1795
				'tag' => 'left',
1796
				'before' => '<div style="text-align: left;">',
1797
				'after' => '</div>',
1798
				'block_level' => true,
1799
			),
1800
			array(
1801
				'tag' => 'li',
1802
				'before' => '<li>',
1803
				'after' => '</li>',
1804
				'trim' => 'outside',
1805
				'require_parents' => array('list'),
1806
				'block_level' => true,
1807
				'disabled_before' => '',
1808
				'disabled_after' => '<br>',
1809
			),
1810
			array(
1811
				'tag' => 'list',
1812
				'before' => '<ul class="bbc_list">',
1813
				'after' => '</ul>',
1814
				'trim' => 'inside',
1815
				'require_children' => array('li', 'list'),
1816
				'block_level' => true,
1817
			),
1818
			array(
1819
				'tag' => 'list',
1820
				'parameters' => array(
1821
					'type' => array('match' => '(none|disc|circle|square|decimal|decimal-leading-zero|lower-roman|upper-roman|lower-alpha|upper-alpha|lower-greek|upper-greek|lower-latin|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha)'),
1822
				),
1823
				'before' => '<ul class="bbc_list" style="list-style-type: {type};">',
1824
				'after' => '</ul>',
1825
				'trim' => 'inside',
1826
				'require_children' => array('li'),
1827
				'block_level' => true,
1828
			),
1829
			array(
1830
				'tag' => 'ltr',
1831
				'before' => '<bdo dir="ltr">',
1832
				'after' => '</bdo>',
1833
				'block_level' => true,
1834
			),
1835
			array(
1836
				'tag' => 'me',
1837
				'type' => 'unparsed_equals',
1838
				'before' => '<div class="meaction">* $1 ',
1839
				'after' => '</div>',
1840
				'quoted' => 'optional',
1841
				'block_level' => true,
1842
				'disabled_before' => '/me ',
1843
				'disabled_after' => '<br>',
1844
			),
1845
			array(
1846
				'tag' => 'member',
1847
				'type' => 'unparsed_equals',
1848
				'before' => '<a href="' . $scripturl . '?action=profile;u=$1" class="mention" data-mention="$1">@',
1849
				'after' => '</a>',
1850
			),
1851
			// Legacy (horrible memories of the 1990s)
1852
			array(
1853
				'tag' => 'move',
1854
				'before' => '<marquee>',
1855
				'after' => '</marquee>',
1856
				'block_level' => true,
1857
				'disallow_children' => array('move'),
1858
			),
1859
			array(
1860
				'tag' => 'nobbc',
1861
				'type' => 'unparsed_content',
1862
				'content' => '$1',
1863
			),
1864
			array(
1865
				'tag' => 'php',
1866
				'type' => 'unparsed_content',
1867
				'content' => '<span class="phpcode">$1</span>',
1868
				'validate' => isset($disabled['php']) ? null : function(&$tag, &$data, $disabled)
1869
				{
1870
					if (!isset($disabled['php']))
1871
					{
1872
						$add_begin = substr(trim($data), 0, 5) != '&lt;?';
1873
						$data = highlight_php_code($add_begin ? '&lt;?php ' . $data . '?&gt;' : $data);
1874
						if ($add_begin)
1875
							$data = preg_replace(array('~^(.+?)&lt;\?.{0,40}?php(?:&nbsp;|\s)~', '~\?&gt;((?:</(font|span)>)*)$~'), '$1', $data, 2);
1876
					}
1877
				},
1878
				'block_level' => false,
1879
				'disabled_content' => '$1',
1880
			),
1881
			array(
1882
				'tag' => 'pre',
1883
				'before' => '<pre>',
1884
				'after' => '</pre>',
1885
			),
1886
			array(
1887
				'tag' => 'quote',
1888
				'before' => '<blockquote><cite>' . $txt['quote'] . '</cite>',
1889
				'after' => '</blockquote>',
1890
				'trim' => 'both',
1891
				'block_level' => true,
1892
			),
1893
			array(
1894
				'tag' => 'quote',
1895
				'parameters' => array(
1896
					'author' => array('match' => '(.{1,192}?)', 'quoted' => true),
1897
				),
1898
				'before' => '<blockquote><cite>' . $txt['quote_from'] . ': {author}</cite>',
1899
				'after' => '</blockquote>',
1900
				'trim' => 'both',
1901
				'block_level' => true,
1902
			),
1903
			array(
1904
				'tag' => 'quote',
1905
				'type' => 'parsed_equals',
1906
				'before' => '<blockquote><cite>' . $txt['quote_from'] . ': $1</cite>',
1907
				'after' => '</blockquote>',
1908
				'trim' => 'both',
1909
				'quoted' => 'optional',
1910
				// Don't allow everything to be embedded with the author name.
1911
				'parsed_tags_allowed' => array('url', 'iurl', 'ftp'),
1912
				'block_level' => true,
1913
			),
1914
			array(
1915
				'tag' => 'quote',
1916
				'parameters' => array(
1917
					'author' => array('match' => '([^<>]{1,192}?)'),
1918
					'link' => array('match' => '(?:board=\d+;)?((?:topic|threadid)=[\dmsg#\./]{1,40}(?:;start=[\dmsg#\./]{1,40})?|msg=\d+?|action=profile;u=\d+)'),
1919
					'date' => array('match' => '(\d+)', 'validate' => 'timeformat'),
1920
				),
1921
				'before' => '<blockquote><cite><a href="' . $scripturl . '?{link}">' . $txt['quote_from'] . ': {author} ' . $txt['search_on'] . ' {date}</a></cite>',
1922
				'after' => '</blockquote>',
1923
				'trim' => 'both',
1924
				'block_level' => true,
1925
			),
1926
			array(
1927
				'tag' => 'quote',
1928
				'parameters' => array(
1929
					'author' => array('match' => '(.{1,192}?)'),
1930
				),
1931
				'before' => '<blockquote><cite>' . $txt['quote_from'] . ': {author}</cite>',
1932
				'after' => '</blockquote>',
1933
				'trim' => 'both',
1934
				'block_level' => true,
1935
			),
1936
			// Legacy (alias of [color=red])
1937
			array(
1938
				'tag' => 'red',
1939
				'before' => '<span style="color: red;" class="bbc_color">',
1940
				'after' => '</span>',
1941
			),
1942
			array(
1943
				'tag' => 'right',
1944
				'before' => '<div style="text-align: right;">',
1945
				'after' => '</div>',
1946
				'block_level' => true,
1947
			),
1948
			array(
1949
				'tag' => 'rtl',
1950
				'before' => '<bdo dir="rtl">',
1951
				'after' => '</bdo>',
1952
				'block_level' => true,
1953
			),
1954
			array(
1955
				'tag' => 's',
1956
				'before' => '<s>',
1957
				'after' => '</s>',
1958
			),
1959
			// Legacy (never a good idea)
1960
			array(
1961
				'tag' => 'shadow',
1962
				'type' => 'unparsed_commas',
1963
				'test' => '[#0-9a-zA-Z\-]{3,12},(left|right|top|bottom|[0123]\d{0,2})\]',
1964
				'before' => '<span style="text-shadow: $1 $2">',
1965
				'after' => '</span>',
1966
				'validate' => function(&$tag, &$data, $disabled)
1967
				{
1968
1969
					if ($data[1] == 'top' || (is_numeric($data[1]) && $data[1] < 50))
1970
						$data[1] = '0 -2px 1px';
1971
1972
					elseif ($data[1] == 'right' || (is_numeric($data[1]) && $data[1] < 100))
1973
						$data[1] = '2px 0 1px';
1974
1975
					elseif ($data[1] == 'bottom' || (is_numeric($data[1]) && $data[1] < 190))
1976
						$data[1] = '0 2px 1px';
1977
1978
					elseif ($data[1] == 'left' || (is_numeric($data[1]) && $data[1] < 280))
1979
						$data[1] = '-2px 0 1px';
1980
1981
					else
1982
						$data[1] = '1px 1px 1px';
1983
				},
1984
			),
1985
			array(
1986
				'tag' => 'size',
1987
				'type' => 'unparsed_equals',
1988
				'test' => '([1-9][\d]?p[xt]|small(?:er)?|large[r]?|x[x]?-(?:small|large)|medium|(0\.[1-9]|[1-9](\.[\d][\d]?)?)?em)\]',
1989
				'before' => '<span style="font-size: $1;" class="bbc_size">',
1990
				'after' => '</span>',
1991
			),
1992
			array(
1993
				'tag' => 'size',
1994
				'type' => 'unparsed_equals',
1995
				'test' => '[1-7]\]',
1996
				'before' => '<span style="font-size: $1;" class="bbc_size">',
1997
				'after' => '</span>',
1998
				'validate' => function(&$tag, &$data, $disabled)
1999
				{
2000
					$sizes = array(1 => 0.7, 2 => 1.0, 3 => 1.35, 4 => 1.45, 5 => 2.0, 6 => 2.65, 7 => 3.95);
2001
					$data = $sizes[$data] . 'em';
2002
				},
2003
			),
2004
			array(
2005
				'tag' => 'sub',
2006
				'before' => '<sub>',
2007
				'after' => '</sub>',
2008
			),
2009
			array(
2010
				'tag' => 'sup',
2011
				'before' => '<sup>',
2012
				'after' => '</sup>',
2013
			),
2014
			array(
2015
				'tag' => 'table',
2016
				'before' => '<table class="bbc_table">',
2017
				'after' => '</table>',
2018
				'trim' => 'inside',
2019
				'require_children' => array('tr'),
2020
				'block_level' => true,
2021
			),
2022
			array(
2023
				'tag' => 'td',
2024
				'before' => '<td>',
2025
				'after' => '</td>',
2026
				'require_parents' => array('tr'),
2027
				'trim' => 'outside',
2028
				'block_level' => true,
2029
				'disabled_before' => '',
2030
				'disabled_after' => '',
2031
			),
2032
			array(
2033
				'tag' => 'time',
2034
				'type' => 'unparsed_content',
2035
				'content' => '$1',
2036
				'validate' => function(&$tag, &$data, $disabled)
2037
				{
2038
					if (is_numeric($data))
2039
						$data = timeformat($data);
2040
					else
2041
						$tag['content'] = '[time]$1[/time]';
2042
				},
2043
			),
2044
			array(
2045
				'tag' => 'tr',
2046
				'before' => '<tr>',
2047
				'after' => '</tr>',
2048
				'require_parents' => array('table'),
2049
				'require_children' => array('td'),
2050
				'trim' => 'both',
2051
				'block_level' => true,
2052
				'disabled_before' => '',
2053
				'disabled_after' => '',
2054
			),
2055
			// Legacy (the <tt> element is dead)
2056
			array(
2057
				'tag' => 'tt',
2058
				'before' => '<span class="monospace">',
2059
				'after' => '</span>',
2060
			),
2061
			array(
2062
				'tag' => 'u',
2063
				'before' => '<u>',
2064
				'after' => '</u>',
2065
			),
2066
			array(
2067
				'tag' => 'url',
2068
				'type' => 'unparsed_content',
2069
				'content' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">$1</a>',
2070
				'validate' => function(&$tag, &$data, $disabled)
2071
				{
2072
					$data = strtr($data, array('<br>' => ''));
2073
					$scheme = parse_url($data, PHP_URL_SCHEME);
2074
					if (empty($scheme))
2075
						$data = '//' . ltrim($data, ':/');
2076
				},
2077
			),
2078
			array(
2079
				'tag' => 'url',
2080
				'type' => 'unparsed_equals',
2081
				'quoted' => 'optional',
2082
				'before' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">',
2083
				'after' => '</a>',
2084
				'validate' => function(&$tag, &$data, $disabled)
2085
				{
2086
					$scheme = parse_url($data, PHP_URL_SCHEME);
2087
					if (empty($scheme))
2088
						$data = '//' . ltrim($data, ':/');
2089
				},
2090
				'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
2091
				'disabled_after' => ' ($1)',
2092
			),
2093
			// Legacy (alias of [color=white])
2094
			array(
2095
				'tag' => 'white',
2096
				'before' => '<span style="color: white;" class="bbc_color">',
2097
				'after' => '</span>',
2098
			),
2099
			array(
2100
				'tag' => 'youtube',
2101
				'type' => 'unparsed_content',
2102
				'content' => '<div class="videocontainer"><div><iframe frameborder="0" src="https://www.youtube.com/embed/$1?origin=' . $hosturl . '&wmode=opaque" data-youtube-id="$1" allowfullscreen></iframe></div></div>',
2103
				'disabled_content' => '<a href="https://www.youtube.com/watch?v=$1" target="_blank" rel="noopener">https://www.youtube.com/watch?v=$1</a>',
2104
				'block_level' => true,
2105
			),
2106
		);
2107
2108
		// Inside these tags autolink is not recommendable.
2109
		$no_autolink_tags = array(
2110
			'url',
2111
			'iurl',
2112
			'email',
2113
			'img',
2114
			'html',
2115
		);
2116
2117
		// Let mods add new BBC without hassle.
2118
		call_integration_hook('integrate_bbc_codes', array(&$codes, &$no_autolink_tags));
2119
2120
		// This is mainly for the bbc manager, so it's easy to add tags above.  Custom BBC should be added above this line.
2121
		if ($message === false)
2122
		{
2123
			usort($codes, function($a, $b)
2124
			{
2125
				return strcmp($a['tag'], $b['tag']);
2126
			});
2127
			return $codes;
2128
		}
2129
2130
		// So the parser won't skip them.
2131
		$itemcodes = array(
2132
			'*' => 'disc',
2133
			'@' => 'disc',
2134
			'+' => 'square',
2135
			'x' => 'square',
2136
			'#' => 'square',
2137
			'o' => 'circle',
2138
			'O' => 'circle',
2139
			'0' => 'circle',
2140
		);
2141
		if (!isset($disabled['li']) && !isset($disabled['list']))
2142
		{
2143
			foreach ($itemcodes as $c => $dummy)
2144
				$bbc_codes[$c] = array();
2145
		}
2146
2147
		// Shhhh!
2148
		if (!isset($disabled['color']))
2149
		{
2150
			$codes[] = array(
2151
				'tag' => 'chrissy',
2152
				'before' => '<span style="color: #cc0099;">',
2153
				'after' => ' :-*</span>',
2154
			);
2155
			$codes[] = array(
2156
				'tag' => 'kissy',
2157
				'before' => '<span style="color: #cc0099;">',
2158
				'after' => ' :-*</span>',
2159
			);
2160
		}
2161
		$codes[] = array(
2162
			'tag' => 'cowsay',
2163
			'parameters' => array(
2164
				'e' => array('optional' => true, 'quoted' => true, 'match' => '(.*?)', 'default' => 'oo', 'validate' => function ($eyes) use ($smcFunc)
2165
					{
2166
						static $css_added;
2167
2168
						if (empty($css_added))
2169
						{
2170
							$css = base64_decode('cHJlW2RhdGEtZV1bZGF0YS10XXt3aGl0ZS1zcGFjZTpwcmUtd3JhcDtsaW5lLWhlaWdodDppbml0aWFsO31wcmVbZGF0YS1lXVtkYXRhLXRdID4gZGl2e2Rpc3BsYXk6dGFibGU7Ym9yZGVyOjFweCBzb2xpZDtib3JkZXItcmFkaXVzOjAuNWVtO3BhZGRpbmc6MWNoO21heC13aWR0aDo4MGNoO21pbi13aWR0aDoxMmNoO31wcmVbZGF0YS1lXVtkYXRhLXRdOjphZnRlcntkaXNwbGF5OmlubGluZS1ibG9jazttYXJnaW4tbGVmdDo4Y2g7bWluLXdpZHRoOjIwY2g7ZGlyZWN0aW9uOmx0cjtjb250ZW50OidcNUMgICBeX19eXEEgIFw1QyAgKCcgYXR0cihkYXRhLWUpICcpXDVDX19fX19fX1xBICAgIChfXylcNUMgICAgICAgIClcNUMvXDVDXEEgICAgICcgYXR0cihkYXRhLXQpICcgfHwtLS0tdyB8XEEgICAgICAgIHx8ICAgICB8fCc7fQ==');
2171
2172
							addInlineJavaScript('
2173
								$("head").append("<style>" + ' . JavaScriptEscape($css) . ' + "</style>");', true);
2174
2175
							$css_added = true;
2176
						}
2177
2178
						return $smcFunc['substr']($eyes . 'oo', 0, 2);
2179
					},
2180
				),
2181
				't' => array('optional' => true, 'quoted' => true, 'match' => '(.*?)', 'default' => '  ', 'validate' => function ($tongue) use ($smcFunc)
2182
					{
2183
						return $smcFunc['substr']($tongue . '  ', 0, 2);
2184
					},
2185
				),
2186
			),
2187
			'before' => '<pre data-e="{e}" data-t="{t}"><div>',
2188
			'after' => '</div></pre>',
2189
			'block_level' => true,
2190
		);
2191
2192
		foreach ($codes as $code)
2193
		{
2194
			// Make it easier to process parameters later
2195
			if (!empty($code['parameters']))
2196
				ksort($code['parameters'], SORT_STRING);
2197
2198
			// If we are not doing every tag only do ones we are interested in.
2199
			if (empty($parse_tags) || in_array($code['tag'], $parse_tags))
2200
				$bbc_codes[substr($code['tag'], 0, 1)][] = $code;
2201
		}
2202
		$codes = null;
2203
	}
2204
2205
	// Shall we take the time to cache this?
2206
	if ($cache_id != '' && !empty($cache_enable) && (($cache_enable >= 2 && isset($message[1000])) || isset($message[2400])) && empty($parse_tags))
2207
	{
2208
		// It's likely this will change if the message is modified.
2209
		$cache_key = 'parse:' . $cache_id . '-' . md5(md5($message) . '-' . $smileys . (empty($disabled) ? '' : implode(',', array_keys($disabled))) . $smcFunc['json_encode']($context['browser']) . $txt['lang_locale'] . $user_info['time_offset'] . $user_info['time_format']);
2210
2211
		if (($temp = cache_get_data($cache_key, 240)) != null)
0 ignored issues
show
It seems like you are loosely comparing $temp = cache_get_data($cache_key, 240) of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
2212
			return $temp;
2213
2214
		$cache_t = microtime(true);
2215
	}
2216
2217
	if ($smileys === 'print')
2218
	{
2219
		// [glow], [shadow], and [move] can't really be printed.
2220
		$disabled['glow'] = true;
2221
		$disabled['shadow'] = true;
2222
		$disabled['move'] = true;
2223
2224
		// Colors can't well be displayed... supposed to be black and white.
2225
		$disabled['color'] = true;
2226
		$disabled['black'] = true;
2227
		$disabled['blue'] = true;
2228
		$disabled['white'] = true;
2229
		$disabled['red'] = true;
2230
		$disabled['green'] = true;
2231
		$disabled['me'] = true;
2232
2233
		// Color coding doesn't make sense.
2234
		$disabled['php'] = true;
2235
2236
		// Links are useless on paper... just show the link.
2237
		$disabled['ftp'] = true;
2238
		$disabled['url'] = true;
2239
		$disabled['iurl'] = true;
2240
		$disabled['email'] = true;
2241
		$disabled['flash'] = true;
2242
2243
		// @todo Change maybe?
2244
		if (!isset($_GET['images']))
2245
			$disabled['img'] = true;
2246
2247
		// Maybe some custom BBC need to be disabled for printing.
2248
		call_integration_hook('integrate_bbc_print', array(&$disabled));
2249
	}
2250
2251
	$open_tags = array();
2252
	$message = strtr($message, array("\n" => '<br>'));
2253
2254
	if (!empty($parse_tags))
2255
	{
2256
		$real_alltags_regex = $alltags_regex;
2257
		$alltags_regex = '';
2258
	}
2259
	if (empty($alltags_regex))
2260
	{
2261
		$alltags = array();
2262
		foreach ($bbc_codes as $section)
2263
		{
2264
			foreach ($section as $code)
2265
				$alltags[] = $code['tag'];
2266
		}
2267
		$alltags_regex = '(?' . '>\b' . build_regex(array_unique($alltags)) . '\b|' . build_regex(array_keys($itemcodes)) . ')';
2268
	}
2269
2270
	$pos = -1;
2271
	while ($pos !== false)
2272
	{
2273
		$last_pos = isset($last_pos) ? max($pos, $last_pos) : $pos;
2274
		preg_match('~\[/?(?=' . $alltags_regex . ')~i', $message, $matches, PREG_OFFSET_CAPTURE, $pos + 1);
2275
		$pos = isset($matches[0][1]) ? $matches[0][1] : false;
2276
2277
		// Failsafe.
2278
		if ($pos === false || $last_pos > $pos)
2279
			$pos = strlen($message) + 1;
2280
2281
		// Can't have a one letter smiley, URL, or email! (Sorry.)
2282
		if ($last_pos < $pos - 1)
2283
		{
2284
			// Make sure the $last_pos is not negative.
2285
			$last_pos = max($last_pos, 0);
2286
2287
			// Pick a block of data to do some raw fixing on.
2288
			$data = substr($message, $last_pos, $pos - $last_pos);
2289
2290
			$placeholders = array();
2291
			$placeholders_counter = 0;
2292
2293
			// Take care of some HTML!
2294
			if (!empty($modSettings['enablePostHTML']) && strpos($data, '&lt;') !== false)
2295
			{
2296
				$data = preg_replace('~&lt;a\s+href=((?:&quot;)?)((?:https?://|ftps?://|mailto:|tel:)\S+?)\\1&gt;(.*?)&lt;/a&gt;~i', '[url=&quot;$2&quot;]$3[/url]', $data);
2297
2298
				// <br> should be empty.
2299
				$empty_tags = array('br', 'hr');
2300
				foreach ($empty_tags as $tag)
2301
					$data = str_replace(array('&lt;' . $tag . '&gt;', '&lt;' . $tag . '/&gt;', '&lt;' . $tag . ' /&gt;'), '<' . $tag . '>', $data);
2302
2303
				// b, u, i, s, pre... basic tags.
2304
				$closable_tags = array('b', 'u', 'i', 's', 'em', 'ins', 'del', 'pre', 'blockquote', 'strong');
2305
				foreach ($closable_tags as $tag)
2306
				{
2307
					$diff = substr_count($data, '&lt;' . $tag . '&gt;') - substr_count($data, '&lt;/' . $tag . '&gt;');
2308
					$data = strtr($data, array('&lt;' . $tag . '&gt;' => '<' . $tag . '>', '&lt;/' . $tag . '&gt;' => '</' . $tag . '>'));
2309
2310
					if ($diff > 0)
2311
						$data = substr($data, 0, -1) . str_repeat('</' . $tag . '>', $diff) . substr($data, -1);
2312
				}
2313
2314
				// Do <img ...> - with security... action= -> action-.
2315
				preg_match_all('~&lt;img\s+src=((?:&quot;)?)((?:https?://|ftps?://)\S+?)\\1(?:\s+alt=(&quot;.*?&quot;|\S*?))?(?:\s?/)?&gt;~i', $data, $matches, PREG_PATTERN_ORDER);
2316
				if (!empty($matches[0]))
2317
				{
2318
					$replaces = array();
2319
					foreach ($matches[2] as $match => $imgtag)
2320
					{
2321
						$alt = empty($matches[3][$match]) ? '' : ' alt=' . preg_replace('~^&quot;|&quot;$~', '', $matches[3][$match]);
2322
2323
						// Remove action= from the URL - no funny business, now.
2324
						// @todo Testing this preg_match seems pointless
2325
						if (preg_match('~action(=|%3d)(?!dlattach)~i', $imgtag) != 0)
2326
							$imgtag = preg_replace('~action(?:=|%3d)(?!dlattach)~i', 'action-', $imgtag);
2327
2328
						$placeholder = '<placeholder ' . ++$placeholders_counter . '>';
2329
						$placeholders[$placeholder] = '[img' . $alt . ']' . $imgtag . '[/img]';
2330
2331
						$replaces[$matches[0][$match]] = $placeholder;
2332
					}
2333
2334
					$data = strtr($data, $replaces);
2335
				}
2336
			}
2337
2338
			if (!empty($modSettings['autoLinkUrls']))
2339
			{
2340
				// Are we inside tags that should be auto linked?
2341
				$no_autolink_area = false;
2342
				if (!empty($open_tags))
2343
				{
2344
					foreach ($open_tags as $open_tag)
2345
						if (in_array($open_tag['tag'], $no_autolink_tags))
2346
							$no_autolink_area = true;
2347
				}
2348
2349
				// Don't go backwards.
2350
				// @todo Don't think is the real solution....
2351
				$lastAutoPos = isset($lastAutoPos) ? $lastAutoPos : 0;
2352
				if ($pos < $lastAutoPos)
2353
					$no_autolink_area = true;
2354
				$lastAutoPos = $pos;
2355
2356
				if (!$no_autolink_area)
2357
				{
2358
					// An &nbsp; right after a URL can break the autolinker
2359
					if (strpos($data, '&nbsp;') !== false)
2360
					{
2361
						$placeholders['<placeholder non-breaking-space>'] = '&nbsp;';
2362
						$data = strtr($data, array('&nbsp;' => '<placeholder non-breaking-space>'));
2363
					}
2364
2365
					// Parse any URLs
2366
					if (!isset($disabled['url']) && strpos($data, '[url') === false)
2367
					{
2368
						// For efficiency, first define the TLD regex in a PCRE subroutine
2369
						$url_regex = '(?(DEFINE)(?<tlds>' . $modSettings['tld_regex'] . '))';
2370
2371
						// Now build the rest of the regex
2372
						$url_regex .=
2373
						// 1. IRI scheme and domain components
2374
						'(?:' .
2375
							// 1a. IRIs with a scheme, or at least an opening "//"
2376
							'(?:' .
2377
2378
								// URI scheme (or lack thereof for schemeless URLs)
2379
								'(?:' .
2380
									// URL scheme and colon
2381
									'\b[a-z][\w\-]+:' .
2382
									// or
2383
									'|' .
2384
									// A boundary followed by two slashes for schemeless URLs
2385
									'(?<=^|\W)(?=//)' .
2386
								')' .
2387
2388
								// IRI "authority" chunk
2389
								'(?:' .
2390
									// 2 slashes for IRIs with an "authority"
2391
									'//' .
2392
									// then a domain name
2393
									'(?:' .
2394
										// Either the reserved "localhost" domain name
2395
										'localhost' .
2396
										// or
2397
										'|' .
2398
										// a run of IRI characters, a dot, and a TLD
2399
										'[\p{L}\p{M}\p{N}\-.:@]+\.(?P>tlds)' .
2400
									')' .
2401
									// followed by a non-domain character or end of line
2402
									'(?=[^\p{L}\p{N}\-.]|$)' .
2403
2404
									// or, if no "authority" per se (e.g. "mailto:" URLs)...
2405
									'|' .
2406
2407
									// a run of IRI characters
2408
									'[\p{L}\p{N}][\p{L}\p{M}\p{N}\-.:@]+[\p{L}\p{M}\p{N}]' .
2409
									// and then a dot and a closing IRI label
2410
									'\.[\p{L}\p{M}\p{N}\-]+' .
2411
								')' .
2412
							')' .
2413
2414
							// Or
2415
							'|' .
2416
2417
							// 1b. Naked domains (e.g. "example.com" in "Go to example.com for an example.")
2418
							'(?:' .
2419
								// Preceded by start of line or a non-domain character
2420
								'(?<=^|[^\p{L}\p{M}\p{N}\-:@])' .
2421
								// A run of Unicode domain name characters (excluding [:@])
2422
								'[\p{L}\p{N}][\p{L}\p{M}\p{N}\-.]+[\p{L}\p{M}\p{N}]' .
2423
								// and then a dot and a valid TLD
2424
								'\.(?P>tlds)' .
2425
								// Followed by either:
2426
								'(?=' .
2427
									// end of line or a non-domain character (excluding [.:@])
2428
									'$|[^\p{L}\p{N}\-]' .
2429
									// or
2430
									'|' .
2431
									// a dot followed by end of line or a non-domain character (excluding [.:@])
2432
									'\.(?=$|[^\p{L}\p{N}\-])' .
2433
								')' .
2434
							')' .
2435
						')' .
2436
2437
						// 2. IRI path, query, and fragment components (if present)
2438
						'(?:' .
2439
2440
							// If any of these parts exist, must start with a single "/"
2441
							'/' .
2442
2443
							// And then optionally:
2444
							'(?:' .
2445
								// One or more of:
2446
								'(?:' .
2447
									// a run of non-space, non-()<>
2448
									'[^\s()<>]+' .
2449
									// or
2450
									'|' .
2451
									// balanced parentheses, up to 2 levels
2452
									'\(([^\s()<>]+|(\([^\s()<>]+\)))*\)' .
2453
								')+' .
2454
								// Ending with:
2455
								'(?:' .
2456
									// balanced parentheses, up to 2 levels
2457
									'\(([^\s()<>]+|(\([^\s()<>]+\)))*\)' .
2458
									// or
2459
									'|' .
2460
									// not a space or one of these punctuation characters
2461
									'[^\s`!()\[\]{};:\'".,<>?«»“”‘’/]' .
2462
									// or
2463
									'|' .
2464
									// a trailing slash (but not two in a row)
2465
									'(?<!/)/' .
2466
								')' .
2467
							')?' .
2468
						')?';
2469
2470
						$data = preg_replace_callback('~' . $url_regex . '~i' . ($context['utf8'] ? 'u' : ''), function($matches)
2471
						{
2472
							$url = array_shift($matches);
2473
2474
							// If this isn't a clean URL, bail out
2475
							if ($url != sanitize_iri($url))
2476
								return $url;
2477
2478
							$scheme = parse_url($url, PHP_URL_SCHEME);
2479
2480
							if ($scheme == 'mailto')
2481
							{
2482
								$email_address = str_replace('mailto:', '', $url);
2483
								if (!isset($disabled['email']) && filter_var($email_address, FILTER_VALIDATE_EMAIL) !== false)
2484
									return '[email=' . $email_address . ']' . $url . '[/email]';
2485
								else
2486
									return $url;
2487
							}
2488
2489
							// Are we linking a schemeless URL or naked domain name (e.g. "example.com")?
2490
							if (empty($scheme))
2491
								$fullUrl = '//' . ltrim($url, ':/');
2492
							else
2493
								$fullUrl = $url;
2494
2495
							// Make sure that $fullUrl really is valid
2496
							if (validate_iri((strpos($fullUrl, '//') === 0 ? 'http:' : '') . $fullUrl) === false)
2497
								return $url;
2498
2499
							return '[url=&quot;' . str_replace(array('[', ']'), array('&#91;', '&#93;'), $fullUrl) . '&quot;]' . $url . '[/url]';
2500
						}, $data);
2501
					}
2502
2503
					// Next, emails...  Must be careful not to step on enablePostHTML logic above...
2504
					if (!isset($disabled['email']) && strpos($data, '@') !== false && strpos($data, '[email') === false && stripos($data, 'mailto:') === false)
2505
					{
2506
						$email_regex = '
2507
						# Preceded by a non-domain character or start of line
2508
						(?<=^|[^\p{L}\p{M}\p{N}\-\.])
2509
2510
						# An email address
2511
						[\p{L}\p{M}\p{N}_\-.]{1,80}
2512
						@
2513
						[\p{L}\p{M}\p{N}\-.]+
2514
						\.
2515
						' . $modSettings['tld_regex'] . '
2516
2517
						# Followed by either:
2518
						(?=
2519
							# end of line or a non-domain character (excluding the dot)
2520
							$|[^\p{L}\p{M}\p{N}\-]
2521
							| # or
2522
							# a dot followed by end of line or a non-domain character
2523
							\.(?=$|[^\p{L}\p{M}\p{N}\-])
2524
						)';
2525
2526
						$data = preg_replace('~' . $email_regex . '~xi' . ($context['utf8'] ? 'u' : ''), '[email]$0[/email]', $data);
2527
					}
2528
				}
2529
			}
2530
2531
			// Restore any placeholders
2532
			$data = strtr($data, $placeholders);
2533
2534
			$data = strtr($data, array("\t" => '&nbsp;&nbsp;&nbsp;'));
2535
2536
			// If it wasn't changed, no copying or other boring stuff has to happen!
2537
			if ($data != substr($message, $last_pos, $pos - $last_pos))
2538
			{
2539
				$message = substr($message, 0, $last_pos) . $data . substr($message, $pos);
2540
2541
				// Since we changed it, look again in case we added or removed a tag.  But we don't want to skip any.
2542
				$old_pos = strlen($data) + $last_pos;
2543
				$pos = strpos($message, '[', $last_pos);
2544
				$pos = $pos === false ? $old_pos : min($pos, $old_pos);
2545
			}
2546
		}
2547
2548
		// Are we there yet?  Are we there yet?
2549
		if ($pos >= strlen($message) - 1)
2550
			break;
2551
2552
		$tag_character = strtolower($message[$pos + 1]);
2553
2554
		if ($tag_character == '/' && !empty($open_tags))
2555
		{
2556
			$pos2 = strpos($message, ']', $pos + 1);
2557
			if ($pos2 == $pos + 2)
2558
				continue;
2559
2560
			$look_for = strtolower(substr($message, $pos + 2, $pos2 - $pos - 2));
2561
2562
			// A closing tag that doesn't match any open tags? Skip it.
2563
			if (!in_array($look_for, array_map(function($code)
2564
			{
2565
				return $code['tag'];
2566
			}, $open_tags)))
2567
				continue;
2568
2569
			$to_close = array();
2570
			$block_level = null;
2571
2572
			do
2573
			{
2574
				$tag = array_pop($open_tags);
2575
				if (!$tag)
2576
					break;
2577
2578
				if (!empty($tag['block_level']))
2579
				{
2580
					// Only find out if we need to.
2581
					if ($block_level === false)
2582
					{
2583
						array_push($open_tags, $tag);
2584
						break;
2585
					}
2586
2587
					// The idea is, if we are LOOKING for a block level tag, we can close them on the way.
2588
					if (strlen($look_for) > 0 && isset($bbc_codes[$look_for[0]]))
2589
					{
2590
						foreach ($bbc_codes[$look_for[0]] as $temp)
2591
							if ($temp['tag'] == $look_for)
2592
							{
2593
								$block_level = !empty($temp['block_level']);
2594
								break;
2595
							}
2596
					}
2597
2598
					if ($block_level !== true)
2599
					{
2600
						$block_level = false;
2601
						array_push($open_tags, $tag);
2602
						break;
2603
					}
2604
				}
2605
2606
				$to_close[] = $tag;
2607
			}
2608
			while ($tag['tag'] != $look_for);
2609
2610
			// Did we just eat through everything and not find it?
2611
			if ((empty($open_tags) && (empty($tag) || $tag['tag'] != $look_for)))
2612
			{
2613
				$open_tags = $to_close;
2614
				continue;
2615
			}
2616
			elseif (!empty($to_close) && $tag['tag'] != $look_for)
2617
			{
2618
				if ($block_level === null && isset($look_for[0], $bbc_codes[$look_for[0]]))
2619
				{
2620
					foreach ($bbc_codes[$look_for[0]] as $temp)
2621
						if ($temp['tag'] == $look_for)
2622
						{
2623
							$block_level = !empty($temp['block_level']);
2624
							break;
2625
						}
2626
				}
2627
2628
				// We're not looking for a block level tag (or maybe even a tag that exists...)
2629
				if (!$block_level)
0 ignored issues
show
Bug Best Practice introduced by
The expression $block_level of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
2630
				{
2631
					foreach ($to_close as $tag)
2632
						array_push($open_tags, $tag);
2633
					continue;
2634
				}
2635
			}
2636
2637
			foreach ($to_close as $tag)
2638
			{
2639
				$message = substr($message, 0, $pos) . "\n" . $tag['after'] . "\n" . substr($message, $pos2 + 1);
2640
				$pos += strlen($tag['after']) + 2;
2641
				$pos2 = $pos - 1;
2642
2643
				// See the comment at the end of the big loop - just eating whitespace ;).
2644
				$whitespace_regex = '';
2645
				if (!empty($tag['block_level']))
2646
					$whitespace_regex .= '(&nbsp;|\s)*(<br\s*/?' . '>)?';
2647
				// Trim one line of whitespace after unnested tags, but all of it after nested ones
2648
				if (!empty($tag['trim']) && $tag['trim'] != 'inside')
2649
					$whitespace_regex .= empty($tag['require_parents']) ? '(&nbsp;|\s)*' : '(<br>|&nbsp;|\s)*';
2650
2651
				if (!empty($whitespace_regex) && preg_match('~' . $whitespace_regex . '~', substr($message, $pos), $matches) != 0)
2652
					$message = substr($message, 0, $pos) . substr($message, $pos + strlen($matches[0]));
2653
			}
2654
2655
			if (!empty($to_close))
2656
			{
2657
				$to_close = array();
2658
				$pos--;
2659
			}
2660
2661
			continue;
2662
		}
2663
2664
		// No tags for this character, so just keep going (fastest possible course.)
2665
		if (!isset($bbc_codes[$tag_character]))
2666
			continue;
2667
2668
		$inside = empty($open_tags) ? null : $open_tags[count($open_tags) - 1];
2669
		$tag = null;
2670
		foreach ($bbc_codes[$tag_character] as $possible)
2671
		{
2672
			$pt_strlen = strlen($possible['tag']);
2673
2674
			// Not a match?
2675
			if (strtolower(substr($message, $pos + 1, $pt_strlen)) != $possible['tag'])
2676
				continue;
2677
2678
			$next_c = isset($message[$pos + 1 + $pt_strlen]) ? $message[$pos + 1 + $pt_strlen] : '';
2679
2680
			// A tag is the last char maybe
2681
			if ($next_c == '')
2682
				break;
2683
2684
			// A test validation?
2685
			if (isset($possible['test']) && preg_match('~^' . $possible['test'] . '~', substr($message, $pos + 1 + $pt_strlen + 1)) === 0)
2686
				continue;
2687
			// Do we want parameters?
2688
			elseif (!empty($possible['parameters']))
2689
			{
2690
				// Are all the parameters optional?
2691
				$param_required = false;
2692
				foreach ($possible['parameters'] as $param)
2693
				{
2694
					if (empty($param['optional']))
2695
					{
2696
						$param_required = true;
2697
						break;
2698
					}
2699
				}
2700
2701
				if ($param_required && $next_c != ' ')
2702
					continue;
2703
			}
2704
			elseif (isset($possible['type']))
2705
			{
2706
				// Do we need an equal sign?
2707
				if (in_array($possible['type'], array('unparsed_equals', 'unparsed_commas', 'unparsed_commas_content', 'unparsed_equals_content', 'parsed_equals')) && $next_c != '=')
2708
					continue;
2709
				// Maybe we just want a /...
2710
				if ($possible['type'] == 'closed' && $next_c != ']' && substr($message, $pos + 1 + $pt_strlen, 2) != '/]' && substr($message, $pos + 1 + $pt_strlen, 3) != ' /]')
2711
					continue;
2712
				// An immediate ]?
2713
				if ($possible['type'] == 'unparsed_content' && $next_c != ']')
2714
					continue;
2715
			}
2716
			// No type means 'parsed_content', which demands an immediate ] without parameters!
2717
			elseif ($next_c != ']')
2718
				continue;
2719
2720
			// Check allowed tree?
2721
			if (isset($possible['require_parents']) && ($inside === null || !in_array($inside['tag'], $possible['require_parents'])))
2722
				continue;
2723
			elseif (isset($inside['require_children']) && !in_array($possible['tag'], $inside['require_children']))
2724
				continue;
2725
			// If this is in the list of disallowed child tags, don't parse it.
2726
			elseif (isset($inside['disallow_children']) && in_array($possible['tag'], $inside['disallow_children']))
2727
				continue;
2728
2729
			$pos1 = $pos + 1 + $pt_strlen + 1;
2730
2731
			// Quotes can have alternate styling, we do this php-side due to all the permutations of quotes.
2732
			if ($possible['tag'] == 'quote')
2733
			{
2734
				// Start with standard
2735
				$quote_alt = false;
2736
				foreach ($open_tags as $open_quote)
2737
				{
2738
					// Every parent quote this quote has flips the styling
2739
					if ($open_quote['tag'] == 'quote')
2740
						$quote_alt = !$quote_alt;
2741
				}
2742
				// Add a class to the quote to style alternating blockquotes
2743
				$possible['before'] = strtr($possible['before'], array('<blockquote>' => '<blockquote class="bbc_' . ($quote_alt ? 'alternate' : 'standard') . '_quote">'));
2744
			}
2745
2746
			// This is long, but it makes things much easier and cleaner.
2747
			if (!empty($possible['parameters']))
2748
			{
2749
				// Build a regular expression for each parameter for the current tag.
2750
				$regex_key = $smcFunc['json_encode']($possible['parameters']);
2751
				if (!isset($params_regexes[$regex_key]))
2752
				{
2753
					$params_regexes[$regex_key] = '';
2754
2755
					foreach ($possible['parameters'] as $p => $info)
2756
						$params_regexes[$regex_key] .= '(\s+' . $p . '=' . (empty($info['quoted']) ? '' : '&quot;') . (isset($info['match']) ? $info['match'] : '(.+?)') . (empty($info['quoted']) ? '' : '&quot;') . '\s*)' . (empty($info['optional']) ? '' : '?');
2757
				}
2758
2759
				// Extract the string that potentially holds our parameters.
2760
				$blob = preg_split('~\[/?(?:' . $alltags_regex . ')~i', substr($message, $pos));
2761
				$blobs = preg_split('~\]~i', $blob[1]);
2762
2763
				$splitters = implode('=|', array_keys($possible['parameters'])) . '=';
2764
2765
				// Progressively append more blobs until we find our parameters or run out of blobs
2766
				$blob_counter = 1;
2767
				while ($blob_counter <= count($blobs))
2768
				{
2769
					$given_param_string = implode(']', array_slice($blobs, 0, $blob_counter++));
2770
2771
					$given_params = preg_split('~\s(?=(' . $splitters . '))~i', $given_param_string);
2772
					sort($given_params, SORT_STRING);
2773
2774
					$match = preg_match('~^' . $params_regexes[$regex_key] . '$~i', implode(' ', $given_params), $matches) !== 0;
2775
2776
					if ($match)
2777
						break;
2778
				}
2779
2780
				// Didn't match our parameter list, try the next possible.
2781
				if (!$match)
2782
					continue;
2783
2784
				$params = array();
2785
				for ($i = 1, $n = count($matches); $i < $n; $i += 2)
2786
				{
2787
					$key = strtok(ltrim($matches[$i]), '=');
2788
					if ($key === false)
2789
						continue;
2790
					elseif (isset($possible['parameters'][$key]['value']))
2791
						$params['{' . $key . '}'] = strtr($possible['parameters'][$key]['value'], array('$1' => $matches[$i + 1]));
2792
					elseif (isset($possible['parameters'][$key]['validate']))
2793
						$params['{' . $key . '}'] = $possible['parameters'][$key]['validate']($matches[$i + 1]);
2794
					else
2795
						$params['{' . $key . '}'] = $matches[$i + 1];
2796
2797
					// Just to make sure: replace any $ or { so they can't interpolate wrongly.
2798
					$params['{' . $key . '}'] = strtr($params['{' . $key . '}'], array('$' => '&#036;', '{' => '&#123;'));
2799
				}
2800
2801
				foreach ($possible['parameters'] as $p => $info)
2802
				{
2803
					if (!isset($params['{' . $p . '}']))
2804
					{
2805
						if (!isset($info['default']))
2806
							$params['{' . $p . '}'] = '';
2807
						elseif (isset($possible['parameters'][$p]['value']))
2808
							$params['{' . $p . '}'] = strtr($possible['parameters'][$p]['value'], array('$1' => $info['default']));
2809
						elseif (isset($possible['parameters'][$p]['validate']))
2810
							$params['{' . $p . '}'] = $possible['parameters'][$p]['validate']($info['default']);
2811
						else
2812
							$params['{' . $p . '}'] = $info['default'];
2813
					}
2814
				}
2815
2816
				$tag = $possible;
2817
2818
				// Put the parameters into the string.
2819
				if (isset($tag['before']))
2820
					$tag['before'] = strtr($tag['before'], $params);
2821
				if (isset($tag['after']))
2822
					$tag['after'] = strtr($tag['after'], $params);
2823
				if (isset($tag['content']))
2824
					$tag['content'] = strtr($tag['content'], $params);
2825
2826
				$pos1 += strlen($given_param_string);
2827
			}
2828
			else
2829
			{
2830
				$tag = $possible;
2831
				$params = array();
2832
			}
2833
			break;
2834
		}
2835
2836
		// Item codes are complicated buggers... they are implicit [li]s and can make [list]s!
2837
		if ($smileys !== false && $tag === null && isset($itemcodes[$message[$pos + 1]]) && $message[$pos + 2] == ']' && !isset($disabled['list']) && !isset($disabled['li']))
2838
		{
2839
			if ($message[$pos + 1] == '0' && !in_array($message[$pos - 1], array(';', ' ', "\t", "\n", '>')))
2840
				continue;
2841
2842
			$tag = $itemcodes[$message[$pos + 1]];
2843
2844
			// First let's set up the tree: it needs to be in a list, or after an li.
2845
			if ($inside === null || ($inside['tag'] != 'list' && $inside['tag'] != 'li'))
2846
			{
2847
				$open_tags[] = array(
2848
					'tag' => 'list',
2849
					'after' => '</ul>',
2850
					'block_level' => true,
2851
					'require_children' => array('li'),
2852
					'disallow_children' => isset($inside['disallow_children']) ? $inside['disallow_children'] : null,
2853
				);
2854
				$code = '<ul class="bbc_list">';
2855
			}
2856
			// We're in a list item already: another itemcode?  Close it first.
2857
			elseif ($inside['tag'] == 'li')
2858
			{
2859
				array_pop($open_tags);
2860
				$code = '</li>';
2861
			}
2862
			else
2863
				$code = '';
2864
2865
			// Now we open a new tag.
2866
			$open_tags[] = array(
2867
				'tag' => 'li',
2868
				'after' => '</li>',
2869
				'trim' => 'outside',
2870
				'block_level' => true,
2871
				'disallow_children' => isset($inside['disallow_children']) ? $inside['disallow_children'] : null,
2872
			);
2873
2874
			// First, open the tag...
2875
			$code .= '<li' . ($tag == '' ? '' : ' type="' . $tag . '"') . '>';
2876
			$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos + 3);
2877
			$pos += strlen($code) - 1 + 2;
2878
2879
			// Next, find the next break (if any.)  If there's more itemcode after it, keep it going - otherwise close!
2880
			$pos2 = strpos($message, '<br>', $pos);
2881
			$pos3 = strpos($message, '[/', $pos);
2882
			if ($pos2 !== false && ($pos2 <= $pos3 || $pos3 === false))
2883
			{
2884
				preg_match('~^(<br>|&nbsp;|\s|\[)+~', substr($message, $pos2 + 4), $matches);
2885
				$message = substr($message, 0, $pos2) . (!empty($matches[0]) && substr($matches[0], -1) == '[' ? '[/li]' : '[/li][/list]') . substr($message, $pos2);
2886
2887
				$open_tags[count($open_tags) - 2]['after'] = '</ul>';
2888
			}
2889
			// Tell the [list] that it needs to close specially.
2890
			else
2891
			{
2892
				// Move the li over, because we're not sure what we'll hit.
2893
				$open_tags[count($open_tags) - 1]['after'] = '';
2894
				$open_tags[count($open_tags) - 2]['after'] = '</li></ul>';
2895
			}
2896
2897
			continue;
2898
		}
2899
2900
		// Implicitly close lists and tables if something other than what's required is in them.  This is needed for itemcode.
2901
		if ($tag === null && $inside !== null && !empty($inside['require_children']))
2902
		{
2903
			array_pop($open_tags);
2904
2905
			$message = substr($message, 0, $pos) . "\n" . $inside['after'] . "\n" . substr($message, $pos);
2906
			$pos += strlen($inside['after']) - 1 + 2;
2907
		}
2908
2909
		// No tag?  Keep looking, then.  Silly people using brackets without actual tags.
2910
		if ($tag === null)
2911
			continue;
2912
2913
		// Propagate the list to the child (so wrapping the disallowed tag won't work either.)
2914
		if (isset($inside['disallow_children']))
2915
			$tag['disallow_children'] = isset($tag['disallow_children']) ? array_unique(array_merge($tag['disallow_children'], $inside['disallow_children'])) : $inside['disallow_children'];
2916
2917
		// Is this tag disabled?
2918
		if (isset($disabled[$tag['tag']]))
2919
		{
2920
			if (!isset($tag['disabled_before']) && !isset($tag['disabled_after']) && !isset($tag['disabled_content']))
2921
			{
2922
				$tag['before'] = !empty($tag['block_level']) ? '<div>' : '';
2923
				$tag['after'] = !empty($tag['block_level']) ? '</div>' : '';
2924
				$tag['content'] = isset($tag['type']) && $tag['type'] == 'closed' ? '' : (!empty($tag['block_level']) ? '<div>$1</div>' : '$1');
2925
			}
2926
			elseif (isset($tag['disabled_before']) || isset($tag['disabled_after']))
2927
			{
2928
				$tag['before'] = isset($tag['disabled_before']) ? $tag['disabled_before'] : (!empty($tag['block_level']) ? '<div>' : '');
2929
				$tag['after'] = isset($tag['disabled_after']) ? $tag['disabled_after'] : (!empty($tag['block_level']) ? '</div>' : '');
2930
			}
2931
			else
2932
				$tag['content'] = $tag['disabled_content'];
2933
		}
2934
2935
		// we use this a lot
2936
		$tag_strlen = strlen($tag['tag']);
2937
2938
		// The only special case is 'html', which doesn't need to close things.
2939
		if (!empty($tag['block_level']) && $tag['tag'] != 'html' && empty($inside['block_level']))
2940
		{
2941
			$n = count($open_tags) - 1;
2942
			while (empty($open_tags[$n]['block_level']) && $n >= 0)
2943
				$n--;
2944
2945
			// Close all the non block level tags so this tag isn't surrounded by them.
2946
			for ($i = count($open_tags) - 1; $i > $n; $i--)
2947
			{
2948
				$message = substr($message, 0, $pos) . "\n" . $open_tags[$i]['after'] . "\n" . substr($message, $pos);
2949
				$ot_strlen = strlen($open_tags[$i]['after']);
2950
				$pos += $ot_strlen + 2;
2951
				$pos1 += $ot_strlen + 2;
2952
2953
				// Trim or eat trailing stuff... see comment at the end of the big loop.
2954
				$whitespace_regex = '';
2955
				if (!empty($tag['block_level']))
2956
					$whitespace_regex .= '(&nbsp;|\s)*(<br>)?';
2957
				if (!empty($tag['trim']) && $tag['trim'] != 'inside')
2958
					$whitespace_regex .= empty($tag['require_parents']) ? '(&nbsp;|\s)*' : '(<br>|&nbsp;|\s)*';
2959
				if (!empty($whitespace_regex) && preg_match('~' . $whitespace_regex . '~', substr($message, $pos), $matches) != 0)
2960
					$message = substr($message, 0, $pos) . substr($message, $pos + strlen($matches[0]));
2961
2962
				array_pop($open_tags);
2963
			}
2964
		}
2965
2966
		// Can't read past the end of the message
2967
		$pos1 = min(strlen($message), $pos1);
2968
2969
		// No type means 'parsed_content'.
2970
		if (!isset($tag['type']))
2971
		{
2972
			$open_tags[] = $tag;
2973
			$message = substr($message, 0, $pos) . "\n" . $tag['before'] . "\n" . substr($message, $pos1);
2974
			$pos += strlen($tag['before']) - 1 + 2;
2975
		}
2976
		// Don't parse the content, just skip it.
2977
		elseif ($tag['type'] == 'unparsed_content')
2978
		{
2979
			$pos2 = stripos($message, '[/' . substr($message, $pos + 1, $tag_strlen) . ']', $pos1);
2980
			if ($pos2 === false)
2981
				continue;
2982
2983
			$data = substr($message, $pos1, $pos2 - $pos1);
2984
2985
			if (!empty($tag['block_level']) && substr($data, 0, 4) == '<br>')
2986
				$data = substr($data, 4);
2987
2988
			if (isset($tag['validate']))
2989
				$tag['validate']($tag, $data, $disabled, $params);
2990
2991
			$code = strtr($tag['content'], array('$1' => $data));
2992
			$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos2 + 3 + $tag_strlen);
2993
2994
			$pos += strlen($code) - 1 + 2;
2995
			$last_pos = $pos + 1;
2996
		}
2997
		// Don't parse the content, just skip it.
2998
		elseif ($tag['type'] == 'unparsed_equals_content')
2999
		{
3000
			// The value may be quoted for some tags - check.
3001
			if (isset($tag['quoted']))
3002
			{
3003
				$quoted = substr($message, $pos1, 6) == '&quot;';
3004
				if ($tag['quoted'] != 'optional' && !$quoted)
3005
					continue;
3006
3007
				if ($quoted)
3008
					$pos1 += 6;
3009
			}
3010
			else
3011
				$quoted = false;
3012
3013
			$pos2 = strpos($message, $quoted == false ? ']' : '&quot;]', $pos1);
3014
			if ($pos2 === false)
3015
				continue;
3016
3017
			$pos3 = stripos($message, '[/' . substr($message, $pos + 1, $tag_strlen) . ']', $pos2);
3018
			if ($pos3 === false)
3019
				continue;
3020
3021
			$data = array(
3022
				substr($message, $pos2 + ($quoted == false ? 1 : 7), $pos3 - ($pos2 + ($quoted == false ? 1 : 7))),
3023
				substr($message, $pos1, $pos2 - $pos1)
3024
			);
3025
3026
			if (!empty($tag['block_level']) && substr($data[0], 0, 4) == '<br>')
3027
				$data[0] = substr($data[0], 4);
3028
3029
			// Validation for my parking, please!
3030
			if (isset($tag['validate']))
3031
				$tag['validate']($tag, $data, $disabled, $params);
3032
3033
			$code = strtr($tag['content'], array('$1' => $data[0], '$2' => $data[1]));
3034
			$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos3 + 3 + $tag_strlen);
3035
			$pos += strlen($code) - 1 + 2;
3036
		}
3037
		// A closed tag, with no content or value.
3038
		elseif ($tag['type'] == 'closed')
3039
		{
3040
			$pos2 = strpos($message, ']', $pos);
3041
			$message = substr($message, 0, $pos) . "\n" . $tag['content'] . "\n" . substr($message, $pos2 + 1);
3042
			$pos += strlen($tag['content']) - 1 + 2;
3043
		}
3044
		// This one is sorta ugly... :/.  Unfortunately, it's needed for flash.
3045
		elseif ($tag['type'] == 'unparsed_commas_content')
3046
		{
3047
			$pos2 = strpos($message, ']', $pos1);
3048
			if ($pos2 === false)
3049
				continue;
3050
3051
			$pos3 = stripos($message, '[/' . substr($message, $pos + 1, $tag_strlen) . ']', $pos2);
3052
			if ($pos3 === false)
3053
				continue;
3054
3055
			// We want $1 to be the content, and the rest to be csv.
3056
			$data = explode(',', ',' . substr($message, $pos1, $pos2 - $pos1));
3057
			$data[0] = substr($message, $pos2 + 1, $pos3 - $pos2 - 1);
3058
3059
			if (isset($tag['validate']))
3060
				$tag['validate']($tag, $data, $disabled, $params);
3061
3062
			$code = $tag['content'];
3063
			foreach ($data as $k => $d)
3064
				$code = strtr($code, array('$' . ($k + 1) => trim($d)));
3065
			$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos3 + 3 + $tag_strlen);
3066
			$pos += strlen($code) - 1 + 2;
3067
		}
3068
		// This has parsed content, and a csv value which is unparsed.
3069
		elseif ($tag['type'] == 'unparsed_commas')
3070
		{
3071
			$pos2 = strpos($message, ']', $pos1);
3072
			if ($pos2 === false)
3073
				continue;
3074
3075
			$data = explode(',', substr($message, $pos1, $pos2 - $pos1));
3076
3077
			if (isset($tag['validate']))
3078
				$tag['validate']($tag, $data, $disabled, $params);
3079
3080
			// Fix after, for disabled code mainly.
3081
			foreach ($data as $k => $d)
3082
				$tag['after'] = strtr($tag['after'], array('$' . ($k + 1) => trim($d)));
3083
3084
			$open_tags[] = $tag;
3085
3086
			// Replace them out, $1, $2, $3, $4, etc.
3087
			$code = $tag['before'];
3088
			foreach ($data as $k => $d)
3089
				$code = strtr($code, array('$' . ($k + 1) => trim($d)));
3090
			$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos2 + 1);
3091
			$pos += strlen($code) - 1 + 2;
3092
		}
3093
		// A tag set to a value, parsed or not.
3094
		elseif ($tag['type'] == 'unparsed_equals' || $tag['type'] == 'parsed_equals')
3095
		{
3096
			// The value may be quoted for some tags - check.
3097
			if (isset($tag['quoted']))
3098
			{
3099
				$quoted = substr($message, $pos1, 6) == '&quot;';
3100
				if ($tag['quoted'] != 'optional' && !$quoted)
3101
					continue;
3102
3103
				if ($quoted)
3104
					$pos1 += 6;
3105
			}
3106
			else
3107
				$quoted = false;
3108
3109
			$pos2 = strpos($message, $quoted == false ? ']' : '&quot;]', $pos1);
3110
			if ($pos2 === false)
3111
				continue;
3112
3113
			$data = substr($message, $pos1, $pos2 - $pos1);
3114
3115
			// Validation for my parking, please!
3116
			if (isset($tag['validate']))
3117
				$tag['validate']($tag, $data, $disabled, $params);
3118
3119
			// For parsed content, we must recurse to avoid security problems.
3120
			if ($tag['type'] != 'unparsed_equals')
3121
				$data = parse_bbc($data, !empty($tag['parsed_tags_allowed']) ? false : true, '', !empty($tag['parsed_tags_allowed']) ? $tag['parsed_tags_allowed'] : array());
3122
3123
			$tag['after'] = strtr($tag['after'], array('$1' => $data));
3124
3125
			$open_tags[] = $tag;
3126
3127
			$code = strtr($tag['before'], array('$1' => $data));
3128
			$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos2 + ($quoted == false ? 1 : 7));
3129
			$pos += strlen($code) - 1 + 2;
3130
		}
3131
3132
		// If this is block level, eat any breaks after it.
3133
		if (!empty($tag['block_level']) && substr($message, $pos + 1, 4) == '<br>')
3134
			$message = substr($message, 0, $pos + 1) . substr($message, $pos + 5);
3135
3136
		// Are we trimming outside this tag?
3137
		if (!empty($tag['trim']) && $tag['trim'] != 'outside' && preg_match('~(<br>|&nbsp;|\s)*~', substr($message, $pos + 1), $matches) != 0)
3138
			$message = substr($message, 0, $pos + 1) . substr($message, $pos + 1 + strlen($matches[0]));
3139
	}
3140
3141
	// Close any remaining tags.
3142
	while ($tag = array_pop($open_tags))
3143
		$message .= "\n" . $tag['after'] . "\n";
3144
3145
	// Parse the smileys within the parts where it can be done safely.
3146
	if ($smileys === true)
3147
	{
3148
		$message_parts = explode("\n", $message);
3149
		for ($i = 0, $n = count($message_parts); $i < $n; $i += 2)
3150
			parsesmileys($message_parts[$i]);
3151
3152
		$message = implode('', $message_parts);
3153
	}
3154
3155
	// No smileys, just get rid of the markers.
3156
	else
3157
		$message = strtr($message, array("\n" => ''));
3158
3159
	if ($message !== '' && $message[0] === ' ')
3160
		$message = '&nbsp;' . substr($message, 1);
3161
3162
	// Cleanup whitespace.
3163
	$message = strtr($message, array('  ' => ' &nbsp;', "\r" => '', "\n" => '<br>', '<br> ' => '<br>&nbsp;', '&#13;' => "\n"));
3164
3165
	// Allow mods access to what parse_bbc created
3166
	call_integration_hook('integrate_post_parsebbc', array(&$message, &$smileys, &$cache_id, &$parse_tags));
3167
3168
	// Cache the output if it took some time...
3169
	if (isset($cache_key, $cache_t) && microtime(true) - $cache_t > 0.05)
3170
		cache_put_data($cache_key, $message, 240);
3171
3172
	// If this was a force parse revert if needed.
3173
	if (!empty($parse_tags))
3174
	{
3175
		$alltags_regex = empty($real_alltags_regex) ? '' : $real_alltags_regex;
3176
		unset($real_alltags_regex);
3177
	}
3178
	elseif (!empty($bbc_codes))
3179
		$bbc_lang_locales[$txt['lang_locale']] = $bbc_codes;
3180
3181
	return $message;
3182
}
3183
3184
/**
3185
 * Parse smileys in the passed message.
3186
 *
3187
 * The smiley parsing function which makes pretty faces appear :).
3188
 * If custom smiley sets are turned off by smiley_enable, the default set of smileys will be used.
3189
 * These are specifically not parsed in code tags [url=mailto:[email protected]]
3190
 * Caches the smileys from the database or array in memory.
3191
 * Doesn't return anything, but rather modifies message directly.
3192
 *
3193
 * @param string &$message The message to parse smileys in
3194
 */
3195
function parsesmileys(&$message)
3196
{
3197
	global $modSettings, $txt, $user_info, $context, $smcFunc;
3198
	static $smileyPregSearch = null, $smileyPregReplacements = array();
3199
3200
	// No smiley set at all?!
3201
	if ($user_info['smiley_set'] == 'none' || trim($message) == '')
3202
		return;
3203
3204
	// Maybe a mod wants to implement an alternative method (e.g. emojis instead of images)
3205
	call_integration_hook('integrate_smileys', array(&$smileyPregSearch, &$smileyPregReplacements));
3206
3207
	// If smileyPregSearch hasn't been set, do it now.
3208
	if (empty($smileyPregSearch))
3209
	{
3210
		// Cache for longer when customized smiley codes aren't enabled
3211
		$cache_time = empty($modSettings['smiley_enable']) ? 7200 : 480;
3212
3213
		// Load the smileys in reverse order by length so they don't get parsed incorrectly.
3214
		if (($temp = cache_get_data('parsing_smileys_' . $user_info['smiley_set'], $cache_time)) == null)
0 ignored issues
show
It seems like you are loosely comparing $temp = cache_get_data('...ley_set'], $cache_time) of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
3215
		{
3216
			$result = $smcFunc['db_query']('', '
3217
				SELECT s.code, f.filename, s.description
3218
				FROM {db_prefix}smileys AS s
3219
					JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley)
3220
				WHERE f.smiley_set = {string:smiley_set}' . (empty($modSettings['smiley_enable']) ? '
3221
					AND s.code IN ({array_string:default_codes})' : '') . '
3222
				ORDER BY LENGTH(s.code) DESC',
3223
				array(
3224
					'default_codes' => array('>:D', ':D', '::)', '>:(', ':))', ':)', ';)', ';D', ':(', ':o', '8)', ':P', '???', ':-[', ':-X', ':-*', ':\'(', ':-\\', '^-^', 'O0', 'C:-)', 'O:-)'),
3225
					'smiley_set' => $user_info['smiley_set'],
3226
				)
3227
			);
3228
			$smileysfrom = array();
3229
			$smileysto = array();
3230
			$smileysdescs = array();
3231
			while ($row = $smcFunc['db_fetch_assoc']($result))
3232
			{
3233
				$smileysfrom[] = $row['code'];
3234
				$smileysto[] = $smcFunc['htmlspecialchars']($row['filename']);
3235
				$smileysdescs[] = !empty($txt['icon_' . strtolower($row['description'])]) ? $txt['icon_' . strtolower($row['description'])] : $row['description'];
3236
			}
3237
			$smcFunc['db_free_result']($result);
3238
3239
			cache_put_data('parsing_smileys_' . $user_info['smiley_set'], array($smileysfrom, $smileysto, $smileysdescs), $cache_time);
3240
		}
3241
		else
3242
			list ($smileysfrom, $smileysto, $smileysdescs) = $temp;
3243
3244
		// The non-breaking-space is a complex thing...
3245
		$non_breaking_space = $context['utf8'] ? '\x{A0}' : '\xA0';
3246
3247
		// This smiley regex makes sure it doesn't parse smileys within code tags (so [url=mailto:[email protected]] doesn't parse the :D smiley)
3248
		$smileyPregReplacements = array();
3249
		$searchParts = array();
3250
		$smileys_path = $smcFunc['htmlspecialchars']($modSettings['smileys_url'] . '/' . $user_info['smiley_set'] . '/');
3251
3252
		for ($i = 0, $n = count($smileysfrom); $i < $n; $i++)
3253
		{
3254
			$specialChars = $smcFunc['htmlspecialchars']($smileysfrom[$i], ENT_QUOTES);
3255
			$smileyCode = '<img src="' . $smileys_path . $smileysto[$i] . '" alt="' . strtr($specialChars, array(':' => '&#58;', '(' => '&#40;', ')' => '&#41;', '$' => '&#36;', '[' => '&#091;')) . '" title="' . strtr($smcFunc['htmlspecialchars']($smileysdescs[$i]), array(':' => '&#58;', '(' => '&#40;', ')' => '&#41;', '$' => '&#36;', '[' => '&#091;')) . '" class="smiley">';
3256
3257
			$smileyPregReplacements[$smileysfrom[$i]] = $smileyCode;
3258
3259
			$searchParts[] = $smileysfrom[$i];
3260
			if ($smileysfrom[$i] != $specialChars)
3261
			{
3262
				$smileyPregReplacements[$specialChars] = $smileyCode;
3263
				$searchParts[] = $specialChars;
3264
3265
				// Some 2.0 hex htmlchars are in there as 3 digits; allow for finding leading 0 or not
3266
				$specialChars2 = preg_replace('/&#(\d{2});/', '&#0$1;', $specialChars);
3267
				if ($specialChars2 != $specialChars)
3268
				{
3269
					$smileyPregReplacements[$specialChars2] = $smileyCode;
3270
					$searchParts[] = $specialChars2;
3271
				}
3272
			}
3273
		}
3274
3275
		$smileyPregSearch = '~(?<=[>:\?\.\s' . $non_breaking_space . '[\]()*\\\;]|(?<![a-zA-Z0-9])\(|^)(' . build_regex($searchParts, '~') . ')(?=[^[:alpha:]0-9]|$)~' . ($context['utf8'] ? 'u' : '');
3276
	}
3277
3278
	// Replace away!
3279
	$message = preg_replace_callback($smileyPregSearch, function($matches) use ($smileyPregReplacements)
3280
		{
3281
			return $smileyPregReplacements[$matches[1]];
3282
		}, $message);
3283
}
3284
3285
/**
3286
 * Highlight any code.
3287
 *
3288
 * Uses PHP's highlight_string() to highlight PHP syntax
3289
 * does special handling to keep the tabs in the code available.
3290
 * used to parse PHP code from inside [code] and [php] tags.
3291
 *
3292
 * @param string $code The code
3293
 * @return string The code with highlighted HTML.
3294
 */
3295
function highlight_php_code($code)
3296
{
3297
	// Remove special characters.
3298
	$code = un_htmlspecialchars(strtr($code, array('<br />' => "\n", '<br>' => "\n", "\t" => 'SMF_TAB();', '&#91;' => '[')));
3299
3300
	$oldlevel = error_reporting(0);
3301
3302
	$buffer = str_replace(array("\n", "\r"), '', @highlight_string($code, true));
3303
3304
	error_reporting($oldlevel);
3305
3306
	// Yes, I know this is kludging it, but this is the best way to preserve tabs from PHP :P.
3307
	$buffer = preg_replace('~SMF_TAB(?:</(?:font|span)><(?:font color|span style)="[^"]*?">)?\\(\\);~', '<pre style="display: inline;">' . "\t" . '</pre>', $buffer);
3308
3309
	return strtr($buffer, array('\'' => '&#039;', '<code>' => '', '</code>' => ''));
3310
}
3311
3312
/**
3313
 * Gets the appropriate URL to use for images (or whatever) when using SSL
3314
 *
3315
 * The returned URL may or may not be a proxied URL, depending on the situation.
3316
 * Mods can implement alternative proxies using the 'integrate_proxy' hook.
3317
 *
3318
 * @param string $url The original URL of the requested resource
3319
 * @return string The URL to use
3320
 */
3321
function get_proxied_url($url)
3322
{
3323
	global $boardurl, $image_proxy_enabled, $image_proxy_secret, $user_info;
3324
3325
	// Only use the proxy if enabled, and never for robots
3326
	if (empty($image_proxy_enabled) || !empty($user_info['possibly_robot']))
3327
		return $url;
3328
3329
	$parsedurl = parse_url($url);
3330
3331
	// Don't bother with HTTPS URLs, schemeless URLs, or obviously invalid URLs
3332
	if (empty($parsedurl['scheme']) || empty($parsedurl['host']) || empty($parsedurl['path']) || $parsedurl['scheme'] === 'https')
3333
		return $url;
3334
3335
	// We don't need to proxy our own resources
3336
	if ($parsedurl['host'] === parse_url($boardurl, PHP_URL_HOST))
3337
		return strtr($url, array('http://' => 'https://'));
3338
3339
	// By default, use SMF's own image proxy script
3340
	$proxied_url = strtr($boardurl, array('http://' => 'https://')) . '/proxy.php?request=' . urlencode($url) . '&hash=' . hash_hmac('sha1', $url, $image_proxy_secret);
3341
3342
	// Allow mods to easily implement an alternative proxy
3343
	// MOD AUTHORS: To add settings UI for your proxy, use the integrate_general_settings hook.
3344
	call_integration_hook('integrate_proxy', array($url, &$proxied_url));
3345
3346
	return $proxied_url;
3347
}
3348
3349
/**
3350
 * Make sure the browser doesn't come back and repost the form data.
3351
 * Should be used whenever anything is posted.
3352
 *
3353
 * @param string $setLocation The URL to redirect them to
3354
 * @param bool $refresh Whether to use a meta refresh instead
3355
 * @param bool $permanent Whether to send a 301 Moved Permanently instead of a 302 Moved Temporarily
3356
 */
3357
function redirectexit($setLocation = '', $refresh = false, $permanent = false)
3358
{
3359
	global $scripturl, $context, $modSettings, $db_show_debug, $db_cache;
3360
3361
	// In case we have mail to send, better do that - as obExit doesn't always quite make it...
3362
	if (!empty($context['flush_mail']))
3363
		// @todo this relies on 'flush_mail' being only set in AddMailQueue itself... :\
3364
		AddMailQueue(true);
3365
3366
	$add = preg_match('~^(ftp|http)[s]?://~', $setLocation) == 0 && substr($setLocation, 0, 6) != 'about:';
3367
3368
	if ($add)
3369
		$setLocation = $scripturl . ($setLocation != '' ? '?' . $setLocation : '');
3370
3371
	// Put the session ID in.
3372
	if (defined('SID') && SID != '')
3373
		$setLocation = preg_replace('/^' . preg_quote($scripturl, '/') . '(?!\?' . preg_quote(SID, '/') . ')\\??/', $scripturl . '?' . SID . ';', $setLocation);
3374
	// Keep that debug in their for template debugging!
3375
	elseif (isset($_GET['debug']))
3376
		$setLocation = preg_replace('/^' . preg_quote($scripturl, '/') . '\\??/', $scripturl . '?debug;', $setLocation);
3377
3378
	if (!empty($modSettings['queryless_urls']) && (empty($context['server']['is_cgi']) || ini_get('cgi.fix_pathinfo') == 1 || @get_cfg_var('cgi.fix_pathinfo') == 1) && (!empty($context['server']['is_apache']) || !empty($context['server']['is_lighttpd']) || !empty($context['server']['is_litespeed'])))
3379
	{
3380
		if (defined('SID') && SID != '')
3381
			$setLocation = preg_replace_callback('~^' . preg_quote($scripturl, '~') . '\?(?:' . SID . '(?:;|&|&amp;))((?:board|topic)=[^#]+?)(#[^"]*?)?$~',
3382
				function($m) use ($scripturl)
3383
				{
3384
					return $scripturl . '/' . strtr("$m[1]", '&;=', '//,') . '.html?' . SID . (isset($m[2]) ? "$m[2]" : "");
3385
				}, $setLocation);
3386
		else
3387
			$setLocation = preg_replace_callback('~^' . preg_quote($scripturl, '~') . '\?((?:board|topic)=[^#"]+?)(#[^"]*?)?$~',
3388
				function($m) use ($scripturl)
3389
				{
3390
					return $scripturl . '/' . strtr("$m[1]", '&;=', '//,') . '.html' . (isset($m[2]) ? "$m[2]" : "");
3391
				}, $setLocation);
3392
	}
3393
3394
	// Maybe integrations want to change where we are heading?
3395
	call_integration_hook('integrate_redirect', array(&$setLocation, &$refresh, &$permanent));
3396
3397
	// Set the header.
3398
	header('location: ' . str_replace(' ', '%20', $setLocation), true, $permanent ? 301 : 302);
3399
3400
	// Debugging.
3401
	if (isset($db_show_debug) && $db_show_debug === true)
3402
		$_SESSION['debug_redirect'] = $db_cache;
3403
3404
	obExit(false);
3405
}
3406
3407
/**
3408
 * Ends execution.  Takes care of template loading and remembering the previous URL.
3409
 *
3410
 * @param bool $header Whether to do the header
3411
 * @param bool $do_footer Whether to do the footer
3412
 * @param bool $from_index Whether we're coming from the board index
3413
 * @param bool $from_fatal_error Whether we're coming from a fatal error
3414
 */
3415
function obExit($header = null, $do_footer = null, $from_index = false, $from_fatal_error = false)
3416
{
3417
	global $context, $settings, $modSettings, $txt, $smcFunc, $should_log;
3418
	static $header_done = false, $footer_done = false, $level = 0, $has_fatal_error = false;
3419
3420
	// Attempt to prevent a recursive loop.
3421
	++$level;
3422
	if ($level > 1 && !$from_fatal_error && !$has_fatal_error)
3423
		exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
3424
	if ($from_fatal_error)
3425
		$has_fatal_error = true;
3426
3427
	// Clear out the stat cache.
3428
	if (function_exists('trackStats'))
3429
		trackStats();
3430
3431
	// If we have mail to send, send it.
3432
	if (function_exists('AddMailQueue') && !empty($context['flush_mail']))
3433
		// @todo this relies on 'flush_mail' being only set in AddMailQueue itself... :\
3434
		AddMailQueue(true);
3435
3436
	$do_header = $header === null ? !$header_done : $header;
3437
	if ($do_footer === null)
3438
		$do_footer = $do_header;
3439
3440
	// Has the template/header been done yet?
3441
	if ($do_header)
3442
	{
3443
		// Was the page title set last minute? Also update the HTML safe one.
3444
		if (!empty($context['page_title']) && empty($context['page_title_html_safe']))
3445
			$context['page_title_html_safe'] = $smcFunc['htmlspecialchars'](un_htmlspecialchars($context['page_title'])) . (!empty($context['current_page']) ? ' - ' . $txt['page'] . ' ' . ($context['current_page'] + 1) : '');
3446
3447
		// Start up the session URL fixer.
3448
		ob_start('ob_sessrewrite');
3449
3450
		if (!empty($settings['output_buffers']) && is_string($settings['output_buffers']))
3451
			$buffers = explode(',', $settings['output_buffers']);
3452
		elseif (!empty($settings['output_buffers']))
3453
			$buffers = $settings['output_buffers'];
3454
		else
3455
			$buffers = array();
3456
3457
		if (isset($modSettings['integrate_buffer']))
3458
			$buffers = array_merge(explode(',', $modSettings['integrate_buffer']), $buffers);
3459
3460
		if (!empty($buffers))
3461
			foreach ($buffers as $function)
3462
			{
3463
				$call = call_helper($function, true);
3464
3465
				// Is it valid?
3466
				if (!empty($call))
3467
					ob_start($call);
3468
			}
3469
3470
		// Display the screen in the logical order.
3471
		template_header();
3472
		$header_done = true;
3473
	}
3474
	if ($do_footer)
3475
	{
3476
		loadSubTemplate(isset($context['sub_template']) ? $context['sub_template'] : 'main');
3477
3478
		// Anything special to put out?
3479
		if (!empty($context['insert_after_template']) && !isset($_REQUEST['xml']))
3480
			echo $context['insert_after_template'];
3481
3482
		// Just so we don't get caught in an endless loop of errors from the footer...
3483
		if (!$footer_done)
3484
		{
3485
			$footer_done = true;
3486
			template_footer();
3487
3488
			// (since this is just debugging... it's okay that it's after </html>.)
3489
			if (!isset($_REQUEST['xml']))
3490
				displayDebug();
3491
		}
3492
	}
3493
3494
	// Remember this URL in case someone doesn't like sending HTTP_REFERER.
3495
	if ($should_log)
3496
		$_SESSION['old_url'] = $_SERVER['REQUEST_URL'];
3497
3498
	// For session check verification.... don't switch browsers...
3499
	$_SESSION['USER_AGENT'] = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];
3500
3501
	// Hand off the output to the portal, etc. we're integrated with.
3502
	call_integration_hook('integrate_exit', array($do_footer));
3503
3504
	// Don't exit if we're coming from index.php; that will pass through normally.
3505
	if (!$from_index)
3506
		exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
3507
}
3508
3509
/**
3510
 * Get the size of a specified image with better error handling.
3511
 *
3512
 * @todo see if it's better in Subs-Graphics, but one step at the time.
3513
 * Uses getimagesize() to determine the size of a file.
3514
 * Attempts to connect to the server first so it won't time out.
3515
 *
3516
 * @param string $url The URL of the image
3517
 * @return array|false The image size as array (width, height), or false on failure
3518
 */
3519
function url_image_size($url)
3520
{
3521
	global $sourcedir;
3522
3523
	// Make sure it is a proper URL.
3524
	$url = str_replace(' ', '%20', $url);
3525
3526
	// Can we pull this from the cache... please please?
3527
	if (($temp = cache_get_data('url_image_size-' . md5($url), 240)) !== null)
3528
		return $temp;
3529
	$t = microtime(true);
3530
3531
	// Get the host to pester...
3532
	preg_match('~^\w+://(.+?)/(.*)$~', $url, $match);
3533
3534
	// Can't figure it out, just try the image size.
3535
	if ($url == '' || $url == 'http://' || $url == 'https://')
3536
	{
3537
		return false;
3538
	}
3539
	elseif (!isset($match[1]))
3540
	{
3541
		$size = @getimagesize($url);
3542
	}
3543
	else
3544
	{
3545
		// Try to connect to the server... give it half a second.
3546
		$temp = 0;
3547
		$fp = @fsockopen($match[1], 80, $temp, $temp, 0.5);
3548
3549
		// Successful?  Continue...
3550
		if ($fp != false)
3551
		{
3552
			// Send the HEAD request (since we don't have to worry about chunked, HTTP/1.1 is fine here.)
3553
			fwrite($fp, 'HEAD /' . $match[2] . ' HTTP/1.1' . "\r\n" . 'Host: ' . $match[1] . "\r\n" . 'user-agent: '. SMF_USER_AGENT . "\r\n" . 'Connection: close' . "\r\n\r\n");
3554
3555
			// Read in the HTTP/1.1 or whatever.
3556
			$test = substr(fgets($fp, 11), -1);
3557
			fclose($fp);
3558
3559
			// See if it returned a 404/403 or something.
3560
			if ($test < 4)
3561
			{
3562
				$size = @getimagesize($url);
3563
3564
				// This probably means allow_url_fopen is off, let's try GD.
3565
				if ($size === false && function_exists('imagecreatefromstring'))
3566
				{
3567
					// It's going to hate us for doing this, but another request...
3568
					$image = @imagecreatefromstring(fetch_web_data($url));
3569
					if ($image !== false)
3570
					{
3571
						$size = array(imagesx($image), imagesy($image));
3572
						imagedestroy($image);
3573
					}
3574
				}
3575
			}
3576
		}
3577
	}
3578
3579
	// If we didn't get it, we failed.
3580
	if (!isset($size))
3581
		$size = false;
3582
3583
	// If this took a long time, we may never have to do it again, but then again we might...
3584
	if (microtime(true) - $t > 0.8)
3585
		cache_put_data('url_image_size-' . md5($url), $size, 240);
3586
3587
	// Didn't work.
3588
	return $size;
3589
}
3590
3591
/**
3592
 * Sets up the basic theme context stuff.
3593
 *
3594
 * @param bool $forceload Whether to load the theme even if it's already loaded
3595
 */
3596
function setupThemeContext($forceload = false)
3597
{
3598
	global $modSettings, $user_info, $scripturl, $context, $settings, $options, $txt, $maintenance;
3599
	global $smcFunc;
3600
	static $loaded = false;
3601
3602
	// Under SSI this function can be called more then once.  That can cause some problems.
3603
	//   So only run the function once unless we are forced to run it again.
3604
	if ($loaded && !$forceload)
3605
		return;
3606
3607
	$loaded = true;
3608
3609
	$context['in_maintenance'] = !empty($maintenance);
3610
	$context['current_time'] = timeformat(time(), false);
3611
	$context['current_action'] = isset($_GET['action']) ? $smcFunc['htmlspecialchars']($_GET['action']) : '';
3612
	$context['random_news_line'] = array();
3613
3614
	// Get some news...
3615
	$context['news_lines'] = array_filter(explode("\n", str_replace("\r", '', trim(addslashes($modSettings['news'])))));
3616
	for ($i = 0, $n = count($context['news_lines']); $i < $n; $i++)
3617
	{
3618
		if (trim($context['news_lines'][$i]) == '')
3619
			continue;
3620
3621
		// Clean it up for presentation ;).
3622
		$context['news_lines'][$i] = parse_bbc(stripslashes(trim($context['news_lines'][$i])), true, 'news' . $i);
3623
	}
3624
3625
	if (!empty($context['news_lines']) && (!empty($modSettings['allow_guestAccess']) || $context['user']['is_logged']))
3626
		$context['random_news_line'] = $context['news_lines'][mt_rand(0, count($context['news_lines']) - 1)];
3627
3628
	if (!$user_info['is_guest'])
3629
	{
3630
		$context['user']['messages'] = &$user_info['messages'];
3631
		$context['user']['unread_messages'] = &$user_info['unread_messages'];
3632
		$context['user']['alerts'] = &$user_info['alerts'];
3633
3634
		// Personal message popup...
3635
		if ($user_info['unread_messages'] > (isset($_SESSION['unread_messages']) ? $_SESSION['unread_messages'] : 0))
3636
			$context['user']['popup_messages'] = true;
3637
		else
3638
			$context['user']['popup_messages'] = false;
3639
		$_SESSION['unread_messages'] = $user_info['unread_messages'];
3640
3641
		if (allowedTo('moderate_forum'))
3642
			$context['unapproved_members'] = !empty($modSettings['unapprovedMembers']) ? $modSettings['unapprovedMembers'] : 0;
3643
3644
		$context['user']['avatar'] = array();
3645
3646
		// Check for gravatar first since we might be forcing them...
3647
		if (($modSettings['gravatarEnabled'] && substr($user_info['avatar']['url'], 0, 11) == 'gravatar://') || !empty($modSettings['gravatarOverride']))
3648
		{
3649
			if (!empty($modSettings['gravatarAllowExtraEmail']) && stristr($user_info['avatar']['url'], 'gravatar://') && strlen($user_info['avatar']['url']) > 11)
3650
				$context['user']['avatar']['href'] = get_gravatar_url($smcFunc['substr']($user_info['avatar']['url'], 11));
3651
			else
3652
				$context['user']['avatar']['href'] = get_gravatar_url($user_info['email']);
3653
		}
3654
		// Uploaded?
3655
		elseif ($user_info['avatar']['url'] == '' && !empty($user_info['avatar']['id_attach']))
3656
			$context['user']['avatar']['href'] = $user_info['avatar']['custom_dir'] ? $modSettings['custom_avatar_url'] . '/' . $user_info['avatar']['filename'] : $scripturl . '?action=dlattach;attach=' . $user_info['avatar']['id_attach'] . ';type=avatar';
3657
		// Full URL?
3658
		elseif (strpos($user_info['avatar']['url'], 'http://') === 0 || strpos($user_info['avatar']['url'], 'https://') === 0)
3659
			$context['user']['avatar']['href'] = $user_info['avatar']['url'];
3660
		// Otherwise we assume it's server stored.
3661
		elseif ($user_info['avatar']['url'] != '')
3662
			$context['user']['avatar']['href'] = $modSettings['avatar_url'] . '/' . $smcFunc['htmlspecialchars']($user_info['avatar']['url']);
3663
		// No avatar at all? Fine, we have a big fat default avatar ;)
3664
		else
3665
			$context['user']['avatar']['href'] = $modSettings['avatar_url'] . '/default.png';
3666
3667
		if (!empty($context['user']['avatar']))
3668
			$context['user']['avatar']['image'] = '<img src="' . $context['user']['avatar']['href'] . '" alt="" class="avatar">';
3669
3670
		// Figure out how long they've been logged in.
3671
		$context['user']['total_time_logged_in'] = array(
3672
			'days' => floor($user_info['total_time_logged_in'] / 86400),
3673
			'hours' => floor(($user_info['total_time_logged_in'] % 86400) / 3600),
3674
			'minutes' => floor(($user_info['total_time_logged_in'] % 3600) / 60)
3675
		);
3676
	}
3677
	else
3678
	{
3679
		$context['user']['messages'] = 0;
3680
		$context['user']['unread_messages'] = 0;
3681
		$context['user']['avatar'] = array();
3682
		$context['user']['total_time_logged_in'] = array('days' => 0, 'hours' => 0, 'minutes' => 0);
3683
		$context['user']['popup_messages'] = false;
3684
3685
		if (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 1)
3686
			$txt['welcome_guest'] .= $txt['welcome_guest_activate'];
3687
3688
		// If we've upgraded recently, go easy on the passwords.
3689
		if (!empty($modSettings['disableHashTime']) && ($modSettings['disableHashTime'] == 1 || time() < $modSettings['disableHashTime']))
3690
			$context['disable_login_hashing'] = true;
3691
	}
3692
3693
	// Setup the main menu items.
3694
	setupMenuContext();
3695
3696
	// This is here because old index templates might still use it.
3697
	$context['show_news'] = !empty($settings['enable_news']);
3698
3699
	// This is done to allow theme authors to customize it as they want.
3700
	$context['show_pm_popup'] = $context['user']['popup_messages'] && !empty($options['popup_messages']) && (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'pm');
3701
3702
	// 2.1+: Add the PM popup here instead. Theme authors can still override it simply by editing/removing the 'fPmPopup' in the array.
3703
	if ($context['show_pm_popup'])
3704
		addInlineJavaScript('
3705
		jQuery(document).ready(function($) {
3706
			new smc_Popup({
3707
				heading: ' . JavaScriptEscape($txt['show_personal_messages_heading']) . ',
3708
				content: ' . JavaScriptEscape(sprintf($txt['show_personal_messages'], $context['user']['unread_messages'], $scripturl . '?action=pm')) . ',
3709
				icon_class: \'main_icons mail_new\'
3710
			});
3711
		});');
3712
3713
	// Add a generic "Are you sure?" confirmation message.
3714
	addInlineJavaScript('
3715
	var smf_you_sure =' . JavaScriptEscape($txt['quickmod_confirm']) . ';');
3716
3717
	// Now add the capping code for avatars.
3718
	if (!empty($modSettings['avatar_max_width_external']) && !empty($modSettings['avatar_max_height_external']) && !empty($modSettings['avatar_action_too_large']) && $modSettings['avatar_action_too_large'] == 'option_css_resize')
3719
		addInlineCss('
3720
	img.avatar { max-width: ' . $modSettings['avatar_max_width_external'] . 'px; max-height: ' . $modSettings['avatar_max_height_external'] . 'px; }');
3721
3722
	// Add max image limits
3723
	if (!empty($modSettings['max_image_width']))
3724
		addInlineCss('
3725
	.postarea .bbc_img { max-width: ' . $modSettings['max_image_width'] . 'px; }');
3726
3727
	if (!empty($modSettings['max_image_height']))
3728
		addInlineCss('
3729
	.postarea .bbc_img { max-height: ' . $modSettings['max_image_height'] . 'px; }');
3730
3731
	// This looks weird, but it's because BoardIndex.php references the variable.
3732
	$context['common_stats']['latest_member'] = array(
3733
		'id' => $modSettings['latestMember'],
3734
		'name' => $modSettings['latestRealName'],
3735
		'href' => $scripturl . '?action=profile;u=' . $modSettings['latestMember'],
3736
		'link' => '<a href="' . $scripturl . '?action=profile;u=' . $modSettings['latestMember'] . '">' . $modSettings['latestRealName'] . '</a>',
3737
	);
3738
	$context['common_stats'] = array(
3739
		'total_posts' => comma_format($modSettings['totalMessages']),
3740
		'total_topics' => comma_format($modSettings['totalTopics']),
3741
		'total_members' => comma_format($modSettings['totalMembers']),
3742
		'latest_member' => $context['common_stats']['latest_member'],
3743
	);
3744
	$context['common_stats']['boardindex_total_posts'] = sprintf($txt['boardindex_total_posts'], $context['common_stats']['total_posts'], $context['common_stats']['total_topics'], $context['common_stats']['total_members']);
3745
3746
	if (empty($settings['theme_version']))
3747
		addJavaScriptVar('smf_scripturl', $scripturl);
3748
3749
	if (!isset($context['page_title']))
3750
		$context['page_title'] = '';
3751
3752
	// Set some specific vars.
3753
	$context['page_title_html_safe'] = $smcFunc['htmlspecialchars'](un_htmlspecialchars($context['page_title'])) . (!empty($context['current_page']) ? ' - ' . $txt['page'] . ' ' . ($context['current_page'] + 1) : '');
3754
	$context['meta_keywords'] = !empty($modSettings['meta_keywords']) ? $smcFunc['htmlspecialchars']($modSettings['meta_keywords']) : '';
3755
3756
	// Content related meta tags, including Open Graph
3757
	$context['meta_tags'][] = array('property' => 'og:site_name', 'content' => $context['forum_name']);
3758
	$context['meta_tags'][] = array('property' => 'og:title', 'content' => $context['page_title_html_safe']);
3759
3760
	if (!empty($context['meta_keywords']))
3761
		$context['meta_tags'][] = array('name' => 'keywords', 'content' => $context['meta_keywords']);
3762
3763
	if (!empty($context['canonical_url']))
3764
		$context['meta_tags'][] = array('property' => 'og:url', 'content' => $context['canonical_url']);
3765
3766
	if (!empty($settings['og_image']))
3767
		$context['meta_tags'][] = array('property' => 'og:image', 'content' => $settings['og_image']);
3768
3769
	if (!empty($context['meta_description']))
3770
	{
3771
		$context['meta_tags'][] = array('property' => 'og:description', 'content' => $context['meta_description']);
3772
		$context['meta_tags'][] = array('name' => 'description', 'content' => $context['meta_description']);
3773
	}
3774
	else
3775
	{
3776
		$context['meta_tags'][] = array('property' => 'og:description', 'content' => $context['page_title_html_safe']);
3777
		$context['meta_tags'][] = array('name' => 'description', 'content' => $context['page_title_html_safe']);
3778
	}
3779
3780
	call_integration_hook('integrate_theme_context');
3781
}
3782
3783
/**
3784
 * Helper function to set the system memory to a needed value
3785
 * - If the needed memory is greater than current, will attempt to get more
3786
 * - if in_use is set to true, will also try to take the current memory usage in to account
3787
 *
3788
 * @param string $needed The amount of memory to request, if needed, like 256M
3789
 * @param bool $in_use Set to true to account for current memory usage of the script
3790
 * @return boolean True if we have at least the needed memory
3791
 */
3792
function setMemoryLimit($needed, $in_use = false)
3793
{
3794
	// everything in bytes
3795
	$memory_current = memoryReturnBytes(ini_get('memory_limit'));
3796
	$memory_needed = memoryReturnBytes($needed);
3797
3798
	// should we account for how much is currently being used?
3799
	if ($in_use)
3800
		$memory_needed += function_exists('memory_get_usage') ? memory_get_usage() : (2 * 1048576);
3801
3802
	// if more is needed, request it
3803
	if ($memory_current < $memory_needed)
3804
	{
3805
		@ini_set('memory_limit', ceil($memory_needed / 1048576) . 'M');
3806
		$memory_current = memoryReturnBytes(ini_get('memory_limit'));
3807
	}
3808
3809
	$memory_current = max($memory_current, memoryReturnBytes(get_cfg_var('memory_limit')));
3810
3811
	// return success or not
3812
	return (bool) ($memory_current >= $memory_needed);
3813
}
3814
3815
/**
3816
 * Helper function to convert memory string settings to bytes
3817
 *
3818
 * @param string $val The byte string, like 256M or 1G
3819
 * @return integer The string converted to a proper integer in bytes
3820
 */
3821
function memoryReturnBytes($val)
3822
{
3823
	if (is_integer($val))
3824
		return $val;
3825
3826
	// Separate the number from the designator
3827
	$val = trim($val);
3828
	$num = intval(substr($val, 0, strlen($val) - 1));
3829
	$last = strtolower(substr($val, -1));
3830
3831
	// convert to bytes
3832
	switch ($last)
3833
	{
3834
		case 'g':
3835
			$num *= 1024;
3836
		case 'm':
3837
			$num *= 1024;
3838
		case 'k':
3839
			$num *= 1024;
3840
	}
3841
	return $num;
3842
}
3843
3844
/**
3845
 * The header template
3846
 */
3847
function template_header()
3848
{
3849
	global $txt, $modSettings, $context, $user_info, $boarddir, $cachedir, $cache_enable;
3850
3851
	setupThemeContext();
3852
3853
	// Print stuff to prevent caching of pages (except on attachment errors, etc.)
3854
	if (empty($context['no_last_modified']))
3855
	{
3856
		header('expires: Mon, 26 Jul 1997 05:00:00 GMT');
3857
		header('last-modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
3858
3859
		// Are we debugging the template/html content?
3860
		if (!isset($_REQUEST['xml']) && isset($_GET['debug']) && !isBrowser('ie'))
3861
			header('content-type: application/xhtml+xml');
3862
		elseif (!isset($_REQUEST['xml']))
3863
			header('content-type: text/html; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
3864
	}
3865
3866
	header('content-type: text/' . (isset($_REQUEST['xml']) ? 'xml' : 'html') . '; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
3867
3868
	// We need to splice this in after the body layer, or after the main layer for older stuff.
3869
	if ($context['in_maintenance'] && $context['user']['is_admin'])
3870
	{
3871
		$position = array_search('body', $context['template_layers']);
3872
		if ($position === false)
3873
			$position = array_search('main', $context['template_layers']);
3874
3875
		if ($position !== false)
3876
		{
3877
			$before = array_slice($context['template_layers'], 0, $position + 1);
3878
			$after = array_slice($context['template_layers'], $position + 1);
3879
			$context['template_layers'] = array_merge($before, array('maint_warning'), $after);
3880
		}
3881
	}
3882
3883
	$checked_securityFiles = false;
3884
	$showed_banned = false;
3885
	foreach ($context['template_layers'] as $layer)
3886
	{
3887
		loadSubTemplate($layer . '_above', true);
3888
3889
		// May seem contrived, but this is done in case the body and main layer aren't there...
3890
		if (in_array($layer, array('body', 'main')) && allowedTo('admin_forum') && !$user_info['is_guest'] && !$checked_securityFiles)
3891
		{
3892
			$checked_securityFiles = true;
3893
3894
			$securityFiles = array('install.php', 'upgrade.php', 'convert.php', 'repair_paths.php', 'repair_settings.php', 'Settings.php~', 'Settings_bak.php~');
3895
3896
			// Add your own files.
3897
			call_integration_hook('integrate_security_files', array(&$securityFiles));
3898
3899
			foreach ($securityFiles as $i => $securityFile)
3900
			{
3901
				if (!file_exists($boarddir . '/' . $securityFile))
3902
					unset($securityFiles[$i]);
3903
			}
3904
3905
			// We are already checking so many files...just few more doesn't make any difference! :P
3906
			if (!empty($modSettings['currentAttachmentUploadDir']))
3907
				$path = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
3908
3909
			else
3910
				$path = $modSettings['attachmentUploadDir'];
3911
3912
			secureDirectory($path, true);
3913
			secureDirectory($cachedir);
3914
3915
			// If agreement is enabled, at least the english version shall exists
3916
			if ($modSettings['requireAgreement'])
3917
				$agreement = !file_exists($boarddir . '/agreement.txt');
3918
3919
			if (!empty($securityFiles) || (!empty($cache_enable) && !is_writable($cachedir)) || !empty($agreement))
3920
			{
3921
				echo '
3922
		<div class="errorbox">
3923
			<p class="alert">!!</p>
3924
			<h3>', empty($securityFiles) ? $txt['generic_warning'] : $txt['security_risk'], '</h3>
3925
			<p>';
3926
3927
				foreach ($securityFiles as $securityFile)
3928
				{
3929
					echo '
3930
				', $txt['not_removed'], '<strong>', $securityFile, '</strong>!<br>';
3931
3932
					if ($securityFile == 'Settings.php~' || $securityFile == 'Settings_bak.php~')
3933
						echo '
3934
				', sprintf($txt['not_removed_extra'], $securityFile, substr($securityFile, 0, -1)), '<br>';
3935
				}
3936
3937
				if (!empty($cache_enable) && !is_writable($cachedir))
3938
					echo '
3939
				<strong>', $txt['cache_writable'], '</strong><br>';
3940
3941
				if (!empty($agreement))
3942
					echo '
3943
				<strong>', $txt['agreement_missing'], '</strong><br>';
3944
3945
				echo '
3946
			</p>
3947
		</div>';
3948
			}
3949
		}
3950
		// If the user is banned from posting inform them of it.
3951
		elseif (in_array($layer, array('main', 'body')) && isset($_SESSION['ban']['cannot_post']) && !$showed_banned)
3952
		{
3953
			$showed_banned = true;
3954
			echo '
3955
				<div class="windowbg alert" style="margin: 2ex; padding: 2ex; border: 2px dashed red;">
3956
					', sprintf($txt['you_are_post_banned'], $user_info['is_guest'] ? $txt['guest_title'] : $user_info['name']);
3957
3958
			if (!empty($_SESSION['ban']['cannot_post']['reason']))
3959
				echo '
3960
					<div style="padding-left: 4ex; padding-top: 1ex;">', $_SESSION['ban']['cannot_post']['reason'], '</div>';
3961
3962
			if (!empty($_SESSION['ban']['expire_time']))
3963
				echo '
3964
					<div>', sprintf($txt['your_ban_expires'], timeformat($_SESSION['ban']['expire_time'], false)), '</div>';
3965
			else
3966
				echo '
3967
					<div>', $txt['your_ban_expires_never'], '</div>';
3968
3969
			echo '
3970
				</div>';
3971
		}
3972
	}
3973
}
3974
3975
/**
3976
 * Show the copyright.
3977
 */
3978
function theme_copyright()
3979
{
3980
	global $forum_copyright;
3981
3982
	// Don't display copyright for things like SSI.
3983
	if (SMF !== 1)
3984
		return;
3985
3986
	// Put in the version...
3987
	printf($forum_copyright, SMF_FULL_VERSION, SMF_SOFTWARE_YEAR);
3988
}
3989
3990
/**
3991
 * The template footer
3992
 */
3993
function template_footer()
3994
{
3995
	global $context, $modSettings, $db_count;
3996
3997
	// Show the load time?  (only makes sense for the footer.)
3998
	$context['show_load_time'] = !empty($modSettings['timeLoadPageEnable']);
3999
	$context['load_time'] = round(microtime(true) - TIME_START, 3);
4000
	$context['load_queries'] = $db_count;
4001
4002
	if (!empty($context['template_layers']) && is_array($context['template_layers']))
4003
		foreach (array_reverse($context['template_layers']) as $layer)
4004
			loadSubTemplate($layer . '_below', true);
4005
}
4006
4007
/**
4008
 * Output the Javascript files
4009
 * 	- tabbing in this function is to make the HTML source look good and proper
4010
 *  - if deferred is set function will output all JS set to load at page end
4011
 *
4012
 * @param bool $do_deferred If true will only output the deferred JS (the stuff that goes right before the closing body tag)
4013
 */
4014
function template_javascript($do_deferred = false)
4015
{
4016
	global $context, $modSettings, $settings;
4017
4018
	// Use this hook to minify/optimize Javascript files and vars
4019
	call_integration_hook('integrate_pre_javascript_output', array(&$do_deferred));
4020
4021
	$toMinify = array(
4022
		'standard' => array(),
4023
		'defer' => array(),
4024
		'async' => array(),
4025
	);
4026
4027
	// Ouput the declared Javascript variables.
4028
	if (!empty($context['javascript_vars']) && !$do_deferred)
4029
	{
4030
		echo '
4031
	<script>';
4032
4033
		foreach ($context['javascript_vars'] as $key => $value)
4034
		{
4035
			if (empty($value))
4036
			{
4037
				echo '
4038
		var ', $key, ';';
4039
			}
4040
			else
4041
			{
4042
				echo '
4043
		var ', $key, ' = ', $value, ';';
4044
			}
4045
		}
4046
4047
		echo '
4048
	</script>';
4049
	}
4050
4051
	// In the dark days before HTML5, deferred JS files needed to be loaded at the end of the body.
4052
	// Now we load them in the head and use 'async' and/or 'defer' attributes. Much better performance.
4053
	if (!$do_deferred)
4054
	{
4055
		// While we have JavaScript files to place in the template.
4056
		foreach ($context['javascript_files'] as $id => $js_file)
4057
		{
4058
			// Last minute call! allow theme authors to disable single files.
4059
			if (!empty($settings['disable_files']) && in_array($id, $settings['disable_files']))
4060
				continue;
4061
4062
			// By default files don't get minimized unless the file explicitly says so!
4063
			if (!empty($js_file['options']['minimize']) && !empty($modSettings['minimize_files']))
4064
			{
4065
				if (!empty($js_file['options']['async']))
4066
					$toMinify['async'][] = $js_file;
4067
4068
				elseif (!empty($js_file['options']['defer']))
4069
					$toMinify['defer'][] = $js_file;
4070
4071
				else
4072
					$toMinify['standard'][] = $js_file;
4073
4074
				// Grab a random seed.
4075
				if (!isset($minSeed) && isset($js_file['options']['seed']))
4076
					$minSeed = $js_file['options']['seed'];
4077
			}
4078
4079
			else
4080
			{
4081
				echo '
4082
	<script src="', $js_file['fileUrl'], isset($js_file['options']['seed']) ? $js_file['options']['seed'] : '', '"', !empty($js_file['options']['async']) ? ' async' : '', !empty($js_file['options']['defer']) ? ' defer' : '';
4083
4084
				if (!empty($js_file['options']['attributes']))
4085
					foreach ($js_file['options']['attributes'] as $key => $value)
4086
					{
4087
						if (is_bool($value))
4088
							echo !empty($value) ? ' ' . $key : '';
4089
4090
						else
4091
							echo ' ', $key, '="', $value, '"';
4092
					}
4093
4094
				echo '></script>';
4095
			}
4096
		}
4097
4098
		foreach ($toMinify as $js_files)
4099
		{
4100
			if (!empty($js_files))
4101
			{
4102
				$result = custMinify($js_files, 'js');
4103
4104
				$minSuccessful = array_keys($result) === array('smf_minified');
4105
4106
				foreach ($result as $minFile)
4107
					echo '
4108
	<script src="', $minFile['fileUrl'], $minSuccessful && isset($minSeed) ? $minSeed : '', '"', !empty($minFile['options']['async']) ? ' async' : '', !empty($minFile['options']['defer']) ? ' defer' : '', '></script>';
4109
			}
4110
		}
4111
	}
4112
4113
	// Inline JavaScript - Actually useful some times!
4114
	if (!empty($context['javascript_inline']))
4115
	{
4116
		if (!empty($context['javascript_inline']['defer']) && $do_deferred)
4117
		{
4118
			echo '
4119
<script>
4120
window.addEventListener("DOMContentLoaded", function() {';
4121
4122
			foreach ($context['javascript_inline']['defer'] as $js_code)
4123
				echo $js_code;
4124
4125
			echo '
4126
});
4127
</script>';
4128
		}
4129
4130
		if (!empty($context['javascript_inline']['standard']) && !$do_deferred)
4131
		{
4132
			echo '
4133
	<script>';
4134
4135
			foreach ($context['javascript_inline']['standard'] as $js_code)
4136
				echo $js_code;
4137
4138
			echo '
4139
	</script>';
4140
		}
4141
	}
4142
}
4143
4144
/**
4145
 * Output the CSS files
4146
 */
4147
function template_css()
4148
{
4149
	global $context, $db_show_debug, $boardurl, $settings, $modSettings;
4150
4151
	// Use this hook to minify/optimize CSS files
4152
	call_integration_hook('integrate_pre_css_output');
4153
4154
	$toMinify = array();
4155
	$normal = array();
4156
4157
	usort($context['css_files'], function ($a, $b)
4158
	{
4159
		return $a['options']['order_pos'] < $b['options']['order_pos'] ? -1 : ($a['options']['order_pos'] > $b['options']['order_pos'] ? 1 : 0);
4160
	});
4161
	foreach ($context['css_files'] as $id => $file)
4162
	{
4163
		// Last minute call! allow theme authors to disable single files.
4164
		if (!empty($settings['disable_files']) && in_array($id, $settings['disable_files']))
4165
			continue;
4166
4167
		// Files are minimized unless they explicitly opt out.
4168
		if (!isset($file['options']['minimize']))
4169
			$file['options']['minimize'] = true;
4170
4171
		if (!empty($file['options']['minimize']) && !empty($modSettings['minimize_files']) && !isset($_REQUEST['normalcss']))
4172
		{
4173
			$toMinify[] = $file;
4174
4175
			// Grab a random seed.
4176
			if (!isset($minSeed) && isset($file['options']['seed']))
4177
				$minSeed = $file['options']['seed'];
4178
		}
4179
		else
4180
			$normal[] = array(
4181
				'url' => $file['fileUrl'] . (isset($file['options']['seed']) ? $file['options']['seed'] : ''),
4182
				'attributes' => !empty($file['options']['attributes']) ? $file['options']['attributes'] : array()
4183
			);
4184
	}
4185
4186
	if (!empty($toMinify))
4187
	{
4188
		$result = custMinify($toMinify, 'css');
4189
4190
		$minSuccessful = array_keys($result) === array('smf_minified');
4191
4192
		foreach ($result as $minFile)
4193
			echo '
4194
	<link rel="stylesheet" href="', $minFile['fileUrl'], $minSuccessful && isset($minSeed) ? $minSeed : '', '">';
4195
	}
4196
4197
	// Print the rest after the minified files.
4198
	if (!empty($normal))
4199
		foreach ($normal as $nf)
4200
		{
4201
			echo '
4202
	<link rel="stylesheet" href="', $nf['url'], '"';
4203
4204
			if (!empty($nf['attributes']))
4205
				foreach ($nf['attributes'] as $key => $value)
4206
				{
4207
					if (is_bool($value))
4208
						echo !empty($value) ? ' ' . $key : '';
4209
					else
4210
						echo ' ', $key, '="', $value, '"';
4211
				}
4212
4213
			echo '>';
4214
		}
4215
4216
	if ($db_show_debug === true)
4217
	{
4218
		// Try to keep only what's useful.
4219
		$repl = array($boardurl . '/Themes/' => '', $boardurl . '/' => '');
4220
		foreach ($context['css_files'] as $file)
4221
			$context['debug']['sheets'][] = strtr($file['fileName'], $repl);
4222
	}
4223
4224
	if (!empty($context['css_header']))
4225
	{
4226
		echo '
4227
	<style>';
4228
4229
		foreach ($context['css_header'] as $css)
4230
			echo $css . '
4231
	';
4232
4233
		echo '
4234
	</style>';
4235
	}
4236
}
4237
4238
/**
4239
 * Get an array of previously defined files and adds them to our main minified files.
4240
 * Sets a one day cache to avoid re-creating a file on every request.
4241
 *
4242
 * @param array $data The files to minify.
4243
 * @param string $type either css or js.
4244
 * @return array Info about the minified file, or about the original files if the minify process failed.
4245
 */
4246
function custMinify($data, $type)
4247
{
4248
	global $settings, $txt;
4249
4250
	$types = array('css', 'js');
4251
	$type = !empty($type) && in_array($type, $types) ? $type : false;
4252
	$data = is_array($data) ? $data : array();
4253
4254
	if (empty($type) || empty($data))
4255
		return $data;
4256
4257
	// Different pages include different files, so we use a hash to label the different combinations
4258
	$hash = md5(implode(' ', array_map(function($file)
4259
	{
4260
		return $file['filePath'] . '-' . $file['mtime'];
4261
	}, $data)));
4262
4263
	// Is this a deferred or asynchronous JavaScript file?
4264
	$async = $type === 'js';
4265
	$defer = $type === 'js';
4266
	if ($type === 'js')
4267
	{
4268
		foreach ($data as $id => $file)
4269
		{
4270
			// A minified script should only be loaded asynchronously if all its components wanted to be.
4271
			if (empty($file['options']['async']))
4272
				$async = false;
4273
4274
			// A minified script should only be deferred if all its components wanted to be.
4275
			if (empty($file['options']['defer']))
4276
				$defer = false;
4277
		}
4278
	}
4279
4280
	// Did we already do this?
4281
	$minified_file = $settings['theme_dir'] . '/' . ($type == 'css' ? 'css' : 'scripts') . '/minified_' . $hash . '.' . $type;
4282
	$already_exists = file_exists($minified_file);
4283
4284
	// Already done?
4285
	if ($already_exists)
4286
	{
4287
		return array('smf_minified' => array(
4288
			'fileUrl' => $settings['theme_url'] . '/' . ($type == 'css' ? 'css' : 'scripts') . '/' . basename($minified_file),
4289
			'filePath' => $minified_file,
4290
			'fileName' => basename($minified_file),
4291
			'options' => array('async' => !empty($async), 'defer' => !empty($defer)),
4292
		));
4293
	}
4294
	// File has to exist. If it doesn't, try to create it.
4295
	elseif (@fopen($minified_file, 'w') === false || !smf_chmod($minified_file))
4296
	{
4297
		loadLanguage('Errors');
4298
		log_error(sprintf($txt['file_not_created'], $minified_file), 'general');
4299
4300
		// The process failed, so roll back to print each individual file.
4301
		return $data;
4302
	}
4303
4304
	// No namespaces, sorry!
4305
	$classType = 'MatthiasMullie\\Minify\\' . strtoupper($type);
4306
4307
	$minifier = new $classType();
4308
4309
	foreach ($data as $id => $file)
4310
	{
4311
		$toAdd = !empty($file['filePath']) && file_exists($file['filePath']) ? $file['filePath'] : false;
4312
4313
		// The file couldn't be located so it won't be added. Log this error.
4314
		if (empty($toAdd))
4315
		{
4316
			loadLanguage('Errors');
4317
			log_error(sprintf($txt['file_minimize_fail'], !empty($file['fileName']) ? $file['fileName'] : $id), 'general');
4318
			continue;
4319
		}
4320
4321
		// Add this file to the list.
4322
		$minifier->add($toAdd);
4323
	}
4324
4325
	// Create the file.
4326
	$minifier->minify($minified_file);
4327
	unset($minifier);
4328
	clearstatcache();
4329
4330
	// Minify process failed.
4331
	if (!filesize($minified_file))
4332
	{
4333
		loadLanguage('Errors');
4334
		log_error(sprintf($txt['file_not_created'], $minified_file), 'general');
4335
4336
		// The process failed so roll back to print each individual file.
4337
		return $data;
4338
	}
4339
4340
	return array('smf_minified' => array(
4341
		'fileUrl' => $settings['theme_url'] . '/' . ($type == 'css' ? 'css' : 'scripts') . '/' . basename($minified_file),
4342
		'filePath' => $minified_file,
4343
		'fileName' => basename($minified_file),
4344
		'options' => array('async' => $async, 'defer' => $defer),
4345
	));
4346
}
4347
4348
/**
4349
 * Clears out old minimized CSS and JavaScript files and ensures $modSettings['browser_cache'] is up to date
4350
 */
4351
function deleteAllMinified()
4352
{
4353
	global $smcFunc, $txt, $modSettings;
4354
4355
	$not_deleted = array();
4356
	$most_recent = 0;
4357
4358
	// Kinda sucks that we need to do another query to get all the theme dirs, but c'est la vie.
4359
	$request = $smcFunc['db_query']('', '
4360
		SELECT id_theme AS id, value AS dir
4361
		FROM {db_prefix}themes
4362
		WHERE variable = {string:var}',
4363
		array(
4364
			'var' => 'theme_dir',
4365
		)
4366
	);
4367
	while ($theme = $smcFunc['db_fetch_assoc']($request))
4368
	{
4369
		foreach (array('css', 'js') as $type)
4370
		{
4371
			foreach (glob(rtrim($theme['dir'], '/') . '/' . ($type == 'css' ? 'css' : 'scripts') . '/*.' . $type) as $filename)
4372
			{
4373
				// We want to find the most recent mtime of non-minified files
4374
				if (strpos(pathinfo($filename, PATHINFO_BASENAME), 'minified') === false)
4375
					$most_recent = max($modSettings['browser_cache'], (int) @filemtime($filename));
4376
4377
				// Try to delete minified files. Add them to our error list if that fails.
4378
				elseif (!@unlink($filename))
4379
					$not_deleted[] = $filename;
4380
			}
4381
		}
4382
	}
4383
	$smcFunc['db_free_result']($request);
4384
4385
	// This setting tracks the most recent modification time of any of our CSS and JS files
4386
	if ($most_recent > $modSettings['browser_cache'])
4387
		updateSettings(array('browser_cache' => $most_recent));
4388
4389
	// If any of the files could not be deleted, log an error about it.
4390
	if (!empty($not_deleted))
4391
	{
4392
		loadLanguage('Errors');
4393
		log_error(sprintf($txt['unlink_minimized_fail'], implode('<br>', $not_deleted)), 'general');
4394
	}
4395
}
4396
4397
/**
4398
 * Get an attachment's encrypted filename. If $new is true, won't check for file existence.
4399
 *
4400
 * @todo this currently returns the hash if new, and the full filename otherwise.
4401
 * Something messy like that.
4402
 * @todo and of course everything relies on this behavior and work around it. :P.
4403
 * Converters included.
4404
 *
4405
 * @param string $filename The name of the file
4406
 * @param int $attachment_id The ID of the attachment
4407
 * @param string|null $dir Which directory it should be in (null to use current one)
4408
 * @param bool $new Whether this is a new attachment
4409
 * @param string $file_hash The file hash
4410
 * @return string The path to the file
4411
 */
4412
function getAttachmentFilename($filename, $attachment_id, $dir = null, $new = false, $file_hash = '')
4413
{
4414
	global $modSettings, $smcFunc;
4415
4416
	// Just make up a nice hash...
4417
	if ($new)
4418
		return sha1(md5($filename . time()) . mt_rand());
4419
4420
	// Just make sure that attachment id is only a int
4421
	$attachment_id = (int) $attachment_id;
4422
4423
	// Grab the file hash if it wasn't added.
4424
	// Left this for legacy.
4425
	if ($file_hash === '')
4426
	{
4427
		$request = $smcFunc['db_query']('', '
4428
			SELECT file_hash
4429
			FROM {db_prefix}attachments
4430
			WHERE id_attach = {int:id_attach}',
4431
			array(
4432
				'id_attach' => $attachment_id,
4433
			)
4434
		);
4435
4436
		if ($smcFunc['db_num_rows']($request) === 0)
4437
			return false;
4438
4439
		list ($file_hash) = $smcFunc['db_fetch_row']($request);
4440
		$smcFunc['db_free_result']($request);
4441
	}
4442
4443
	// Still no hash? mmm...
4444
	if (empty($file_hash))
4445
		$file_hash = sha1(md5($filename . time()) . mt_rand());
4446
4447
	// Are we using multiple directories?
4448
	if (is_array($modSettings['attachmentUploadDir']))
4449
		$path = $modSettings['attachmentUploadDir'][$dir];
4450
4451
	else
4452
		$path = $modSettings['attachmentUploadDir'];
4453
4454
	return $path . '/' . $attachment_id . '_' . $file_hash . '.dat';
4455
}
4456
4457
/**
4458
 * Convert a single IP to a ranged IP.
4459
 * internal function used to convert a user-readable format to a format suitable for the database.
4460
 *
4461
 * @param string $fullip The full IP
4462
 * @return array An array of IP parts
4463
 */
4464
function ip2range($fullip)
4465
{
4466
	// Pretend that 'unknown' is 255.255.255.255. (since that can't be an IP anyway.)
4467
	if ($fullip == 'unknown')
4468
		$fullip = '255.255.255.255';
4469
4470
	$ip_parts = explode('-', $fullip);
4471
	$ip_array = array();
4472
4473
	// if ip 22.12.31.21
4474
	if (count($ip_parts) == 1 && isValidIP($fullip))
4475
	{
4476
		$ip_array['low'] = $fullip;
4477
		$ip_array['high'] = $fullip;
4478
		return $ip_array;
4479
	} // if ip 22.12.* -> 22.12.* - 22.12.*
4480
	elseif (count($ip_parts) == 1)
4481
	{
4482
		$ip_parts[0] = $fullip;
4483
		$ip_parts[1] = $fullip;
4484
	}
4485
4486
	// if ip 22.12.31.21-12.21.31.21
4487
	if (count($ip_parts) == 2 && isValidIP($ip_parts[0]) && isValidIP($ip_parts[1]))
4488
	{
4489
		$ip_array['low'] = $ip_parts[0];
4490
		$ip_array['high'] = $ip_parts[1];
4491
		return $ip_array;
4492
	}
4493
	elseif (count($ip_parts) == 2) // if ip 22.22.*-22.22.*
4494
	{
4495
		$valid_low = isValidIP($ip_parts[0]);
4496
		$valid_high = isValidIP($ip_parts[1]);
4497
		$count = 0;
4498
		$mode = (preg_match('/:/', $ip_parts[0]) > 0 ? ':' : '.');
4499
		$max = ($mode == ':' ? 'ffff' : '255');
4500
		$min = 0;
4501
		if (!$valid_low)
4502
		{
4503
			$ip_parts[0] = preg_replace('/\*/', '0', $ip_parts[0]);
4504
			$valid_low = isValidIP($ip_parts[0]);
4505
			while (!$valid_low)
4506
			{
4507
				$ip_parts[0] .= $mode . $min;
4508
				$valid_low = isValidIP($ip_parts[0]);
4509
				$count++;
4510
				if ($count > 9) break;
4511
			}
4512
		}
4513
4514
		$count = 0;
4515
		if (!$valid_high)
4516
		{
4517
			$ip_parts[1] = preg_replace('/\*/', $max, $ip_parts[1]);
4518
			$valid_high = isValidIP($ip_parts[1]);
4519
			while (!$valid_high)
4520
			{
4521
				$ip_parts[1] .= $mode . $max;
4522
				$valid_high = isValidIP($ip_parts[1]);
4523
				$count++;
4524
				if ($count > 9) break;
4525
			}
4526
		}
4527
4528
		if ($valid_high && $valid_low)
4529
		{
4530
			$ip_array['low'] = $ip_parts[0];
4531
			$ip_array['high'] = $ip_parts[1];
4532
		}
4533
	}
4534
4535
	return $ip_array;
4536
}
4537
4538
/**
4539
 * Lookup an IP; try shell_exec first because we can do a timeout on it.
4540
 *
4541
 * @param string $ip The IP to get the hostname from
4542
 * @return string The hostname
4543
 */
4544
function host_from_ip($ip)
4545
{
4546
	global $modSettings;
4547
4548
	if (($host = cache_get_data('hostlookup-' . $ip, 600)) !== null)
4549
		return $host;
4550
	$t = microtime(true);
4551
4552
	// Try the Linux host command, perhaps?
4553
	if (!isset($host) && (strpos(strtolower(PHP_OS), 'win') === false || strpos(strtolower(PHP_OS), 'darwin') !== false) && mt_rand(0, 1) == 1)
4554
	{
4555
		if (!isset($modSettings['host_to_dis']))
4556
			$test = @shell_exec('host -W 1 ' . @escapeshellarg($ip));
4557
		else
4558
			$test = @shell_exec('host ' . @escapeshellarg($ip));
4559
4560
		// Did host say it didn't find anything?
4561
		if (strpos($test, 'not found') !== false)
4562
			$host = '';
4563
		// Invalid server option?
4564
		elseif ((strpos($test, 'invalid option') || strpos($test, 'Invalid query name 1')) && !isset($modSettings['host_to_dis']))
4565
			updateSettings(array('host_to_dis' => 1));
4566
		// Maybe it found something, after all?
4567
		elseif (preg_match('~\s([^\s]+?)\.\s~', $test, $match) == 1)
4568
			$host = $match[1];
4569
	}
4570
4571
	// This is nslookup; usually only Windows, but possibly some Unix?
4572
	if (!isset($host) && stripos(PHP_OS, 'win') !== false && strpos(strtolower(PHP_OS), 'darwin') === false && mt_rand(0, 1) == 1)
4573
	{
4574
		$test = @shell_exec('nslookup -timeout=1 ' . @escapeshellarg($ip));
4575
		if (strpos($test, 'Non-existent domain') !== false)
4576
			$host = '';
4577
		elseif (preg_match('~Name:\s+([^\s]+)~', $test, $match) == 1)
4578
			$host = $match[1];
4579
	}
4580
4581
	// This is the last try :/.
4582
	if (!isset($host) || $host === false)
4583
		$host = @gethostbyaddr($ip);
4584
4585
	// It took a long time, so let's cache it!
4586
	if (microtime(true) - $t > 0.5)
4587
		cache_put_data('hostlookup-' . $ip, $host, 600);
4588
4589
	return $host;
4590
}
4591
4592
/**
4593
 * Chops a string into words and prepares them to be inserted into (or searched from) the database.
4594
 *
4595
 * @param string $text The text to split into words
4596
 * @param int $max_chars The maximum number of characters per word
4597
 * @param bool $encrypt Whether to encrypt the results
4598
 * @return array An array of ints or words depending on $encrypt
4599
 */
4600
function text2words($text, $max_chars = 20, $encrypt = false)
4601
{
4602
	global $smcFunc, $context;
4603
4604
	// Step 1: Remove entities/things we don't consider words:
4605
	$words = preg_replace('~(?:[\x0B\0' . ($context['utf8'] ? '\x{A0}' : '\xA0') . '\t\r\s\n(){}\\[\\]<>!@$%^*.,:+=`\~\?/\\\\]+|&(?:amp|lt|gt|quot);)+~' . ($context['utf8'] ? 'u' : ''), ' ', strtr($text, array('<br>' => ' ')));
4606
4607
	// Step 2: Entities we left to letters, where applicable, lowercase.
4608
	$words = un_htmlspecialchars($smcFunc['strtolower']($words));
4609
4610
	// Step 3: Ready to split apart and index!
4611
	$words = explode(' ', $words);
4612
4613
	if ($encrypt)
4614
	{
4615
		$possible_chars = array_flip(array_merge(range(46, 57), range(65, 90), range(97, 122)));
4616
		$returned_ints = array();
4617
		foreach ($words as $word)
4618
		{
4619
			if (($word = trim($word, '-_\'')) !== '')
4620
			{
4621
				$encrypted = substr(crypt($word, 'uk'), 2, $max_chars);
4622
				$total = 0;
4623
				for ($i = 0; $i < $max_chars; $i++)
4624
					$total += $possible_chars[ord($encrypted[$i])] * pow(63, $i);
4625
				$returned_ints[] = $max_chars == 4 ? min($total, 16777215) : $total;
4626
			}
4627
		}
4628
		return array_unique($returned_ints);
4629
	}
4630
	else
4631
	{
4632
		// Trim characters before and after and add slashes for database insertion.
4633
		$returned_words = array();
4634
		foreach ($words as $word)
4635
			if (($word = trim($word, '-_\'')) !== '')
4636
				$returned_words[] = $max_chars === null ? $word : substr($word, 0, $max_chars);
4637
4638
		// Filter out all words that occur more than once.
4639
		return array_unique($returned_words);
4640
	}
4641
}
4642
4643
/**
4644
 * Creates an image/text button
4645
 *
4646
 * @deprecated since 2.1
4647
 * @param string $name The name of the button (should be a main_icons class or the name of an image)
4648
 * @param string $alt The alt text
4649
 * @param string $label The $txt string to use as the label
4650
 * @param string $custom Custom text/html to add to the img tag (only when using an actual image)
4651
 * @param boolean $force_use Whether to force use of this when template_create_button is available
4652
 * @return string The HTML to display the button
4653
 */
4654
function create_button($name, $alt, $label = '', $custom = '', $force_use = false)
4655
{
4656
	global $settings, $txt;
4657
4658
	// Does the current loaded theme have this and we are not forcing the usage of this function?
4659
	if (function_exists('template_create_button') && !$force_use)
4660
		return template_create_button($name, $alt, $label = '', $custom = '');
4661
4662
	if (!$settings['use_image_buttons'])
4663
		return $txt[$alt];
4664
	elseif (!empty($settings['use_buttons']))
4665
		return '<span class="main_icons ' . $name . '" alt="' . $txt[$alt] . '"></span>' . ($label != '' ? '&nbsp;<strong>' . $txt[$label] . '</strong>' : '');
4666
	else
4667
		return '<img src="' . $settings['lang_images_url'] . '/' . $name . '" alt="' . $txt[$alt] . '" ' . $custom . '>';
4668
}
4669
4670
/**
4671
 * Sets up all of the top menu buttons
4672
 * Saves them in the cache if it is available and on
4673
 * Places the results in $context
4674
 */
4675
function setupMenuContext()
4676
{
4677
	global $context, $modSettings, $user_info, $txt, $scripturl, $sourcedir, $settings, $smcFunc, $cache_enable;
4678
4679
	// Set up the menu privileges.
4680
	$context['allow_search'] = !empty($modSettings['allow_guestAccess']) ? allowedTo('search_posts') : (!$user_info['is_guest'] && allowedTo('search_posts'));
4681
	$context['allow_admin'] = allowedTo(array('admin_forum', 'manage_boards', 'manage_permissions', 'moderate_forum', 'manage_membergroups', 'manage_bans', 'send_mail', 'edit_news', 'manage_attachments', 'manage_smileys'));
4682
4683
	$context['allow_memberlist'] = allowedTo('view_mlist');
4684
	$context['allow_calendar'] = allowedTo('calendar_view') && !empty($modSettings['cal_enabled']);
4685
	$context['allow_moderation_center'] = $context['user']['can_mod'];
4686
	$context['allow_pm'] = allowedTo('pm_read');
4687
4688
	$cacheTime = $modSettings['lastActive'] * 60;
4689
4690
	// Initial "can you post an event in the calendar" option - but this might have been set in the calendar already.
4691
	if (!isset($context['allow_calendar_event']))
4692
	{
4693
		$context['allow_calendar_event'] = $context['allow_calendar'] && allowedTo('calendar_post');
4694
4695
		// If you don't allow events not linked to posts and you're not an admin, we have more work to do...
4696
		if ($context['allow_calendar'] && $context['allow_calendar_event'] && empty($modSettings['cal_allow_unlinked']) && !$user_info['is_admin'])
4697
		{
4698
			$boards_can_post = boardsAllowedTo('post_new');
4699
			$context['allow_calendar_event'] &= !empty($boards_can_post);
4700
		}
4701
	}
4702
4703
	// There is some menu stuff we need to do if we're coming at this from a non-guest perspective.
4704
	if (!$context['user']['is_guest'])
4705
	{
4706
		addInlineJavaScript('
4707
	var user_menus = new smc_PopupMenu();
4708
	user_menus.add("profile", "' . $scripturl . '?action=profile;area=popup");
4709
	user_menus.add("alerts", "' . $scripturl . '?action=profile;area=alerts_popup;u=' . $context['user']['id'] . '");', true);
4710
		if ($context['allow_pm'])
4711
			addInlineJavaScript('
4712
	user_menus.add("pm", "' . $scripturl . '?action=pm;sa=popup");', true);
4713
4714
		if (!empty($modSettings['enable_ajax_alerts']))
4715
		{
4716
			require_once($sourcedir . '/Subs-Notify.php');
4717
4718
			$timeout = getNotifyPrefs($context['user']['id'], 'alert_timeout', true);
4719
			$timeout = empty($timeout) ? 10000 : $timeout[$context['user']['id']]['alert_timeout'] * 1000;
4720
4721
			addInlineJavaScript('
4722
	var new_alert_title = "' . $context['forum_name'] . '";
4723
	var alert_timeout = ' . $timeout . ';');
4724
			loadJavaScriptFile('alerts.js', array('minimize' => true), 'smf_alerts');
4725
		}
4726
	}
4727
4728
	// All the buttons we can possible want and then some, try pulling the final list of buttons from cache first.
4729
	if (($menu_buttons = cache_get_data('menu_buttons-' . implode('_', $user_info['groups']) . '-' . $user_info['language'], $cacheTime)) === null || time() - $cacheTime <= $modSettings['settings_updated'])
4730
	{
4731
		$buttons = array(
4732
			'home' => array(
4733
				'title' => $txt['home'],
4734
				'href' => $scripturl,
4735
				'show' => true,
4736
				'sub_buttons' => array(
4737
				),
4738
				'is_last' => $context['right_to_left'],
4739
			),
4740
			'search' => array(
4741
				'title' => $txt['search'],
4742
				'href' => $scripturl . '?action=search',
4743
				'show' => $context['allow_search'],
4744
				'sub_buttons' => array(
4745
				),
4746
			),
4747
			'admin' => array(
4748
				'title' => $txt['admin'],
4749
				'href' => $scripturl . '?action=admin',
4750
				'show' => $context['allow_admin'],
4751
				'sub_buttons' => array(
4752
					'featuresettings' => array(
4753
						'title' => $txt['modSettings_title'],
4754
						'href' => $scripturl . '?action=admin;area=featuresettings',
4755
						'show' => allowedTo('admin_forum'),
4756
					),
4757
					'packages' => array(
4758
						'title' => $txt['package'],
4759
						'href' => $scripturl . '?action=admin;area=packages',
4760
						'show' => allowedTo('admin_forum'),
4761
					),
4762
					'errorlog' => array(
4763
						'title' => $txt['errorlog'],
4764
						'href' => $scripturl . '?action=admin;area=logs;sa=errorlog;desc',
4765
						'show' => allowedTo('admin_forum') && !empty($modSettings['enableErrorLogging']),
4766
					),
4767
					'permissions' => array(
4768
						'title' => $txt['edit_permissions'],
4769
						'href' => $scripturl . '?action=admin;area=permissions',
4770
						'show' => allowedTo('manage_permissions'),
4771
					),
4772
					'memberapprove' => array(
4773
						'title' => $txt['approve_members_waiting'],
4774
						'href' => $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve',
4775
						'show' => !empty($context['unapproved_members']),
4776
						'is_last' => true,
4777
					),
4778
				),
4779
			),
4780
			'moderate' => array(
4781
				'title' => $txt['moderate'],
4782
				'href' => $scripturl . '?action=moderate',
4783
				'show' => $context['allow_moderation_center'],
4784
				'sub_buttons' => array(
4785
					'modlog' => array(
4786
						'title' => $txt['modlog_view'],
4787
						'href' => $scripturl . '?action=moderate;area=modlog',
4788
						'show' => !empty($modSettings['modlog_enabled']) && !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1',
4789
					),
4790
					'poststopics' => array(
4791
						'title' => $txt['mc_unapproved_poststopics'],
4792
						'href' => $scripturl . '?action=moderate;area=postmod;sa=posts',
4793
						'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']),
4794
					),
4795
					'attachments' => array(
4796
						'title' => $txt['mc_unapproved_attachments'],
4797
						'href' => $scripturl . '?action=moderate;area=attachmod;sa=attachments',
4798
						'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']),
4799
					),
4800
					'reports' => array(
4801
						'title' => $txt['mc_reported_posts'],
4802
						'href' => $scripturl . '?action=moderate;area=reportedposts',
4803
						'show' => !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1',
4804
					),
4805
					'reported_members' => array(
4806
						'title' => $txt['mc_reported_members'],
4807
						'href' => $scripturl . '?action=moderate;area=reportedmembers',
4808
						'show' => allowedTo('moderate_forum'),
4809
						'is_last' => true,
4810
					)
4811
				),
4812
			),
4813
			'calendar' => array(
4814
				'title' => $txt['calendar'],
4815
				'href' => $scripturl . '?action=calendar',
4816
				'show' => $context['allow_calendar'],
4817
				'sub_buttons' => array(
4818
					'view' => array(
4819
						'title' => $txt['calendar_menu'],
4820
						'href' => $scripturl . '?action=calendar',
4821
						'show' => $context['allow_calendar_event'],
4822
					),
4823
					'post' => array(
4824
						'title' => $txt['calendar_post_event'],
4825
						'href' => $scripturl . '?action=calendar;sa=post',
4826
						'show' => $context['allow_calendar_event'],
4827
						'is_last' => true,
4828
					),
4829
				),
4830
			),
4831
			'mlist' => array(
4832
				'title' => $txt['members_title'],
4833
				'href' => $scripturl . '?action=mlist',
4834
				'show' => $context['allow_memberlist'],
4835
				'sub_buttons' => array(
4836
					'mlist_view' => array(
4837
						'title' => $txt['mlist_menu_view'],
4838
						'href' => $scripturl . '?action=mlist',
4839
						'show' => true,
4840
					),
4841
					'mlist_search' => array(
4842
						'title' => $txt['mlist_search'],
4843
						'href' => $scripturl . '?action=mlist;sa=search',
4844
						'show' => true,
4845
						'is_last' => true,
4846
					),
4847
				),
4848
				'is_last' => !$context['right_to_left'] && (!$user_info['is_guest'] || !$context['can_register']),
4849
			),
4850
			'signup' => array(
4851
				'title' => $txt['register'],
4852
				'href' => $scripturl . '?action=signup',
4853
				'show' => $user_info['is_guest'] && $context['can_register'],
4854
				'sub_buttons' => array(
4855
				),
4856
				'is_last' => !$context['right_to_left'],
4857
			),
4858
		);
4859
4860
		// Allow editing menu buttons easily.
4861
		call_integration_hook('integrate_menu_buttons', array(&$buttons));
4862
4863
		// Now we put the buttons in the context so the theme can use them.
4864
		$menu_buttons = array();
4865
		foreach ($buttons as $act => $button)
4866
			if (!empty($button['show']))
4867
			{
4868
				$button['active_button'] = false;
4869
4870
				// This button needs some action.
4871
				if (isset($button['action_hook']))
4872
					$needs_action_hook = true;
4873
4874
				// Make sure the last button truly is the last button.
4875
				if (!empty($button['is_last']))
4876
				{
4877
					if (isset($last_button))
4878
						unset($menu_buttons[$last_button]['is_last']);
4879
					$last_button = $act;
4880
				}
4881
4882
				// Go through the sub buttons if there are any.
4883
				if (!empty($button['sub_buttons']))
4884
					foreach ($button['sub_buttons'] as $key => $subbutton)
4885
					{
4886
						if (empty($subbutton['show']))
4887
							unset($button['sub_buttons'][$key]);
4888
4889
						// 2nd level sub buttons next...
4890
						if (!empty($subbutton['sub_buttons']))
4891
						{
4892
							foreach ($subbutton['sub_buttons'] as $key2 => $sub_button2)
4893
							{
4894
								if (empty($sub_button2['show']))
4895
									unset($button['sub_buttons'][$key]['sub_buttons'][$key2]);
4896
							}
4897
						}
4898
					}
4899
4900
				// Does this button have its own icon?
4901
				if (isset($button['icon']) && file_exists($settings['theme_dir'] . '/images/' . $button['icon']))
4902
					$button['icon'] = '<img src="' . $settings['images_url'] . '/' . $button['icon'] . '" alt="">';
4903
				elseif (isset($button['icon']) && file_exists($settings['default_theme_dir'] . '/images/' . $button['icon']))
4904
					$button['icon'] = '<img src="' . $settings['default_images_url'] . '/' . $button['icon'] . '" alt="">';
4905
				elseif (isset($button['icon']))
4906
					$button['icon'] = '<span class="main_icons ' . $button['icon'] . '"></span>';
4907
				else
4908
					$button['icon'] = '<span class="main_icons ' . $act . '"></span>';
4909
4910
				$menu_buttons[$act] = $button;
4911
			}
4912
4913
		if (!empty($cache_enable) && $cache_enable >= 2)
4914
			cache_put_data('menu_buttons-' . implode('_', $user_info['groups']) . '-' . $user_info['language'], $menu_buttons, $cacheTime);
4915
	}
4916
4917
	$context['menu_buttons'] = $menu_buttons;
4918
4919
	// Logging out requires the session id in the url.
4920
	if (isset($context['menu_buttons']['logout']))
4921
		$context['menu_buttons']['logout']['href'] = sprintf($context['menu_buttons']['logout']['href'], $context['session_var'], $context['session_id']);
4922
4923
	// Figure out which action we are doing so we can set the active tab.
4924
	// Default to home.
4925
	$current_action = 'home';
4926
4927
	if (isset($context['menu_buttons'][$context['current_action']]))
4928
		$current_action = $context['current_action'];
4929
	elseif ($context['current_action'] == 'search2')
4930
		$current_action = 'search';
4931
	elseif ($context['current_action'] == 'theme')
4932
		$current_action = isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'pick' ? 'profile' : 'admin';
4933
	elseif ($context['current_action'] == 'register2')
4934
		$current_action = 'register';
4935
	elseif ($context['current_action'] == 'login2' || ($user_info['is_guest'] && $context['current_action'] == 'reminder'))
4936
		$current_action = 'login';
4937
	elseif ($context['current_action'] == 'groups' && $context['allow_moderation_center'])
4938
		$current_action = 'moderate';
4939
4940
	// There are certain exceptions to the above where we don't want anything on the menu highlighted.
4941
	if ($context['current_action'] == 'profile' && !empty($context['user']['is_owner']))
4942
	{
4943
		$current_action = !empty($_GET['area']) && $_GET['area'] == 'showalerts' ? 'self_alerts' : 'self_profile';
4944
		$context[$current_action] = true;
4945
	}
4946
	elseif ($context['current_action'] == 'pm')
4947
	{
4948
		$current_action = 'self_pm';
4949
		$context['self_pm'] = true;
4950
	}
4951
4952
	$context['total_mod_reports'] = 0;
4953
	$context['total_admin_reports'] = 0;
4954
4955
	if (!empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1' && !empty($context['open_mod_reports']) && !empty($context['menu_buttons']['moderate']['sub_buttons']['reports']))
4956
	{
4957
		$context['total_mod_reports'] = $context['open_mod_reports'];
4958
		$context['menu_buttons']['moderate']['sub_buttons']['reports']['amt'] = $context['open_mod_reports'];
4959
	}
4960
4961
	// Show how many errors there are
4962
	if (!empty($context['menu_buttons']['admin']['sub_buttons']['errorlog']))
4963
	{
4964
		// Get an error count, if necessary
4965
		if (!isset($context['num_errors']))
4966
		{
4967
			$query = $smcFunc['db_query']('', '
4968
				SELECT COUNT(*)
4969
				FROM {db_prefix}log_errors',
4970
				array()
4971
			);
4972
4973
			list($context['num_errors']) = $smcFunc['db_fetch_row']($query);
4974
			$smcFunc['db_free_result']($query);
4975
		}
4976
4977
		if (!empty($context['num_errors']))
4978
		{
4979
			$context['total_admin_reports'] += $context['num_errors'];
4980
			$context['menu_buttons']['admin']['sub_buttons']['errorlog']['amt'] = $context['num_errors'];
4981
		}
4982
	}
4983
4984
	// Show number of reported members
4985
	if (!empty($context['open_member_reports']) && !empty($context['menu_buttons']['moderate']['sub_buttons']['reported_members']))
4986
	{
4987
		$context['total_mod_reports'] += $context['open_member_reports'];
4988
		$context['menu_buttons']['moderate']['sub_buttons']['reported_members']['amt'] = $context['open_member_reports'];
4989
	}
4990
4991
	if (!empty($context['unapproved_members']) && !empty($context['menu_buttons']['admin']))
4992
	{
4993
		$context['menu_buttons']['admin']['sub_buttons']['memberapprove']['amt'] = $context['unapproved_members'];
4994
		$context['total_admin_reports'] += $context['unapproved_members'];
4995
	}
4996
4997
	if ($context['total_admin_reports'] > 0 && !empty($context['menu_buttons']['admin']))
4998
	{
4999
		$context['menu_buttons']['admin']['amt'] = $context['total_admin_reports'];
5000
	}
5001
5002
	// Do we have any open reports?
5003
	if ($context['total_mod_reports'] > 0 && !empty($context['menu_buttons']['moderate']))
5004
	{
5005
		$context['menu_buttons']['moderate']['amt'] = $context['total_mod_reports'];
5006
	}
5007
5008
	// Not all actions are simple.
5009
	if (!empty($needs_action_hook))
5010
		call_integration_hook('integrate_current_action', array(&$current_action));
5011
5012
	if (isset($context['menu_buttons'][$current_action]))
5013
		$context['menu_buttons'][$current_action]['active_button'] = true;
5014
}
5015
5016
/**
5017
 * Generate a random seed and ensure it's stored in settings.
5018
 */
5019
function smf_seed_generator()
5020
{
5021
	updateSettings(array('rand_seed' => microtime(true)));
5022
}
5023
5024
/**
5025
 * Process functions of an integration hook.
5026
 * calls all functions of the given hook.
5027
 * supports static class method calls.
5028
 *
5029
 * @param string $hook The hook name
5030
 * @param array $parameters An array of parameters this hook implements
5031
 * @return array The results of the functions
5032
 */
5033
function call_integration_hook($hook, $parameters = array())
5034
{
5035
	global $modSettings, $settings, $boarddir, $sourcedir, $db_show_debug;
5036
	global $context, $txt;
5037
5038
	if ($db_show_debug === true)
5039
		$context['debug']['hooks'][] = $hook;
5040
5041
	// Need to have some control.
5042
	if (!isset($context['instances']))
5043
		$context['instances'] = array();
5044
5045
	$results = array();
5046
	if (empty($modSettings[$hook]))
5047
		return $results;
5048
5049
	$functions = explode(',', $modSettings[$hook]);
5050
	// Loop through each function.
5051
	foreach ($functions as $function)
5052
	{
5053
		// Hook has been marked as "disabled". Skip it!
5054
		if (strpos($function, '!') !== false)
5055
			continue;
5056
5057
		$call = call_helper($function, true);
5058
5059
		// Is it valid?
5060
		if (!empty($call))
5061
			$results[$function] = call_user_func_array($call, $parameters);
5062
		// This failed, but we want to do so silently.
5063
		elseif (!empty($function) && !empty($context['ignore_hook_errors']))
5064
			return $results;
5065
		// Whatever it was suppose to call, it failed :(
5066
		elseif (!empty($function))
5067
		{
5068
			loadLanguage('Errors');
5069
5070
			// Get a full path to show on error.
5071
			if (strpos($function, '|') !== false)
5072
			{
5073
				list ($file, $string) = explode('|', $function);
5074
				$absPath = empty($settings['theme_dir']) ? (strtr(trim($file), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir))) : (strtr(trim($file), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir'])));
5075
				log_error(sprintf($txt['hook_fail_call_to'], $string, $absPath), 'general');
5076
			}
5077
			// "Assume" the file resides on $boarddir somewhere...
5078
			else
5079
				log_error(sprintf($txt['hook_fail_call_to'], $function, $boarddir), 'general');
5080
		}
5081
	}
5082
5083
	return $results;
5084
}
5085
5086
/**
5087
 * Add a function for integration hook.
5088
 * does nothing if the function is already added.
5089
 *
5090
 * @param string $hook The complete hook name.
5091
 * @param string $function The function name. Can be a call to a method via Class::method.
5092
 * @param bool $permanent If true, updates the value in settings table.
5093
 * @param string $file The file. Must include one of the following wildcards: $boarddir, $sourcedir, $themedir, example: $sourcedir/Test.php
5094
 * @param bool $object Indicates if your class will be instantiated when its respective hook is called. If true, your function must be a method.
5095
 */
5096
function add_integration_function($hook, $function, $permanent = true, $file = '', $object = false)
5097
{
5098
	global $smcFunc, $modSettings;
5099
5100
	// Any objects?
5101
	if ($object)
5102
		$function = $function . '#';
5103
5104
	// Any files  to load?
5105
	if (!empty($file) && is_string($file))
5106
		$function = $file . (!empty($function) ? '|' . $function : '');
5107
5108
	// Get the correct string.
5109
	$integration_call = $function;
5110
5111
	// Is it going to be permanent?
5112
	if ($permanent)
5113
	{
5114
		$request = $smcFunc['db_query']('', '
5115
			SELECT value
5116
			FROM {db_prefix}settings
5117
			WHERE variable = {string:variable}',
5118
			array(
5119
				'variable' => $hook,
5120
			)
5121
		);
5122
		list ($current_functions) = $smcFunc['db_fetch_row']($request);
5123
		$smcFunc['db_free_result']($request);
5124
5125
		if (!empty($current_functions))
5126
		{
5127
			$current_functions = explode(',', $current_functions);
5128
			if (in_array($integration_call, $current_functions))
5129
				return;
5130
5131
			$permanent_functions = array_merge($current_functions, array($integration_call));
5132
		}
5133
		else
5134
			$permanent_functions = array($integration_call);
5135
5136
		updateSettings(array($hook => implode(',', $permanent_functions)));
5137
	}
5138
5139
	// Make current function list usable.
5140
	$functions = empty($modSettings[$hook]) ? array() : explode(',', $modSettings[$hook]);
5141
5142
	// Do nothing, if it's already there.
5143
	if (in_array($integration_call, $functions))
5144
		return;
5145
5146
	$functions[] = $integration_call;
5147
	$modSettings[$hook] = implode(',', $functions);
5148
}
5149
5150
/**
5151
 * Remove an integration hook function.
5152
 * Removes the given function from the given hook.
5153
 * Does nothing if the function is not available.
5154
 *
5155
 * @param string $hook The complete hook name.
5156
 * @param string $function The function name. Can be a call to a method via Class::method.
5157
 * @param boolean $permanent Irrelevant for the function itself but need to declare it to match
5158
 * @param string $file The filename. Must include one of the following wildcards: $boarddir, $sourcedir, $themedir, example: $sourcedir/Test.php
5159
 * @param boolean $object Indicates if your class will be instantiated when its respective hook is called. If true, your function must be a method.
5160
 * @see add_integration_function
5161
 */
5162
function remove_integration_function($hook, $function, $permanent = true, $file = '', $object = false)
5163
{
5164
	global $smcFunc, $modSettings;
5165
5166
	// Any objects?
5167
	if ($object)
5168
		$function = $function . '#';
5169
5170
	// Any files  to load?
5171
	if (!empty($file) && is_string($file))
5172
		$function = $file . '|' . $function;
5173
5174
	// Get the correct string.
5175
	$integration_call = $function;
5176
5177
	// Get the permanent functions.
5178
	$request = $smcFunc['db_query']('', '
5179
		SELECT value
5180
		FROM {db_prefix}settings
5181
		WHERE variable = {string:variable}',
5182
		array(
5183
			'variable' => $hook,
5184
		)
5185
	);
5186
	list ($current_functions) = $smcFunc['db_fetch_row']($request);
5187
	$smcFunc['db_free_result']($request);
5188
5189
	if (!empty($current_functions))
5190
	{
5191
		$current_functions = explode(',', $current_functions);
5192
5193
		if (in_array($integration_call, $current_functions))
5194
			updateSettings(array($hook => implode(',', array_diff($current_functions, array($integration_call)))));
5195
	}
5196
5197
	// Turn the function list into something usable.
5198
	$functions = empty($modSettings[$hook]) ? array() : explode(',', $modSettings[$hook]);
5199
5200
	// You can only remove it if it's available.
5201
	if (!in_array($integration_call, $functions))
5202
		return;
5203
5204
	$functions = array_diff($functions, array($integration_call));
5205
	$modSettings[$hook] = implode(',', $functions);
5206
}
5207
5208
/**
5209
 * Receives a string and tries to figure it out if its a method or a function.
5210
 * If a method is found, it looks for a "#" which indicates SMF should create a new instance of the given class.
5211
 * Checks the string/array for is_callable() and return false/fatal_lang_error is the given value results in a non callable string/array.
5212
 * Prepare and returns a callable depending on the type of method/function found.
5213
 *
5214
 * @param mixed $string The string containing a function name or a static call. The function can also accept a closure, object or a callable array (object/class, valid_callable)
5215
 * @param boolean $return If true, the function will not call the function/method but instead will return the formatted string.
5216
 * @return string|array|boolean Either a string or an array that contains a callable function name or an array with a class and method to call. Boolean false if the given string cannot produce a callable var.
5217
 */
5218
function call_helper($string, $return = false)
5219
{
5220
	global $context, $smcFunc, $txt, $db_show_debug;
5221
5222
	// Really?
5223
	if (empty($string))
5224
		return false;
5225
5226
	// An array? should be a "callable" array IE array(object/class, valid_callable).
5227
	// A closure? should be a callable one.
5228
	if (is_array($string) || $string instanceof Closure)
5229
		return $return ? $string : (is_callable($string) ? call_user_func($string) : false);
5230
5231
	// No full objects, sorry! pass a method or a property instead!
5232
	if (is_object($string))
5233
		return false;
5234
5235
	// Stay vitaminized my friends...
5236
	$string = $smcFunc['htmlspecialchars']($smcFunc['htmltrim']($string));
5237
5238
	// Is there a file to load?
5239
	$string = load_file($string);
5240
5241
	// Loaded file failed
5242
	if (empty($string))
5243
		return false;
5244
5245
	// Found a method.
5246
	if (strpos($string, '::') !== false)
5247
	{
5248
		list ($class, $method) = explode('::', $string);
5249
5250
		// Check if a new object will be created.
5251
		if (strpos($method, '#') !== false)
5252
		{
5253
			// Need to remove the # thing.
5254
			$method = str_replace('#', '', $method);
5255
5256
			// Don't need to create a new instance for every method.
5257
			if (empty($context['instances'][$class]) || !($context['instances'][$class] instanceof $class))
5258
			{
5259
				$context['instances'][$class] = new $class;
5260
5261
				// Add another one to the list.
5262
				if ($db_show_debug === true)
5263
				{
5264
					if (!isset($context['debug']['instances']))
5265
						$context['debug']['instances'] = array();
5266
5267
					$context['debug']['instances'][$class] = $class;
5268
				}
5269
			}
5270
5271
			$func = array($context['instances'][$class], $method);
5272
		}
5273
5274
		// Right then. This is a call to a static method.
5275
		else
5276
			$func = array($class, $method);
5277
	}
5278
5279
	// Nope! just a plain regular function.
5280
	else
5281
		$func = $string;
5282
5283
	// We can't call this helper, but we want to silently ignore this.
5284
	if (!is_callable($func, false, $callable_name) && !empty($context['ignore_hook_errors']))
5285
		return false;
5286
	// Right, we got what we need, time to do some checks.
5287
	elseif (!is_callable($func, false, $callable_name))
5288
	{
5289
		loadLanguage('Errors');
5290
		log_error(sprintf($txt['sub_action_fail'], $callable_name), 'general');
5291
5292
		// Gotta tell everybody.
5293
		return false;
5294
	}
5295
5296
	// Everything went better than expected.
5297
	else
5298
	{
5299
		// What are we gonna do about it?
5300
		if ($return)
5301
			return $func;
5302
5303
		// If this is a plain function, avoid the heat of calling call_user_func().
5304
		else
5305
		{
5306
			if (is_array($func))
5307
				call_user_func($func);
5308
5309
			else
5310
				$func();
5311
		}
5312
	}
5313
}
5314
5315
/**
5316
 * Receives a string and tries to figure it out if it contains info to load a file.
5317
 * Checks for a | (pipe) symbol and tries to load a file with the info given.
5318
 * The string should be format as follows File.php|. You can use the following wildcards: $boarddir, $sourcedir and if available at the moment of execution, $themedir.
5319
 *
5320
 * @param string $string The string containing a valid format.
5321
 * @return string|boolean The given string with the pipe and file info removed. Boolean false if the file couldn't be loaded.
5322
 */
5323
function load_file($string)
5324
{
5325
	global $sourcedir, $txt, $boarddir, $settings, $context;
5326
5327
	if (empty($string))
5328
		return false;
5329
5330
	if (strpos($string, '|') !== false)
5331
	{
5332
		list ($file, $string) = explode('|', $string);
5333
5334
		// Match the wildcards to their regular vars.
5335
		if (empty($settings['theme_dir']))
5336
			$absPath = strtr(trim($file), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir));
5337
5338
		else
5339
			$absPath = strtr(trim($file), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir']));
5340
5341
		// Load the file if it can be loaded.
5342
		if (file_exists($absPath))
5343
			require_once($absPath);
5344
5345
		// No? try a fallback to $sourcedir
5346
		else
5347
		{
5348
			$absPath = $sourcedir . '/' . $file;
5349
5350
			if (file_exists($absPath))
5351
				require_once($absPath);
5352
5353
			// Sorry, can't do much for you at this point.
5354
			elseif (empty($context['uninstalling']))
5355
			{
5356
				loadLanguage('Errors');
5357
				log_error(sprintf($txt['hook_fail_loading_file'], $absPath), 'general');
5358
5359
				// File couldn't be loaded.
5360
				return false;
5361
			}
5362
		}
5363
	}
5364
5365
	return $string;
5366
}
5367
5368
/**
5369
 * Get the contents of a URL, irrespective of allow_url_fopen.
5370
 *
5371
 * - reads the contents of an http or ftp address and returns the page in a string
5372
 * - will accept up to 3 page redirections (redirectio_level in the function call is private)
5373
 * - if post_data is supplied, the value and length is posted to the given url as form data
5374
 * - URL must be supplied in lowercase
5375
 *
5376
 * @param string $url The URL
5377
 * @param string $post_data The data to post to the given URL
5378
 * @param bool $keep_alive Whether to send keepalive info
5379
 * @param int $redirection_level How many levels of redirection
5380
 * @return string|false The fetched data or false on failure
5381
 */
5382
function fetch_web_data($url, $post_data = '', $keep_alive = false, $redirection_level = 0)
5383
{
5384
	global $webmaster_email, $sourcedir;
5385
	static $keep_alive_dom = null, $keep_alive_fp = null;
5386
5387
	preg_match('~^(http|ftp)(s)?://([^/:]+)(:(\d+))?(.+)$~', $url, $match);
5388
5389
	// No scheme? No data for you!
5390
	if (empty($match[1]))
5391
		return false;
5392
5393
	// An FTP url. We should try connecting and RETRieving it...
5394
	elseif ($match[1] == 'ftp')
5395
	{
5396
		// Include the file containing the ftp_connection class.
5397
		require_once($sourcedir . '/Class-Package.php');
5398
5399
		// Establish a connection and attempt to enable passive mode.
5400
		$ftp = new ftp_connection(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? 21 : $match[5], 'anonymous', $webmaster_email);
5401
		if ($ftp->error !== false || !$ftp->passive())
5402
			return false;
5403
5404
		// I want that one *points*!
5405
		fwrite($ftp->connection, 'RETR ' . $match[6] . "\r\n");
5406
5407
		// Since passive mode worked (or we would have returned already!) open the connection.
5408
		$fp = @fsockopen($ftp->pasv['ip'], $ftp->pasv['port'], $err, $err, 5);
5409
		if (!$fp)
5410
			return false;
5411
5412
		// The server should now say something in acknowledgement.
5413
		$ftp->check_response(150);
5414
5415
		$data = '';
5416
		while (!feof($fp))
5417
			$data .= fread($fp, 4096);
5418
		fclose($fp);
5419
5420
		// All done, right?  Good.
5421
		$ftp->check_response(226);
5422
		$ftp->close();
5423
	}
5424
5425
	// This is more likely; a standard HTTP URL.
5426
	elseif (isset($match[1]) && $match[1] == 'http')
5427
	{
5428
		// First try to use fsockopen, because it is fastest.
5429
		if ($keep_alive && $match[3] == $keep_alive_dom)
5430
			$fp = $keep_alive_fp;
5431
		if (empty($fp))
5432
		{
5433
			// Open the socket on the port we want...
5434
			$fp = @fsockopen(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? ($match[2] ? 443 : 80) : $match[5], $err, $err, 5);
5435
		}
5436
		if (!empty($fp))
5437
		{
5438
			if ($keep_alive)
5439
			{
5440
				$keep_alive_dom = $match[3];
5441
				$keep_alive_fp = $fp;
5442
			}
5443
5444
			// I want this, from there, and I'm not going to be bothering you for more (probably.)
5445
			if (empty($post_data))
5446
			{
5447
				fwrite($fp, 'GET ' . ($match[6] !== '/' ? str_replace(' ', '%20', $match[6]) : '') . ' HTTP/1.0' . "\r\n");
5448
				fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
5449
				fwrite($fp, 'user-agent: '. SMF_USER_AGENT . "\r\n");
5450
				if ($keep_alive)
5451
					fwrite($fp, 'connection: Keep-Alive' . "\r\n\r\n");
5452
				else
5453
					fwrite($fp, 'connection: close' . "\r\n\r\n");
5454
			}
5455
			else
5456
			{
5457
				fwrite($fp, 'POST ' . ($match[6] !== '/' ? $match[6] : '') . ' HTTP/1.0' . "\r\n");
5458
				fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
5459
				fwrite($fp, 'user-agent: '. SMF_USER_AGENT . "\r\n");
5460
				if ($keep_alive)
5461
					fwrite($fp, 'connection: Keep-Alive' . "\r\n");
5462
				else
5463
					fwrite($fp, 'connection: close' . "\r\n");
5464
				fwrite($fp, 'content-type: application/x-www-form-urlencoded' . "\r\n");
5465
				fwrite($fp, 'content-length: ' . strlen($post_data) . "\r\n\r\n");
5466
				fwrite($fp, $post_data);
5467
			}
5468
5469
			$response = fgets($fp, 768);
5470
5471
			// Redirect in case this location is permanently or temporarily moved.
5472
			if ($redirection_level < 3 && preg_match('~^HTTP/\S+\s+30[127]~i', $response) === 1)
5473
			{
5474
				$header = '';
5475
				$location = '';
5476
				while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
5477
					if (stripos($header, 'location:') !== false)
5478
						$location = trim(substr($header, strpos($header, ':') + 1));
5479
5480
				if (empty($location))
5481
					return false;
5482
				else
5483
				{
5484
					if (!$keep_alive)
5485
						fclose($fp);
5486
					return fetch_web_data($location, $post_data, $keep_alive, $redirection_level + 1);
5487
				}
5488
			}
5489
5490
			// Make sure we get a 200 OK.
5491
			elseif (preg_match('~^HTTP/\S+\s+20[01]~i', $response) === 0)
5492
				return false;
5493
5494
			// Skip the headers...
5495
			while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
5496
			{
5497
				if (preg_match('~content-length:\s*(\d+)~i', $header, $match) != 0)
5498
					$content_length = $match[1];
5499
				elseif (preg_match('~connection:\s*close~i', $header) != 0)
5500
				{
5501
					$keep_alive_dom = null;
5502
					$keep_alive = false;
5503
				}
5504
5505
				continue;
5506
			}
5507
5508
			$data = '';
5509
			if (isset($content_length))
5510
			{
5511
				while (!feof($fp) && strlen($data) < $content_length)
5512
					$data .= fread($fp, $content_length - strlen($data));
5513
			}
5514
			else
5515
			{
5516
				while (!feof($fp))
5517
					$data .= fread($fp, 4096);
5518
			}
5519
5520
			if (!$keep_alive)
5521
				fclose($fp);
5522
		}
5523
5524
		// If using fsockopen didn't work, try to use cURL if available.
5525
		elseif (function_exists('curl_init'))
5526
		{
5527
			// Include the file containing the curl_fetch_web_data class.
5528
			require_once($sourcedir . '/Class-CurlFetchWeb.php');
5529
5530
			$fetch_data = new curl_fetch_web_data();
5531
			$fetch_data->get_url_data($url, $post_data);
5532
5533
			// no errors and a 200 result, then we have a good dataset, well we at least have data. ;)
5534
			if ($fetch_data->result('code') == 200 && !$fetch_data->result('error'))
5535
				$data = $fetch_data->result('body');
5536
			else
5537
				return false;
5538
		}
5539
5540
		// Neither fsockopen nor curl are available. Well, phooey.
5541
		else
5542
			return false;
5543
	}
5544
	else
5545
	{
5546
		// Umm, this shouldn't happen?
5547
		trigger_error('fetch_web_data(): Bad URL', E_USER_NOTICE);
5548
		$data = false;
5549
	}
5550
5551
	return $data;
5552
}
5553
5554
/**
5555
 * Attempts to determine the MIME type of some data or a file.
5556
 *
5557
 * @param string $data The data to check, or the path or URL of a file to check.
5558
 * @param string $is_path If true, $data is a path or URL to a file.
5559
 * @return string|bool A MIME type, or false if we cannot determine it.
5560
 */
5561
function get_mime_type($data, $is_path = false)
5562
{
5563
	global $cachedir;
5564
5565
	$finfo_loaded = extension_loaded('fileinfo');
5566
	$exif_loaded = extension_loaded('exif') && function_exists('image_type_to_mime_type');
5567
5568
	// Oh well. We tried.
5569
	if (!$finfo_loaded && !$exif_loaded)
5570
		return false;
5571
5572
	// Start with the 'empty' MIME type.
5573
	$mime_type = 'application/x-empty';
5574
5575
	if ($finfo_loaded)
5576
	{
5577
		// Just some nice, simple data to analyze.
5578
		if (empty($is_path))
5579
			$mime_type = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
5580
5581
		// A file, or maybe a URL?
5582
		else
5583
		{
5584
			// Local file.
5585
			if (file_exists($data))
5586
				$mime_type = mime_content_type($data);
5587
5588
			// URL.
5589
			elseif ($data = fetch_web_data($data))
5590
				$mime_type = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
5591
		}
5592
	}
5593
	// Workaround using Exif requires a local file.
5594
	else
5595
	{
5596
		// If $data is a URL to fetch, do so.
5597
		if (!empty($is_path) && !file_exists($data) && url_exists($data))
5598
		{
5599
			$data = fetch_web_data($data);
5600
			$is_path = false;
5601
		}
5602
5603
		// If we don't have a local file, create one and use it.
5604
		if (empty($is_path))
5605
		{
5606
			$temp_file = tempnam($cachedir, md5($data));
5607
			file_put_contents($temp_file, $data);
5608
			$is_path = true;
5609
			$data = $temp_file;
5610
		}
5611
5612
		$imagetype = @exif_imagetype($data);
5613
5614
		if (isset($temp_file))
5615
			unlink($temp_file);
5616
5617
		// Unfortunately, this workaround only works for image files.
5618
		if ($imagetype !== false)
5619
			$mime_type = image_type_to_mime_type($imagetype);
5620
	}
5621
5622
	return $mime_type;
5623
}
5624
5625
/**
5626
 * Checks whether a file or data has the expected MIME type.
5627
 *
5628
 * @param string $data The data to check, or the path or URL of a file to check.
5629
 * @param string $type_pattern A regex pattern to match the acceptable MIME types.
5630
 * @param string $is_path If true, $data is a path or URL to a file.
5631
 * @return int 1 if the detected MIME type matches the pattern, 0 if it doesn't, or 2 if we can't check.
5632
 */
5633
function check_mime_type($data, $type_pattern, $is_path = false)
5634
{
5635
	// Get the MIME type.
5636
	$mime_type = get_mime_type($data, $is_path);
5637
5638
	// Couldn't determine it.
5639
	if ($mime_type === false)
5640
		return 2;
5641
5642
	// Check whether the MIME type matches expectations.
5643
	return (int) @preg_match('~' . $type_pattern . '~', $mime_type);
5644
}
5645
5646
/**
5647
 * Prepares an array of "likes" info for the topic specified by $topic
5648
 *
5649
 * @param integer $topic The topic ID to fetch the info from.
5650
 * @return array An array of IDs of messages in the specified topic that the current user likes
5651
 */
5652
function prepareLikesContext($topic)
5653
{
5654
	global $user_info, $smcFunc;
5655
5656
	// Make sure we have something to work with.
5657
	if (empty($topic))
5658
		return array();
5659
5660
	// We already know the number of likes per message, we just want to know whether the current user liked it or not.
5661
	$user = $user_info['id'];
5662
	$cache_key = 'likes_topic_' . $topic . '_' . $user;
5663
	$ttl = 180;
5664
5665
	if (($temp = cache_get_data($cache_key, $ttl)) === null)
5666
	{
5667
		$temp = array();
5668
		$request = $smcFunc['db_query']('', '
5669
			SELECT content_id
5670
			FROM {db_prefix}user_likes AS l
5671
				INNER JOIN {db_prefix}messages AS m ON (l.content_id = m.id_msg)
5672
			WHERE l.id_member = {int:current_user}
5673
				AND l.content_type = {literal:msg}
5674
				AND m.id_topic = {int:topic}',
5675
			array(
5676
				'current_user' => $user,
5677
				'topic' => $topic,
5678
			)
5679
		);
5680
		while ($row = $smcFunc['db_fetch_assoc']($request))
5681
			$temp[] = (int) $row['content_id'];
5682
5683
		cache_put_data($cache_key, $temp, $ttl);
5684
	}
5685
5686
	return $temp;
5687
}
5688
5689
/**
5690
 * Microsoft uses their own character set Code Page 1252 (CP1252), which is a
5691
 * superset of ISO 8859-1, defining several characters between DEC 128 and 159
5692
 * that are not normally displayable.  This converts the popular ones that
5693
 * appear from a cut and paste from windows.
5694
 *
5695
 * @param string $string The string
5696
 * @return string The sanitized string
5697
 */
5698
function sanitizeMSCutPaste($string)
5699
{
5700
	global $context;
5701
5702
	if (empty($string))
5703
		return $string;
5704
5705
	// UTF-8 occurences of MS special characters
5706
	$findchars_utf8 = array(
5707
		"\xe2\x80\x9a",	// single low-9 quotation mark
5708
		"\xe2\x80\x9e",	// double low-9 quotation mark
5709
		"\xe2\x80\xa6",	// horizontal ellipsis
5710
		"\xe2\x80\x98",	// left single curly quote
5711
		"\xe2\x80\x99",	// right single curly quote
5712
		"\xe2\x80\x9c",	// left double curly quote
5713
		"\xe2\x80\x9d",	// right double curly quote
5714
	);
5715
5716
	// windows 1252 / iso equivalents
5717
	$findchars_iso = array(
5718
		chr(130),
5719
		chr(132),
5720
		chr(133),
5721
		chr(145),
5722
		chr(146),
5723
		chr(147),
5724
		chr(148),
5725
	);
5726
5727
	// safe replacements
5728
	$replacechars = array(
5729
		',',	// &sbquo;
5730
		',,',	// &bdquo;
5731
		'...',	// &hellip;
5732
		"'",	// &lsquo;
5733
		"'",	// &rsquo;
5734
		'"',	// &ldquo;
5735
		'"',	// &rdquo;
5736
	);
5737
5738
	if ($context['utf8'])
5739
		$string = str_replace($findchars_utf8, $replacechars, $string);
5740
	else
5741
		$string = str_replace($findchars_iso, $replacechars, $string);
5742
5743
	return $string;
5744
}
5745
5746
/**
5747
 * Decode numeric html entities to their ascii or UTF8 equivalent character.
5748
 *
5749
 * Callback function for preg_replace_callback in subs-members
5750
 * Uses capture group 2 in the supplied array
5751
 * Does basic scan to ensure characters are inside a valid range
5752
 *
5753
 * @param array $matches An array of matches (relevant info should be the 3rd item)
5754
 * @return string A fixed string
5755
 */
5756
function replaceEntities__callback($matches)
5757
{
5758
	global $context;
5759
5760
	if (!isset($matches[2]))
5761
		return '';
5762
5763
	$num = $matches[2][0] === 'x' ? hexdec(substr($matches[2], 1)) : (int) $matches[2];
5764
5765
	// remove left to right / right to left overrides
5766
	if ($num === 0x202D || $num === 0x202E)
5767
		return '';
5768
5769
	// Quote, Ampersand, Apostrophe, Less/Greater Than get html replaced
5770
	if (in_array($num, array(0x22, 0x26, 0x27, 0x3C, 0x3E)))
5771
		return '&#' . $num . ';';
5772
5773
	if (empty($context['utf8']))
5774
	{
5775
		// no control characters
5776
		if ($num < 0x20)
5777
			return '';
5778
		// text is text
5779
		elseif ($num < 0x80)
5780
			return chr($num);
5781
		// all others get html-ised
5782
		else
5783
			return '&#' . $matches[2] . ';';
5784
	}
5785
	else
5786
	{
5787
		// <0x20 are control characters, 0x20 is a space, > 0x10FFFF is past the end of the utf8 character set
5788
		// 0xD800 >= $num <= 0xDFFF are surrogate markers (not valid for utf8 text)
5789
		if ($num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF))
5790
			return '';
5791
		// <0x80 (or less than 128) are standard ascii characters a-z A-Z 0-9 and punctuation
5792
		elseif ($num < 0x80)
5793
			return chr($num);
5794
		// <0x800 (2048)
5795
		elseif ($num < 0x800)
5796
			return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
5797
		// < 0x10000 (65536)
5798
		elseif ($num < 0x10000)
5799
			return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
5800
		// <= 0x10FFFF (1114111)
5801
		else
5802
			return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
5803
	}
5804
}
5805
5806
/**
5807
 * Converts html entities to utf8 equivalents
5808
 *
5809
 * Callback function for preg_replace_callback
5810
 * Uses capture group 1 in the supplied array
5811
 * Does basic checks to keep characters inside a viewable range.
5812
 *
5813
 * @param array $matches An array of matches (relevant info should be the 2nd item in the array)
5814
 * @return string The fixed string
5815
 */
5816
function fixchar__callback($matches)
5817
{
5818
	if (!isset($matches[1]))
5819
		return '';
5820
5821
	$num = $matches[1][0] === 'x' ? hexdec(substr($matches[1], 1)) : (int) $matches[1];
5822
5823
	// <0x20 are control characters, > 0x10FFFF is past the end of the utf8 character set
5824
	// 0xD800 >= $num <= 0xDFFF are surrogate markers (not valid for utf8 text), 0x202D-E are left to right overrides
5825
	if ($num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num === 0x202D || $num === 0x202E)
5826
		return '';
5827
	// <0x80 (or less than 128) are standard ascii characters a-z A-Z 0-9 and punctuation
5828
	elseif ($num < 0x80)
5829
		return chr($num);
5830
	// <0x800 (2048)
5831
	elseif ($num < 0x800)
5832
		return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
5833
	// < 0x10000 (65536)
5834
	elseif ($num < 0x10000)
5835
		return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
5836
	// <= 0x10FFFF (1114111)
5837
	else
5838
		return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
5839
}
5840
5841
/**
5842
 * Strips out invalid html entities, replaces others with html style &#123; codes
5843
 *
5844
 * Callback function used of preg_replace_callback in smcFunc $ent_checks, for example
5845
 * strpos, strlen, substr etc
5846
 *
5847
 * @param array $matches An array of matches (relevant info should be the 3rd item in the array)
5848
 * @return string The fixed string
5849
 */
5850
function entity_fix__callback($matches)
5851
{
5852
	if (!isset($matches[2]))
5853
		return '';
5854
5855
	$num = $matches[2][0] === 'x' ? hexdec(substr($matches[2], 1)) : (int) $matches[2];
5856
5857
	// we don't allow control characters, characters out of range, byte markers, etc
5858
	if ($num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num == 0x202D || $num == 0x202E)
5859
		return '';
5860
	else
5861
		return '&#' . $num . ';';
5862
}
5863
5864
/**
5865
 * Return a Gravatar URL based on
5866
 * - the supplied email address,
5867
 * - the global maximum rating,
5868
 * - the global default fallback,
5869
 * - maximum sizes as set in the admin panel.
5870
 *
5871
 * It is SSL aware, and caches most of the parameters.
5872
 *
5873
 * @param string $email_address The user's email address
5874
 * @return string The gravatar URL
5875
 */
5876
function get_gravatar_url($email_address)
5877
{
5878
	global $modSettings, $smcFunc;
5879
	static $url_params = null;
5880
5881
	if ($url_params === null)
5882
	{
5883
		$ratings = array('G', 'PG', 'R', 'X');
5884
		$defaults = array('mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank');
5885
		$url_params = array();
5886
		if (!empty($modSettings['gravatarMaxRating']) && in_array($modSettings['gravatarMaxRating'], $ratings))
5887
			$url_params[] = 'rating=' . $modSettings['gravatarMaxRating'];
5888
		if (!empty($modSettings['gravatarDefault']) && in_array($modSettings['gravatarDefault'], $defaults))
5889
			$url_params[] = 'default=' . $modSettings['gravatarDefault'];
5890
		if (!empty($modSettings['avatar_max_width_external']))
5891
			$size_string = (int) $modSettings['avatar_max_width_external'];
5892
		if (!empty($modSettings['avatar_max_height_external']) && !empty($size_string))
5893
			if ((int) $modSettings['avatar_max_height_external'] < $size_string)
5894
				$size_string = $modSettings['avatar_max_height_external'];
5895
5896
		if (!empty($size_string))
5897
			$url_params[] = 's=' . $size_string;
5898
	}
5899
	$http_method = !empty($modSettings['force_ssl']) ? 'https://secure' : 'http://www';
5900
5901
	return $http_method . '.gravatar.com/avatar/' . md5($smcFunc['strtolower']($email_address)) . '?' . implode('&', $url_params);
5902
}
5903
5904
/**
5905
 * Get a list of timezones.
5906
 *
5907
 * @param string $when An optional date or time for which to calculate the timezone offset values. May be a Unix timestamp or any string that strtotime() can understand. Defaults to 'now'.
5908
 * @return array An array of timezone info.
5909
 */
5910
function smf_list_timezones($when = 'now')
5911
{
5912
	global $smcFunc, $modSettings, $tztxt, $txt, $cur_profile;
5913
	static $timezones = null, $lastwhen = null;
5914
5915
	// No point doing this over if we already did it once
5916
	if (!empty($timezones) && $when == $lastwhen)
5917
		return $timezones;
5918
	else
5919
		$lastwhen = $when;
5920
5921
	// Parseable datetime string?
5922
	if (is_int($timestamp = strtotime($when)))
5923
		$when = $timestamp;
5924
5925
	// A Unix timestamp?
5926
	elseif (is_numeric($when))
5927
		$when = intval($when);
5928
5929
	// Invalid value? Just get current Unix timestamp.
5930
	else
5931
		$when = time();
5932
5933
	// We'll need these too
5934
	$date_when = date_create('@' . $when);
5935
	$later = strtotime('@' . $when . ' + 1 year');
5936
5937
	// Load up any custom time zone descriptions we might have
5938
	loadLanguage('Timezones');
5939
5940
	// Should we put time zones from certain countries at the top of the list?
5941
	$priority_countries = !empty($modSettings['timezone_priority_countries']) ? explode(',', $modSettings['timezone_priority_countries']) : array();
5942
	$priority_tzids = array();
5943
	foreach ($priority_countries as $country)
5944
	{
5945
		$country_tzids = @timezone_identifiers_list(DateTimeZone::PER_COUNTRY, strtoupper(trim($country)));
5946
		if (!empty($country_tzids))
5947
			$priority_tzids = array_merge($priority_tzids, $country_tzids);
5948
	}
5949
5950
	// Antarctic research stations should be listed last, unless you're running a penguin forum
5951
	$low_priority_tzids = !in_array('AQ', $priority_countries) ? timezone_identifiers_list(DateTimeZone::ANTARCTICA) : array();
5952
5953
	// Process the preferred timezones first, then the normal ones, then the low priority ones.
5954
	$tzids = array_merge(array_keys($tztxt), array_diff(timezone_identifiers_list(), array_keys($tztxt), $low_priority_tzids), $low_priority_tzids);
5955
5956
	// Idea here is to get exactly one representative identifier for each and every unique set of time zone rules.
5957
	foreach ($tzids as $tzid)
5958
	{
5959
		// We don't want UTC right now
5960
		if ($tzid == 'UTC')
5961
			continue;
5962
5963
		$tz = timezone_open($tzid);
5964
5965
		// First, get the set of transition rules for this tzid
5966
		$tzinfo = timezone_transitions_get($tz, $when, $later);
5967
5968
		// Use the entire set of transition rules as the array *key* so we can avoid duplicates
5969
		$tzkey = serialize($tzinfo);
5970
5971
		// Next, get the geographic info for this tzid
5972
		$tzgeo = timezone_location_get($tz);
5973
5974
		// Don't overwrite our preferred tzids
5975
		if (empty($zones[$tzkey]['tzid']))
5976
		{
5977
			$zones[$tzkey]['tzid'] = $tzid;
5978
			$zones[$tzkey]['abbr'] = $tzinfo[0]['abbr'];
5979
		}
5980
5981
		// A time zone from a prioritized country?
5982
		if (in_array($tzid, $priority_tzids))
5983
			$priority_zones[$tzkey] = true;
5984
5985
		// Keep track of the location and offset for this tzid
5986
		if (!empty($txt[$tzid]))
5987
			$zones[$tzkey]['locations'][] = $txt[$tzid];
5988
		else
5989
		{
5990
			$tzid_parts = explode('/', $tzid);
5991
			$zones[$tzkey]['locations'][] = str_replace(array('St_', '_'), array('St. ', ' '), array_pop($tzid_parts));
5992
		}
5993
		$offsets[$tzkey] = $tzinfo[0]['offset'];
5994
		$longitudes[$tzkey] = empty($longitudes[$tzkey]) ? $tzgeo['longitude'] : $longitudes[$tzkey];
5995
5996
		// Remember this for later
5997
		if (isset($cur_profile['timezone']) && $cur_profile['timezone'] == $tzid)
5998
			$member_tzkey = $tzkey;
5999
	}
6000
6001
	// Sort by offset then longitude
6002
	array_multisort($offsets, SORT_ASC, SORT_NUMERIC, $longitudes, SORT_ASC, SORT_NUMERIC, $zones);
6003
6004
	// Build the final array of formatted values
6005
	$priority_timezones = array();
6006
	$timezones = array();
6007
	foreach ($zones as $tzkey => $tzvalue)
6008
	{
6009
		date_timezone_set($date_when, timezone_open($tzvalue['tzid']));
6010
6011
		// Use the custom description, if there is one
6012
		if (!empty($tztxt[$tzvalue['tzid']]))
6013
			$desc = $tztxt[$tzvalue['tzid']];
6014
		// Otherwise, use the list of locations (max 5, so things don't get silly)
6015
		else
6016
			$desc = implode(', ', array_slice(array_unique($tzvalue['locations']), 0, 5)) . (count($tzvalue['locations']) > 5 ? ', ' . $txt['etc'] : '');
6017
6018
		// Show the UTC offset and the abbreviation, if it's something like 'MST' and not '-06'
6019
		$desc = '[UTC' . date_format($date_when, 'P') . '] - ' . (!strspn($tzvalue['abbr'], '+-') ? $tzvalue['abbr'] . ' - ' : '') . $desc;
6020
6021
		if (isset($priority_zones[$tzkey]))
6022
			$priority_timezones[$tzvalue['tzid']] = $desc;
6023
		else
6024
			$timezones[$tzvalue['tzid']] = $desc;
6025
6026
		// Automatically fix orphaned timezones on the member profile page
6027
		if (isset($member_tzkey) && $member_tzkey == $tzkey)
6028
			$cur_profile['timezone'] = $tzvalue['tzid'];
6029
	}
6030
6031
	if (!empty($priority_timezones))
6032
		$priority_timezones[] = '-----';
6033
6034
	$timezones = array_merge(
6035
		$priority_timezones,
6036
		array('UTC' => 'UTC' . (!empty($tztxt['UTC']) ? ' - ' . $tztxt['UTC'] : ''), '-----'),
6037
		$timezones
6038
	);
6039
6040
	return $timezones;
6041
}
6042
6043
/**
6044
 * @param string $ip_address An IP address in IPv4, IPv6 or decimal notation
6045
 * @return string|false The IP address in binary or false
6046
 */
6047
function inet_ptod($ip_address)
6048
{
6049
	if (!isValidIP($ip_address))
6050
		return $ip_address;
6051
6052
	$bin = inet_pton($ip_address);
6053
	return $bin;
6054
}
6055
6056
/**
6057
 * @param string $bin An IP address in IPv4, IPv6 (Either string (postgresql) or binary (other databases))
6058
 * @return string|false The IP address in presentation format or false on error
6059
 */
6060
function inet_dtop($bin)
6061
{
6062
	global $db_type;
6063
6064
	if (empty($bin))
6065
		return '';
6066
	elseif ($db_type == 'postgresql')
6067
		return $bin;
6068
	// Already a String?
6069
	elseif (isValidIP($bin))
6070
		return $bin;
6071
	return inet_ntop($bin);
6072
}
6073
6074
/**
6075
 * Safe serialize() and unserialize() replacements
6076
 *
6077
 * @license Public Domain
6078
 *
6079
 * @author anthon (dot) pang (at) gmail (dot) com
6080
 */
6081
6082
/**
6083
 * Safe serialize() replacement. Recursive
6084
 * - output a strict subset of PHP's native serialized representation
6085
 * - does not serialize objects
6086
 *
6087
 * @param mixed $value
6088
 * @return string
6089
 */
6090
function _safe_serialize($value)
6091
{
6092
	if (is_null($value))
6093
		return 'N;';
6094
6095
	if (is_bool($value))
6096
		return 'b:' . (int) $value . ';';
6097
6098
	if (is_int($value))
6099
		return 'i:' . $value . ';';
6100
6101
	if (is_float($value))
6102
		return 'd:' . str_replace(',', '.', $value) . ';';
6103
6104
	if (is_string($value))
6105
		return 's:' . strlen($value) . ':"' . $value . '";';
6106
6107
	if (is_array($value))
6108
	{
6109
		$out = '';
6110
		foreach ($value as $k => $v)
6111
			$out .= _safe_serialize($k) . _safe_serialize($v);
6112
6113
		return 'a:' . count($value) . ':{' . $out . '}';
6114
	}
6115
6116
	// safe_serialize cannot serialize resources or objects.
6117
	return false;
6118
}
6119
6120
/**
6121
 * Wrapper for _safe_serialize() that handles exceptions and multibyte encoding issues.
6122
 *
6123
 * @param mixed $value
6124
 * @return string
6125
 */
6126
function safe_serialize($value)
6127
{
6128
	// Make sure we use the byte count for strings even when strlen() is overloaded by mb_strlen()
6129
	if (function_exists('mb_internal_encoding') &&
6130
		(((int) ini_get('mbstring.func_overload')) & 2))
6131
	{
6132
		$mbIntEnc = mb_internal_encoding();
6133
		mb_internal_encoding('ASCII');
6134
	}
6135
6136
	$out = _safe_serialize($value);
6137
6138
	if (isset($mbIntEnc))
6139
		mb_internal_encoding($mbIntEnc);
6140
6141
	return $out;
6142
}
6143
6144
/**
6145
 * Safe unserialize() replacement
6146
 * - accepts a strict subset of PHP's native serialized representation
6147
 * - does not unserialize objects
6148
 *
6149
 * @param string $str
6150
 * @return mixed
6151
 * @throw Exception if $str is malformed or contains unsupported types (e.g., resources, objects)
6152
 */
6153
function _safe_unserialize($str)
6154
{
6155
	// Input  is not a string.
6156
	if (empty($str) || !is_string($str))
6157
		return false;
6158
6159
	$stack = array();
6160
	$expected = array();
6161
6162
	/*
6163
	 * states:
6164
	 *   0 - initial state, expecting a single value or array
6165
	 *   1 - terminal state
6166
	 *   2 - in array, expecting end of array or a key
6167
	 *   3 - in array, expecting value or another array
6168
	 */
6169
	$state = 0;
6170
	while ($state != 1)
6171
	{
6172
		$type = isset($str[0]) ? $str[0] : '';
6173
		if ($type == '}')
6174
			$str = substr($str, 1);
6175
6176
		elseif ($type == 'N' && $str[1] == ';')
6177
		{
6178
			$value = null;
6179
			$str = substr($str, 2);
6180
		}
6181
		elseif ($type == 'b' && preg_match('/^b:([01]);/', $str, $matches))
6182
		{
6183
			$value = $matches[1] == '1' ? true : false;
6184
			$str = substr($str, 4);
6185
		}
6186
		elseif ($type == 'i' && preg_match('/^i:(-?[0-9]+);(.*)/s', $str, $matches))
6187
		{
6188
			$value = (int) $matches[1];
6189
			$str = $matches[2];
6190
		}
6191
		elseif ($type == 'd' && preg_match('/^d:(-?[0-9]+\.?[0-9]*(E[+-][0-9]+)?);(.*)/s', $str, $matches))
6192
		{
6193
			$value = (float) $matches[1];
6194
			$str = $matches[3];
6195
		}
6196
		elseif ($type == 's' && preg_match('/^s:([0-9]+):"(.*)/s', $str, $matches) && substr($matches[2], (int) $matches[1], 2) == '";')
6197
		{
6198
			$value = substr($matches[2], 0, (int) $matches[1]);
6199
			$str = substr($matches[2], (int) $matches[1] + 2);
6200
		}
6201
		elseif ($type == 'a' && preg_match('/^a:([0-9]+):{(.*)/s', $str, $matches))
6202
		{
6203
			$expectedLength = (int) $matches[1];
6204
			$str = $matches[2];
6205
		}
6206
6207
		// Object or unknown/malformed type.
6208
		else
6209
			return false;
6210
6211
		switch ($state)
6212
		{
6213
			case 3: // In array, expecting value or another array.
6214
				if ($type == 'a')
6215
				{
6216
					$stack[] = &$list;
6217
					$list[$key] = array();
6218
					$list = &$list[$key];
6219
					$expected[] = $expectedLength;
6220
					$state = 2;
6221
					break;
6222
				}
6223
				if ($type != '}')
6224
				{
6225
					$list[$key] = $value;
6226
					$state = 2;
6227
					break;
6228
				}
6229
6230
				// Missing array value.
6231
				return false;
6232
6233
			case 2: // in array, expecting end of array or a key
6234
				if ($type == '}')
6235
				{
6236
					// Array size is less than expected.
6237
					if (count($list) < end($expected))
6238
						return false;
6239
6240
					unset($list);
6241
					$list = &$stack[count($stack) - 1];
6242
					array_pop($stack);
6243
6244
					// Go to terminal state if we're at the end of the root array.
6245
					array_pop($expected);
6246
6247
					if (count($expected) == 0)
6248
						$state = 1;
6249
6250
					break;
6251
				}
6252
6253
				if ($type == 'i' || $type == 's')
6254
				{
6255
					// Array size exceeds expected length.
6256
					if (count($list) >= end($expected))
6257
						return false;
6258
6259
					$key = $value;
6260
					$state = 3;
6261
					break;
6262
				}
6263
6264
				// Illegal array index type.
6265
				return false;
6266
6267
			// Expecting array or value.
6268
			case 0:
6269
				if ($type == 'a')
6270
				{
6271
					$data = array();
6272
					$list = &$data;
6273
					$expected[] = $expectedLength;
6274
					$state = 2;
6275
					break;
6276
				}
6277
6278
				if ($type != '}')
6279
				{
6280
					$data = $value;
6281
					$state = 1;
6282
					break;
6283
				}
6284
6285
				// Not in array.
6286
				return false;
6287
		}
6288
	}
6289
6290
	// Trailing data in input.
6291
	if (!empty($str))
6292
		return false;
6293
6294
	return $data;
6295
}
6296
6297
/**
6298
 * Wrapper for _safe_unserialize() that handles exceptions and multibyte encoding issue
6299
 *
6300
 * @param string $str
6301
 * @return mixed
6302
 */
6303
function safe_unserialize($str)
6304
{
6305
	// Make sure we use the byte count for strings even when strlen() is overloaded by mb_strlen()
6306
	if (function_exists('mb_internal_encoding') &&
6307
		(((int) ini_get('mbstring.func_overload')) & 0x02))
6308
	{
6309
		$mbIntEnc = mb_internal_encoding();
6310
		mb_internal_encoding('ASCII');
6311
	}
6312
6313
	$out = _safe_unserialize($str);
6314
6315
	if (isset($mbIntEnc))
6316
		mb_internal_encoding($mbIntEnc);
6317
6318
	return $out;
6319
}
6320
6321
/**
6322
 * Tries different modes to make file/dirs writable. Wrapper function for chmod()
6323
 *
6324
 * @param string $file The file/dir full path.
6325
 * @param int $value Not needed, added for legacy reasons.
6326
 * @return boolean  true if the file/dir is already writable or the function was able to make it writable, false if the function couldn't make the file/dir writable.
6327
 */
6328
function smf_chmod($file, $value = 0)
6329
{
6330
	// No file? no checks!
6331
	if (empty($file))
6332
		return false;
6333
6334
	// Already writable?
6335
	if (is_writable($file))
6336
		return true;
6337
6338
	// Do we have a file or a dir?
6339
	$isDir = is_dir($file);
6340
	$isWritable = false;
6341
6342
	// Set different modes.
6343
	$chmodValues = $isDir ? array(0750, 0755, 0775, 0777) : array(0644, 0664, 0666);
6344
6345
	foreach ($chmodValues as $val)
6346
	{
6347
		// If it's writable, break out of the loop.
6348
		if (is_writable($file))
6349
		{
6350
			$isWritable = true;
6351
			break;
6352
		}
6353
6354
		else
6355
			@chmod($file, $val);
6356
	}
6357
6358
	return $isWritable;
6359
}
6360
6361
/**
6362
 * Wrapper function for json_decode() with error handling.
6363
 *
6364
 * @param string $json The string to decode.
6365
 * @param bool $returnAsArray To return the decoded string as an array or an object, SMF only uses Arrays but to keep on compatibility with json_decode its set to false as default.
6366
 * @param bool $logIt To specify if the error will be logged if theres any.
6367
 * @return array Either an empty array or the decoded data as an array.
6368
 */
6369
function smf_json_decode($json, $returnAsArray = false, $logIt = true)
6370
{
6371
	global $txt;
6372
6373
	// Come on...
6374
	if (empty($json) || !is_string($json))
6375
		return array();
6376
6377
	$returnArray = @json_decode($json, $returnAsArray);
6378
6379
	// PHP 5.3 so no json_last_error_msg()
6380
	switch (json_last_error())
6381
	{
6382
		case JSON_ERROR_NONE:
6383
			$jsonError = false;
6384
			break;
6385
		case JSON_ERROR_DEPTH:
6386
			$jsonError = 'JSON_ERROR_DEPTH';
6387
			break;
6388
		case JSON_ERROR_STATE_MISMATCH:
6389
			$jsonError = 'JSON_ERROR_STATE_MISMATCH';
6390
			break;
6391
		case JSON_ERROR_CTRL_CHAR:
6392
			$jsonError = 'JSON_ERROR_CTRL_CHAR';
6393
			break;
6394
		case JSON_ERROR_SYNTAX:
6395
			$jsonError = 'JSON_ERROR_SYNTAX';
6396
			break;
6397
		case JSON_ERROR_UTF8:
6398
			$jsonError = 'JSON_ERROR_UTF8';
6399
			break;
6400
		default:
6401
			$jsonError = 'unknown';
6402
			break;
6403
	}
6404
6405
	// Something went wrong!
6406
	if (!empty($jsonError) && $logIt)
6407
	{
6408
		// Being a wrapper means we lost our smf_error_handler() privileges :(
6409
		$jsonDebug = debug_backtrace();
6410
		$jsonDebug = $jsonDebug[0];
6411
		loadLanguage('Errors');
6412
6413
		if (!empty($jsonDebug))
6414
			log_error($txt['json_' . $jsonError], 'critical', $jsonDebug['file'], $jsonDebug['line']);
6415
6416
		else
6417
			log_error($txt['json_' . $jsonError], 'critical');
6418
6419
		// Everyone expects an array.
6420
		return array();
6421
	}
6422
6423
	return $returnArray;
6424
}
6425
6426
/**
6427
 * Check the given String if he is a valid IPv4 or IPv6
6428
 * return true or false
6429
 *
6430
 * @param string $IPString
6431
 *
6432
 * @return bool
6433
 */
6434
function isValidIP($IPString)
6435
{
6436
	return filter_var($IPString, FILTER_VALIDATE_IP) !== false;
6437
}
6438
6439
/**
6440
 * Outputs a response.
6441
 * It assumes the data is already a string.
6442
 *
6443
 * @param string $data The data to print
6444
 * @param string $type The content type. Defaults to Json.
6445
 * @return void
6446
 */
6447
function smf_serverResponse($data = '', $type = 'content-type: application/json')
6448
{
6449
	global $db_show_debug, $modSettings;
6450
6451
	// Defensive programming anyone?
6452
	if (empty($data))
6453
		return false;
6454
6455
	// Don't need extra stuff...
6456
	$db_show_debug = false;
6457
6458
	// Kill anything else.
6459
	ob_end_clean();
6460
6461
	if (!empty($modSettings['CompressedOutput']))
6462
		@ob_start('ob_gzhandler');
6463
6464
	else
6465
		ob_start();
6466
6467
	// Set the header.
6468
	header($type);
6469
6470
	// Echo!
6471
	echo $data;
6472
6473
	// Done.
6474
	obExit(false);
6475
}
6476
6477
/**
6478
 * Creates an optimized regex to match all known top level domains.
6479
 *
6480
 * The optimized regex is stored in $modSettings['tld_regex'].
6481
 *
6482
 * To update the stored version of the regex to use the latest list of valid
6483
 * TLDs from iana.org, set the $update parameter to true. Updating can take some
6484
 * time, based on network connectivity, so it should normally only be done by
6485
 * calling this function from a background or scheduled task.
6486
 *
6487
 * If $update is not true, but the regex is missing or invalid, the regex will
6488
 * be regenerated from a hard-coded list of TLDs. This regenerated regex will be
6489
 * overwritten on the next scheduled update.
6490
 *
6491
 * @param bool $update If true, fetch and process the latest official list of TLDs from iana.org.
6492
 */
6493
function set_tld_regex($update = false)
6494
{
6495
	global $sourcedir, $smcFunc, $modSettings;
6496
	static $done = false;
6497
6498
	// If we don't need to do anything, don't
6499
	if (!$update && $done)
6500
		return;
6501
6502
	// Should we get a new copy of the official list of TLDs?
6503
	if ($update)
6504
	{
6505
		$tlds = fetch_web_data('https://data.iana.org/TLD/tlds-alpha-by-domain.txt');
6506
		$tlds_md5 = fetch_web_data('https://data.iana.org/TLD/tlds-alpha-by-domain.txt.md5');
6507
6508
		/**
6509
		 * If the Internet Assigned Numbers Authority can't be reached, the Internet is GONE!
6510
		 * We're probably running on a server hidden in a bunker deep underground to protect
6511
		 * it from marauding bandits roaming on the surface. We don't want to waste precious
6512
		 * electricity on pointlessly repeating background tasks, so we'll wait until the next
6513
		 * regularly scheduled update to see if civilization has been restored.
6514
		 */
6515
		if ($tlds === false || $tlds_md5 === false)
6516
			$postapocalypticNightmare = true;
6517
6518
		// Make sure nothing went horribly wrong along the way.
6519
		if (md5($tlds) != substr($tlds_md5, 0, 32))
6520
			$tlds = array();
6521
	}
6522
	// If we aren't updating and the regex is valid, we're done
6523
	elseif (!empty($modSettings['tld_regex']) && @preg_match('~' . $modSettings['tld_regex'] . '~', null) !== false)
6524
	{
6525
		$done = true;
6526
		return;
6527
	}
6528
6529
	// If we successfully got an update, process the list into an array
6530
	if (!empty($tlds))
6531
	{
6532
		// Clean $tlds and convert it to an array
6533
		$tlds = array_filter(explode("\n", strtolower($tlds)), function($line)
6534
		{
6535
			$line = trim($line);
6536
			if (empty($line) || strlen($line) != strspn($line, 'abcdefghijklmnopqrstuvwxyz0123456789-'))
6537
				return false;
6538
			else
6539
				return true;
6540
		});
6541
6542
		// Convert Punycode to Unicode
6543
		require_once($sourcedir . '/Class-Punycode.php');
6544
		$Punycode = new Punycode();
6545
		$tlds = array_map(function($input) use ($Punycode)
6546
		{
6547
			return $Punycode->decode($input);
6548
		}, $tlds);
6549
	}
6550
	// Otherwise, use the 2012 list of gTLDs and ccTLDs for now and schedule a background update
6551
	else
6552
	{
6553
		$tlds = array('com', 'net', 'org', 'edu', 'gov', 'mil', 'aero', 'asia', 'biz',
6554
			'cat', 'coop', 'info', 'int', 'jobs', 'mobi', 'museum', 'name', 'post',
6555
			'pro', 'tel', 'travel', 'xxx', 'ac', 'ad', 'ae', 'af', 'ag', 'ai', 'al',
6556
			'am', 'ao', 'aq', 'ar', 'as', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd',
6557
			'be', 'bf', 'bg', 'bh', 'bi', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv',
6558
			'bw', 'by', 'bz', 'ca', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm',
6559
			'cn', 'co', 'cr', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do',
6560
			'dz', 'ec', 'ee', 'eg', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo',
6561
			'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gp',
6562
			'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu',
6563
			'id', 'ie', 'il', 'im', 'in', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo',
6564
			'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', 'kz', 'la',
6565
			'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly', 'ma', 'mc', 'md',
6566
			'me', 'mg', 'mh', 'mk', 'ml', 'mm', 'mn', 'mo', 'mp', 'mq', 'mr', 'ms', 'mt',
6567
			'mu', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'nc', 'ne', 'nf', 'ng', 'ni', 'nl',
6568
			'no', 'np', 'nr', 'nu', 'nz', 'om', 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl',
6569
			'pm', 'pn', 'pr', 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro', 'rs', 'ru', 'rw',
6570
			'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn',
6571
			'so', 'sr', 'ss', 'st', 'su', 'sv', 'sx', 'sy', 'sz', 'tc', 'td', 'tf', 'tg',
6572
			'th', 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'tr', 'tt', 'tv', 'tw', 'tz', 'ua',
6573
			'ug', 'uk', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf',
6574
			'ws', 'ye', 'yt', 'za', 'zm', 'zw',
6575
		);
6576
6577
		// Schedule a background update, unless civilization has collapsed and/or we are having connectivity issues.
6578
		if (empty($postapocalypticNightmare))
6579
		{
6580
			$smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
6581
				array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
6582
				array('$sourcedir/tasks/UpdateTldRegex.php', 'Update_TLD_Regex', '', 0), array()
6583
			);
6584
		}
6585
	}
6586
6587
	// Tack on some "special use domain names" that aren't in DNS but may possibly resolve.
6588
	// See https://www.iana.org/assignments/special-use-domain-names/ for more info.
6589
	$tlds = array_merge($tlds, array('local', 'onion', 'test'));
6590
6591
	// Get an optimized regex to match all the TLDs
6592
	$tld_regex = build_regex($tlds);
6593
6594
	// Remember the new regex in $modSettings
6595
	updateSettings(array('tld_regex' => $tld_regex));
6596
6597
	// Redundant repetition is redundant
6598
	$done = true;
6599
}
6600
6601
/**
6602
 * Creates optimized regular expressions from an array of strings.
6603
 *
6604
 * An optimized regex built using this function will be much faster than a
6605
 * simple regex built using `implode('|', $strings)` --- anywhere from several
6606
 * times to several orders of magnitude faster.
6607
 *
6608
 * However, the time required to build the optimized regex is approximately
6609
 * equal to the time it takes to execute the simple regex. Therefore, it is only
6610
 * worth calling this function if the resulting regex will be used more than
6611
 * once.
6612
 *
6613
 * Because PHP places an upper limit on the allowed length of a regex, very
6614
 * large arrays of $strings may not fit in a single regex. Normally, the excess
6615
 * strings will simply be dropped. However, if the $returnArray parameter is set
6616
 * to true, this function will build as many regexes as necessary to accommodate
6617
 * everything in $strings and return them in an array. You will need to iterate
6618
 * through all elements of the returned array in order to test all possible
6619
 * matches.
6620
 *
6621
 * @param array $strings An array of strings to make a regex for.
6622
 * @param string $delim An optional delimiter character to pass to preg_quote().
6623
 * @param bool $returnArray If true, returns an array of regexes.
6624
 * @return string|array One or more regular expressions to match any of the input strings.
6625
 */
6626
function build_regex($strings, $delim = null, $returnArray = false)
6627
{
6628
	global $smcFunc;
6629
	static $regexes = array();
6630
6631
	// If it's not an array, there's not much to do. ;)
6632
	if (!is_array($strings))
6633
		return preg_quote(@strval($strings), $delim);
6634
6635
	$regex_key = md5(json_encode(array($strings, $delim, $returnArray)));
6636
6637
	if (isset($regexes[$regex_key]))
6638
		return $regexes[$regex_key];
6639
6640
	// The mb_* functions are faster than the $smcFunc ones, but may not be available
6641
	if (function_exists('mb_internal_encoding') && function_exists('mb_detect_encoding') && function_exists('mb_strlen') && function_exists('mb_substr'))
6642
	{
6643
		if (($string_encoding = mb_detect_encoding(implode(' ', $strings))) !== false)
6644
		{
6645
			$current_encoding = mb_internal_encoding();
6646
			mb_internal_encoding($string_encoding);
6647
		}
6648
6649
		$strlen = 'mb_strlen';
6650
		$substr = 'mb_substr';
6651
	}
6652
	else
6653
	{
6654
		$strlen = $smcFunc['strlen'];
6655
		$substr = $smcFunc['substr'];
6656
	}
6657
6658
	// This recursive function creates the index array from the strings
6659
	$add_string_to_index = function($string, $index) use (&$strlen, &$substr, &$add_string_to_index)
6660
	{
6661
		static $depth = 0;
6662
		$depth++;
6663
6664
		$first = (string) @$substr($string, 0, 1);
6665
6666
		// No first character? That's no good.
6667
		if ($first === '')
6668
		{
6669
			// A nested array? Really? Ugh. Fine.
6670
			if (is_array($string) && $depth < 20)
6671
			{
6672
				foreach ($string as $str)
6673
					$index = $add_string_to_index($str, $index);
6674
			}
6675
6676
			$depth--;
6677
			return $index;
6678
		}
6679
6680
		if (empty($index[$first]))
6681
			$index[$first] = array();
6682
6683
		if ($strlen($string) > 1)
6684
		{
6685
			// Sanity check on recursion
6686
			if ($depth > 99)
6687
				$index[$first][$substr($string, 1)] = '';
6688
6689
			else
6690
				$index[$first] = $add_string_to_index($substr($string, 1), $index[$first]);
6691
		}
6692
		else
6693
			$index[$first][''] = '';
6694
6695
		$depth--;
6696
		return $index;
6697
	};
6698
6699
	// This recursive function turns the index array into a regular expression
6700
	$index_to_regex = function(&$index, $delim) use (&$strlen, &$index_to_regex)
6701
	{
6702
		static $depth = 0;
6703
		$depth++;
6704
6705
		// Absolute max length for a regex is 32768, but we might need wiggle room
6706
		$max_length = 30000;
6707
6708
		$regex = array();
6709
		$length = 0;
6710
6711
		foreach ($index as $key => $value)
6712
		{
6713
			$key_regex = preg_quote($key, $delim);
6714
			$new_key = $key;
6715
6716
			if (empty($value))
6717
				$sub_regex = '';
6718
			else
6719
			{
6720
				$sub_regex = $index_to_regex($value, $delim);
6721
6722
				if (count(array_keys($value)) == 1)
6723
				{
6724
					$new_key_array = explode('(?' . '>', $sub_regex);
6725
					$new_key .= $new_key_array[0];
6726
				}
6727
				else
6728
					$sub_regex = '(?' . '>' . $sub_regex . ')';
6729
			}
6730
6731
			if ($depth > 1)
6732
				$regex[$new_key] = $key_regex . $sub_regex;
6733
			else
6734
			{
6735
				if (($length += strlen($key_regex) + 1) < $max_length || empty($regex))
6736
				{
6737
					$regex[$new_key] = $key_regex . $sub_regex;
6738
					unset($index[$key]);
6739
				}
6740
				else
6741
					break;
6742
			}
6743
		}
6744
6745
		// Sort by key length and then alphabetically
6746
		uksort($regex, function($k1, $k2) use (&$strlen)
6747
		{
6748
			$l1 = $strlen($k1);
6749
			$l2 = $strlen($k2);
6750
6751
			if ($l1 == $l2)
6752
				return strcmp($k1, $k2) > 0 ? 1 : -1;
6753
			else
6754
				return $l1 > $l2 ? -1 : 1;
6755
		});
6756
6757
		$depth--;
6758
		return implode('|', $regex);
6759
	};
6760
6761
	// Now that the functions are defined, let's do this thing
6762
	$index = array();
6763
	$regex = '';
6764
6765
	foreach ($strings as $string)
6766
		$index = $add_string_to_index($string, $index);
6767
6768
	if ($returnArray === true)
6769
	{
6770
		$regex = array();
6771
		while (!empty($index))
6772
			$regex[] = '(?' . '>' . $index_to_regex($index, $delim) . ')';
6773
	}
6774
	else
6775
		$regex = '(?' . '>' . $index_to_regex($index, $delim) . ')';
6776
6777
	// Restore PHP's internal character encoding to whatever it was originally
6778
	if (!empty($current_encoding))
6779
		mb_internal_encoding($current_encoding);
6780
6781
	$regexes[$regex_key] = $regex;
6782
	return $regex;
6783
}
6784
6785
/**
6786
 * Check if the passed url has an SSL certificate.
6787
 *
6788
 * Returns true if a cert was found & false if not.
6789
 *
6790
 * @param string $url to check, in $boardurl format (no trailing slash).
6791
 */
6792
function ssl_cert_found($url)
6793
{
6794
	// This check won't work without OpenSSL
6795
	if (!extension_loaded('openssl'))
6796
		return true;
6797
6798
	// First, strip the subfolder from the passed url, if any
6799
	$parsedurl = parse_url($url);
6800
	$url = 'ssl://' . $parsedurl['host'] . ':443';
6801
6802
	// Next, check the ssl stream context for certificate info
6803
	if (version_compare(PHP_VERSION, '5.6.0', '<'))
6804
		$ssloptions = array("capture_peer_cert" => true);
6805
	else
6806
		$ssloptions = array("capture_peer_cert" => true, "verify_peer" => true, "allow_self_signed" => true);
6807
6808
	$result = false;
6809
	$context = stream_context_create(array("ssl" => $ssloptions));
6810
	$stream = @stream_socket_client($url, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
6811
	if ($stream !== false)
6812
	{
6813
		$params = stream_context_get_params($stream);
6814
		$result = isset($params["options"]["ssl"]["peer_certificate"]) ? true : false;
6815
	}
6816
	return $result;
6817
}
6818
6819
/**
6820
 * Check if the passed url has a redirect to https:// by querying headers.
6821
 *
6822
 * Returns true if a redirect was found & false if not.
6823
 * Note that when force_ssl = 2, SMF issues its own redirect...  So if this
6824
 * returns true, it may be caused by SMF, not necessarily an .htaccess redirect.
6825
 *
6826
 * @param string $url to check, in $boardurl format (no trailing slash).
6827
 */
6828
function https_redirect_active($url)
6829
{
6830
	// Ask for the headers for the passed url, but via http...
6831
	// Need to add the trailing slash, or it puts it there & thinks there's a redirect when there isn't...
6832
	$url = str_ireplace('https://', 'http://', $url) . '/';
6833
	$headers = @get_headers($url);
6834
	if ($headers === false)
6835
		return false;
6836
6837
	// Now to see if it came back https...
6838
	// First check for a redirect status code in first row (301, 302, 307)
6839
	if (strstr($headers[0], '301') === false && strstr($headers[0], '302') === false && strstr($headers[0], '307') === false)
6840
		return false;
6841
6842
	// Search for the location entry to confirm https
6843
	$result = false;
6844
	foreach ($headers as $header)
6845
	{
6846
		if (stristr($header, 'Location: https://') !== false)
6847
		{
6848
			$result = true;
6849
			break;
6850
		}
6851
	}
6852
	return $result;
6853
}
6854
6855
/**
6856
 * Build query_wanna_see_board and query_see_board for a userid
6857
 *
6858
 * Returns array with keys query_wanna_see_board and query_see_board
6859
 *
6860
 * @param int $userid of the user
6861
 */
6862
function build_query_board($userid)
6863
{
6864
	global $user_info, $modSettings, $smcFunc, $db_prefix;
6865
6866
	$query_part = array();
6867
6868
	// If we come from cron, we can't have a $user_info.
6869
	if (isset($user_info['id']) && $user_info['id'] == $userid && SMF != 'BACKGROUND')
6870
	{
6871
		$groups = $user_info['groups'];
6872
		$can_see_all_boards = $user_info['is_admin'] || $user_info['can_manage_boards'];
6873
		$ignoreboards = !empty($user_info['ignoreboards']) ? $user_info['ignoreboards'] : null;
6874
	}
6875
	else
6876
	{
6877
		$request = $smcFunc['db_query']('', '
6878
			SELECT mem.ignore_boards, mem.id_group, mem.additional_groups, mem.id_post_group
6879
			FROM {db_prefix}members AS mem
6880
			WHERE mem.id_member = {int:id_member}
6881
			LIMIT 1',
6882
			array(
6883
				'id_member' => $userid,
6884
			)
6885
		);
6886
6887
		$row = $smcFunc['db_fetch_assoc']($request);
6888
6889
		if (empty($row['additional_groups']))
6890
			$groups = array($row['id_group'], $row['id_post_group']);
6891
		else
6892
			$groups = array_merge(
6893
				array($row['id_group'], $row['id_post_group']),
6894
				explode(',', $row['additional_groups'])
6895
			);
6896
6897
		// Because history has proven that it is possible for groups to go bad - clean up in case.
6898
		foreach ($groups as $k => $v)
6899
			$groups[$k] = (int) $v;
6900
6901
		$can_see_all_boards = in_array(1, $groups) || (!empty($modSettings['board_manager_groups']) && count(array_intersect($groups, explode(',', $modSettings['board_manager_groups']))) > 0);
6902
6903
		$ignoreboards = !empty($row['ignore_boards']) && !empty($modSettings['allow_ignore_boards']) ? explode(',', $row['ignore_boards']) : array();
6904
	}
6905
6906
	// Just build this here, it makes it easier to change/use - administrators can see all boards.
6907
	if ($can_see_all_boards)
6908
		$query_part['query_see_board'] = '1=1';
6909
	// Otherwise just the groups in $user_info['groups'].
6910
	else
6911
	{
6912
		$query_part['query_see_board'] = '
6913
			EXISTS (
6914
				SELECT bpv.id_board
6915
				FROM ' . $db_prefix . 'board_permissions_view AS bpv
6916
				WHERE bpv.id_group IN ('. implode(',', $groups) .')
6917
					AND bpv.deny = 0
6918
					AND bpv.id_board = b.id_board
6919
			)';
6920
6921
		if (!empty($modSettings['deny_boards_access']))
6922
			$query_part['query_see_board'] .= '
6923
			AND NOT EXISTS (
6924
				SELECT bpv.id_board
6925
				FROM ' . $db_prefix . 'board_permissions_view AS bpv
6926
				WHERE bpv.id_group IN ( '. implode(',', $groups) .')
6927
					AND bpv.deny = 1
6928
					AND bpv.id_board = b.id_board
6929
			)';
6930
	}
6931
6932
	$query_part['query_see_message_board'] = str_replace('b.', 'm.', $query_part['query_see_board']);
6933
	$query_part['query_see_topic_board'] = str_replace('b.', 't.', $query_part['query_see_board']);
6934
6935
	// Build the list of boards they WANT to see.
6936
	// This will take the place of query_see_boards in certain spots, so it better include the boards they can see also
6937
6938
	// If they aren't ignoring any boards then they want to see all the boards they can see
6939
	if (empty($ignoreboards))
6940
	{
6941
		$query_part['query_wanna_see_board'] = $query_part['query_see_board'];
6942
		$query_part['query_wanna_see_message_board'] = $query_part['query_see_message_board'];
6943
		$query_part['query_wanna_see_topic_board'] = $query_part['query_see_topic_board'];
6944
	}
6945
	// Ok I guess they don't want to see all the boards
6946
	else
6947
	{
6948
		$query_part['query_wanna_see_board'] = '(' . $query_part['query_see_board'] . ' AND b.id_board NOT IN (' . implode(',', $ignoreboards) . '))';
6949
		$query_part['query_wanna_see_message_board'] = '(' . $query_part['query_see_message_board'] . ' AND m.id_board NOT IN (' . implode(',', $ignoreboards) . '))';
6950
		$query_part['query_wanna_see_topic_board'] = '(' . $query_part['query_see_topic_board'] . ' AND t.id_board NOT IN (' . implode(',', $ignoreboards) . '))';
6951
	}
6952
6953
	return $query_part;
6954
}
6955
6956
/**
6957
 * Check if the connection is using https.
6958
 *
6959
 * @return boolean true if connection used https
6960
 */
6961
function httpsOn()
6962
{
6963
	$secure = false;
6964
6965
	if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
6966
		$secure = true;
6967
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')
6968
		$secure = true;
6969
6970
	return $secure;
6971
}
6972
6973
/**
6974
 * A wrapper for `filter_var($url, FILTER_VALIDATE_URL)` that can handle URLs
6975
 * with international characters (a.k.a. IRIs)
6976
 *
6977
 * @param string $iri The IRI to test.
6978
 * @param int $flags Optional flags to pass to filter_var()
6979
 * @return string|bool Either the original IRI, or false if the IRI was invalid.
6980
 */
6981
function validate_iri($iri, $flags = null)
6982
{
6983
	$url = iri_to_url($iri);
6984
6985
	if (filter_var($url, FILTER_VALIDATE_URL, $flags) !== false)
6986
		return $iri;
6987
	else
6988
		return false;
6989
}
6990
6991
/**
6992
 * A wrapper for `filter_var($url, FILTER_SANITIZE_URL)` that can handle URLs
6993
 * with international characters (a.k.a. IRIs)
6994
 *
6995
 * Note: The returned value will still be an IRI, not a URL. To convert to URL,
6996
 * feed the result of this function to iri_to_url()
6997
 *
6998
 * @param string $iri The IRI to sanitize.
6999
 * @return string|bool The sanitized version of the IRI
7000
 */
7001
function sanitize_iri($iri)
7002
{
7003
	// Encode any non-ASCII characters (but not space or control characters of any sort)
7004
	$iri = preg_replace_callback('~[^\x00-\x7F\pZ\pC]~u', function($matches)
7005
	{
7006
		return rawurlencode($matches[0]);
7007
	}, $iri);
7008
7009
	// Perform normal sanitization
7010
	$iri = filter_var($iri, FILTER_SANITIZE_URL);
7011
7012
	// Decode the non-ASCII characters
7013
	$iri = rawurldecode($iri);
7014
7015
	return $iri;
7016
}
7017
7018
/**
7019
 * Converts a URL with international characters (an IRI) into a pure ASCII URL
7020
 *
7021
 * Uses Punycode to encode any non-ASCII characters in the domain name, and uses
7022
 * standard URL encoding on the rest.
7023
 *
7024
 * @param string $iri A IRI that may or may not contain non-ASCII characters.
7025
 * @return string|bool The URL version of the IRI.
7026
 */
7027
function iri_to_url($iri)
7028
{
7029
	global $sourcedir;
7030
7031
	$host = parse_url((strpos($iri, '://') === false ? 'http://' : '') . ltrim($iri, ':/'), PHP_URL_HOST);
7032
7033
	if (empty($host))
7034
		return $iri;
7035
7036
	// Convert the domain using the Punycode algorithm
7037
	require_once($sourcedir . '/Class-Punycode.php');
7038
	$Punycode = new Punycode();
7039
	$encoded_host = $Punycode->encode($host);
7040
	$pos = strpos($iri, $host);
7041
	$iri = substr_replace($iri, $encoded_host, $pos, strlen($host));
7042
7043
	// Encode any disallowed characters in the rest of the URL
7044
	$unescaped = array(
7045
		'%21' => '!', '%23' => '#', '%24' => '$', '%26' => '&',
7046
		'%27' => "'", '%28' => '(', '%29' => ')', '%2A' => '*',
7047
		'%2B' => '+', '%2C' => ',', '%2F' => '/', '%3A' => ':',
7048
		'%3B' => ';', '%3D' => '=', '%3F' => '?', '%40' => '@',
7049
		'%25' => '%',
7050
	);
7051
	$iri = strtr(rawurlencode($iri), $unescaped);
7052
7053
	return $iri;
7054
}
7055
7056
/**
7057
 * Decodes a URL containing encoded international characters to UTF-8
7058
 *
7059
 * Decodes any Punycode encoded characters in the domain name, then uses
7060
 * standard URL decoding on the rest.
7061
 *
7062
 * @param string $url The pure ASCII version of a URL.
7063
 * @return string|bool The UTF-8 version of the URL.
7064
 */
7065
function url_to_iri($url)
7066
{
7067
	global $sourcedir;
7068
7069
	$host = parse_url((strpos($url, '://') === false ? 'http://' : '') . ltrim($url, ':/'), PHP_URL_HOST);
7070
7071
	if (empty($host))
7072
		return $url;
7073
7074
	// Decode the domain from Punycode
7075
	require_once($sourcedir . '/Class-Punycode.php');
7076
	$Punycode = new Punycode();
7077
	$decoded_host = $Punycode->decode($host);
7078
	$pos = strpos($url, $host);
7079
	$url = substr_replace($url, $decoded_host, $pos, strlen($host));
7080
7081
	// Decode the rest of the URL
7082
	$url = rawurldecode($url);
7083
7084
	return $url;
7085
}
7086
7087
/**
7088
 * Ensures SMF's scheduled tasks are being run as intended
7089
 *
7090
 * If the admin activated the cron_is_real_cron setting, but the cron job is
7091
 * not running things at least once per day, we need to go back to SMF's default
7092
 * behaviour using "web cron" JavaScript calls.
7093
 */
7094
function check_cron()
7095
{
7096
	global $modSettings, $smcFunc, $txt;
7097
7098
	if (!empty($modSettings['cron_is_real_cron']) && time() - @intval($modSettings['cron_last_checked']) > 84600)
7099
	{
7100
		$request = $smcFunc['db_query']('', '
7101
			SELECT COUNT(*)
7102
			FROM {db_prefix}scheduled_tasks
7103
			WHERE disabled = {int:not_disabled}
7104
				AND next_time < {int:yesterday}',
7105
			array(
7106
				'not_disabled' => 0,
7107
				'yesterday' => time() - 84600,
7108
			)
7109
		);
7110
		list($overdue) = $smcFunc['db_fetch_row']($request);
7111
		$smcFunc['db_free_result']($request);
7112
7113
		// If we have tasks more than a day overdue, cron isn't doing its job.
7114
		if (!empty($overdue))
7115
		{
7116
			loadLanguage('ManageScheduledTasks');
7117
			log_error($txt['cron_not_working']);
7118
			updateSettings(array('cron_is_real_cron' => 0));
7119
		}
7120
		else
7121
			updateSettings(array('cron_last_checked' => time()));
7122
	}
7123
}
7124
7125
/**
7126
 * Sends an appropriate HTTP status header based on a given status code
7127
 *
7128
 * @param int $code The status code
7129
 * @param string $status The string for the status. Set automatically if not provided.
7130
 */
7131
function send_http_status($code, $status = '')
7132
{
7133
	$statuses = array(
7134
		206 => 'Partial Content',
7135
		304 => 'Not Modified',
7136
		400 => 'Bad Request',
7137
		403 => 'Forbidden',
7138
		404 => 'Not Found',
7139
		410 => 'Gone',
7140
		500 => 'Internal Server Error',
7141
		503 => 'Service Unavailable',
7142
	);
7143
7144
	$protocol = preg_match('~^\s*(HTTP/[12]\.\d)\s*$~i', $_SERVER['SERVER_PROTOCOL'], $matches) ? $matches[1] : 'HTTP/1.0';
7145
7146
	if (!isset($statuses[$code]) && empty($status))
7147
		header($protocol . ' 500 Internal Server Error');
7148
	else
7149
		header($protocol . ' ' . $code . ' ' . (!empty($status) ? $status : $statuses[$code]));
7150
}
7151
7152
/**
7153
 * Concatenates an array of strings into a grammatically correct sentence list
7154
 *
7155
 * Uses formats defined in the language files to build the list appropropriately
7156
 * for the currently loaded language.
7157
 *
7158
 * @param array $list An array of strings to concatenate.
7159
 * @return string The localized sentence list.
7160
 */
7161
function sentence_list($list)
7162
{
7163
	global $txt;
7164
7165
	// Make sure the bare necessities are defined
7166
	if (empty($txt['sentence_list_format']['n']))
7167
		$txt['sentence_list_format']['n'] = '{series}';
7168
	if (!isset($txt['sentence_list_separator']))
7169
		$txt['sentence_list_separator'] = ', ';
7170
	if (!isset($txt['sentence_list_separator_alt']))
7171
		$txt['sentence_list_separator_alt'] = '; ';
7172
7173
	// Which format should we use?
7174
	if (isset($txt['sentence_list_format'][count($list)]))
7175
		$format = $txt['sentence_list_format'][count($list)];
7176
	else
7177
		$format = $txt['sentence_list_format']['n'];
7178
7179
	// Do we want the normal separator or the alternate?
7180
	$separator = $txt['sentence_list_separator'];
7181
	foreach ($list as $item)
7182
	{
7183
		if (strpos($item, $separator) !== false)
7184
		{
7185
			$separator = $txt['sentence_list_separator_alt'];
7186
			$format = strtr($format, trim($txt['sentence_list_separator']), trim($separator));
7187
			break;
7188
		}
7189
	}
7190
7191
	$replacements = array();
7192
7193
	// Special handling for the last items on the list
7194
	$i = 0;
7195
	while (empty($done))
7196
	{
7197
		if (strpos($format, '{'. --$i . '}') !== false)
7198
			$replacements['{'. $i . '}'] = array_pop($list);
7199
		else
7200
			$done = true;
7201
	}
7202
	unset($done);
7203
7204
	// Special handling for the first items on the list
7205
	$i = 0;
7206
	while (empty($done))
7207
	{
7208
		if (strpos($format, '{'. ++$i . '}') !== false)
7209
			$replacements['{'. $i . '}'] = array_shift($list);
7210
		else
7211
			$done = true;
7212
	}
7213
	unset($done);
7214
7215
	// Whatever is left
7216
	$replacements['{series}'] = implode($separator, $list);
7217
7218
	// Do the deed
7219
	return strtr($format, $replacements);
7220
}
7221
7222
/**
7223
 * Truncate an array to a specified length
7224
 *
7225
 * @param array $array The array to truncate
7226
 * @param int $max_length The upperbound on the length
7227
 * @param int $deep How levels in an multidimensional array should the function take into account.
7228
 * @return array The truncated array
7229
 */
7230
function truncate_array($array, $max_length = 1900, $deep = 3)
7231
{
7232
    $array = (array) $array;
7233
7234
    $curr_length = array_length($array, $deep);
7235
7236
    if ($curr_length <= $max_length)
7237
        return $array;
7238
7239
    else
7240
    {
7241
        // Truncate each element's value to a reasonable length
7242
        $param_max = floor($max_length / count($array));
7243
7244
        $current_deep = $deep - 1;
7245
7246
        foreach ($array as $key => &$value)
7247
        {
7248
            if (is_array($value))
7249
                if ($current_deep > 0)
7250
                    $value = truncate_array($value, $current_deep);
7251
7252
            else
7253
                $value = substr($value, 0, $param_max - strlen($key) - 5);
7254
        }
7255
7256
        return $array;
7257
    }
7258
}
7259
7260
/**
7261
 * array_length Recursive
7262
 * @param $array
7263
 * @param int $deep How many levels should the function
7264
 * @return int
7265
 */
7266
function array_length($array, $deep = 3)
7267
{
7268
    // Work with arrays
7269
    $array = (array) $array;
7270
    $length = 0;
7271
7272
    $deep_count = $deep - 1;
7273
7274
    foreach ($array as $value)
7275
    {
7276
        // Recursive?
7277
        if (is_array($value))
7278
        {
7279
            // No can't do
7280
            if ($deep_count <= 0)
7281
                continue;
7282
7283
            $length += array_length($value, $deep_count);
7284
        }
7285
7286
        else
7287
            $length += strlen($value);
7288
    }
7289
7290
    return $length;
7291
}
7292
7293
?>