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

Subs-Editor.php ➔ legalise_bbc()   F

Complexity

Conditions 73
Paths 1081

Size

Total Lines 367

Duplication

Lines 83
Ratio 22.62 %

Importance

Changes 0
Metric Value
cc 73
nc 1081
nop 1
dl 83
loc 367
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file contains those functions specific to the editing box and is
5
 * generally used for WYSIWYG type functionality.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines http://www.simplemachines.org
11
 * @copyright 2018 Simple Machines and individual contributors
12
 * @license http://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 Beta 4
15
 */
16
17
if (!defined('SMF'))
18
	die('No direct access...');
19
20
/**
21
 * As of SMF 2.1, this is unused. But it is available if any mod wants to use it.
22
 * Convert only the BBC that can be edited in HTML mode for the (old) editor.
23
 *
24
 * @deprecated since version 2.1
25
 * @param string $text The text with bbcode in it
26
 * @param boolean $compat_mode Whether to actually convert the text
27
 * @return string The text
28
 */
29
function bbc_to_html($text, $compat_mode = false)
30
{
31
	global $modSettings;
32
33
	if (!$compat_mode)
34
		return $text;
35
36
	// Turn line breaks back into br's.
37
	$text = strtr($text, array("\r" => '', "\n" => '<br>'));
38
39
	// Prevent conversion of all bbcode inside these bbcodes.
40
	// @todo Tie in with bbc permissions ?
41
	foreach (array('code', 'php', 'nobbc') as $code)
42
	{
43
		if (strpos($text, '[' . $code) !== false)
44
		{
45
			$parts = preg_split('~(\[/' . $code . '\]|\[' . $code . '(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
46
47
			// Only mess with stuff inside tags.
48
			for ($i = 0, $n = count($parts); $i < $n; $i++)
0 ignored issues
show
Bug introduced by
It seems like $parts can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

48
			for ($i = 0, $n = count(/** @scrutinizer ignore-type */ $parts); $i < $n; $i++)
Loading history...
49
			{
50
				// Value of 2 means we're inside the tag.
51
				if ($i % 4 == 2)
52
					$parts[$i] = strtr($parts[$i], array('[' => '&#91;', ']' => '&#93;', "'" => "'"));
53
			}
54
			// Put our humpty dumpty message back together again.
55
			$text = implode('', $parts);
0 ignored issues
show
Bug introduced by
It seems like $parts can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

55
			$text = implode('', /** @scrutinizer ignore-type */ $parts);
Loading history...
56
		}
57
	}
58
59
	// What tags do we allow?
60
	$allowed_tags = array('b', 'u', 'i', 's', 'hr', 'list', 'li', 'font', 'size', 'color', 'img', 'left', 'center', 'right', 'url', 'email', 'ftp', 'sub', 'sup');
61
62
	$text = parse_bbc($text, true, '', $allowed_tags);
63
64
	// Fix for having a line break then a thingy.
65
	$text = strtr($text, array('<br><div' => '<div', "\n" => '', "\r" => ''));
66
67
	// Note that IE doesn't understand spans really - make them something "legacy"
68
	$working_html = array(
69
		'~<del>(.+?)</del>~i' => '<strike>$1</strike>',
70
		'~<span\sclass="bbc_u">(.+?)</span>~i' => '<u>$1</u>',
71
		'~<span\sstyle="color:\s*([#\d\w]+);" class="bbc_color">(.+?)</span>~i' => '<font color="$1">$2</font>',
72
		'~<span\sstyle="font-family:\s*([#\d\w\s]+);" class="bbc_font">(.+?)</span>~i' => '<font face="$1">$2</font>',
73
		'~<div\sstyle="text-align:\s*(left|right);">(.+?)</div>~i' => '<p align="$1">$2</p>',
74
	);
75
	$text = preg_replace(array_keys($working_html), array_values($working_html), $text);
76
77
	// Parse unique ID's and disable javascript into the smileys - using the double space.
78
	$i = 1;
79
	$text = preg_replace_callback('~(?:\s|&nbsp;)?<(img\ssrc="' . preg_quote($modSettings['smileys_url'], '~') . '/[^<>]+?/([^<>]+?)"\s*)[^<>]*?class="smiley">~',
80
		function($m) use (&$i)
81
		{
82
			return '<' . stripslashes($m[1]) . 'alt="" title="" onresizestart="return false;" id="smiley_' . $i++ . '_' . $m[2] . '" style="padding: 0 3px 0 3px;">';
83
		}, $text);
84
85
	return $text;
86
}
87
88
/**
89
 * Converts HTML to BBC
90
 * As of SMF 2.1, only used by ManageBoards.php (and possibly mods)
91
 *
92
 * @param string $text Text containing HTML
93
 * @return string The text with html converted to bbc
94
 */
95
function html_to_bbc($text)
96
{
97
	global $modSettings, $smcFunc, $scripturl, $context;
98
99
	// Replace newlines with spaces, as that's how browsers usually interpret them.
100
	$text = preg_replace("~\s*[\r\n]+\s*~", ' ', $text);
101
102
	// Though some of us love paragraphs, the parser will do better with breaks.
103
	$text = preg_replace('~</p>\s*?<p~i', '</p><br><p', $text);
104
	$text = preg_replace('~</p>\s*(?!<)~i', '</p><br>', $text);
105
106
	// Safari/webkit wraps lines in Wysiwyg in <div>'s.
107
	if (isBrowser('webkit'))
108
		$text = preg_replace(array('~<div(?:\s(?:[^<>]*?))?' . '>~i', '</div>'), array('<br>', ''), $text);
109
110
	// If there's a trailing break get rid of it - Firefox tends to add one.
111
	$text = preg_replace('~<br\s?/?' . '>$~i', '', $text);
112
113
	// Remove any formatting within code tags.
114
	if (strpos($text, '[code') !== false)
115
	{
116
		$text = preg_replace('~<br\s?/?' . '>~i', '#smf_br_spec_grudge_cool!#', $text);
117
		$parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
118
119
		// Only mess with stuff outside [code] tags.
120
		for ($i = 0, $n = count($parts); $i < $n; $i++)
0 ignored issues
show
Bug introduced by
It seems like $parts can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

120
		for ($i = 0, $n = count(/** @scrutinizer ignore-type */ $parts); $i < $n; $i++)
Loading history...
121
		{
122
			// Value of 2 means we're inside the tag.
123
			if ($i % 4 == 2)
124
				$parts[$i] = strip_tags($parts[$i]);
125
		}
126
127
		$text = strtr(implode('', $parts), array('#smf_br_spec_grudge_cool!#' => '<br>'));
0 ignored issues
show
Bug introduced by
It seems like $parts can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

127
		$text = strtr(implode('', /** @scrutinizer ignore-type */ $parts), array('#smf_br_spec_grudge_cool!#' => '<br>'));
Loading history...
128
	}
129
130
	// Remove scripts, style and comment blocks.
131
	$text = preg_replace('~<script[^>]*[^/]?' . '>.*?</script>~i', '', $text);
132
	$text = preg_replace('~<style[^>]*[^/]?' . '>.*?</style>~i', '', $text);
133
	$text = preg_replace('~\\<\\!--.*?-->~i', '', $text);
134
	$text = preg_replace('~\\<\\!\\[CDATA\\[.*?\\]\\]\\>~i', '', $text);
135
136
	// Do the smileys ultra first!
137
	preg_match_all('~<img\s+[^<>]*?id="*smiley_\d+_([^<>]+?)[\s"/>]\s*[^<>]*?/*>(?:\s)?~i', $text, $matches);
138
	if (!empty($matches[0]))
139
	{
140
		// Easy if it's not custom.
141
		if (empty($modSettings['smiley_enable']))
142
		{
143
			$smileysfrom = array('>:D', ':D', '::)', '>:(', ':)', ';)', ';D', ':(', ':o', '8)', ':P', '???', ':-[', ':-X', ':-*', ':\'(', ':-\\', '^-^', 'O0', 'C:-)', '0:)');
144
			$smileysto = array('evil.png', 'cheesy.png', 'rolleyes.png', 'angry.png', 'smiley.png', 'wink.png', 'grin.png', 'sad.png', 'shocked.png', 'cool.png', 'tongue.png', 'huh.png', 'embarrassed.png', 'lipsrsealed.png', 'kiss.png', 'cry.png', 'undecided.png', 'azn.png', 'afro.png', 'police.png', 'angel.png');
145
146
			foreach ($matches[1] as $k => $file)
147
			{
148
				$found = array_search($file, $smileysto);
149
				// Note the weirdness here is to stop double spaces between smileys.
150
				if ($found)
151
					$matches[1][$k] = '-[]-smf_smily_start#|#' . $smcFunc['htmlspecialchars']($smileysfrom[$found]) . '-[]-smf_smily_end#|#';
152
				else
153
					$matches[1][$k] = '';
154
			}
155
		}
156
		else
157
		{
158
			// Load all the smileys.
159
			$names = array();
160
			foreach ($matches[1] as $file)
161
				$names[] = $file;
162
			$names = array_unique($names);
163
164
			if (!empty($names))
165
			{
166
				$request = $smcFunc['db_query']('', '
167
					SELECT code, filename
168
					FROM {db_prefix}smileys
169
					WHERE filename IN ({array_string:smiley_filenames})',
170
					array(
171
						'smiley_filenames' => $names,
172
					)
173
				);
174
				$mappings = array();
175
				while ($row = $smcFunc['db_fetch_assoc']($request))
176
					$mappings[$row['filename']] = $smcFunc['htmlspecialchars']($row['code']);
177
				$smcFunc['db_free_result']($request);
178
179
				foreach ($matches[1] as $k => $file)
180
					if (isset($mappings[$file]))
181
						$matches[1][$k] = '-[]-smf_smily_start#|#' . $mappings[$file] . '-[]-smf_smily_end#|#';
182
			}
183
		}
184
185
		// Replace the tags!
186
		$text = str_replace($matches[0], $matches[1], $text);
187
188
		// Now sort out spaces
189
		$text = str_replace(array('-[]-smf_smily_end#|#-[]-smf_smily_start#|#', '-[]-smf_smily_end#|#', '-[]-smf_smily_start#|#'), ' ', $text);
190
	}
191
192
	// Only try to buy more time if the client didn't quit.
193
	if (connection_aborted() && $context['server']['is_apache'])
194
		@apache_reset_timeout();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for apache_reset_timeout(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

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

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

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

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
195
196
	$parts = preg_split('~(<[A-Za-z]+\s*[^<>]*?style="?[^<>"]+"?[^<>]*?(?:/?)>|</[A-Za-z]+>)~', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
197
	$replacement = '';
198
	$stack = array();
199
200
	foreach ($parts as $part)
201
	{
202
		if (preg_match('~(<([A-Za-z]+)\s*[^<>]*?)style="?([^<>"]+)"?([^<>]*?(/?)>)~', $part, $matches) === 1)
203
		{
204
			// If it's being closed instantly, we can't deal with it...yet.
205
			if ($matches[5] === '/')
206
				continue;
207
			else
208
			{
209
				// Get an array of styles that apply to this element. (The strtr is there to combat HTML generated by Word.)
210
				$styles = explode(';', strtr($matches[3], array('&quot;' => '')));
211
				$curElement = $matches[2];
212
				$precedingStyle = $matches[1];
213
				$afterStyle = $matches[4];
214
				$curCloseTags = '';
215
				$extra_attr = '';
216
217
				foreach ($styles as $type_value_pair)
218
				{
219
					// Remove spaces and convert uppercase letters.
220
					$clean_type_value_pair = strtolower(strtr(trim($type_value_pair), '=', ':'));
221
222
					// Something like 'font-weight: bold' is expected here.
223
					if (strpos($clean_type_value_pair, ':') === false)
224
						continue;
225
226
					// Capture the elements of a single style item (e.g. 'font-weight' and 'bold').
227
					list ($style_type, $style_value) = explode(':', $type_value_pair);
228
229
					$style_value = trim($style_value);
230
231
					switch (trim($style_type))
232
					{
233
						case 'font-weight':
234
							if ($style_value === 'bold')
235
							{
236
								$curCloseTags .= '[/b]';
237
								$replacement .= '[b]';
238
							}
239
						break;
240
241
						case 'text-decoration':
242
							if ($style_value == 'underline')
243
							{
244
								$curCloseTags .= '[/u]';
245
								$replacement .= '[u]';
246
							}
247
							elseif ($style_value == 'line-through')
248
							{
249
								$curCloseTags .= '[/s]';
250
								$replacement .= '[s]';
251
							}
252
						break;
253
254
						case 'text-align':
255
							if ($style_value == 'left')
256
							{
257
								$curCloseTags .= '[/left]';
258
								$replacement .= '[left]';
259
							}
260
							elseif ($style_value == 'center')
261
							{
262
								$curCloseTags .= '[/center]';
263
								$replacement .= '[center]';
264
							}
265
							elseif ($style_value == 'right')
266
							{
267
								$curCloseTags .= '[/right]';
268
								$replacement .= '[right]';
269
							}
270
						break;
271
272
						case 'font-style':
273
							if ($style_value == 'italic')
274
							{
275
								$curCloseTags .= '[/i]';
276
								$replacement .= '[i]';
277
							}
278
						break;
279
280
						case 'color':
281
							$curCloseTags .= '[/color]';
282
							$replacement .= '[color=' . $style_value . ']';
283
						break;
284
285
						case 'font-size':
286
							// Sometimes people put decimals where decimals should not be.
287
							if (preg_match('~(\d)+\.\d+(p[xt])~i', $style_value, $dec_matches) === 1)
288
								$style_value = $dec_matches[1] . $dec_matches[2];
289
290
							$curCloseTags .= '[/size]';
291
							$replacement .= '[size=' . $style_value . ']';
292
						break;
293
294
						case 'font-family':
295
							// Only get the first freaking font if there's a list!
296
							if (strpos($style_value, ',') !== false)
297
								$style_value = substr($style_value, 0, strpos($style_value, ','));
298
299
							$curCloseTags .= '[/font]';
300
							$replacement .= '[font=' . strtr($style_value, array("'" => '')) . ']';
301
						break;
302
303
						// This is a hack for images with dimensions embedded.
304
						case 'width':
305
						case 'height':
306
							if (preg_match('~[1-9]\d*~i', $style_value, $dimension) === 1)
307
								$extra_attr .= ' ' . $style_type . '="' . $dimension[0] . '"';
308
						break;
309
310
						case 'list-style-type':
311
							if (preg_match('~none|disc|circle|square|decimal|decimal-leading-zero|lower-roman|upper-roman|lower-alpha|upper-alpha|lower-greek|lower-latin|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha~i', $style_value, $listType) === 1)
312
								$extra_attr .= ' listtype="' . $listType[0] . '"';
313
						break;
314
					}
315
				}
316
317
				// Preserve some tags stripping the styling.
318
				if (in_array($matches[2], array('a', 'font', 'td')))
319
				{
320
					$replacement .= $precedingStyle . $afterStyle;
321
					$curCloseTags = '</' . $matches[2] . '>' . $curCloseTags;
322
				}
323
324
				// If there's something that still needs closing, push it to the stack.
325
				if (!empty($curCloseTags))
326
					array_push($stack, array(
327
							'element' => strtolower($curElement),
328
							'closeTags' => $curCloseTags
329
						)
330
					);
331
				elseif (!empty($extra_attr))
332
					$replacement .= $precedingStyle . $extra_attr . $afterStyle;
333
			}
334
		}
335
336
		elseif (preg_match('~</([A-Za-z]+)>~', $part, $matches) === 1)
337
		{
338
			// Is this the element that we've been waiting for to be closed?
339
			if (!empty($stack) && strtolower($matches[1]) === $stack[count($stack) - 1]['element'])
340
			{
341
				$byebyeTag = array_pop($stack);
342
				$replacement .= $byebyeTag['closeTags'];
343
			}
344
345
			// Must've been something else.
346
			else
347
				$replacement .= $part;
348
		}
349
		// In all other cases, just add the part to the replacement.
350
		else
351
			$replacement .= $part;
352
	}
353
354
	// Now put back the replacement in the text.
355
	$text = $replacement;
356
357
	// We are not finished yet, request more time.
358
	if (connection_aborted() && $context['server']['is_apache'])
359
		@apache_reset_timeout();
360
361
	// Let's pull out any legacy alignments.
362
	while (preg_match('~<([A-Za-z]+)\s+[^<>]*?(align="*(left|center|right)"*)[^<>]*?(/?)>~i', $text, $matches) === 1)
363
	{
364
		// Find the position in the text of this tag over again.
365
		$start_pos = strpos($text, $matches[0]);
366
		if ($start_pos === false)
367
			break;
368
369
		// End tag?
370
		if ($matches[4] != '/' && strpos($text, '</' . $matches[1] . '>', $start_pos) !== false)
371
		{
372
			$end_pos = strpos($text, '</' . $matches[1] . '>', $start_pos);
373
374
			// Remove the align from that tag so it's never checked again.
375
			$tag = substr($text, $start_pos, strlen($matches[0]));
376
			$content = substr($text, $start_pos + strlen($matches[0]), $end_pos - $start_pos - strlen($matches[0]));
377
			$tag = str_replace($matches[2], '', $tag);
378
379
			// Put the tags back into the body.
380
			$text = substr($text, 0, $start_pos) . $tag . '[' . $matches[3] . ']' . $content . '[/' . $matches[3] . ']' . substr($text, $end_pos);
381
		}
382
		else
383
		{
384
			// Just get rid of this evil tag.
385
			$text = substr($text, 0, $start_pos) . substr($text, $start_pos + strlen($matches[0]));
386
		}
387
	}
388
389
	// Let's do some special stuff for fonts - cause we all love fonts.
390
	while (preg_match('~<font\s+([^<>]*)>~i', $text, $matches) === 1)
391
	{
392
		// Find the position of this again.
393
		$start_pos = strpos($text, $matches[0]);
394
		$end_pos = false;
395
		if ($start_pos === false)
396
			break;
397
398
		// This must have an end tag - and we must find the right one.
399
		$lower_text = strtolower($text);
400
401
		$start_pos_test = $start_pos + 4;
402
		// How many starting tags must we find closing ones for first?
403
		$start_font_tag_stack = 0;
404
		while ($start_pos_test < strlen($text))
405
		{
406
			// Where is the next starting font?
407
			$next_start_pos = strpos($lower_text, '<font', $start_pos_test);
408
			$next_end_pos = strpos($lower_text, '</font>', $start_pos_test);
409
410
			// Did we past another starting tag before an end one?
411
			if ($next_start_pos !== false && $next_start_pos < $next_end_pos)
412
			{
413
				$start_font_tag_stack++;
414
				$start_pos_test = $next_start_pos + 4;
415
			}
416
			// Otherwise we have an end tag but not the right one?
417
			elseif ($start_font_tag_stack)
418
			{
419
				$start_font_tag_stack--;
420
				$start_pos_test = $next_end_pos + 4;
421
			}
422
			// Otherwise we're there!
423
			else
424
			{
425
				$end_pos = $next_end_pos;
426
				break;
427
			}
428
		}
429
		if ($end_pos === false)
430
			break;
431
432
		// Now work out what the attributes are.
433
		$attribs = fetchTagAttributes($matches[1]);
434
		$tags = array();
435
		$sizes_equivalence = array(1 => '8pt', '10pt', '12pt', '14pt', '18pt', '24pt', '36pt');
436
		foreach ($attribs as $s => $v)
437
		{
438
			if ($s == 'size')
439
			{
440
				// Cast before empty chech because casting a string results in a 0 and we don't have zeros in the array! ;)
441
				$v = (int) trim($v);
442
				$v = empty($v) ? 1 : $v;
443
				$tags[] = array('[size=' . $sizes_equivalence[$v] . ']', '[/size]');
444
			}
445
			elseif ($s == 'face')
446
				$tags[] = array('[font=' . trim(strtolower($v)) . ']', '[/font]');
447
			elseif ($s == 'color')
448
				$tags[] = array('[color=' . trim(strtolower($v)) . ']', '[/color]');
449
		}
450
451
		// As before add in our tags.
452
		$before = $after = '';
453
		foreach ($tags as $tag)
454
		{
455
			$before .= $tag[0];
456
			if (isset($tag[1]))
457
				$after = $tag[1] . $after;
458
		}
459
460
		// Remove the tag so it's never checked again.
461
		$content = substr($text, $start_pos + strlen($matches[0]), $end_pos - $start_pos - strlen($matches[0]));
462
463
		// Put the tags back into the body.
464
		$text = substr($text, 0, $start_pos) . $before . $content . $after . substr($text, $end_pos + 7);
465
	}
466
467
	// Almost there, just a little more time.
468
	if (connection_aborted() && $context['server']['is_apache'])
469
		@apache_reset_timeout();
470
471
	if (count($parts = preg_split('~<(/?)(li|ol|ul)([^>]*)>~i', $text, null, PREG_SPLIT_DELIM_CAPTURE)) > 1)
472
	{
473
		// A toggle that dermines whether we're directly under a <ol> or <ul>.
474
		$inList = false;
475
476
		// Keep track of the number of nested list levels.
477
		$listDepth = 0;
478
479
		// Map what we can expect from the HTML to what is supported by SMF.
480
		$listTypeMapping = array(
481
			'1' => 'decimal',
482
			'A' => 'upper-alpha',
483
			'a' => 'lower-alpha',
484
			'I' => 'upper-roman',
485
			'i' => 'lower-roman',
486
			'disc' => 'disc',
487
			'square' => 'square',
488
			'circle' => 'circle',
489
		);
490
491
		// $i: text, $i + 1: '/', $i + 2: tag, $i + 3: tail.
492
		for ($i = 0, $numParts = count($parts) - 1; $i < $numParts; $i += 4)
493
		{
494
			$tag = strtolower($parts[$i + 2]);
495
			$isOpeningTag = $parts[$i + 1] === '';
496
497
			if ($isOpeningTag)
498
			{
499
				switch ($tag)
500
				{
501
					case 'ol':
502
					case 'ul':
503
504
						// We have a problem, we're already in a list.
505
						if ($inList)
506
						{
507
							// Inject a list opener, we'll deal with the ol/ul next loop.
508
							array_splice($parts, $i, 0, array(
509
								'',
510
								'',
511
								str_repeat("\t", $listDepth) . '[li]',
512
								'',
513
							));
514
							$numParts = count($parts) - 1;
515
516
							// The inlist status changes a bit.
517
							$inList = false;
518
						}
519
520
						// Just starting a new list.
521
						else
522
						{
523
							$inList = true;
524
525
							if ($tag === 'ol')
526
								$listType = 'decimal';
527
							elseif (preg_match('~type="?(' . implode('|', array_keys($listTypeMapping)) . ')"?~', $parts[$i + 3], $match) === 1)
528
								$listType = $listTypeMapping[$match[1]];
529
							else
530
								$listType = null;
531
532
							$listDepth++;
533
534
							$parts[$i + 2] = '[list' . ($listType === null ? '' : ' type=' . $listType) . ']' . "\n";
535
							$parts[$i + 3] = '';
536
						}
537
					break;
538
539
					case 'li':
540
541
						// This is how it should be: a list item inside the list.
542
						if ($inList)
543
						{
544
							$parts[$i + 2] = str_repeat("\t", $listDepth) . '[li]';
545
							$parts[$i + 3] = '';
546
547
							// Within a list item, it's almost as if you're outside.
548
							$inList = false;
549
						}
550
551
						// The li is no direct child of a list.
552
						else
553
						{
554
							// We are apparently in a list item.
555
							if ($listDepth > 0)
556
							{
557
								$parts[$i + 2] = '[/li]' . "\n" . str_repeat("\t", $listDepth) . '[li]';
558
								$parts[$i + 3] = '';
559
							}
560
561
							// We're not even near a list.
562
							else
563
							{
564
								// Quickly create a list with an item.
565
								$listDepth++;
566
567
								$parts[$i + 2] = '[list]' . "\n\t" . '[li]';
568
								$parts[$i + 3] = '';
569
							}
570
						}
571
572
					break;
573
				}
574
			}
575
576
			// Handle all the closing tags.
577
			else
578
			{
579
				switch ($tag)
580
				{
581
					case 'ol':
582
					case 'ul':
583
584
						// As we expected it, closing the list while we're in it.
585
						if ($inList)
586
						{
587
							$inList = false;
588
589
							$listDepth--;
590
591
							$parts[$i + 1] = '';
592
							$parts[$i + 2] = str_repeat("\t", $listDepth) . '[/list]';
593
							$parts[$i + 3] = '';
594
						}
595
596
						else
597
						{
598
							// We're in a list item.
599
							if ($listDepth > 0)
600
							{
601
								// Inject closure for this list item first.
602
								// The content of $parts[$i] is left as is!
603
								array_splice($parts, $i + 1, 0, array(
604
									'', // $i + 1
605
									'[/li]' . "\n", // $i + 2
606
									'', // $i + 3
607
									'', // $i + 4
608
								));
609
								$numParts = count($parts) - 1;
610
611
								// Now that we've closed the li, we're in list space.
612
								$inList = true;
613
							}
614
615
							// We're not even in a list, ignore
616
							else
617
							{
618
								$parts[$i + 1] = '';
619
								$parts[$i + 2] = '';
620
								$parts[$i + 3] = '';
621
							}
622
						}
623
					break;
624
625
					case 'li':
626
627
						if ($inList)
628
						{
629
							// There's no use for a </li> after <ol> or <ul>, ignore.
630
							$parts[$i + 1] = '';
631
							$parts[$i + 2] = '';
632
							$parts[$i + 3] = '';
633
						}
634
635
						else
636
						{
637
							// Remove the trailing breaks from the list item.
638
							$parts[$i] = preg_replace('~\s*<br\s*' . '/?' . '>\s*$~', '', $parts[$i]);
639
							$parts[$i + 1] = '';
640
							$parts[$i + 2] = '[/li]' . "\n";
641
							$parts[$i + 3] = '';
642
643
							// And we're back in the [list] space.
644
							$inList = true;
645
						}
646
647
					break;
648
				}
649
			}
650
651
			// If we're in the [list] space, no content is allowed.
652
			if ($inList && trim(preg_replace('~\s*<br\s*' . '/?' . '>\s*~', '', $parts[$i + 4])) !== '')
653
			{
654
				// Fix it by injecting an extra list item.
655
				array_splice($parts, $i + 4, 0, array(
656
					'', // No content.
657
					'', // Opening tag.
658
					'li', // It's a <li>.
659
					'', // No tail.
660
				));
661
				$numParts = count($parts) - 1;
662
			}
663
		}
664
665
		$text = implode('', $parts);
666
667
		if ($inList)
668
		{
669
			$listDepth--;
670
			$text .= str_repeat("\t", $listDepth) . '[/list]';
671
		}
672
673
		for ($i = $listDepth; $i > 0; $i--)
674
			$text .= '[/li]' . "\n" . str_repeat("\t", $i - 1) . '[/list]';
675
	}
676
677
	// I love my own image...
678
	while (preg_match('~<img\s+([^<>]*)/*>~i', $text, $matches) === 1)
679
	{
680
		// Find the position of the image.
681
		$start_pos = strpos($text, $matches[0]);
682
		if ($start_pos === false)
683
			break;
684
		$end_pos = $start_pos + strlen($matches[0]);
685
686
		$params = '';
687
		$src = '';
688
689
		$attrs = fetchTagAttributes($matches[1]);
690
		foreach ($attrs as $attrib => $value)
691
		{
692
			if (in_array($attrib, array('width', 'height')))
693
				$params .= ' ' . $attrib . '=' . (int) $value;
694
			elseif ($attrib == 'alt' && trim($value) != '')
695
				$params .= ' alt=' . trim($value);
696
			elseif ($attrib == 'src')
697
				$src = trim($value);
698
		}
699
700
		$tag = '';
701
		if (!empty($src))
702
		{
703
			// Attempt to fix the path in case it's not present.
704
			if (preg_match('~^https?://~i', $src) === 0 && is_array($parsedURL = parse_url($scripturl)) && isset($parsedURL['host']))
705
			{
706
				$baseURL = (isset($parsedURL['scheme']) ? $parsedURL['scheme'] : 'http') . '://' . $parsedURL['host'] . (empty($parsedURL['port']) ? '' : ':' . $parsedURL['port']);
707
708
				if (substr($src, 0, 1) === '/')
709
					$src = $baseURL . $src;
710
				else
711
					$src = $baseURL . (empty($parsedURL['path']) ? '/' : preg_replace('~/(?:index\\.php)?$~', '', $parsedURL['path'])) . '/' . $src;
712
			}
713
714
			$tag = '[img' . $params . ']' . $src . '[/img]';
715
		}
716
717
		// Replace the tag
718
		$text = substr($text, 0, $start_pos) . $tag . substr($text, $end_pos);
719
	}
720
721
	// The final bits are the easy ones - tags which map to tags which map to tags - etc etc.
722
	$tags = array(
723
		'~<b(\s(.)*?)*?' . '>~i' => function()
724
		{
725
			return '[b]';
726
		},
727
		'~</b>~i' => function()
728
		{
729
			return '[/b]';
730
		},
731
		'~<i(\s(.)*?)*?' . '>~i' => function()
732
		{
733
			return '[i]';
734
		},
735
		'~</i>~i' => function()
736
		{
737
			return '[/i]';
738
		},
739
		'~<u(\s(.)*?)*?' . '>~i' => function()
740
		{
741
			return '[u]';
742
		},
743
		'~</u>~i' => function()
744
		{
745
			return '[/u]';
746
		},
747
		'~<strong(\s(.)*?)*?' . '>~i' => function()
748
		{
749
			return '[b]';
750
		},
751
		'~</strong>~i' => function()
752
		{
753
			return '[/b]';
754
		},
755
		'~<em(\s(.)*?)*?' . '>~i' => function()
756
		{
757
			return '[i]';
758
		},
759
		'~</em>~i' => function()
760
		{
761
			return '[i]';
762
		},
763
		'~<s(\s(.)*?)*?' . '>~i' => function()
764
		{
765
			return "[s]";
766
		},
767
		'~</s>~i' => function()
768
		{
769
			return "[/s]";
770
		},
771
		'~<strike(\s(.)*?)*?' . '>~i' => function()
772
		{
773
			return '[s]';
774
		},
775
		'~</strike>~i' => function()
776
		{
777
			return '[/s]';
778
		},
779
		'~<del(\s(.)*?)*?' . '>~i' => function()
780
		{
781
			return '[s]';
782
		},
783
		'~</del>~i' => function()
784
		{
785
			return '[/s]';
786
		},
787
		'~<center(\s(.)*?)*?' . '>~i' => function()
788
		{
789
			return '[center]';
790
		},
791
		'~</center>~i' => function()
792
		{
793
			return '[/center]';
794
		},
795
		'~<pre(\s(.)*?)*?' . '>~i' => function()
796
		{
797
			return '[pre]';
798
		},
799
		'~</pre>~i' => function()
800
		{
801
			return '[/pre]';
802
		},
803
		'~<sub(\s(.)*?)*?' . '>~i' => function()
804
		{
805
			return '[sub]';
806
		},
807
		'~</sub>~i' => function()
808
		{
809
			return '[/sub]';
810
		},
811
		'~<sup(\s(.)*?)*?' . '>~i' => function()
812
		{
813
			return '[sup]';
814
		},
815
		'~</sup>~i' => function()
816
		{
817
			return '[/sup]';
818
		},
819
		'~<tt(\s(.)*?)*?' . '>~i' => function()
820
		{
821
			return '[tt]';
822
		},
823
		'~</tt>~i' => function()
824
		{
825
			return '[/tt]';
826
		},
827
		'~<table(\s(.)*?)*?' . '>~i' => function()
828
		{
829
			return '[table]';
830
		},
831
		'~</table>~i' => function()
832
		{
833
			return '[/table]';
834
		},
835
		'~<tr(\s(.)*?)*?' . '>~i' => function()
836
		{
837
			return '[tr]';
838
		},
839
		'~</tr>~i' => function()
840
		{
841
			return '[/tr]';
842
		},
843
		'~<(td|th)\s[^<>]*?colspan="?(\d{1,2})"?.*?' . '>~i' => function($matches)
844
		{
845
			return str_repeat('[td][/td]', $matches[2] - 1) . '[td]';
846
		},
847
		'~<(td|th)(\s(.)*?)*?' . '>~i' => function()
848
		{
849
			return '[td]';
850
		},
851
		'~</(td|th)>~i' => function()
852
		{
853
			return '[/td]';
854
		},
855
		'~<br(?:\s[^<>]*?)?' . '>~i' => function()
856
		{
857
			return "\n";
858
		},
859
		'~<hr[^<>]*>(\n)?~i' => function($matches)
860
		{
861
			return "[hr]\n" . $matches[0];
862
		},
863
		'~(\n)?\\[hr\\]~i' => function()
864
		{
865
			return "\n[hr]";
866
		},
867
		'~^\n\\[hr\\]~i' => function()
868
		{
869
			return "[hr]";
870
		},
871
		'~<blockquote(\s(.)*?)*?' . '>~i' =>  function()
872
		{
873
			return "&lt;blockquote&gt;";
874
		},
875
		'~</blockquote>~i' => function()
876
		{
877
			return "&lt;/blockquote&gt;";
878
		},
879
		'~<ins(\s(.)*?)*?' . '>~i' => function()
880
		{
881
			return "&lt;ins&gt;";
882
		},
883
		'~</ins>~i' => function()
884
		{
885
			return "&lt;/ins&gt;";
886
		},
887
	);
888
889
	foreach ($tags as $tag => $replace)
890
		$text = preg_replace_callback($tag, $replace, $text);
891
892
	// Please give us just a little more time.
893
	if (connection_aborted() && $context['server']['is_apache'])
894
		@apache_reset_timeout();
895
896
	// What about URL's - the pain in the ass of the tag world.
897
	while (preg_match('~<a\s+([^<>]*)>([^<>]*)</a>~i', $text, $matches) === 1)
898
	{
899
		// Find the position of the URL.
900
		$start_pos = strpos($text, $matches[0]);
901
		if ($start_pos === false)
902
			break;
903
		$end_pos = $start_pos + strlen($matches[0]);
904
905
		$tag_type = 'url';
906
		$href = '';
907
908
		$attrs = fetchTagAttributes($matches[1]);
909
		foreach ($attrs as $attrib => $value)
910
		{
911
			if ($attrib == 'href')
912
			{
913
				$href = trim($value);
914
915
				// Are we dealing with an FTP link?
916
				if (preg_match('~^ftps?://~', $href) === 1)
917
					$tag_type = 'ftp';
918
919
				// Or is this a link to an email address?
920
				elseif (substr($href, 0, 7) == 'mailto:')
921
				{
922
					$tag_type = 'email';
923
					$href = substr($href, 7);
924
				}
925
926
				// No http(s), so attempt to fix this potential relative URL.
927
				elseif (preg_match('~^https?://~i', $href) === 0 && is_array($parsedURL = parse_url($scripturl)) && isset($parsedURL['host']))
928
				{
929
					$baseURL = (isset($parsedURL['scheme']) ? $parsedURL['scheme'] : 'http') . '://' . $parsedURL['host'] . (empty($parsedURL['port']) ? '' : ':' . $parsedURL['port']);
930
931
					if (substr($href, 0, 1) === '/')
932
						$href = $baseURL . $href;
933
					else
934
						$href = $baseURL . (empty($parsedURL['path']) ? '/' : preg_replace('~/(?:index\\.php)?$~', '', $parsedURL['path'])) . '/' . $href;
935
				}
936
			}
937
938
			// External URL?
939
			if ($attrib == 'target' && $tag_type == 'url')
940
			{
941
				if (trim($value) == '_blank')
942
					$tag_type == 'iurl';
943
			}
944
		}
945
946
		$tag = '';
947
		if ($href != '')
948
		{
949
			if ($matches[2] == $href)
950
				$tag = '[' . $tag_type . ']' . $href . '[/' . $tag_type . ']';
951
			else
952
				$tag = '[' . $tag_type . '=' . $href . ']' . $matches[2] . '[/' . $tag_type . ']';
953
		}
954
955
		// Replace the tag
956
		$text = substr($text, 0, $start_pos) . $tag . substr($text, $end_pos);
957
	}
958
959
	$text = strip_tags($text);
960
961
	// Some tags often end up as just dummy tags - remove those.
962
	$text = preg_replace('~\[[bisu]\]\s*\[/[bisu]\]~', '', $text);
963
964
	// Fix up entities.
965
	$text = preg_replace('~&#38;~i', '&#38;#38;', $text);
966
967
	$text = legalise_bbc($text);
968
969
	return $text;
970
}
971
972
/**
973
 * Returns an array of attributes associated with a tag.
974
 *
975
 * @param string $text A tag
976
 * @return array An array of attributes
977
 */
978
function fetchTagAttributes($text)
979
{
980
	$attribs = array();
981
	$key = $value = '';
982
	$tag_state = 0; // 0 = key, 1 = attribute with no string, 2 = attribute with string
983
	for ($i = 0; $i < strlen($text); $i++)
984
	{
985
		// We're either moving from the key to the attribute or we're in a string and this is fine.
986
		if ($text[$i] == '=')
987
		{
988
			if ($tag_state == 0)
989
				$tag_state = 1;
990
			elseif ($tag_state == 2)
991
				$value .= '=';
992
		}
993
		// A space is either moving from an attribute back to a potential key or in a string is fine.
994
		elseif ($text[$i] == ' ')
995
		{
996
			if ($tag_state == 2)
997
				$value .= ' ';
998
			elseif ($tag_state == 1)
999
			{
1000
				$attribs[$key] = $value;
1001
				$key = $value = '';
1002
				$tag_state = 0;
1003
			}
1004
		}
1005
		// A quote?
1006
		elseif ($text[$i] == '"')
1007
		{
1008
			// Must be either going into or out of a string.
1009
			if ($tag_state == 1)
1010
				$tag_state = 2;
1011
			else
1012
				$tag_state = 1;
1013
		}
1014
		// Otherwise it's fine.
1015
		else
1016
		{
1017
			if ($tag_state == 0)
1018
				$key .= $text[$i];
1019
			else
1020
				$value .= $text[$i];
1021
		}
1022
	}
1023
1024
	// Anything left?
1025
	if ($key != '' && $value != '')
0 ignored issues
show
introduced by
The condition $key != '' is always false.
Loading history...
1026
		$attribs[$key] = $value;
1027
1028
	return $attribs;
1029
}
1030
1031
/**
1032
 * Attempt to clean up illegal BBC caused by browsers like Opera which don't obey the rules
1033
 *
1034
 * @param string $text Text
1035
 * @return string Cleaned up text
1036
 */
1037
function legalise_bbc($text)
1038
{
1039
	global $modSettings;
1040
1041
	// Don't care about the texts that are too short.
1042
	if (strlen($text) < 3)
1043
		return $text;
1044
1045
	// A list of tags that's disabled by the admin.
1046
	$disabled = empty($modSettings['disabledBBC']) ? array() : array_flip(explode(',', strtolower($modSettings['disabledBBC'])));
1047
1048
	// Get a list of all the tags that are not disabled.
1049
	$all_tags = parse_bbc(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $message of parse_bbc(). ( Ignorable by Annotation )

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

1049
	$all_tags = parse_bbc(/** @scrutinizer ignore-type */ false);
Loading history...
1050
	$valid_tags = array();
1051
	$self_closing_tags = array();
1052
	foreach ($all_tags as $tag)
0 ignored issues
show
Bug introduced by
The expression $all_tags of type string is not traversable.
Loading history...
1053
	{
1054
		if (!isset($disabled[$tag['tag']]))
1055
			$valid_tags[$tag['tag']] = !empty($tag['block_level']);
1056
		if (isset($tag['type']) && $tag['type'] == 'closed')
1057
			$self_closing_tags[] = $tag['tag'];
1058
	}
1059
1060
	// Right - we're going to start by going through the whole lot to make sure we don't have align stuff crossed as this happens load and is stupid!
1061
	$align_tags = array('left', 'center', 'right', 'pre');
1062
1063
	// Remove those align tags that are not valid.
1064
	$align_tags = array_intersect($align_tags, array_keys($valid_tags));
1065
1066
	// These keep track of where we are!
1067
	if (!empty($align_tags) && count($matches = preg_split('~(\\[/?(?:' . implode('|', $align_tags) . ')\\])~', $text, -1, PREG_SPLIT_DELIM_CAPTURE)) > 1)
0 ignored issues
show
Bug introduced by
It seems like $matches = preg_split('~...EG_SPLIT_DELIM_CAPTURE) can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

1067
	if (!empty($align_tags) && count(/** @scrutinizer ignore-type */ $matches = preg_split('~(\\[/?(?:' . implode('|', $align_tags) . ')\\])~', $text, -1, PREG_SPLIT_DELIM_CAPTURE)) > 1)
Loading history...
1068
	{
1069
		// The first one is never a tag.
1070
		$isTag = false;
1071
1072
		// By default we're not inside a tag too.
1073
		$insideTag = null;
1074
1075
		foreach ($matches as $i => $match)
1076
		{
1077
			// We're only interested in tags, not text.
1078
			if ($isTag)
1079
			{
1080
				$isClosingTag = substr($match, 1, 1) === '/';
1081
				$tagName = substr($match, $isClosingTag ? 2 : 1, -1);
1082
1083
				// We're closing the exact same tag that we opened.
1084
				if ($isClosingTag && $insideTag === $tagName)
1085
					$insideTag = null;
1086
1087
				// We're opening a tag and we're not yet inside one either
1088
				elseif (!$isClosingTag && $insideTag === null)
1089
					$insideTag = $tagName;
1090
1091
				// In all other cases, this tag must be invalid
1092
				else
1093
					unset($matches[$i]);
1094
			}
1095
1096
			// The next one is gonna be the other one.
1097
			$isTag = !$isTag;
0 ignored issues
show
introduced by
$isTag is of type mixed, thus it always evaluated to false.
Loading history...
1098
		}
1099
1100
		// We're still inside a tag and had no chance for closure?
1101
		if ($insideTag !== null)
0 ignored issues
show
introduced by
The condition $insideTag !== null is always false.
Loading history...
1102
			$matches[] = '[/' . $insideTag . ']';
1103
1104
		// And a complete text string again.
1105
		$text = implode('', $matches);
0 ignored issues
show
Bug introduced by
It seems like $matches can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

1105
		$text = implode('', /** @scrutinizer ignore-type */ $matches);
Loading history...
1106
	}
1107
1108
	// Quickly remove any tags which are back to back.
1109
	$backToBackPattern = '~\\[(' . implode('|', array_diff(array_keys($valid_tags), array('td', 'anchor'))) . ')[^<>\\[\\]]*\\]\s*\\[/\\1\\]~';
1110
	$lastlen = 0;
1111
	while (strlen($text) !== $lastlen)
1112
		$lastlen = strlen($text = preg_replace($backToBackPattern, '', $text));
1113
1114
	// Need to sort the tags by name length.
1115
	uksort($valid_tags, function ($a, $b) {
1116
		return strlen($a) < strlen($b) ? 1 : -1;
1117
	});
1118
1119
	// These inline tags can compete with each other regarding style.
1120
	$competing_tags = array(
1121
		'color',
1122
		'size',
1123
	);
1124
1125
	// These keep track of where we are!
1126
	if (count($parts = preg_split(sprintf('~(\\[)(/?)(%1$s)((?:[\\s=][^\\]\\[]*)?\\])~', implode('|', array_keys($valid_tags))), $text, -1, PREG_SPLIT_DELIM_CAPTURE)) > 1)
1127
	{
1128
		// Start outside [nobbc] or [code] blocks.
1129
		$inCode = false;
1130
		$inNoBbc = false;
1131
1132
		// A buffer containing all opened inline elements.
1133
		$inlineElements = array();
1134
1135
		// A buffer containing all opened block elements.
1136
		$blockElements = array();
1137
1138
		// A buffer containing the opened inline elements that might compete.
1139
		$competingElements = array();
1140
1141
		// $i: text, $i + 1: '[', $i + 2: '/', $i + 3: tag, $i + 4: tag tail.
1142
		for ($i = 0, $n = count($parts) - 1; $i < $n; $i += 5)
1143
		{
1144
			$tag = $parts[$i + 3];
1145
			$isOpeningTag = $parts[$i + 2] === '';
1146
			$isClosingTag = $parts[$i + 2] === '/';
1147
			$isBlockLevelTag = isset($valid_tags[$tag]) && $valid_tags[$tag] && !in_array($tag, $self_closing_tags);
1148
			$isCompetingTag = in_array($tag, $competing_tags);
1149
1150
			// Check if this might be one of those cleaned out tags.
1151
			if ($tag === '')
1152
				continue;
1153
1154
			// Special case: inside [code] blocks any code is left untouched.
1155
			elseif ($tag === 'code')
1156
			{
1157
				// We're inside a code block and closing it.
1158
				if ($inCode && $isClosingTag)
1159
				{
1160
					$inCode = false;
1161
1162
					// Reopen tags that were closed before the code block.
1163
					if (!empty($inlineElements))
1164
						$parts[$i + 4] .= '[' . implode('][', array_keys($inlineElements)) . ']';
1165
				}
1166
1167
				// We're outside a coding and nobbc block and opening it.
1168
				elseif (!$inCode && !$inNoBbc && $isOpeningTag)
1169
				{
1170
					// If there are still inline elements left open, close them now.
1171
					if (!empty($inlineElements))
1172
					{
1173
						$parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
1174
						//$inlineElements = array();
1175
					}
1176
1177
					$inCode = true;
1178
				}
1179
1180
				// Nothing further to do.
1181
				continue;
1182
			}
1183
1184
			// Special case: inside [nobbc] blocks any BBC is left untouched.
1185
			elseif ($tag === 'nobbc')
1186
			{
1187
				// We're inside a nobbc block and closing it.
1188
				if ($inNoBbc && $isClosingTag)
1189
				{
1190
					$inNoBbc = false;
1191
1192
					// Some inline elements might've been closed that need reopening.
1193
					if (!empty($inlineElements))
1194
						$parts[$i + 4] .= '[' . implode('][', array_keys($inlineElements)) . ']';
1195
				}
1196
1197
				// We're outside a nobbc and coding block and opening it.
1198
				elseif (!$inNoBbc && !$inCode && $isOpeningTag)
1199
				{
1200
					// Can't have inline elements still opened.
1201
					if (!empty($inlineElements))
1202
					{
1203
						$parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
1204
						//$inlineElements = array();
1205
					}
1206
1207
					$inNoBbc = true;
1208
				}
1209
1210
				continue;
1211
			}
1212
1213
			// So, we're inside one of the special blocks: ignore any tag.
1214
			elseif ($inCode || $inNoBbc)
1215
				continue;
1216
1217
			// We're dealing with an opening tag.
1218
			if ($isOpeningTag)
1219
			{
1220
				// Everyting inside the square brackets of the opening tag.
1221
				$elementContent = $parts[$i + 3] . substr($parts[$i + 4], 0, -1);
1222
1223
				// A block level opening tag.
1224
				if ($isBlockLevelTag)
1225
				{
1226
					// Are there inline elements still open?
1227
					if (!empty($inlineElements))
1228
					{
1229
						// Close all the inline tags, a block tag is coming...
1230
						$parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
1231
1232
						// Now open them again, we're inside the block tag now.
1233
						$parts[$i + 5] = '[' . implode('][', array_keys($inlineElements)) . ']' . $parts[$i + 5];
1234
					}
1235
1236
					$blockElements[] = $tag;
1237
				}
1238
1239
				// Inline opening tag.
1240
				elseif (!in_array($tag, $self_closing_tags))
1241
				{
1242
					// Can't have two opening elements with the same contents!
1243
					if (isset($inlineElements[$elementContent]))
1244
					{
1245
						// Get rid of this tag.
1246
						$parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
1247
1248
						// Now try to find the corresponding closing tag.
1249
						$curLevel = 1;
1250
						for ($j = $i + 5, $m = count($parts) - 1; $j < $m; $j += 5)
1251
						{
1252
							// Find the tags with the same tagname
1253
							if ($parts[$j + 3] === $tag)
1254
							{
1255
								// If it's an opening tag, increase the level.
1256
								if ($parts[$j + 2] === '')
1257
									$curLevel++;
1258
1259
								// A closing tag, decrease the level.
1260
								else
1261
								{
1262
									$curLevel--;
1263
1264
									// Gotcha! Clean out this closing tag gone rogue.
1265
									if ($curLevel === 0)
1266
									{
1267
										$parts[$j + 1] = $parts[$j + 2] = $parts[$j + 3] = $parts[$j + 4] = '';
1268
										break;
1269
									}
1270
								}
1271
							}
1272
						}
1273
					}
1274
1275
					// Otherwise, add this one to the list.
1276
					else
1277
					{
1278
						if ($isCompetingTag)
1279
						{
1280
							if (!isset($competingElements[$tag]))
1281
								$competingElements[$tag] = array();
1282
1283
							$competingElements[$tag][] = $parts[$i + 4];
1284
1285
							if (count($competingElements[$tag]) > 1)
1286
								$parts[$i] .= '[/' . $tag . ']';
1287
						}
1288
1289
						$inlineElements[$elementContent] = $tag;
1290
					}
1291
				}
1292
			}
1293
1294
			// Closing tag.
1295
			else
1296
			{
1297
				// Closing the block tag.
1298
				if ($isBlockLevelTag)
1299
				{
1300
					// Close the elements that should've been closed by closing this tag.
1301
					if (!empty($blockElements))
1302
					{
1303
						$addClosingTags = array();
1304
						while ($element = array_pop($blockElements))
1305
						{
1306
							if ($element === $tag)
1307
								break;
1308
1309
							// Still a block tag was open not equal to this tag.
1310
							$addClosingTags[] = $element['type'];
1311
						}
1312
1313
						if (!empty($addClosingTags))
1314
							$parts[$i + 1] = '[/' . implode('][/', array_reverse($addClosingTags)) . ']' . $parts[$i + 1];
1315
1316
						// Apparently the closing tag was not found on the stack.
1317
						if (!is_string($element) || $element !== $tag)
1318
						{
1319
							// Get rid of this particular closing tag, it was never opened.
1320
							$parts[$i + 1] = substr($parts[$i + 1], 0, -1);
1321
							$parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
1322
							continue;
1323
						}
1324
					}
1325
					else
1326
					{
1327
						// Get rid of this closing tag!
1328
						$parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
1329
						continue;
1330
					}
1331
1332
					// Inline elements are still left opened?
1333
					if (!empty($inlineElements))
1334
					{
1335
						// Close them first..
1336
						$parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
1337
1338
						// Then reopen them.
1339
						$parts[$i + 5] = '[' . implode('][', array_keys($inlineElements)) . ']' . $parts[$i + 5];
1340
					}
1341
				}
1342
				// Inline tag.
1343
				else
1344
				{
1345
					// Are we expecting this tag to end?
1346
					if (in_array($tag, $inlineElements))
1347
					{
1348
						foreach (array_reverse($inlineElements, true) as $tagContentToBeClosed => $tagToBeClosed)
1349
						{
1350
							// Closing it one way or the other.
1351
							unset($inlineElements[$tagContentToBeClosed]);
1352
1353
							// Was this the tag we were looking for?
1354
							if ($tagToBeClosed === $tag)
1355
								break;
1356
1357
							// Nope, close it and look further!
1358
							else
1359
								$parts[$i] .= '[/' . $tagToBeClosed . ']';
1360
						}
1361
1362
						if ($isCompetingTag && !empty($competingElements[$tag]))
1363
						{
1364
							array_pop($competingElements[$tag]);
1365
1366
							if (count($competingElements[$tag]) > 0)
1367
								$parts[$i + 5] = '[' . $tag . $competingElements[$tag][count($competingElements[$tag]) - 1] . $parts[$i + 5];
1368
						}
1369
					}
1370
1371
					// Unexpected closing tag, ex-ter-mi-nate.
1372
					else
1373
						$parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
1374
				}
1375
			}
1376
		}
1377
1378
		// Close the code tags.
1379
		if ($inCode)
0 ignored issues
show
introduced by
The condition $inCode is always false.
Loading history...
1380
			$parts[$i] .= '[/code]';
1381
1382
		// The same for nobbc tags.
1383
		elseif ($inNoBbc)
0 ignored issues
show
introduced by
The condition $inNoBbc is always false.
Loading history...
1384
			$parts[$i] .= '[/nobbc]';
1385
1386
		// Still inline tags left unclosed? Close them now, better late than never.
1387
		elseif (!empty($inlineElements))
1388
			$parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
1389
1390
		// Now close the block elements.
1391
		if (!empty($blockElements))
1392
			$parts[$i] .= '[/' . implode('][/', array_reverse($blockElements)) . ']';
1393
1394
		$text = implode('', $parts);
1395
	}
1396
1397
	// Final clean up of back to back tags.
1398
	$lastlen = 0;
1399
	while (strlen($text) !== $lastlen)
1400
		$lastlen = strlen($text = preg_replace($backToBackPattern, '', $text));
1401
1402
	return $text;
1403
}
1404
1405
/**
1406
 * Creates the javascript code for localization of the editor (SCEditor)
1407
 */
1408
function loadLocale()
1409
{
1410
	global $context, $txt, $editortxt, $modSettings;
1411
1412
	loadLanguage('Editor');
1413
1414
	$context['template_layers'] = array();
1415
	// Lets make sure we aren't going to output anything nasty.
1416
	@ob_end_clean();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for ob_end_clean(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

1416
	/** @scrutinizer ignore-unhandled */ @ob_end_clean();

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

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

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

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

1418
		/** @scrutinizer ignore-unhandled */ @ob_start('ob_gzhandler');

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

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

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1419
	else
1420
		@ob_start();
1421
1422
	// If we don't have any locale better avoid broken js
1423
	if (empty($txt['lang_locale']))
1424
		die();
0 ignored issues
show
Best Practice introduced by
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...
1425
1426
	$file_data = '(function ($) {
1427
	\'use strict\';
1428
1429
	$.sceditor.locale[' . JavaScriptEscape($txt['lang_locale']) . '] = {';
1430
	foreach ($editortxt as $key => $val)
1431
		$file_data .= '
1432
		' . JavaScriptEscape($key) . ': ' . JavaScriptEscape($val) . ',';
1433
1434
	$file_data .= '
1435
		dateFormat: "day.month.year"
1436
	}
1437
})(jQuery);';
1438
1439
	// Make sure they know what type of file we are.
1440
	header('content-type: text/javascript');
1441
	echo $file_data;
1442
	obExit(false);
1443
}
1444
1445
/**
1446
 * Retrieves a list of message icons.
1447
 * - Based on the settings, the array will either contain a list of default
1448
 *   message icons or a list of custom message icons retrieved from the database.
1449
 * - The board_id is needed for the custom message icons (which can be set for
1450
 *   each board individually).
1451
 *
1452
 * @param int $board_id The ID of the board
1453
 * @return array An array of info about available icons
1454
 */
1455
function getMessageIcons($board_id)
1456
{
1457
	global $modSettings, $txt, $settings, $smcFunc;
1458
1459
	if (empty($modSettings['messageIcons_enable']))
1460
	{
1461
		loadLanguage('Post');
1462
1463
		$icons = array(
1464
			array('value' => 'xx', 'name' => $txt['standard']),
1465
			array('value' => 'thumbup', 'name' => $txt['thumbs_up']),
1466
			array('value' => 'thumbdown', 'name' => $txt['thumbs_down']),
1467
			array('value' => 'exclamation', 'name' => $txt['exclamation_point']),
1468
			array('value' => 'question', 'name' => $txt['question_mark']),
1469
			array('value' => 'lamp', 'name' => $txt['lamp']),
1470
			array('value' => 'smiley', 'name' => $txt['icon_smiley']),
1471
			array('value' => 'angry', 'name' => $txt['icon_angry']),
1472
			array('value' => 'cheesy', 'name' => $txt['icon_cheesy']),
1473
			array('value' => 'grin', 'name' => $txt['icon_grin']),
1474
			array('value' => 'sad', 'name' => $txt['icon_sad']),
1475
			array('value' => 'wink', 'name' => $txt['icon_wink']),
1476
			array('value' => 'poll', 'name' => $txt['icon_poll']),
1477
		);
1478
1479
		foreach ($icons as $k => $dummy)
1480
		{
1481
			$icons[$k]['url'] = $settings['images_url'] . '/post/' . $dummy['value'] . '.png';
1482
			$icons[$k]['is_last'] = false;
1483
		}
1484
	}
1485
	// Otherwise load the icons, and check we give the right image too...
1486
	else
1487
	{
1488
		if (($temp = cache_get_data('posting_icons-' . $board_id, 480)) == null)
1489
		{
1490
			$request = $smcFunc['db_query']('', '
1491
				SELECT title, filename
1492
				FROM {db_prefix}message_icons
1493
				WHERE id_board IN (0, {int:board_id})
1494
				ORDER BY icon_order',
1495
				array(
1496
					'board_id' => $board_id,
1497
				)
1498
			);
1499
			$icon_data = array();
1500
			while ($row = $smcFunc['db_fetch_assoc']($request))
1501
				$icon_data[] = $row;
1502
			$smcFunc['db_free_result']($request);
1503
1504
			$icons = array();
1505
			foreach ($icon_data as $icon)
1506
			{
1507
				$icons[$icon['filename']] = array(
1508
					'value' => $icon['filename'],
1509
					'name' => $icon['title'],
1510
					'url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $icon['filename'] . '.png') ? 'images_url' : 'default_images_url'] . '/post/' . $icon['filename'] . '.png',
1511
					'is_last' => false,
1512
				);
1513
			}
1514
1515
			cache_put_data('posting_icons-' . $board_id, $icons, 480);
1516
		}
1517
		else
1518
			$icons = $temp;
1519
	}
1520
	call_integration_hook('integrate_load_message_icons', array(&$icons));
1521
1522
	return array_values($icons);
1523
}
1524
1525
/**
1526
 * Creates a box that can be used for richedit stuff like BBC, Smileys etc.
1527
 * @param array $editorOptions Various options for the editor
1528
 */
1529
function create_control_richedit($editorOptions)
1530
{
1531
	global $txt, $modSettings, $options, $smcFunc, $editortxt;
1532
	global $context, $settings, $user_info, $scripturl;
1533
1534
	// Load the Post language file... for the moment at least.
1535
	loadLanguage('Post');
1536
	loadLanguage('Editor');
1537
1538
	// Every control must have a ID!
1539
	assert(isset($editorOptions['id']));
1540
	assert(isset($editorOptions['value']));
1541
1542
	// Is this the first richedit - if so we need to ensure some template stuff is initialised.
1543
	if (empty($context['controls']['richedit']))
1544
	{
1545
		// Some general stuff.
1546
		$settings['smileys_url'] = $modSettings['smileys_url'] . '/' . $user_info['smiley_set'];
1547
		if (!empty($context['drafts_autosave']))
1548
			$context['drafts_autosave_frequency'] = empty($modSettings['drafts_autosave_frequency']) ? 60000 : $modSettings['drafts_autosave_frequency'] * 1000;
1549
1550
		// This really has some WYSIWYG stuff.
1551
		loadCSSFile('jquery.sceditor.css', array('force_current' => false, 'validate' => true), 'smf_jquery_sceditor');
1552
		loadTemplate('GenericControls');
1553
1554
		// JS makes the editor go round
1555
		loadJavaScriptFile('editor.js', array('minimize' => true), 'smf_editor');
1556
		loadJavaScriptFile('jquery.sceditor.bbcode.min.js', array(), 'smf_sceditor_bbcode');
1557
		loadJavaScriptFile('jquery.sceditor.smf.js', array('minimize' => true), 'smf_sceditor_smf');
1558
		addInlineJavaScript('
1559
		var smf_smileys_url = \'' . $settings['smileys_url'] . '\';
1560
		var bbc_quote_from = \'' . addcslashes($txt['quote_from'], "'") . '\';
1561
		var bbc_quote = \'' . addcslashes($txt['quote'], "'") . '\';
1562
		var bbc_search_on = \'' . addcslashes($txt['search_on'], "'") . '\';');
1563
		// editor language file
1564
		if (!empty($txt['lang_locale']) && $txt['lang_locale'] != 'en_US')
1565
			loadJavaScriptFile($scripturl . '?action=loadeditorlocale', array('external' => true), 'sceditor_language');
1566
1567
		$context['shortcuts_text'] = $txt['shortcuts' . (!empty($context['drafts_save']) ? '_drafts' : '') . (stripos($_SERVER['HTTP_USER_AGENT'], 'Macintosh') !== false ? '_mac' : (isBrowser('is_firefox') ? '_firefox' : ''))];
1568
		$context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && (function_exists('pspell_new') || (function_exists('enchant_broker_init') && ($txt['lang_character_set'] == 'UTF-8' || function_exists('iconv'))));
1569
		if ($context['show_spellchecking'])
1570
		{
1571
			loadJavaScriptFile('spellcheck.js', array('minimize' => true), 'smf_spellcheck');
1572
1573
			// Some hidden information is needed in order to make the spell checking work.
1574
			if (!isset($_REQUEST['xml']))
1575
				$context['insert_after_template'] .= '
1576
		<form name="spell_form" id="spell_form" method="post" accept-charset="' . $context['character_set'] . '" target="spellWindow" action="' . $scripturl . '?action=spellcheck">
1577
			<input type="hidden" name="spellstring" value="">
1578
		</form>';
1579
		}
1580
	}
1581
1582
	// Start off the editor...
1583
	$context['controls']['richedit'][$editorOptions['id']] = array(
1584
		'id' => $editorOptions['id'],
1585
		'value' => $editorOptions['value'],
1586
		'rich_value' => $editorOptions['value'], // 2.0 editor compatibility
1587
		'rich_active' => empty($modSettings['disable_wysiwyg']) && (!empty($options['wysiwyg_default']) || !empty($editorOptions['force_rich']) || !empty($_REQUEST[$editorOptions['id'] . '_mode'])),
1588
		'disable_smiley_box' => !empty($editorOptions['disable_smiley_box']),
1589
		'columns' => isset($editorOptions['columns']) ? $editorOptions['columns'] : 60,
1590
		'rows' => isset($editorOptions['rows']) ? $editorOptions['rows'] : 18,
1591
		'width' => isset($editorOptions['width']) ? $editorOptions['width'] : '70%',
1592
		'height' => isset($editorOptions['height']) ? $editorOptions['height'] : '250px',
1593
		'form' => isset($editorOptions['form']) ? $editorOptions['form'] : 'postmodify',
1594
		'bbc_level' => !empty($editorOptions['bbc_level']) ? $editorOptions['bbc_level'] : 'full',
1595
		'preview_type' => isset($editorOptions['preview_type']) ? (int) $editorOptions['preview_type'] : 1,
1596
		'labels' => !empty($editorOptions['labels']) ? $editorOptions['labels'] : array(),
1597
		'locale' => !empty($txt['lang_locale']) && substr($txt['lang_locale'], 0, 5) != 'en_US' ? $txt['lang_locale'] : '',
1598
		'required' => !empty($editorOptions['required']),
1599
	);
1600
1601
	if (empty($context['bbc_tags']))
1602
	{
1603
		// The below array makes it dead easy to add images to this control. Add it to the array and everything else is done for you!
1604
		// Note: 'before' and 'after' are deprecated as of SMF 2.1. Instead, use a separate JS file to configure the functionality of your toolbar buttons.
1605
		/*
1606
			array(
1607
				'code' => 'b', // Required
1608
				'description' => $editortxt['bold'], // Required
1609
				'image' => 'bold', // Optional
1610
				'before' => '[b]', // Deprecated
1611
				'after' => '[/b]', // Deprecated
1612
			),
1613
		*/
1614
		$context['bbc_tags'] = array();
1615
		$context['bbc_tags'][] = array(
1616
			array(
1617
				'code' => 'bold',
1618
				'description' => $editortxt['Bold'],
1619
			),
1620
			array(
1621
				'code' => 'italic',
1622
				'description' => $editortxt['Italic'],
1623
			),
1624
			array(
1625
				'code' => 'underline',
1626
				'description' => $editortxt['Underline']
1627
			),
1628
			array(
1629
				'code' => 'strike',
1630
				'description' => $editortxt['Strikethrough']
1631
			),
1632
			array(
1633
				'code' => 'superscript',
1634
				'description' => $editortxt['Superscript']
1635
			),
1636
			array(
1637
				'code' => 'subscript',
1638
				'description' => $editortxt['Subscript']
1639
			),
1640
			array(),
1641
			array(
1642
				'code' => 'pre',
1643
				'description' => $editortxt['Preformatted Text']
1644
			),
1645
			array(
1646
				'code' => 'left',
1647
				'description' => $editortxt['Align left']
1648
			),
1649
			array(
1650
				'code' => 'center',
1651
				'description' => $editortxt['Center']
1652
			),
1653
			array(
1654
				'code' => 'right',
1655
				'description' => $editortxt['Align right']
1656
			),
1657
			array(
1658
				'code' => 'justify',
1659
				'description' => $editortxt['Justify']
1660
			),
1661
			array(),
1662
			array(
1663
				'code' => 'font',
1664
				'description' => $editortxt['Font Name']
1665
			),
1666
			array(
1667
				'code' => 'size',
1668
				'description' => $editortxt['Font Size']
1669
			),
1670
			array(
1671
				'code' => 'color',
1672
				'description' => $editortxt['Font Color']
1673
			),
1674
		);
1675
		if (empty($modSettings['disable_wysiwyg']))
1676
		{
1677
			$context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
1678
				'code' => 'removeformat',
1679
				'description' => $editortxt['Remove Formatting'],
1680
			);
1681
		}
1682
		$context['bbc_tags'][] = array(
1683
			array(
1684
				'code' => 'floatleft',
1685
				'description' => $editortxt['Float left']
1686
			),
1687
			array(
1688
				'code' => 'floatright',
1689
				'description' => $editortxt['Float right']
1690
			),
1691
			array(),
1692
			array(
1693
				'code' => 'flash',
1694
				'description' => $editortxt['Flash']
1695
			),
1696
			array(
1697
				'code' => 'youtube',
1698
				'description' => $editortxt['Insert a YouTube video']
1699
			),
1700
			array(
1701
				'code' => 'image',
1702
				'description' => $editortxt['Insert an image']
1703
			),
1704
			array(
1705
				'code' => 'link',
1706
				'description' => $editortxt['Insert a link']
1707
			),
1708
			array(
1709
				'code' => 'email',
1710
				'description' => $editortxt['Insert an email']
1711
			),
1712
			array(),
1713
			array(
1714
				'code' => 'table',
1715
				'description' => $editortxt['Insert a table']
1716
			),
1717
			array(
1718
				'code' => 'code',
1719
				'description' => $editortxt['Code']
1720
			),
1721
			array(
1722
				'code' => 'quote',
1723
				'description' => $editortxt['Insert a Quote']
1724
			),
1725
			array(),
1726
			array(
1727
				'code' => 'bulletlist',
1728
				'description' => $editortxt['Bullet list']
1729
			),
1730
			array(
1731
				'code' => 'orderedlist',
1732
				'description' => $editortxt['Numbered list']
1733
			),
1734
			array(
1735
				'code' => 'horizontalrule',
1736
				'description' => $editortxt['Insert a horizontal rule']
1737
			),
1738
			array(),
1739
			array(
1740
				'code' => 'maximize',
1741
				'description' => $editortxt['Maximize']
1742
			),
1743
		);
1744
		if (empty($modSettings['disable_wysiwyg']))
1745
		{
1746
			$context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
1747
				'code' => 'source',
1748
				'description' => $editortxt['View source'],
1749
			);
1750
		}
1751
1752
		$editor_tag_map = array(
1753
			'b' => 'bold',
1754
			'i' => 'italic',
1755
			'u' => 'underline',
1756
			's' => 'strike',
1757
			'img' => 'image',
1758
			'url' => 'link',
1759
			'sup' => 'superscript',
1760
			'sub' => 'subscript',
1761
			'hr' => 'horizontalrule',
1762
		);
1763
1764
		// Allow mods to modify BBC buttons.
1765
		// Note: passing the array here is not necessary and is deprecated, but it is kept for backward compatibility with 2.0
1766
		call_integration_hook('integrate_bbc_buttons', array(&$context['bbc_tags'], &$editor_tag_map));
1767
1768
		// Generate a list of buttons that shouldn't be shown - this should be the fastest way to do this.
1769
		$disabled_tags = array();
1770
		if (!empty($modSettings['disabledBBC']))
1771
			$disabled_tags = explode(',', $modSettings['disabledBBC']);
1772
1773
		foreach ($disabled_tags as $tag)
1774
		{
1775
			$tag = trim($tag);
1776
1777
			if ($tag === 'list')
1778
			{
1779
				$context['disabled_tags']['bulletlist'] = true;
1780
				$context['disabled_tags']['orderedlist'] = true;
1781
			}
1782
1783
			foreach ($editor_tag_map as $thisTag => $tagNameBBC)
1784
				if ($tag === $thisTag)
1785
					$context['disabled_tags'][$tagNameBBC] = true;
1786
1787
			$context['disabled_tags'][$tag] = true;
1788
		}
1789
1790
		$bbcodes_styles = '';
1791
		$context['bbcodes_handlers'] = '';
1792
		$context['bbc_toolbar'] = array();
1793
1794
		foreach ($context['bbc_tags'] as $row => $tagRow)
1795
		{
1796
			if (!isset($context['bbc_toolbar'][$row]))
1797
				$context['bbc_toolbar'][$row] = array();
1798
1799
			$tagsRow = array();
1800
1801
			foreach ($tagRow as $tag)
1802
			{
1803
				if ((!empty($tag['code'])) && empty($context['disabled_tags'][$tag['code']]))
1804
				{
1805
					$tagsRow[] = $tag['code'];
1806
1807
					// If we have a custom button image, set it now.
1808
					if (isset($tag['image']))
1809
					{
1810
						$bbcodes_styles .= '
1811
						.sceditor-button-' . $tag['code'] . ' div {
1812
							background: url(\'' . $settings['default_theme_url'] . '/images/bbc/' . $tag['image'] . '.png\');
1813
						}';
1814
					}
1815
1816
					// Set the tooltip and possibly the command info
1817
					$context['bbcodes_handlers'] .= '
1818
						sceditor.command.set(' . JavaScriptEscape($tag['code']) . ', {
1819
							tooltip: ' . JavaScriptEscape(isset($tag['description']) ? $tag['description'] : $tag['code']);
1820
1821
					// Legacy support for 2.0 BBC mods
1822
					if (isset($tag['before']))
1823
					{
1824
						$context['bbcodes_handlers'] .= ',
1825
							exec: function () {
1826
								this.insert(' . JavaScriptEscape($tag['before']) . (isset($tag['after']) ? ', ' . JavaScriptEscape($tag['after']) : '') . ');
1827
							},
1828
							txtExec: [' . JavaScriptEscape($tag['before']) . (isset($tag['after']) ? ', ' . JavaScriptEscape($tag['after']) : '') . ']';
1829
					}
1830
1831
					$context['bbcodes_handlers'] .= '
1832
						});';
1833
				}
1834
				else
1835
				{
1836
					$context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
1837
					$tagsRow = array();
1838
				}
1839
			}
1840
1841
			if (!empty($tagsRow))
1842
				$context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
1843
		}
1844
1845
		if (!empty($bbcodes_styles))
1846
			addInlineCss($bbcodes_styles);
1847
	}
1848
1849
	// Initialize smiley array... if not loaded before.
1850
	if (empty($context['smileys']) && empty($editorOptions['disable_smiley_box']))
1851
	{
1852
		$context['smileys'] = array(
1853
			'postform' => array(),
1854
			'popup' => array(),
1855
		);
1856
1857
		// Load smileys - don't bother to run a query if we're not using the database's ones anyhow.
1858
		if (empty($modSettings['smiley_enable']) && $user_info['smiley_set'] != 'none')
1859
			$context['smileys']['postform'][] = array(
1860
				'smileys' => array(
1861
					array(
1862
						'code' => ':)',
1863
						'filename' => 'smiley',
1864
						'description' => $txt['icon_smiley'],
1865
					),
1866
					array(
1867
						'code' => ';)',
1868
						'filename' => 'wink',
1869
						'description' => $txt['icon_wink'],
1870
					),
1871
					array(
1872
						'code' => ':D',
1873
						'filename' => 'cheesy',
1874
						'description' => $txt['icon_cheesy'],
1875
					),
1876
					array(
1877
						'code' => ';D',
1878
						'filename' => 'grin',
1879
						'description' => $txt['icon_grin']
1880
					),
1881
					array(
1882
						'code' => '>:(',
1883
						'filename' => 'angry',
1884
						'description' => $txt['icon_angry'],
1885
					),
1886
					array(
1887
						'code' => ':(',
1888
						'filename' => 'sad',
1889
						'description' => $txt['icon_sad'],
1890
					),
1891
					array(
1892
						'code' => ':o',
1893
						'filename' => 'shocked',
1894
						'description' => $txt['icon_shocked'],
1895
					),
1896
					array(
1897
						'code' => '8)',
1898
						'filename' => 'cool',
1899
						'description' => $txt['icon_cool'],
1900
					),
1901
					array(
1902
						'code' => '???',
1903
						'filename' => 'huh',
1904
						'description' => $txt['icon_huh'],
1905
					),
1906
					array(
1907
						'code' => '::)',
1908
						'filename' => 'rolleyes',
1909
						'description' => $txt['icon_rolleyes'],
1910
					),
1911
					array(
1912
						'code' => ':P',
1913
						'filename' => 'tongue',
1914
						'description' => $txt['icon_tongue'],
1915
					),
1916
					array(
1917
						'code' => ':-[',
1918
						'filename' => 'embarrassed',
1919
						'description' => $txt['icon_embarrassed'],
1920
					),
1921
					array(
1922
						'code' => ':-X',
1923
						'filename' => 'lipsrsealed',
1924
						'description' => $txt['icon_lips'],
1925
					),
1926
					array(
1927
						'code' => ':-\\',
1928
						'filename' => 'undecided',
1929
						'description' => $txt['icon_undecided'],
1930
					),
1931
					array(
1932
						'code' => ':-*',
1933
						'filename' => 'kiss',
1934
						'description' => $txt['icon_kiss'],
1935
					),
1936
					array(
1937
						'code' => ':\'(',
1938
						'filename' => 'cry',
1939
						'description' => $txt['icon_cry'],
1940
						'isLast' => true,
1941
					),
1942
				),
1943
				'isLast' => true,
1944
			);
1945
		elseif ($user_info['smiley_set'] != 'none')
1946
		{
1947
			if (($temp = cache_get_data('posting_smileys', 480)) == null)
1948
			{
1949
				$request = $smcFunc['db_query']('', '
1950
					SELECT code, filename, description, smiley_row, hidden
1951
					FROM {db_prefix}smileys
1952
					WHERE hidden IN (0, 2)
1953
					ORDER BY smiley_row, smiley_order',
1954
					array(
1955
					)
1956
				);
1957
				while ($row = $smcFunc['db_fetch_assoc']($request))
1958
				{
1959
					$row['filename'] = $smcFunc['htmlspecialchars']($row['filename']);
1960
					$row['description'] = $smcFunc['htmlspecialchars']($row['description']);
1961
1962
					$context['smileys'][empty($row['hidden']) ? 'postform' : 'popup'][$row['smiley_row']]['smileys'][] = $row;
1963
				}
1964
				$smcFunc['db_free_result']($request);
1965
1966
				foreach ($context['smileys'] as $section => $smileyRows)
1967
				{
1968
					foreach ($smileyRows as $rowIndex => $smileys)
1969
						$context['smileys'][$section][$rowIndex]['smileys'][count($smileys['smileys']) - 1]['isLast'] = true;
1970
1971
					if (!empty($smileyRows))
1972
						$context['smileys'][$section][count($smileyRows) - 1]['isLast'] = true;
1973
				}
1974
1975
				cache_put_data('posting_smileys', $context['smileys'], 480);
1976
			}
1977
			else
1978
				$context['smileys'] = $temp;
1979
		}
1980
1981
		// Set proper extensions; do this post caching so cache doesn't become extension-specific
1982
		array_walk_recursive($context['smileys'], function (&$filename, $key)
1983
			{
1984
				global $context, $user_info, $modSettings;
1985
				if ($key == 'filename')
1986
					// Need to use the default if user selection is disabled
1987
					if (empty($modSettings['smiley_sets_enable']))
1988
						$filename .= $context['user']['smiley_set_default_ext'];
1989
					else
1990
						$filename .= $user_info['smiley_set_ext'];
1991
1992
			}
1993
		);
1994
	}
1995
1996
	// Set a flag so the sub template knows what to do...
1997
	$context['show_bbc'] = !empty($modSettings['enableBBC']);
1998
1999
	// Set up the SCEditor options
2000
	$sce_options = array(
2001
		'style' => $settings['default_theme_url'] . '/css/jquery.sceditor.default.css',
2002
		'emoticonsCompat' => true,
2003
		'colors' => 'black,maroon,brown,green,navy,grey,red,orange,teal,blue,white,hotpink,yellow,limegreen,purple',
2004
		'format' => 'bbcode',
2005
		'plugins' => '',
2006
		'bbcodeTrim' => true,
2007
	);
2008
	if (!empty($context['controls']['richedit'][$editorOptions['id']]['locale']))
2009
		$sce_options['locale'] = $context['controls']['richedit'][$editorOptions['id']]['locale'];
2010
	if (!empty($context['right_to_left']))
2011
		$sce_options['rtl'] = true;
2012
	if ($editorOptions['id'] != 'quickReply')
2013
		$sce_options['autofocus'] = true;
2014
2015
	$sce_options['emoticons'] = array();
2016
	$sce_options['emoticonsDescriptions'] = array();
2017
	$sce_options['emoticonsEnabled'] = false;
2018
	if ((!empty($context['smileys']['postform']) || !empty($context['smileys']['popup'])) && !$context['controls']['richedit'][$editorOptions['id']]['disable_smiley_box'])
2019
	{
2020
		$sce_options['emoticonsEnabled'] = true;
2021
		$sce_options['emoticons']['dropdown'] = array();
2022
		$sce_options['emoticons']['popup'] = array();
2023
2024
		$countLocations = count($context['smileys']);
2025
		foreach ($context['smileys'] as $location => $smileyRows)
2026
		{
2027
			$countLocations--;
2028
2029
			unset($smiley_location);
2030
			if ($location == 'postform')
2031
				$smiley_location = &$sce_options['emoticons']['dropdown'];
2032
			elseif ($location == 'popup')
2033
				$smiley_location = &$sce_options['emoticons']['popup'];
2034
2035
			$numRows = count($smileyRows);
2036
2037
			// This is needed because otherwise the editor will remove all the duplicate (empty) keys and leave only 1 additional line
2038
			$emptyPlaceholder = 0;
2039
			foreach ($smileyRows as $smileyRow)
2040
			{
2041
				foreach ($smileyRow['smileys'] as $smiley)
2042
				{
2043
					$smiley_location[$smiley['code']] = $settings['smileys_url'] . '/' . $smiley['filename'];
2044
					$sce_options['emoticonsDescriptions'][$smiley['code']] = $smiley['description'];
2045
				}
2046
2047
				if (empty($smileyRow['isLast']) && $numRows != 1)
2048
					$smiley_location['-' . $emptyPlaceholder++] = '';
2049
			}
2050
		}
2051
	}
2052
2053
	$sce_options['toolbar'] = '';
2054
	if ($context['show_bbc'])
2055
	{
2056
		$count_tags = count($context['bbc_tags']);
2057
		foreach ($context['bbc_toolbar'] as $i => $buttonRow)
2058
		{
2059
			$sce_options['toolbar'] .= implode('|', $buttonRow);
2060
2061
			$count_tags--;
2062
2063
			if (!empty($count_tags))
2064
				$sce_options['toolbar'] .= '||';
2065
		}
2066
	}
2067
2068
	// Allow mods to change $sce_options. Usful if, e.g., a mod wants to add an SCEditor plugin.
2069
	call_integration_hook('integrate_sceditor_options', array(&$sce_options));
2070
2071
	$context['controls']['richedit'][$editorOptions['id']]['sce_options'] = $sce_options;
2072
}
2073
2074
/**
2075
 * Create a anti-bot verification control?
2076
 * @param array &$verificationOptions Options for the verification control
2077
 * @param bool $do_test Whether to check to see if the user entered the code correctly
2078
 * @return bool|array False if there's nothing to show, true if everything went well or an array containing error indicators if the test failed
2079
 */
2080
function create_control_verification(&$verificationOptions, $do_test = false)
2081
{
2082
	global $modSettings, $smcFunc;
2083
	global $context, $user_info, $scripturl, $language;
2084
2085
	// First verification means we need to set up some bits...
2086
	if (empty($context['controls']['verification']))
2087
	{
2088
		// The template
2089
		loadTemplate('GenericControls');
2090
2091
		// Some javascript ma'am?
2092
		if (!empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])))
2093
			loadJavaScriptFile('captcha.js', array('minimize' => true), 'smf_captcha');
2094
2095
		$context['use_graphic_library'] = in_array('gd', get_loaded_extensions());
2096
2097
		// Skip I, J, L, O, Q, S and Z.
2098
		$context['standard_captcha_range'] = array_merge(range('A', 'H'), array('K', 'M', 'N', 'P', 'R'), range('T', 'Y'));
2099
	}
2100
2101
	// Always have an ID.
2102
	assert(isset($verificationOptions['id']));
2103
	$isNew = !isset($context['controls']['verification'][$verificationOptions['id']]);
2104
2105
	// Log this into our collection.
2106
	if ($isNew)
2107
		$context['controls']['verification'][$verificationOptions['id']] = array(
2108
			'id' => $verificationOptions['id'],
2109
			'empty_field' => empty($verificationOptions['no_empty_field']),
2110
			'show_visual' => !empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])),
