~azzar1/unity/add-show-desktop-key

443 by dcoles
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0
1
<?php
2
/**
3
*
4
* @package phpBB3
5
* @version $Id: bbcode.php,v 1.114 2007/10/07 10:34:45 naderman Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
if (!defined('IN_PHPBB'))
15
{
16
	exit;
17
}
18
19
/**
20
* BBCode class
21
* @package phpBB3
22
*/
23
class bbcode
24
{
25
	var $bbcode_uid = '';
26
	var $bbcode_bitfield = '';
27
	var $bbcode_cache = array();
28
	var $bbcode_template = array();
29
30
	var $bbcodes = array();
31
32
	var $template_bitfield;
33
	var $template_filename = '';
34
35
	/**
36
	* Constructor
37
	* Init bbcode cache entries if bitfield is specified
38
	*/
39
	function bbcode($bitfield = '')
40
	{
41
		if ($bitfield)
42
		{
43
			$this->bbcode_bitfield = $bitfield;
44
			$this->bbcode_cache_init();
45
		}
46
	}
47
48
	/**
49
	* Second pass bbcodes
50
	*/
51
	function bbcode_second_pass(&$message, $bbcode_uid = '', $bbcode_bitfield = false)
52
	{
53
		if ($bbcode_uid)
54
		{
55
			$this->bbcode_uid = $bbcode_uid;
56
		}
57
58
		if ($bbcode_bitfield !== false)
59
		{
60
			$this->bbcode_bitfield = $bbcode_bitfield;
61
62
			// Init those added with a new bbcode_bitfield (already stored codes will not get parsed again)
63
			$this->bbcode_cache_init();
64
		}
65
66
		if (!$this->bbcode_bitfield)
67
		{
68
			// Remove the uid from tags that have not been transformed into HTML
69
			if ($this->bbcode_uid)
70
			{
71
				$message = str_replace(':' . $this->bbcode_uid, '', $message);
72
			}
73
74
			return;
75
		}
76
77
		$str = array('search' => array(), 'replace' => array());
78
		$preg = array('search' => array(), 'replace' => array());
79
80
		$bitfield = new bitfield($this->bbcode_bitfield);
81
		$bbcodes_set = $bitfield->get_all_set();
82
83
		$undid_bbcode_specialchars = false;
84
		foreach ($bbcodes_set as $bbcode_id)
85
		{
86
			if (!empty($this->bbcode_cache[$bbcode_id]))
87
			{
88
				foreach ($this->bbcode_cache[$bbcode_id] as $type => $array)
89
				{
90
					foreach ($array as $search => $replace)
91
					{
92
						${$type}['search'][] = str_replace('$uid', $this->bbcode_uid, $search);
93
						${$type}['replace'][] = $replace;
94
					}
95
96
					if (sizeof($str['search']))
97
					{
98
						$message = str_replace($str['search'], $str['replace'], $message);
99
						$str = array('search' => array(), 'replace' => array());
100
					}
101
102
					if (sizeof($preg['search']))
103
					{
104
						// we need to turn the entities back into their original form to allow the
105
						// search patterns to work properly
106
						if (!$undid_bbcode_specialchars)
107
						{
108
							$message = str_replace(array('&#58;', '&#46;'), array(':', '.'), $message);
109
							$undid_bbcode_specialchars = true;
110
						}
111
112
						$message = preg_replace($preg['search'], $preg['replace'], $message);
113
						$preg = array('search' => array(), 'replace' => array());
114
					}
115
				}
116
			}
117
		}
118
119
		// Remove the uid from tags that have not been transformed into HTML
120
		$message = str_replace(':' . $this->bbcode_uid, '', $message);
121
	}
122
123
	/**
124
	* Init bbcode cache
125
	*
126
	* requires: $this->bbcode_bitfield
127
	* sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
128
	*/
129
	function bbcode_cache_init()
130
	{
131
		global $user, $phpbb_root_path;
132
133
		if (empty($this->template_filename))
134
		{
135
			$this->template_bitfield = new bitfield($user->theme['bbcode_bitfield']);
136
			$this->template_filename = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/bbcode.html';
137
138
			if (!@file_exists($this->template_filename))
139
			{
140
				trigger_error('The file ' . $this->template_filename . ' is missing.', E_USER_ERROR);
141
			}
142
		}
143
144
		$bbcode_ids = $rowset = $sql = array();
145
146
		$bitfield = new bitfield($this->bbcode_bitfield);
147
		$bbcodes_set = $bitfield->get_all_set();
148
149
		foreach ($bbcodes_set as $bbcode_id)
150
		{
151
			if (isset($this->bbcode_cache[$bbcode_id]))
152
			{
153
				// do not try to re-cache it if it's already in
154
				continue;
155
			}
156
			$bbcode_ids[] = $bbcode_id;
157
158
			if ($bbcode_id > NUM_CORE_BBCODES)
159
			{
160
				$sql[] = $bbcode_id;
161
			}
162
		}
163
164
		if (sizeof($sql))
165
		{
166
			global $db;
167
168
			$sql = 'SELECT *
169
				FROM ' . BBCODES_TABLE . '
170
				WHERE ' . $db->sql_in_set('bbcode_id', $sql);
171
			$result = $db->sql_query($sql, 3600);
172
173
			while ($row = $db->sql_fetchrow($result))
174
			{
175
				// To circumvent replacing newlines with <br /> for the generated html,
176
				// we use carriage returns here. They are later changed back to newlines
177
				$row['bbcode_tpl'] = str_replace("\n", "\r", $row['bbcode_tpl']);
178
				$row['second_pass_replace'] = str_replace("\n", "\r", $row['second_pass_replace']);
179
180
				$rowset[$row['bbcode_id']] = $row;
181
			}
182
			$db->sql_freeresult($result);
183
		}
184
185
		foreach ($bbcode_ids as $bbcode_id)
186
		{
187
			switch ($bbcode_id)
188
			{
189
				case 0:
190
					$this->bbcode_cache[$bbcode_id] = array(
191
						'str' => array(
192
							'[/quote:$uid]'	=> $this->bbcode_tpl('quote_close', $bbcode_id)
193
						),
194
						'preg' => array(
195
							'#\[quote(?:=&quot;(.*?)&quot;)?:$uid\]((?!\[quote(?:=&quot;.*?&quot;)?:$uid\]).)?#ise'	=> "\$this->bbcode_second_pass_quote('\$1', '\$2')"
196
						)
197
					);
198
				break;
199
200
				case 1:
201
					$this->bbcode_cache[$bbcode_id] = array(
202
						'str' => array(
203
							'[b:$uid]'	=> $this->bbcode_tpl('b_open', $bbcode_id),
204
							'[/b:$uid]'	=> $this->bbcode_tpl('b_close', $bbcode_id),
205
						)
206
					);
207
				break;
208
209
				case 2:
210
					$this->bbcode_cache[$bbcode_id] = array(
211
						'str' => array(
212
							'[i:$uid]'	=> $this->bbcode_tpl('i_open', $bbcode_id),
213
							'[/i:$uid]'	=> $this->bbcode_tpl('i_close', $bbcode_id),
214
						)
215
					);
216
				break;
217
218
				case 3:
219
					$this->bbcode_cache[$bbcode_id] = array(
220
						'preg' => array(
221
							'#\[url:$uid\]((.*?))\[/url:$uid\]#s'			=> $this->bbcode_tpl('url', $bbcode_id),
222
							'#\[url=([^\[]+?):$uid\](.*?)\[/url:$uid\]#s'	=> $this->bbcode_tpl('url', $bbcode_id),
223
						)
224
					);
225
				break;
226
227
				case 4:
228
					if ($user->optionget('viewimg'))
229
					{
230
						$this->bbcode_cache[$bbcode_id] = array(
231
							'preg' => array(
232
								'#\[img:$uid\](.*?)\[/img:$uid\]#s'		=> $this->bbcode_tpl('img', $bbcode_id),
233
							)
234
						);
235
					}
236
					else
237
					{
238
						$this->bbcode_cache[$bbcode_id] = array(
239
							'preg' => array(
240
								'#\[img:$uid\](.*?)\[/img:$uid\]#s'		=> str_replace('$2', '[ img ]', $this->bbcode_tpl('url', $bbcode_id, true)),
241
							)
242
						);
243
					}
244
				break;
245
246
				case 5:
247
					$this->bbcode_cache[$bbcode_id] = array(
248
						'preg' => array(
249
							'#\[size=([\-\+]?\d+):$uid\](.*?)\[/size:$uid\]#s'	=> $this->bbcode_tpl('size', $bbcode_id),
250
						)
251
					);
252
				break;
253
254
				case 6:
255
					$this->bbcode_cache[$bbcode_id] = array(
256
						'preg' => array(
257
							'!\[color=(#[0-9a-f]{6}|[a-z\-]+):$uid\](.*?)\[/color:$uid\]!is'	=> $this->bbcode_tpl('color', $bbcode_id),
258
						)
259
					);
260
				break;
261
262
				case 7:
263
					$this->bbcode_cache[$bbcode_id] = array(
264
						'str' => array(
265
							'[u:$uid]'	=> $this->bbcode_tpl('u_open', $bbcode_id),
266
							'[/u:$uid]'	=> $this->bbcode_tpl('u_close', $bbcode_id),
267
						)
268
					);
269
				break;
270
271
				case 8:
272
					$this->bbcode_cache[$bbcode_id] = array(
273
						'preg' => array(
274
							'#\[code(?:=([a-z]+))?:$uid\](.*?)\[/code:$uid\]#ise'	=> "\$this->bbcode_second_pass_code('\$1', '\$2')",
275
						)
276
					);
277
				break;
278
279
				case 9:
280
					$this->bbcode_cache[$bbcode_id] = array(
281
						'preg' => array(
282
							'#(\[\/?(list|\*):[mou]?:?$uid\])[\n]{1}#'	=> "\$1",
283
							'#(\[list=([^\[]+):$uid\])[\n]{1}#'			=> "\$1",
284
							'#\[list=([^\[]+):$uid\]#e'					=> "\$this->bbcode_list('\$1')",
285
						),
286
						'str' => array(
287
							'[list:$uid]'		=> $this->bbcode_tpl('ulist_open_default', $bbcode_id),
288
							'[/list:u:$uid]'	=> $this->bbcode_tpl('ulist_close', $bbcode_id),
289
							'[/list:o:$uid]'	=> $this->bbcode_tpl('olist_close', $bbcode_id),
290
							'[*:$uid]'			=> $this->bbcode_tpl('listitem', $bbcode_id),
291
							'[/*:$uid]'			=> $this->bbcode_tpl('listitem_close', $bbcode_id),
292
							'[/*:m:$uid]'		=> $this->bbcode_tpl('listitem_close', $bbcode_id)
293
						),
294
					);
295
				break;
296
297
				case 10:
298
					$this->bbcode_cache[$bbcode_id] = array(
299
						'preg' => array(
300
							'#\[email:$uid\]((.*?))\[/email:$uid\]#is'			=> $this->bbcode_tpl('email', $bbcode_id),
301
							'#\[email=([^\[]+):$uid\](.*?)\[/email:$uid\]#is'	=> $this->bbcode_tpl('email', $bbcode_id)
302
						)
303
					);
304
				break;
305
306
				case 11:
307
					if ($user->optionget('viewflash'))
308
					{
309
						$this->bbcode_cache[$bbcode_id] = array(
310
							'preg' => array(
311
								'#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#'	=> $this->bbcode_tpl('flash', $bbcode_id),
312
							)
313
						);
314
					}
315
					else
316
					{
317
						$this->bbcode_cache[$bbcode_id] = array(
318
							'preg' => array(
319
								'#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#'	=> str_replace('$1', '$3', str_replace('$2', '[ flash ]', $this->bbcode_tpl('url', $bbcode_id, true)))
320
							)
321
						);
322
					}
323
				break;
324
325
				case 12:
326
					$this->bbcode_cache[$bbcode_id] = array(
327
						'str'	=> array(
328
							'[/attachment:$uid]'	=> $this->bbcode_tpl('inline_attachment_close', $bbcode_id)
329
						),
330
						'preg'	=> array(
331
							'#\[attachment=([0-9]+):$uid\]#'	=> $this->bbcode_tpl('inline_attachment_open', $bbcode_id)
332
						)
333
					);
334
				break;
335
336
				default:
337
					if (isset($rowset[$bbcode_id]))
338
					{
339
						if ($this->template_bitfield->get($bbcode_id))
340
						{
341
							// The bbcode requires a custom template to be loaded
342
							if (!$bbcode_tpl = $this->bbcode_tpl($rowset[$bbcode_id]['bbcode_tag'], $bbcode_id))
343
							{
344
								// For some reason, the required template seems not to be available, use the default template
345
								$bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
346
							}
347
							else
348
							{
349
								// In order to use templates with custom bbcodes we need
350
								// to replace all {VARS} to corresponding backreferences
351
								// Note that backreferences are numbered from bbcode_match
352
								if (preg_match_all('/\{(URL|LOCAL_URL|EMAIL|TEXT|SIMPLETEXT|IDENTIFIER|COLOR|NUMBER)[0-9]*\}/', $rowset[$bbcode_id]['bbcode_match'], $m))
353
								{
354
									foreach ($m[0] as $i => $tok)
355
									{
356
										$bbcode_tpl = str_replace($tok, '$' . ($i + 1), $bbcode_tpl);
357
									}
358
								}
359
							}
360
						}
361
						else
362
						{
363
							// Default template
364
							$bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
365
						}
366
367
						// Replace {L_*} lang strings
368
						$bbcode_tpl = preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $bbcode_tpl);
369
370
						if (!empty($rowset[$bbcode_id]['second_pass_replace']))
371
						{
372
							// The custom BBCode requires second-pass pattern replacements
373
							$this->bbcode_cache[$bbcode_id] = array(
374
								'preg' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl)
375
							);
376
						}
377
						else
378
						{
379
							$this->bbcode_cache[$bbcode_id] = array(
380
								'str' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl)
381
							);
382
						}
383
					}
384
					else
385
					{
386
						$this->bbcode_cache[$bbcode_id] = false;
387
					}
388
				break;
389
			}