2111
			'number_questions' => isset($verificationOptions['override_qs']) ? $verificationOptions['override_qs'] : (!empty($modSettings['qa_verification_number']) ? $modSettings['qa_verification_number'] : 0),
2112
			'max_errors' => isset($verificationOptions['max_errors']) ? $verificationOptions['max_errors'] : 3,
2113
			'image_href' => $scripturl . '?action=verificationcode;vid=' . $verificationOptions['id'] . ';rand=' . md5(mt_rand()),
2114
			'text_value' => '',
2115
			'questions' => array(),
2116
			'can_recaptcha' => !empty($modSettings['recaptcha_enabled']) && !empty($modSettings['recaptcha_site_key']) && !empty($modSettings['recaptcha_secret_key']),
2117
		);
2118
	$thisVerification = &$context['controls']['verification'][$verificationOptions['id']];
2119
2120
	// Is there actually going to be anything?
2121
	if (empty($thisVerification['show_visual']) && empty($thisVerification['number_questions']) && empty($thisVerification['can_recaptcha']))
2122
		return false;
2123
	elseif (!$isNew && !$do_test)
2124
		return true;
2125
2126
	// Sanitize reCAPTCHA fields?
2127
	if ($thisVerification['can_recaptcha'])
2128
	{
2129
		// Only allow 40 alphanumeric, underscore and dash characters.
2130
		$thisVerification['recaptcha_site_key'] = preg_replace('/(0-9a-zA-Z_){40}/', '$1', $modSettings['recaptcha_site_key']);
2131
2132
		// Light or dark theme...
2133
		$thisVerification['recaptcha_theme'] = preg_replace('/(light|dark)/', '$1', $modSettings['recaptcha_theme']);
2134
	}