390
		}
391
	}
392
393
	/**
394
	* Return bbcode template
395
	*/
396
	function bbcode_tpl($tpl_name, $bbcode_id = -1, $skip_bitfield_check = false)
397
	{
398
		static $bbcode_hardtpl = array();
399
		if (empty($bbcode_hardtpl))
400
		{
401
			global $user;
402
			
403
			$bbcode_hardtpl = array(
404
				'b_open'	=> '<span style="font-weight: bold">',
405
				'b_close'	=> '</span>',
406
				'i_open'	=> '<span style="font-style: italic">',
407
				'i_close'	=> '</span>',
408
				'u_open'	=> '<span style="text-decoration: underline">',
409
				'u_close'	=> '</span>',
410
				'img'		=> '<img src="$1" alt="' . $user->lang['IMAGE'] . '" />',
411
				'size'		=> '<span style="font-size: $1%; line-height: normal">$2</span>',
412
				'color'		=> '<span style="color: $1">$2</span>',
413
				'email'		=> '<a href="mailto:$1">$2</a>'
414
			);
415
		}
416
417
		if ($bbcode_id != -1 && !$skip_bitfield_check && !$this->template_bitfield->get($bbcode_id))
418
		{
419
			return (isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false;
420
		}
421
422
		if (empty($this->bbcode_template))
423
		{
424
			if (($tpl = file_get_contents($this->template_filename)) === false)
425
			{
426
				trigger_error('Could not load bbcode template', E_USER_ERROR);
427
			}
428
429
			// replace \ with \\ and then ' with \'.
430
			$tpl = str_replace('\\', '\\\\', $tpl);
431
			$tpl = str_replace("'", "\'", $tpl);
432
433
			// strip newlines and indent
434
			$tpl = preg_replace("/\n[\n\r\s\t]*/", '', $tpl);
435
436
			// Turn template blocks into PHP assignment statements for the values of $bbcode_tpl..
437
			$this->bbcode_template = array();
438
439
			$matches = preg_match_all('#<!-- BEGIN (.*?) -->(.*?)<!-- END (?:.*?) -->#', $tpl, $match);
440
441
			for ($i = 0; $i < $matches; $i++)
442
			{
443
				if (empty($match[1][$i]))
444
				{
445
					continue;
446
				}
447
448
				$this->bbcode_template[$match[1][$i]] = $this->bbcode_tpl_replace($match[1][$i], $match[2][$i]);
449
			}
450
		}
451
452
		return (isset($this->bbcode_template[$tpl_name])) ? $this->bbcode_template[$tpl_name] : ((isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false);
453
	}
454
455
	/**
456
	* Return bbcode template replacement
457
	*/
458
	function bbcode_tpl_replace($tpl_name, $tpl)
459
	{
460
		global $user;
461
462
		static $replacements = array(
463
			'quote_username_open'	=> array('{USERNAME}'	=> '$1'),
464
			'color'					=> array('{COLOR}'		=> '$1', '{TEXT}'			=> '$2'),
465
			'size'					=> array('{SIZE}'		=> '$1', '{TEXT}'			=> '$2'),
466
			'img'					=> array('{URL}'		=> '$1'),
467
			'flash'					=> array('{WIDTH}'		=> '$1', '{HEIGHT}'			=> '$2', '{URL}'	=> '$3'),
468
			'url'					=> array('{URL}'		=> '$1', '{DESCRIPTION}'	=> '$2'),
469
			'email'					=> array('{EMAIL}'		=> '$1', '{DESCRIPTION}'	=> '$2')
470
		);
471
472
		$tpl = preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $tpl);
473
474
		if (!empty($replacements[$tpl_name]))
475
		{
476
			$tpl = strtr($tpl, $replacements[$tpl_name]);
477
		}
478
479
		return trim($tpl);
480
	}
481
482
	/**
483
	* Second parse list bbcode
484
	*/
485
	function bbcode_list($type)
486
	{
487
		if ($type == '')
488
		{
489
			$tpl = 'ulist_open_default';
490
			$type = 'default';
491
		}
492
		else if ($type == 'i')
493
		{
494
			$tpl = 'olist_open';
495
			$type = 'lower-roman';
496
		}
497
		else if ($type == 'I')
498
		{
499
			$tpl = 'olist_open';
500
			$type = 'upper-roman';
501
		}
502
		else if (preg_match('#^(disc|circle|square)$#i', $type))
503
		{
504
			$tpl = 'ulist_open';
505
			$type = strtolower($type);
506
		}
507
		else if (preg_match('#^[a-z]$#', $type))
508
		{
509
			$tpl = 'olist_open';
510
			$type = 'lower-alpha';
511
		}
512
		else if (preg_match('#[A-Z]#', $type))
513
		{
514
			$tpl = 'olist_open';
515
			$type = 'upper-alpha';
516
		}
517
		else if (is_numeric($type))
518
		{
519
			$tpl = 'olist_open';
520
			$type = 'arabic-numbers';
521
		}
522
		else
523
		{
524
			$tpl = 'olist_open';
525
			$type = 'arabic-numbers';
526
		}
527
528
		return str_replace('{LIST_TYPE}', $type, $this->bbcode_tpl($tpl));
529
	}
530
531
	/**
532
	* Second parse quote tag
533
	*/
534
	function bbcode_second_pass_quote($username, $quote)
535
	{
536
		// when using the /e modifier, preg_replace slashes double-quotes but does not
537
		// seem to slash anything else
538
		$quote = str_replace('\"', '"', $quote);
539
		$username = str_replace('\"', '"', $username);
540
541
		// remove newline at the beginning
542
		if ($quote == "\n")
543
		{
544
			$quote = '';
545
		}
546
547
		$quote = (($username) ? str_replace('$1', $username, $this->bbcode_tpl('quote_username_open')) : $this->bbcode_tpl('quote_open')) . $quote;
548
549
		return $quote;
550
	}
551
552
	/**
553
	* Second parse code tag
554
	*/
555
	function bbcode_second_pass_code($type, $code)
556
	{
557
		// when using the /e modifier, preg_replace slashes double-quotes but does not
558
		// seem to slash anything else
559
		$code = str_replace('\"', '"', $code);
560
561
		switch ($type)
562
		{
563
			case 'php':
564
				// Not the english way, but valid because of hardcoded syntax highlighting
565
				if (strpos($code, '<span class="syntaxdefault"><br /></span>') === 0)
566
				{
567
					$code = substr($code, 41);
568
				}
569
570
			// no break;
571
572
			default:
573
				$code = str_replace("\t", '&nbsp; &nbsp;', $code);
574
				$code = str_replace('  ', '&nbsp; ', $code);
575
				$code = str_replace('  ', ' &nbsp;', $code);
576
577
				// remove newline at the beginning
578
				if (!empty($code) && $code[0] == "\n")
579
				{
580
					$code = substr($code, 1);
581
				}
582
			break;
583
		}
584
585
		$code = $this->bbcode_tpl('code_open') . $code . $this->bbcode_tpl('code_close');
586
587
		return $code;
588
	}
589
}
590
591
?>