2135
2136
	// Add javascript for the object.
2137
	if ($context['controls']['verification'][$verificationOptions['id']]['show_visual'])
2138
		$context['insert_after_template'] .= '
2139
			<script>
2140
				var verification' . $verificationOptions['id'] . 'Handle = new smfCaptcha("' . $thisVerification['image_href'] . '", "' . $verificationOptions['id'] . '", ' . ($context['use_graphic_library'] ? 1 : 0) . ');
2141
			</script>';
2142
2143
	// If we want questions do we have a cache of all the IDs?
2144
	if (!empty($thisVerification['number_questions']) && empty($modSettings['question_id_cache']))
2145
	{
2146
		if (($modSettings['question_id_cache'] = cache_get_data('verificationQuestions', 300)) == null)
2147
		{
2148
			$request = $smcFunc['db_query']('', '
2149
				SELECT id_question, lngfile, question, answers
2150
				FROM {db_prefix}qanda',
2151
				array()
2152
			);
2153
			$modSettings['question_id_cache'] = array(
2154
				'questions' => array(),
2155
				'langs' => array(),
2156
			);
2157
			// This is like Captain Kirk climbing a mountain in some ways. This is L's fault, mkay? :P
2158
			while ($row = $smcFunc['db_fetch_assoc']($request))
2159
			{
2160
				$id_question = $row['id_question'];
2161
				unset ($row['id_question']);
2162
				// Make them all lowercase. We can't directly use $smcFunc['strtolower'] with array_walk, so do it manually, eh?
2163
				$row['answers'] = $smcFunc['json_decode']($row['answers'], true);
2164
				foreach ($row['answers'] as $k => $v)
2165
					$row['answers'][$k] = $smcFunc['strtolower']($v);
2166
2167
				$modSettings['question_id_cache']['questions'][$id_question] = $row;
2168
				$modSettings['question_id_cache']['langs'][$row['lngfile']][] = $id_question;
2169
			}
2170
			$smcFunc['db_free_result']($request);
2171
2172
			cache_put_data('verificationQuestions', $modSettings['question_id_cache'], 300);
2173
		}
2174
	}
2175
2176
	if (!isset($_SESSION[$verificationOptions['id'] . '_vv']))
2177
		$_SESSION[$verificationOptions['id'] . '_vv'] = array();
2178
2179
	// Do we need to refresh the verification?
2180
	if (!$do_test && (!empty($_SESSION[$verificationOptions['id'] . '_vv']['did_pass']) || empty($_SESSION[$verificationOptions['id'] . '_vv']['count']) || $_SESSION[$verificationOptions['id'] . '_vv']['count'] > 3) && empty($verificationOptions['dont_refresh']))
2181
		$force_refresh = true;
2182
	else
2183
		$force_refresh = false;
2184
2185
	// This can also force a fresh, although unlikely.
2186
	if (($thisVerification['show_visual'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['code'])) || ($thisVerification['number_questions'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['q'])))
2187
		$force_refresh = true;
2188
2189
	$verification_errors = array();
2190
	// Start with any testing.
2191
	if ($do_test)
2192
	{
2193
		// This cannot happen!
2194
		if (!isset($_SESSION[$verificationOptions['id'] . '_vv']['count']))
2195
			fatal_lang_error('no_access', false);
2196
		// ... nor this!
2197
		if ($thisVerification['number_questions'] && (!isset($_SESSION[$verificationOptions['id'] . '_vv']['q']) || !isset($_REQUEST[$verificationOptions['id'] . '_vv']['q'])))
2198
			fatal_lang_error('no_access', false);
2199
		// Hmm, it's requested but not actually declared. This shouldn't happen.
2200
		if ($thisVerification['empty_field'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['empty_field']))
2201
			fatal_lang_error('no_access', false);
2202
		// While we're here, did the user do something bad?
2203
		if ($thisVerification['empty_field'] && !empty($_SESSION[$verificationOptions['id'] . '_vv']['empty_field']) && !empty($_REQUEST[$_SESSION[$verificationOptions['id'] . '_vv']['empty_field']]))
2204
			$verification_errors[] = 'wrong_verification_answer';
2205
2206
		if ($thisVerification['can_recaptcha'])
2207
		{
2208
			$reCaptcha = new \ReCaptcha\ReCaptcha($modSettings['recaptcha_secret_key'], new \ReCaptcha\RequestMethod\SocketPost());
0 ignored issues
show
Bug introduced by
The type ReCaptcha\RequestMethod\SocketPost was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type ReCaptcha\ReCaptcha was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
2209
2210
			// Was there a reCAPTCHA response?
2211
			if (isset($_POST['g-recaptcha-response']))
2212
			{
2213
				$resp = $reCaptcha->verify($_POST['g-recaptcha-response'], $user_info['ip']);
2214
2215
				if (!$resp->isSuccess())
2216
					$verification_errors[] = 'wrong_verification_code';
2217
			}
2218
			else
2219
				$verification_errors[] = 'wrong_verification_code';
2220
		}
2221
		if ($thisVerification['show_visual'] && (empty($_REQUEST[$verificationOptions['id'] . '_vv']['code']) || empty($_SESSION[$verificationOptions['id'] . '_vv']['code']) || strtoupper($_REQUEST[$verificationOptions['id'] . '_vv']['code']) !== $_SESSION[$verificationOptions['id'] . '_vv']['code']))
2222
			$verification_errors[] = 'wrong_verification_code';
2223
		if ($thisVerification['number_questions'])
2224
		{
2225
			$incorrectQuestions = array();
2226
			foreach ($_SESSION[$verificationOptions['id'] . '_vv']['q'] as $q)
2227
			{
2228
				// We don't have this question any more, thus no answers.
2229
				if (!isset($modSettings['question_id_cache']['questions'][$q]))
2230
					continue;
2231
				// This is quite complex. We have our question but it might have multiple answers.
2232
				// First, did they actually answer this question?
2233
				if (!isset($_REQUEST[$verificationOptions['id'] . '_vv']['q'][$q]) || trim($_REQUEST[$verificationOptions['id'] . '_vv']['q'][$q]) == '')
2234
				{
2235
					$incorrectQuestions[] = $q;
2236
					continue;
2237
				}
2238
				// Second, is their answer in the list of possible answers?
2239
				else
2240
				{
2241
					$given_answer = trim($smcFunc['htmlspecialchars'](strtolower($_REQUEST[$verificationOptions['id'] . '_vv']['q'][$q])));
2242
					if (!in_array($given_answer, $modSettings['question_id_cache']['questions'][$q]['answers']))
2243
						$incorrectQuestions[] = $q;
2244
				}
2245
			}
2246
2247
			if (!empty($incorrectQuestions))
2248
				$verification_errors[] = 'wrong_verification_answer';
2249
		}
2250
	}
2251
2252
	// Any errors means we refresh potentially.
2253
	if (!empty($verification_errors))
2254
	{
2255
		if (empty($_SESSION[$verificationOptions['id'] . '_vv']['errors']))
2256
			$_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
2257
		// Too many errors?
2258
		elseif ($_SESSION[$verificationOptions['id'] . '_vv']['errors'] > $thisVerification['max_errors'])
2259
			$force_refresh = true;
2260
2261
		// Keep a track of these.
2262
		$_SESSION[$verificationOptions['id'] . '_vv']['errors']++;
2263
	}
2264
2265
	// Are we refreshing then?
2266
	if ($force_refresh)
2267
	{
2268
		// Assume nothing went before.
2269
		$_SESSION[$verificationOptions['id'] . '_vv']['count'] = 0;
2270
		$_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
2271
		$_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = false;
2272
		$_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
2273
		$_SESSION[$verificationOptions['id'] . '_vv']['code'] = '';
2274
2275
		// Make our magic empty field.
2276
		if ($thisVerification['empty_field'])
2277
		{
2278
			// We're building a field that lives in the template, that we hope to be empty later. But at least we give it a believable name.
2279
			$terms = array('gadget', 'device', 'uid', 'gid', 'guid', 'uuid', 'unique', 'identifier');
2280
			$second_terms = array('hash', 'cipher', 'code', 'key', 'unlock', 'bit', 'value');
2281
			$start = mt_rand(0, 27);
2282
			$hash = substr(md5(time()), $start, 4);
2283
			$_SESSION[$verificationOptions['id'] . '_vv']['empty_field'] = $terms[array_rand($terms)] . '-' . $second_terms[array_rand($second_terms)] . '-' . $hash;
2284
		}
2285
2286
		// Generating a new image.
2287
		if ($thisVerification['show_visual'])
2288
		{
2289
			// Are we overriding the range?
2290
			$character_range = !empty($verificationOptions['override_range']) ? $verificationOptions['override_range'] : $context['standard_captcha_range'];
2291
2292
			for ($i = 0; $i < 6; $i++)
2293
				$_SESSION[$verificationOptions['id'] . '_vv']['code'] .= $character_range[array_rand($character_range)];
2294
		}
2295
2296
		// Getting some new questions?
2297
		if ($thisVerification['number_questions'])
2298
		{
2299
			// Attempt to try the current page's language, followed by the user's preference, followed by the site default.
2300
			$possible_langs = array();
2301
			if (isset($_SESSION['language']))
2302
				$possible_langs[] = strtr($_SESSION['language'], array('-utf8' => ''));
2303
			if (!empty($user_info['language']));
2304
			$possible_langs[] = $user_info['language'];
2305
			$possible_langs[] = $language;
2306
2307
			$questionIDs = array();
2308
			foreach ($possible_langs as $lang)
2309
			{
2310
				$lang = strtr($lang, array('-utf8' => ''));
2311
				if (isset($modSettings['question_id_cache']['langs'][$lang]))
2312
				{
2313
					// If we find questions for this, grab the ids from this language's ones, randomize the array and take just the number we need.
2314
					$questionIDs = $modSettings['question_id_cache']['langs'][$lang];
2315
					shuffle($questionIDs);
2316
					$questionIDs = array_slice($questionIDs, 0, $thisVerification['number_questions']);
2317
					break;
2318
				}
2319
			}
2320
		}
2321
	}
2322
	else
2323
	{
2324
		// Same questions as before.
2325
		$questionIDs = !empty($_SESSION[$verificationOptions['id'] . '_vv']['q']) ? $_SESSION[$verificationOptions['id'] . '_vv']['q'] : array();
2326
		$thisVerification['text_value'] = !empty($_REQUEST[$verificationOptions['id'] . '_vv']['code']) ? $smcFunc['htmlspecialchars']($_REQUEST[$verificationOptions['id'] . '_vv']['code']) : '';
2327
	}
2328
2329
	// If we do have an empty field, it would be nice to hide it from legitimate users who shouldn't be populating it anyway.
2330
	if (!empty($_SESSION[$verificationOptions['id'] . '_vv']['empty_field']))
2331
	{
2332
		if (!isset($context['html_headers']))
2333
			$context['html_headers'] = '';
2334
		$context['html_headers'] .= '<style>.vv_special { display:none; }</style>';
2335
	}
2336
2337
	// Have we got some questions to load?
2338
	if (!empty($questionIDs))
2339
	{
2340
		$_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
2341
		foreach ($questionIDs as $q)
2342
		{
2343
			// Bit of a shortcut this.
2344
			$row = &$modSettings['question_id_cache']['questions'][$q];
2345
			$thisVerification['questions'][] = array(
2346
				'id' => $q,
2347
				'q' => parse_bbc($row['question']),
2348
				'is_error' => !empty($incorrectQuestions) && in_array($q, $incorrectQuestions),
2349
				// Remember a previous submission?
2350
				'a' => isset($_REQUEST[$verificationOptions['id'] . '_vv'], $_REQUEST[$verificationOptions['id'] . '_vv']['q'], $_REQUEST[$verificationOptions['id'] . '_vv']['q'][$q]) ? $smcFunc['htmlspecialchars']($_REQUEST[$verificationOptions['id'] . '_vv']['q'][$q]) : '',
2351
			);
2352
			$_SESSION[$verificationOptions['id'] . '_vv']['q'][] = $q;
2353
		}
2354
	}
2355
2356
	$_SESSION[$verificationOptions['id'] . '_vv']['count'] = empty($_SESSION[$verificationOptions['id'] . '_vv']['count']) ? 1 : $_SESSION[$verificationOptions['id'] . '_vv']['count'] + 1;
2357
2358
	// Return errors if we have them.
2359
	if (!empty($verification_errors))
2360
		return $verification_errors;
2361
	// If we had a test that one, make a note.
2362
	elseif ($do_test)
2363
		$_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = true;
2364
2365
	// Say that everything went well chaps.
2366
	return true;
2367
}
2368
2369
/**
2370
 * This keeps track of all registered handling functions for auto suggest functionality and passes execution to them.
2371
 * @param bool $checkRegistered If set to something other than null, checks whether the callback function is registered
2372
 * @return void|bool Returns whether the callback function is registered if $checkRegistered isn't null
2373
 */
2374
function AutoSuggestHandler($checkRegistered = null)
2375
{
2376
	global $smcFunc, $context;
2377
2378
	// These are all registered types.
2379
	$searchTypes = array(
2380
		'member' => 'Member',
2381
		'membergroups' => 'MemberGroups',
2382
		'versions' => 'SMFVersions',
2383
	);
2384
2385
	call_integration_hook('integrate_autosuggest', array(&$searchTypes));
2386
2387
	// If we're just checking the callback function is registered return true or false.
2388
	if ($checkRegistered != null)
2389
		return isset($searchTypes[$checkRegistered]) && function_exists('AutoSuggest_Search_' . $checkRegistered);
0 ignored issues
show
Bug introduced by
Are you sure $checkRegistered of type true can be used in concatenation? ( Ignorable by Annotation )

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

2389
		return isset($searchTypes[$checkRegistered]) && function_exists('AutoSuggest_Search_' . /** @scrutinizer ignore-type */ $checkRegistered);
Loading history...
2390
2391
	checkSession('get');
2392
	loadTemplate('Xml');
2393
2394
	// Any parameters?
2395
	$context['search_param'] = isset($_REQUEST['search_param']) ? $smcFunc['json_decode'](base64_decode($_REQUEST['search_param']), true) : array();
2396
2397
	if (isset($_REQUEST['suggest_type'], $_REQUEST['search']) && isset($searchTypes[$_REQUEST['suggest_type']]))
2398
	{
2399
		$function = 'AutoSuggest_Search_' . $searchTypes[$_REQUEST['suggest_type']];
2400
		$context['sub_template'] = 'generic_xml';
2401
		$context['xml_data'] = $function();
2402
	}
2403
}
2404
2405
/**
2406
 * Search for a member - by real_name or member_name by default.
2407
 *
2408
 * @return array An array of information for displaying the suggestions
2409
 */
2410
function AutoSuggest_Search_Member()
2411
{
2412
	global $user_info, $smcFunc, $context;
2413
2414
	$_REQUEST['search'] = trim($smcFunc['strtolower']($_REQUEST['search'])) . '*';
2415
	$_REQUEST['search'] = strtr($_REQUEST['search'], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_', '&#038;' => '&amp;'));
2416
2417
	// Find the member.
2418
	$request = $smcFunc['db_query']('', '
2419
		SELECT id_member, real_name
2420
		FROM {db_prefix}members
2421
		WHERE {raw:real_name} LIKE {string:search}' . (!empty($context['search_param']['buddies']) ? '
2422
			AND id_member IN ({array_int:buddy_list})' : '') . '
2423
			AND is_activated IN (1, 11)
2424
		LIMIT ' . ($smcFunc['strlen']($_REQUEST['search']) <= 2 ? '100' : '800'),
2425
		array(
2426
			'real_name' => $smcFunc['db_case_sensitive'] ? 'LOWER(real_name)' : 'real_name',
2427
			'buddy_list' => $user_info['buddies'],
2428
			'search' => $_REQUEST['search'],
2429
		)
2430
	);
2431
	$xml_data = array(
2432
		'items' => array(
2433
			'identifier' => 'item',
2434
			'children' => array(),
2435
		),
2436
	);
2437
	while ($row = $smcFunc['db_fetch_assoc']($request))
2438
	{
2439
		$row['real_name'] = strtr($row['real_name'], array('&amp;' => '&#038;', '&lt;' => '&#060;', '&gt;' => '&#062;', '&quot;' => '&#034;'));
2440
2441
		$xml_data['items']['children'][] = array(
2442
			'attributes' => array(
2443
				'id' => $row['id_member'],
2444
			),
2445
			'value' => $row['real_name'],
2446
		);
2447
	}
2448
	$smcFunc['db_free_result']($request);
2449
2450
	return $xml_data;
2451
}
2452
2453
/**
2454
 * Search for a membergroup by name
2455
 *
2456
 * @return array An array of information for displaying the suggestions
2457
 */
2458
function AutoSuggest_Search_MemberGroups()
2459
{
2460
	global $smcFunc;
2461
2462
	$_REQUEST['search'] = trim($smcFunc['strtolower']($_REQUEST['search'])) . '*';
2463
	$_REQUEST['search'] = strtr($_REQUEST['search'], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_', '&#038;' => '&amp;'));
2464
2465
	// Find the group.
2466
	// Only return groups which are not post-based and not "Hidden", but not the "Administrators" or "Moderators" groups.
2467
	$request = $smcFunc['db_query']('', '
2468
		SELECT id_group, group_name
2469
		FROM {db_prefix}membergroups
2470
		WHERE {raw:group_name} LIKE {string:search}
2471
			AND min_posts = {int:min_posts}
2472
			AND id_group NOT IN ({array_int:invalid_groups})
2473
			AND hidden != {int:hidden}
2474
		',
2475
		array(
2476
			'group_name' => $smcFunc['db_case_sensitive'] ? 'LOWER(group_name}' : 'group_name',
2477
			'min_posts' => -1,
2478
			'invalid_groups' => array(1, 3),
2479
			'hidden' => 2,
2480
			'search' => $_REQUEST['search'],
2481
		)
2482
	);
2483
	$xml_data = array(
2484
		'items' => array(
2485
			'identifier' => 'item',
2486
			'children' => array(),
2487
		),
2488
	);
2489
	while ($row = $smcFunc['db_fetch_assoc']($request))
2490
	{
2491
		$row['group_name'] = strtr($row['group_name'], array('&amp;' => '&#038;', '&lt;' => '&#060;', '&gt;' => '&#062;', '&quot;' => '&#034;'));
2492
2493
		$xml_data['items']['children'][] = array(
2494
			'attributes' => array(
2495
				'id' => $row['id_group'],
2496
			),
2497
			'value' => $row['group_name'],
2498
		);
2499
	}
2500
	$smcFunc['db_free_result']($request);
2501
2502
	return $xml_data;
2503
}
2504
2505
/**
2506
 * Provides a list of possible SMF versions to use in emulation
2507
 *
2508
 * @return array An array of data for displaying the suggestions
2509
 */
2510
function AutoSuggest_Search_SMFVersions()
2511
{
2512
	global $smcFunc;
2513
2514
	$xml_data = array(
2515
		'items' => array(
2516
			'identifier' => 'item',
2517
			'children' => array(),
2518
		),
2519
	);
2520
2521
	// First try and get it from the database.
2522
	$versions = array();
2523
	$request = $smcFunc['db_query']('', '
2524
		SELECT data
2525
		FROM {db_prefix}admin_info_files
2526
		WHERE filename = {string:latest_versions}
2527
			AND path = {string:path}',
2528
		array(
2529
			'latest_versions' => 'latest-versions.txt',
2530
			'path' => '/smf/',
2531
		)
2532
	);
2533
	if (($smcFunc['db_num_rows']($request) > 0) && ($row = $smcFunc['db_fetch_assoc']($request)) && !empty($row['data']))
2534
	{
2535
		// The file can be either Windows or Linux line endings, but let's ensure we clean it as best we can.
2536
		$possible_versions = explode("\n", $row['data']);
2537
		foreach ($possible_versions as $ver)
2538
		{
2539
			$ver = trim($ver);
2540
			if (strpos($ver, 'SMF') === 0)
2541
				$versions[] = $ver;
2542
		}
2543
	}
2544
	$smcFunc['db_free_result']($request);
2545
2546
	// Just in case we don't have ANYthing.
2547
	if (empty($versions))
2548
		$versions = array('SMF 2.0');
2549
2550
	foreach ($versions as $id => $version)
2551
		if (strpos($version, strtoupper($_REQUEST['search'])) !== false)
2552
			$xml_data['items']['children'][] = array(
2553
				'attributes' => array(
2554
					'id' => $id,
2555
				),
2556
				'value' => $version,
2557
			);
2558
2559
	return $xml_data;
2560
}
2561
2562
?>