includes/message_parser.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 16:41:10 +0400
changeset 0 ceef6d64c21d
permissions -rw-r--r--
Added phpBB-3.0.10
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
if (!class_exists('bbcode'))
indvdum@0
    20
{
indvdum@0
    21
	include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
indvdum@0
    22
}
indvdum@0
    23
indvdum@0
    24
/**
indvdum@0
    25
* BBCODE FIRSTPASS
indvdum@0
    26
* BBCODE first pass class (functions for parsing messages for db storage)
indvdum@0
    27
* @package phpBB3
indvdum@0
    28
*/
indvdum@0
    29
class bbcode_firstpass extends bbcode
indvdum@0
    30
{
indvdum@0
    31
	var $message = '';
indvdum@0
    32
	var $warn_msg = array();
indvdum@0
    33
	var $parsed_items = array();
indvdum@0
    34
indvdum@0
    35
	/**
indvdum@0
    36
	* Parse BBCode
indvdum@0
    37
	*/
indvdum@0
    38
	function parse_bbcode()
indvdum@0
    39
	{
indvdum@0
    40
		if (!$this->bbcodes)
indvdum@0
    41
		{
indvdum@0
    42
			$this->bbcode_init();
indvdum@0
    43
		}
indvdum@0
    44
indvdum@0
    45
		global $user;
indvdum@0
    46
indvdum@0
    47
		$this->bbcode_bitfield = '';
indvdum@0
    48
		$bitfield = new bitfield();
indvdum@0
    49
indvdum@0
    50
		foreach ($this->bbcodes as $bbcode_name => $bbcode_data)
indvdum@0
    51
		{
indvdum@0
    52
			if (isset($bbcode_data['disabled']) && $bbcode_data['disabled'])
indvdum@0
    53
			{
indvdum@0
    54
				foreach ($bbcode_data['regexp'] as $regexp => $replacement)
indvdum@0
    55
				{
indvdum@0
    56
					if (preg_match($regexp, $this->message))
indvdum@0
    57
					{
indvdum@0
    58
						$this->warn_msg[] = sprintf($user->lang['UNAUTHORISED_BBCODE'] , '[' . $bbcode_name . ']');
indvdum@0
    59
						continue;
indvdum@0
    60
					}
indvdum@0
    61
				}
indvdum@0
    62
			}
indvdum@0
    63
			else
indvdum@0
    64
			{
indvdum@0
    65
				foreach ($bbcode_data['regexp'] as $regexp => $replacement)
indvdum@0
    66
				{
indvdum@0
    67
					// The pattern gets compiled and cached by the PCRE extension,
indvdum@0
    68
					// it should not demand recompilation
indvdum@0
    69
					if (preg_match($regexp, $this->message))
indvdum@0
    70
					{
indvdum@0
    71
						$this->message = preg_replace($regexp, $replacement, $this->message);
indvdum@0
    72
						$bitfield->set($bbcode_data['bbcode_id']);
indvdum@0
    73
					}
indvdum@0
    74
				}
indvdum@0
    75
			}
indvdum@0
    76
		}
indvdum@0
    77
indvdum@0
    78
		$this->bbcode_bitfield = $bitfield->get_base64();
indvdum@0
    79
	}
indvdum@0
    80
indvdum@0
    81
	/**
indvdum@0
    82
	* Prepare some bbcodes for better parsing
indvdum@0
    83
	*/
indvdum@0
    84
	function prepare_bbcodes()
indvdum@0
    85
	{
indvdum@0
    86
		// Ok, seems like users instead want the no-parsing of urls, smilies, etc. after and before and within quote tags being tagged as "not a bug".
indvdum@0
    87
		// Fine by me ;) Will ease our live... but do not come back and cry at us, we won't hear you.
indvdum@0
    88
indvdum@0
    89
		/* Add newline at the end and in front of each quote block to prevent parsing errors (urls, smilies, etc.)
indvdum@0
    90
		if (strpos($this->message, '[quote') !== false && strpos($this->message, '[/quote]') !== false)
indvdum@0
    91
		{
indvdum@0
    92
			$this->message = str_replace("\r\n", "\n", $this->message);
indvdum@0
    93
indvdum@0
    94
			// We strip newlines and spaces after and before quotes in quotes (trimming) and then add exactly one newline
indvdum@0
    95
			$this->message = preg_replace('#\[quote(=&quot;.*?&quot;)?\]\s*(.*?)\s*\[/quote\]#siu', '[quote\1]' . "\n" . '\2' ."\n[/quote]", $this->message);
indvdum@0
    96
		}
indvdum@0
    97
		*/
indvdum@0
    98
indvdum@0
    99
		// Add other checks which needs to be placed before actually parsing anything (be it bbcodes, smilies, urls...)
indvdum@0
   100
	}
indvdum@0
   101
indvdum@0
   102
	/**
indvdum@0
   103
	* Init bbcode data for later parsing
indvdum@0
   104
	*/
indvdum@0
   105
	function bbcode_init($allow_custom_bbcode = true)
indvdum@0
   106
	{
indvdum@0
   107
		static $rowset;
indvdum@0
   108
indvdum@0
   109
		// This array holds all bbcode data. BBCodes will be processed in this
indvdum@0
   110
		// order, so it is important to keep [code] in first position and
indvdum@0
   111
		// [quote] in second position.
indvdum@0
   112
		// To parse multiline URL we enable dotall option setting only for URL text
indvdum@0
   113
		// but not for link itself, thus [url][/url] is not affected.
indvdum@0
   114
		$this->bbcodes = array(
indvdum@0
   115
			'code'			=> array('bbcode_id' => 8,	'regexp' => array('#\[code(?:=([a-z]+))?\](.+\[/code\])#uise' => "\$this->bbcode_code('\$1', '\$2')")),
indvdum@0
   116
			'quote'			=> array('bbcode_id' => 0,	'regexp' => array('#\[quote(?:=&quot;(.*?)&quot;)?\](.+)\[/quote\]#uise' => "\$this->bbcode_quote('\$0')")),
indvdum@0
   117
			'attachment'	=> array('bbcode_id' => 12,	'regexp' => array('#\[attachment=([0-9]+)\](.*?)\[/attachment\]#uise' => "\$this->bbcode_attachment('\$1', '\$2')")),
indvdum@0
   118
			'b'				=> array('bbcode_id' => 1,	'regexp' => array('#\[b\](.*?)\[/b\]#uise' => "\$this->bbcode_strong('\$1')")),
indvdum@0
   119
			'i'				=> array('bbcode_id' => 2,	'regexp' => array('#\[i\](.*?)\[/i\]#uise' => "\$this->bbcode_italic('\$1')")),
indvdum@0
   120
			'url'			=> array('bbcode_id' => 3,	'regexp' => array('#\[url(=(.*))?\](?(1)((?s).*(?-s))|(.*))\[/url\]#uiUe' => "\$this->validate_url('\$2', ('\$3') ? '\$3' : '\$4')")),
indvdum@0
   121
			'img'			=> array('bbcode_id' => 4,	'regexp' => array('#\[img\](.*)\[/img\]#uiUe' => "\$this->bbcode_img('\$1')")),
indvdum@0
   122
			'size'			=> array('bbcode_id' => 5,	'regexp' => array('#\[size=([\-\+]?\d+)\](.*?)\[/size\]#uise' => "\$this->bbcode_size('\$1', '\$2')")),
indvdum@0
   123
			'color'			=> array('bbcode_id' => 6,	'regexp' => array('!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!uise' => "\$this->bbcode_color('\$1', '\$2')")),
indvdum@0
   124
			'u'				=> array('bbcode_id' => 7,	'regexp' => array('#\[u\](.*?)\[/u\]#uise' => "\$this->bbcode_underline('\$1')")),
indvdum@0
   125
			'list'			=> array('bbcode_id' => 9,	'regexp' => array('#\[list(?:=(?:[a-z0-9]|disc|circle|square))?].*\[/list]#uise' => "\$this->bbcode_parse_list('\$0')")),
indvdum@0
   126
			'email'			=> array('bbcode_id' => 10,	'regexp' => array('#\[email=?(.*?)?\](.*?)\[/email\]#uise' => "\$this->validate_email('\$1', '\$2')")),
indvdum@0
   127
			'flash'			=> array('bbcode_id' => 11,	'regexp' => array('#\[flash=([0-9]+),([0-9]+)\](.*?)\[/flash\]#uie' => "\$this->bbcode_flash('\$1', '\$2', '\$3')"))
indvdum@0
   128
		);
indvdum@0
   129
indvdum@0
   130
		// Zero the parsed items array
indvdum@0
   131
		$this->parsed_items = array();
indvdum@0
   132
indvdum@0
   133
		foreach ($this->bbcodes as $tag => $bbcode_data)
indvdum@0
   134
		{
indvdum@0
   135
			$this->parsed_items[$tag] = 0;
indvdum@0
   136
		}
indvdum@0
   137
indvdum@0
   138
		if (!$allow_custom_bbcode)
indvdum@0
   139
		{
indvdum@0
   140
			return;
indvdum@0
   141
		}
indvdum@0
   142
indvdum@0
   143
		if (!is_array($rowset))
indvdum@0
   144
		{
indvdum@0
   145
			global $db;
indvdum@0
   146
			$rowset = array();
indvdum@0
   147
indvdum@0
   148
			$sql = 'SELECT *
indvdum@0
   149
				FROM ' . BBCODES_TABLE;
indvdum@0
   150
			$result = $db->sql_query($sql);
indvdum@0
   151
indvdum@0
   152
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   153
			{
indvdum@0
   154
				$rowset[] = $row;
indvdum@0
   155
			}
indvdum@0
   156
			$db->sql_freeresult($result);
indvdum@0
   157
		}
indvdum@0
   158
indvdum@0
   159
		foreach ($rowset as $row)
indvdum@0
   160
		{
indvdum@0
   161
			$this->bbcodes[$row['bbcode_tag']] = array(
indvdum@0
   162
				'bbcode_id'	=> (int) $row['bbcode_id'],
indvdum@0
   163
				'regexp'	=> array($row['first_pass_match'] => str_replace('$uid', $this->bbcode_uid, $row['first_pass_replace']))
indvdum@0
   164
			);
indvdum@0
   165
		}
indvdum@0
   166
	}
indvdum@0
   167
indvdum@0
   168
	/**
indvdum@0
   169
	* Making some pre-checks for bbcodes as well as increasing the number of parsed items
indvdum@0
   170
	*/
indvdum@0
   171
	function check_bbcode($bbcode, &$in)
indvdum@0
   172
	{
indvdum@0
   173
		// when using the /e modifier, preg_replace slashes double-quotes but does not
indvdum@0
   174
		// seem to slash anything else
indvdum@0
   175
		$in = str_replace("\r\n", "\n", str_replace('\"', '"', $in));
indvdum@0
   176
indvdum@0
   177
		// Trimming here to make sure no empty bbcodes are parsed accidently
indvdum@0
   178
		if (trim($in) == '')
indvdum@0
   179
		{
indvdum@0
   180
			return false;
indvdum@0
   181
		}
indvdum@0
   182
indvdum@0
   183
		$this->parsed_items[$bbcode]++;
indvdum@0
   184
indvdum@0
   185
		return true;
indvdum@0
   186
	}
indvdum@0
   187
indvdum@0
   188
	/**
indvdum@0
   189
	* Transform some characters in valid bbcodes
indvdum@0
   190
	*/
indvdum@0
   191
	function bbcode_specialchars($text)
indvdum@0
   192
	{
indvdum@0
   193
		$str_from = array('<', '>', '[', ']', '.', ':');
indvdum@0
   194
		$str_to = array('&lt;', '&gt;', '&#91;', '&#93;', '&#46;', '&#58;');
indvdum@0
   195
indvdum@0
   196
		return str_replace($str_from, $str_to, $text);
indvdum@0
   197
	}
indvdum@0
   198
indvdum@0
   199
	/**
indvdum@0
   200
	* Parse size tag
indvdum@0
   201
	*/
indvdum@0
   202
	function bbcode_size($stx, $in)
indvdum@0
   203
	{
indvdum@0
   204
		global $user, $config;
indvdum@0
   205
indvdum@0
   206
		if (!$this->check_bbcode('size', $in))
indvdum@0
   207
		{
indvdum@0
   208
			return $in;
indvdum@0
   209
		}
indvdum@0
   210
indvdum@0
   211
		if ($config['max_' . $this->mode . '_font_size'] && $config['max_' . $this->mode . '_font_size'] < $stx)
indvdum@0
   212
		{
indvdum@0
   213
			$this->warn_msg[] = sprintf($user->lang['MAX_FONT_SIZE_EXCEEDED'], $config['max_' . $this->mode . '_font_size']);
indvdum@0
   214
indvdum@0
   215
			return '[size=' . $stx . ']' . $in . '[/size]';
indvdum@0
   216
		}
indvdum@0
   217
indvdum@0
   218
		// Do not allow size=0
indvdum@0
   219
		if ($stx <= 0)
indvdum@0
   220
		{
indvdum@0
   221
			return '[size=' . $stx . ']' . $in . '[/size]';
indvdum@0
   222
		}
indvdum@0
   223
indvdum@0
   224
		return '[size=' . $stx . ':' . $this->bbcode_uid . ']' . $in . '[/size:' . $this->bbcode_uid . ']';
indvdum@0
   225
	}
indvdum@0
   226
indvdum@0
   227
	/**
indvdum@0
   228
	* Parse color tag
indvdum@0
   229
	*/
indvdum@0
   230
	function bbcode_color($stx, $in)
indvdum@0
   231
	{
indvdum@0
   232
		if (!$this->check_bbcode('color', $in))
indvdum@0
   233
		{
indvdum@0
   234
			return $in;
indvdum@0
   235
		}
indvdum@0
   236
indvdum@0
   237
		return '[color=' . $stx . ':' . $this->bbcode_uid . ']' . $in . '[/color:' . $this->bbcode_uid . ']';
indvdum@0
   238
	}
indvdum@0
   239
indvdum@0
   240
	/**
indvdum@0
   241
	* Parse u tag
indvdum@0
   242
	*/
indvdum@0
   243
	function bbcode_underline($in)
indvdum@0
   244
	{
indvdum@0
   245
		if (!$this->check_bbcode('u', $in))
indvdum@0
   246
		{
indvdum@0
   247
			return $in;
indvdum@0
   248
		}
indvdum@0
   249
indvdum@0
   250
		return '[u:' . $this->bbcode_uid . ']' . $in . '[/u:' . $this->bbcode_uid . ']';
indvdum@0
   251
	}
indvdum@0
   252
indvdum@0
   253
	/**
indvdum@0
   254
	* Parse b tag
indvdum@0
   255
	*/
indvdum@0
   256
	function bbcode_strong($in)
indvdum@0
   257
	{
indvdum@0
   258
		if (!$this->check_bbcode('b', $in))
indvdum@0
   259
		{
indvdum@0
   260
			return $in;
indvdum@0
   261
		}
indvdum@0
   262
indvdum@0
   263
		return '[b:' . $this->bbcode_uid . ']' . $in . '[/b:' . $this->bbcode_uid . ']';
indvdum@0
   264
	}
indvdum@0
   265
indvdum@0
   266
	/**
indvdum@0
   267
	* Parse i tag
indvdum@0
   268
	*/
indvdum@0
   269
	function bbcode_italic($in)
indvdum@0
   270
	{
indvdum@0
   271
		if (!$this->check_bbcode('i', $in))
indvdum@0
   272
		{
indvdum@0
   273
			return $in;
indvdum@0
   274
		}
indvdum@0
   275
indvdum@0
   276
		return '[i:' . $this->bbcode_uid . ']' . $in . '[/i:' . $this->bbcode_uid . ']';
indvdum@0
   277
	}
indvdum@0
   278
indvdum@0
   279
	/**
indvdum@0
   280
	* Parse img tag
indvdum@0
   281
	*/
indvdum@0
   282
	function bbcode_img($in)
indvdum@0
   283
	{
indvdum@0
   284
		global $user, $config;
indvdum@0
   285
indvdum@0
   286
		if (!$this->check_bbcode('img', $in))
indvdum@0
   287
		{
indvdum@0
   288
			return $in;
indvdum@0
   289
		}
indvdum@0
   290
indvdum@0
   291
		$in = trim($in);
indvdum@0
   292
		$error = false;
indvdum@0
   293
indvdum@0
   294
		$in = str_replace(' ', '%20', $in);
indvdum@0
   295
indvdum@0
   296
		// Checking urls
indvdum@0
   297
		if (!preg_match('#^' . get_preg_expression('url') . '$#i', $in) && !preg_match('#^' . get_preg_expression('www_url') . '$#i', $in))
indvdum@0
   298
		{
indvdum@0
   299
			return '[img]' . $in . '[/img]';
indvdum@0
   300
		}
indvdum@0
   301
indvdum@0
   302
		// Try to cope with a common user error... not specifying a protocol but only a subdomain
indvdum@0
   303
		if (!preg_match('#^[a-z0-9]+://#i', $in))
indvdum@0
   304
		{
indvdum@0
   305
			$in = 'http://' . $in;
indvdum@0
   306
		}
indvdum@0
   307
indvdum@0
   308
		if ($config['max_' . $this->mode . '_img_height'] || $config['max_' . $this->mode . '_img_width'])
indvdum@0
   309
		{
indvdum@0
   310
			$stats = @getimagesize(htmlspecialchars_decode($in));
indvdum@0
   311
indvdum@0
   312
			if ($stats === false)
indvdum@0
   313
			{
indvdum@0
   314
				$error = true;
indvdum@0
   315
				$this->warn_msg[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
indvdum@0
   316
			}
indvdum@0
   317
			else
indvdum@0
   318
			{
indvdum@0
   319
				if ($config['max_' . $this->mode . '_img_height'] && $config['max_' . $this->mode . '_img_height'] < $stats[1])
indvdum@0
   320
				{
indvdum@0
   321
					$error = true;
indvdum@0
   322
					$this->warn_msg[] = sprintf($user->lang['MAX_IMG_HEIGHT_EXCEEDED'], $config['max_' . $this->mode . '_img_height']);
indvdum@0
   323
				}
indvdum@0
   324
indvdum@0
   325
				if ($config['max_' . $this->mode . '_img_width'] && $config['max_' . $this->mode . '_img_width'] < $stats[0])
indvdum@0
   326
				{
indvdum@0
   327
					$error = true;
indvdum@0
   328
					$this->warn_msg[] = sprintf($user->lang['MAX_IMG_WIDTH_EXCEEDED'], $config['max_' . $this->mode . '_img_width']);
indvdum@0
   329
				}
indvdum@0
   330
			}
indvdum@0
   331
		}
indvdum@0
   332
indvdum@0
   333
		if ($error || $this->path_in_domain($in))
indvdum@0
   334
		{
indvdum@0
   335
			return '[img]' . $in . '[/img]';
indvdum@0
   336
		}
indvdum@0
   337
indvdum@0
   338
		return '[img:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($in) . '[/img:' . $this->bbcode_uid . ']';
indvdum@0
   339
	}
indvdum@0
   340
indvdum@0
   341
	/**
indvdum@0
   342
	* Parse flash tag
indvdum@0
   343
	*/
indvdum@0
   344
	function bbcode_flash($width, $height, $in)
indvdum@0
   345
	{
indvdum@0
   346
		global $user, $config;
indvdum@0
   347
indvdum@0
   348
		if (!$this->check_bbcode('flash', $in))
indvdum@0
   349
		{
indvdum@0
   350
			return $in;
indvdum@0
   351
		}
indvdum@0
   352
indvdum@0
   353
		$in = trim($in);
indvdum@0
   354
		$error = false;
indvdum@0
   355
indvdum@0
   356
		// Do not allow 0-sizes generally being entered
indvdum@0
   357
		if ($width <= 0 || $height <= 0)
indvdum@0
   358
		{
indvdum@0
   359
			return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]';
indvdum@0
   360
		}
indvdum@0
   361
indvdum@0
   362
		$in = str_replace(' ', '%20', $in);
indvdum@0
   363
indvdum@0
   364
		// Make sure $in is a URL.
indvdum@0
   365
		if (!preg_match('#^' . get_preg_expression('url') . '$#i', $in) &&
indvdum@0
   366
			!preg_match('#^' . get_preg_expression('www_url') . '$#i', $in))
indvdum@0
   367
		{
indvdum@0
   368
			return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]';
indvdum@0
   369
		}
indvdum@0
   370
indvdum@0
   371
		// Apply the same size checks on flash files as on images
indvdum@0
   372
		if ($config['max_' . $this->mode . '_img_height'] || $config['max_' . $this->mode . '_img_width'])
indvdum@0
   373
		{
indvdum@0
   374
			if ($config['max_' . $this->mode . '_img_height'] && $config['max_' . $this->mode . '_img_height'] < $height)
indvdum@0
   375
			{
indvdum@0
   376
				$error = true;
indvdum@0
   377
				$this->warn_msg[] = sprintf($user->lang['MAX_FLASH_HEIGHT_EXCEEDED'], $config['max_' . $this->mode . '_img_height']);
indvdum@0
   378
			}
indvdum@0
   379
indvdum@0
   380
			if ($config['max_' . $this->mode . '_img_width'] && $config['max_' . $this->mode . '_img_width'] < $width)
indvdum@0
   381
			{
indvdum@0
   382
				$error = true;
indvdum@0
   383
				$this->warn_msg[] = sprintf($user->lang['MAX_FLASH_WIDTH_EXCEEDED'], $config['max_' . $this->mode . '_img_width']);
indvdum@0
   384
			}
indvdum@0
   385
		}
indvdum@0
   386
indvdum@0
   387
		if ($error || $this->path_in_domain($in))
indvdum@0
   388
		{
indvdum@0
   389
			return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]';
indvdum@0
   390
		}
indvdum@0
   391
indvdum@0
   392
		return '[flash=' . $width . ',' . $height . ':' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($in) . '[/flash:' . $this->bbcode_uid . ']';
indvdum@0
   393
	}
indvdum@0
   394
indvdum@0
   395
	/**
indvdum@0
   396
	* Parse inline attachments [ia]
indvdum@0
   397
	*/
indvdum@0
   398
	function bbcode_attachment($stx, $in)
indvdum@0
   399
	{
indvdum@0
   400
		if (!$this->check_bbcode('attachment', $in))
indvdum@0
   401
		{
indvdum@0
   402
			return $in;
indvdum@0
   403
		}
indvdum@0
   404
indvdum@0
   405
		return '[attachment=' . $stx . ':' . $this->bbcode_uid . ']<!-- ia' . $stx . ' -->' . trim($in) . '<!-- ia' . $stx . ' -->[/attachment:' . $this->bbcode_uid . ']';
indvdum@0
   406
	}
indvdum@0
   407
indvdum@0
   408
	/**
indvdum@0
   409
	* Parse code text from code tag
indvdum@0
   410
	* @access private
indvdum@0
   411
	*/
indvdum@0
   412
	function bbcode_parse_code($stx, &$code)
indvdum@0
   413
	{
indvdum@0
   414
		switch (strtolower($stx))
indvdum@0
   415
		{
indvdum@0
   416
			case 'php':
indvdum@0
   417
indvdum@0
   418
				$remove_tags = false;
indvdum@0
   419
indvdum@0
   420
				$str_from = array('&lt;', '&gt;', '&#91;', '&#93;', '&#46;', '&#58;', '&#058;');
indvdum@0
   421
				$str_to = array('<', '>', '[', ']', '.', ':', ':');
indvdum@0
   422
				$code = str_replace($str_from, $str_to, $code);
indvdum@0
   423
indvdum@0
   424
				if (!preg_match('/\<\?.*?\?\>/is', $code))
indvdum@0
   425
				{
indvdum@0
   426
					$remove_tags = true;
indvdum@0
   427
					$code = "<?php $code ?>";
indvdum@0
   428
				}
indvdum@0
   429
indvdum@0
   430
				$conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string');
indvdum@0
   431
				foreach ($conf as $ini_var)
indvdum@0
   432
				{
indvdum@0
   433
					@ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var));
indvdum@0
   434
				}
indvdum@0
   435
indvdum@0
   436
				// Because highlight_string is specialcharing the text (but we already did this before), we have to reverse this in order to get correct results
indvdum@0
   437
				$code = htmlspecialchars_decode($code);
indvdum@0
   438
				$code = highlight_string($code, true);
indvdum@0
   439
indvdum@0
   440
				$str_from = array('<span style="color: ', '<font color="syntax', '</font>', '<code>', '</code>','[', ']', '.', ':');
indvdum@0
   441
				$str_to = array('<span class="', '<span class="syntax', '</span>', '', '', '&#91;', '&#93;', '&#46;', '&#58;');
indvdum@0
   442
indvdum@0
   443
				if ($remove_tags)
indvdum@0
   444
				{
indvdum@0
   445
					$str_from[] = '<span class="syntaxdefault">&lt;?php </span>';
indvdum@0
   446
					$str_to[] = '';
indvdum@0
   447
					$str_from[] = '<span class="syntaxdefault">&lt;?php&nbsp;';
indvdum@0
   448
					$str_to[] = '<span class="syntaxdefault">';
indvdum@0
   449
				}
indvdum@0
   450
indvdum@0
   451
				$code = str_replace($str_from, $str_to, $code);
indvdum@0
   452
				$code = preg_replace('#^(<span class="[a-z_]+">)\n?(.*?)\n?(</span>)$#is', '$1$2$3', $code);
indvdum@0
   453
indvdum@0
   454
				if ($remove_tags)
indvdum@0
   455
				{
indvdum@0
   456
					$code = preg_replace('#(<span class="[a-z]+">)?\?&gt;(</span>)#', '$1&nbsp;$2', $code);
indvdum@0
   457
				}
indvdum@0
   458
indvdum@0
   459
				$code = preg_replace('#^<span class="[a-z]+"><span class="([a-z]+)">(.*)</span></span>#s', '<span class="$1">$2</span>', $code);
indvdum@0
   460
				$code = preg_replace('#(?:\s++|&nbsp;)*+</span>$#u', '</span>', $code);
indvdum@0
   461
indvdum@0
   462
				// remove newline at the end
indvdum@0
   463
				if (!empty($code) && substr($code, -1) == "\n")
indvdum@0
   464
				{
indvdum@0
   465
					$code = substr($code, 0, -1);
indvdum@0
   466
				}
indvdum@0
   467
indvdum@0
   468
				return "[code=$stx:" . $this->bbcode_uid . ']' . $code . '[/code:' . $this->bbcode_uid . ']';
indvdum@0
   469
			break;
indvdum@0
   470
indvdum@0
   471
			default:
indvdum@0
   472
				return '[code:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($code) . '[/code:' . $this->bbcode_uid . ']';
indvdum@0
   473
			break;
indvdum@0
   474
		}
indvdum@0
   475
	}
indvdum@0
   476
indvdum@0
   477
	/**
indvdum@0
   478
	* Parse code tag
indvdum@0
   479
	* Expects the argument to start right after the opening [code] tag and to end with [/code]
indvdum@0
   480
	*/
indvdum@0
   481
	function bbcode_code($stx, $in)
indvdum@0
   482
	{
indvdum@0
   483
		if (!$this->check_bbcode('code', $in))
indvdum@0
   484
		{
indvdum@0
   485
			return $in;
indvdum@0
   486
		}
indvdum@0
   487
indvdum@0
   488
		// We remove the hardcoded elements from the code block here because it is not used in code blocks
indvdum@0
   489
		// Having it here saves us one preg_replace per message containing [code] blocks
indvdum@0
   490
		// Additionally, magic url parsing should go after parsing bbcodes, but for safety those are stripped out too...
indvdum@0
   491
		$htm_match = get_preg_expression('bbcode_htm');
indvdum@0
   492
		unset($htm_match[4], $htm_match[5]);
indvdum@0
   493
		$htm_replace = array('\1', '\1', '\2', '\1');
indvdum@0
   494
indvdum@0
   495
		$out = $code_block = '';
indvdum@0
   496
		$open = 1;
indvdum@0
   497
indvdum@0
   498
		while ($in)
indvdum@0
   499
		{
indvdum@0
   500
			// Determine position and tag length of next code block
indvdum@0
   501
			preg_match('#(.*?)(\[code(?:=([a-z]+))?\])(.+)#is', $in, $buffer);
indvdum@0
   502
			$pos = (isset($buffer[1])) ? strlen($buffer[1]) : false;
indvdum@0
   503
			$tag_length = (isset($buffer[2])) ? strlen($buffer[2]) : false;
indvdum@0
   504
indvdum@0
   505
			// Determine position of ending code tag
indvdum@0
   506
			$pos2 = stripos($in, '[/code]');
indvdum@0
   507
indvdum@0
   508
			// Which is the next block, ending code or code block
indvdum@0
   509
			if ($pos !== false && $pos < $pos2)
indvdum@0
   510
			{
indvdum@0
   511
				// Open new block
indvdum@0
   512
				if (!$open)
indvdum@0
   513
				{
indvdum@0
   514
					$out .= substr($in, 0, $pos);
indvdum@0
   515
					$in = substr($in, $pos);
indvdum@0
   516
					$stx = (isset($buffer[3])) ? $buffer[3] : '';
indvdum@0
   517
					$code_block = '';
indvdum@0
   518
				}
indvdum@0
   519
				else
indvdum@0
   520
				{
indvdum@0
   521
					// Already opened block, just append to the current block
indvdum@0
   522
					$code_block .= substr($in, 0, $pos) . ((isset($buffer[2])) ? $buffer[2] : '');
indvdum@0
   523
					$in = substr($in, $pos);
indvdum@0
   524
				}
indvdum@0
   525
indvdum@0
   526
				$in = substr($in, $tag_length);
indvdum@0
   527
				$open++;
indvdum@0
   528
			}
indvdum@0
   529
			else
indvdum@0
   530
			{
indvdum@0
   531
				// Close the block
indvdum@0
   532
				if ($open == 1)
indvdum@0
   533
				{
indvdum@0
   534
					$code_block .= substr($in, 0, $pos2);
indvdum@0
   535
					$code_block = preg_replace($htm_match, $htm_replace, $code_block);
indvdum@0
   536
indvdum@0
   537
					// Parse this code block
indvdum@0
   538
					$out .= $this->bbcode_parse_code($stx, $code_block);
indvdum@0
   539
					$code_block = '';
indvdum@0
   540
					$open--;
indvdum@0
   541
				}
indvdum@0
   542
				else if ($open)
indvdum@0
   543
				{
indvdum@0
   544
					// Close one open tag... add to the current code block
indvdum@0
   545
					$code_block .= substr($in, 0, $pos2 + 7);
indvdum@0
   546
					$open--;
indvdum@0
   547
				}
indvdum@0
   548
				else
indvdum@0
   549
				{
indvdum@0
   550
					// end code without opening code... will be always outside code block
indvdum@0
   551
					$out .= substr($in, 0, $pos2 + 7);
indvdum@0
   552
				}
indvdum@0
   553
indvdum@0
   554
				$in = substr($in, $pos2 + 7);
indvdum@0
   555
			}
indvdum@0
   556
		}
indvdum@0
   557
indvdum@0
   558
		// if now $code_block has contents we need to parse the remaining code while removing the last closing tag to match up.
indvdum@0
   559
		if ($code_block)
indvdum@0
   560
		{
indvdum@0
   561
			$code_block = substr($code_block, 0, -7);
indvdum@0
   562
			$code_block = preg_replace($htm_match, $htm_replace, $code_block);
indvdum@0
   563
indvdum@0
   564
			$out .= $this->bbcode_parse_code($stx, $code_block);
indvdum@0
   565
		}
indvdum@0
   566
indvdum@0
   567
		return $out;
indvdum@0
   568
	}
indvdum@0
   569
indvdum@0
   570
	/**
indvdum@0
   571
	* Parse list bbcode
indvdum@0
   572
	* Expects the argument to start with a tag
indvdum@0
   573
	*/
indvdum@0
   574
	function bbcode_parse_list($in)
indvdum@0
   575
	{
indvdum@0
   576
		if (!$this->check_bbcode('list', $in))
indvdum@0
   577
		{
indvdum@0
   578
			return $in;
indvdum@0
   579
		}
indvdum@0
   580
indvdum@0
   581
		// $tok holds characters to stop at. Since the string starts with a '[' we'll get everything up to the first ']' which should be the opening [list] tag
indvdum@0
   582
		$tok = ']';
indvdum@0
   583
		$out = '[';
indvdum@0
   584
indvdum@0
   585
		// First character is [
indvdum@0
   586
		$in = substr($in, 1);
indvdum@0
   587
		$list_end_tags = $item_end_tags = array();
indvdum@0
   588
indvdum@0
   589
		do
indvdum@0
   590
		{
indvdum@0
   591
			$pos = strlen($in);
indvdum@0
   592
indvdum@0
   593
			for ($i = 0, $tok_len = strlen($tok); $i < $tok_len; ++$i)
indvdum@0
   594
			{
indvdum@0
   595
				$tmp_pos = strpos($in, $tok[$i]);
indvdum@0
   596
indvdum@0
   597
				if ($tmp_pos !== false && $tmp_pos < $pos)
indvdum@0
   598
				{
indvdum@0
   599
					$pos = $tmp_pos;
indvdum@0
   600
				}
indvdum@0
   601
			}
indvdum@0
   602
indvdum@0
   603
			$buffer = substr($in, 0, $pos);
indvdum@0
   604
			$tok = $in[$pos];
indvdum@0
   605
indvdum@0
   606
			$in = substr($in, $pos + 1);
indvdum@0
   607
indvdum@0
   608
			if ($tok == ']')
indvdum@0
   609
			{
indvdum@0
   610
				// if $tok is ']' the buffer holds a tag
indvdum@0
   611
				if (strtolower($buffer) == '/list' && sizeof($list_end_tags))
indvdum@0
   612
				{
indvdum@0
   613
					// valid [/list] tag, check nesting so that we don't hit false positives
indvdum@0
   614
					if (sizeof($item_end_tags) && sizeof($item_end_tags) >= sizeof($list_end_tags))
indvdum@0
   615
					{
indvdum@0
   616
						// current li tag has not been closed
indvdum@0
   617
						$out = preg_replace('/\n?\[$/', '[', $out) . array_pop($item_end_tags) . '][';
indvdum@0
   618
					}
indvdum@0
   619
indvdum@0
   620
					$out .= array_pop($list_end_tags) . ']';
indvdum@0
   621
					$tok = '[';
indvdum@0
   622
				}
indvdum@0
   623
				else if (preg_match('#^list(=[0-9a-z]+)?$#i', $buffer, $m))
indvdum@0
   624
				{
indvdum@0
   625
					// sub-list, add a closing tag
indvdum@0
   626
					if (empty($m[1]) || preg_match('/^=(?:disc|square|circle)$/i', $m[1]))
indvdum@0
   627
					{
indvdum@0
   628
						array_push($list_end_tags, '/list:u:' . $this->bbcode_uid);
indvdum@0
   629
					}
indvdum@0
   630
					else
indvdum@0
   631
					{
indvdum@0
   632
						array_push($list_end_tags, '/list:o:' . $this->bbcode_uid);
indvdum@0
   633
					}
indvdum@0
   634
					$out .= 'list' . substr($buffer, 4) . ':' . $this->bbcode_uid . ']';
indvdum@0
   635
					$tok = '[';
indvdum@0
   636
				}
indvdum@0
   637
				else
indvdum@0
   638
				{
indvdum@0
   639
					if (($buffer == '*' || substr($buffer, -2) == '[*') && sizeof($list_end_tags))
indvdum@0
   640
					{
indvdum@0
   641
						// the buffer holds a bullet tag and we have a [list] tag open
indvdum@0
   642
						if (sizeof($item_end_tags) >= sizeof($list_end_tags))
indvdum@0
   643
						{
indvdum@0
   644
							if (substr($buffer, -2) == '[*')
indvdum@0
   645
							{
indvdum@0
   646
								$out .= substr($buffer, 0, -2) . '[';
indvdum@0
   647
							}
indvdum@0
   648
							// current li tag has not been closed
indvdum@0
   649
							if (preg_match('/\n\[$/', $out, $m))
indvdum@0
   650
							{
indvdum@0
   651
								$out = preg_replace('/\n\[$/', '[', $out);
indvdum@0
   652
								$buffer = array_pop($item_end_tags) . "]\n[*:" . $this->bbcode_uid;
indvdum@0
   653
							}
indvdum@0
   654
							else
indvdum@0
   655
							{
indvdum@0
   656
								$buffer = array_pop($item_end_tags) . '][*:' . $this->bbcode_uid;
indvdum@0
   657
							}
indvdum@0
   658
						}
indvdum@0
   659
						else
indvdum@0
   660
						{
indvdum@0
   661
							$buffer = '*:' . $this->bbcode_uid;
indvdum@0
   662
						}
indvdum@0
   663
indvdum@0
   664
						$item_end_tags[] = '/*:m:' . $this->bbcode_uid;
indvdum@0
   665
					}
indvdum@0
   666
					else if ($buffer == '/*')
indvdum@0
   667
					{
indvdum@0
   668
						array_pop($item_end_tags);
indvdum@0
   669
						$buffer = '/*:' . $this->bbcode_uid;
indvdum@0
   670
					}
indvdum@0
   671
indvdum@0
   672
					$out .= $buffer . $tok;
indvdum@0
   673
					$tok = '[]';
indvdum@0
   674
				}
indvdum@0
   675
			}
indvdum@0
   676
			else
indvdum@0
   677
			{
indvdum@0
   678
				// Not within a tag, just add buffer to the return string
indvdum@0
   679
				$out .= $buffer . $tok;
indvdum@0
   680
				$tok = ($tok == '[') ? ']' : '[]';
indvdum@0
   681
			}
indvdum@0
   682
		}
indvdum@0
   683
		while ($in);
indvdum@0
   684
indvdum@0
   685
		// do we have some tags open? close them now
indvdum@0
   686
		if (sizeof($item_end_tags))
indvdum@0
   687
		{
indvdum@0
   688
			$out .= '[' . implode('][', $item_end_tags) . ']';
indvdum@0
   689
		}
indvdum@0
   690
		if (sizeof($list_end_tags))
indvdum@0
   691
		{
indvdum@0
   692
			$out .= '[' . implode('][', $list_end_tags) . ']';
indvdum@0
   693
		}
indvdum@0
   694
indvdum@0
   695
		return $out;
indvdum@0
   696
	}
indvdum@0
   697
indvdum@0
   698
	/**
indvdum@0
   699
	* Parse quote bbcode
indvdum@0
   700
	* Expects the argument to start with a tag
indvdum@0
   701
	*/
indvdum@0
   702
	function bbcode_quote($in)
indvdum@0
   703
	{
indvdum@0
   704
		global $config, $user;
indvdum@0
   705
indvdum@0
   706
		/**
indvdum@0
   707
		* If you change this code, make sure the cases described within the following reports are still working:
indvdum@0
   708
		* #3572 - [quote="[test]test"]test [ test[/quote] - (correct: parsed)
indvdum@0
   709
		* #14667 - [quote]test[/quote] test ] and [ test [quote]test[/quote] (correct: parsed)
indvdum@0
   710
		* #14770 - [quote="["]test[/quote] (correct: parsed)
indvdum@0
   711
		* [quote="[i]test[/i]"]test[/quote] (correct: parsed)
indvdum@0
   712
		* [quote="[quote]test[/quote]"]test[/quote] (correct: parsed - Username displayed as [quote]test[/quote])
indvdum@0
   713
		* #20735 - [quote]test[/[/b]quote] test [/quote][/quote] test - (correct: quoted: "test[/[/b]quote] test" / non-quoted: "[/quote] test" - also failed if layout distorted)
indvdum@0
   714
		* #40565 - [quote="a"]a[/quote][quote="a]a[/quote] (correct: first quote tag parsed, second quote tag unparsed)
indvdum@0
   715
		*/
indvdum@0
   716
indvdum@0
   717
		$in = str_replace("\r\n", "\n", str_replace('\"', '"', trim($in)));
indvdum@0
   718
indvdum@0
   719
		if (!$in)
indvdum@0
   720
		{
indvdum@0
   721
			return '';
indvdum@0
   722
		}
indvdum@0
   723
indvdum@0
   724
		// To let the parser not catch tokens within quote_username quotes we encode them before we start this...
indvdum@0
   725
		$in = preg_replace('#quote=&quot;(.*?)&quot;\]#ie', "'quote=&quot;' . str_replace(array('[', ']', '\\\"'), array('&#91;', '&#93;', '\"'), '\$1') . '&quot;]'", $in);
indvdum@0
   726
indvdum@0
   727
		$tok = ']';
indvdum@0
   728
		$out = '[';
indvdum@0
   729
indvdum@0
   730
		$in = substr($in, 1);
indvdum@0
   731
		$close_tags = $error_ary = array();
indvdum@0
   732
		$buffer = '';
indvdum@0
   733
indvdum@0
   734
		do
indvdum@0
   735
		{
indvdum@0
   736
			$pos = strlen($in);
indvdum@0
   737
			for ($i = 0, $tok_len = strlen($tok); $i < $tok_len; ++$i)
indvdum@0
   738
			{
indvdum@0
   739
				$tmp_pos = strpos($in, $tok[$i]);
indvdum@0
   740
				if ($tmp_pos !== false && $tmp_pos < $pos)
indvdum@0
   741
				{
indvdum@0
   742
					$pos = $tmp_pos;
indvdum@0
   743
				}
indvdum@0
   744
			}
indvdum@0
   745
indvdum@0
   746
			$buffer .= substr($in, 0, $pos);
indvdum@0
   747
			$tok = $in[$pos];
indvdum@0
   748
			$in = substr($in, $pos + 1);
indvdum@0
   749
indvdum@0
   750
			if ($tok == ']')
indvdum@0
   751
			{
indvdum@0
   752
				if (strtolower($buffer) == '/quote' && sizeof($close_tags) && substr($out, -1, 1) == '[')
indvdum@0
   753
				{
indvdum@0
   754
					// we have found a closing tag
indvdum@0
   755
					$out .= array_pop($close_tags) . ']';
indvdum@0
   756
					$tok = '[';
indvdum@0
   757
					$buffer = '';
indvdum@0
   758
indvdum@0
   759
					/* Add space at the end of the closing tag if not happened before to allow following urls/smilies to be parsed correctly
indvdum@0
   760
					* Do not try to think for the user. :/ Do not parse urls/smilies if there is no space - is the same as with other bbcodes too.
indvdum@0
   761
					* Also, we won't have any spaces within $in anyway, only adding up spaces -> #10982
indvdum@0
   762
					if (!$in || $in[0] !== ' ')
indvdum@0
   763
					{
indvdum@0
   764
						$out .= ' ';
indvdum@0
   765
					}*/
indvdum@0
   766
				}
indvdum@0
   767
				else if (preg_match('#^quote(?:=&quot;(.*?)&quot;)?$#is', $buffer, $m) && substr($out, -1, 1) == '[')
indvdum@0
   768
				{
indvdum@0
   769
					$this->parsed_items['quote']++;
indvdum@0
   770
indvdum@0
   771
					// the buffer holds a valid opening tag
indvdum@0
   772
					if ($config['max_quote_depth'] && sizeof($close_tags) >= $config['max_quote_depth'])
indvdum@0
   773
					{
indvdum@0
   774
						// there are too many nested quotes
indvdum@0
   775
						$error_ary['quote_depth'] = sprintf($user->lang['QUOTE_DEPTH_EXCEEDED'], $config['max_quote_depth']);
indvdum@0
   776
indvdum@0
   777
						$out .= $buffer . $tok;
indvdum@0
   778
						$tok = '[]';
indvdum@0
   779
						$buffer = '';
indvdum@0
   780
indvdum@0
   781
						continue;
indvdum@0
   782
					}
indvdum@0
   783
indvdum@0
   784
					array_push($close_tags, '/quote:' . $this->bbcode_uid);
indvdum@0
   785
indvdum@0
   786
					if (isset($m[1]) && $m[1])
indvdum@0
   787
					{
indvdum@0
   788
						$username = str_replace(array('&#91;', '&#93;'), array('[', ']'), $m[1]);
indvdum@0
   789
						$username = preg_replace('#\[(?!b|i|u|color|url|email|/b|/i|/u|/color|/url|/email)#iU', '&#91;$1', $username);
indvdum@0
   790
indvdum@0
   791
						$end_tags = array();
indvdum@0
   792
						$error = false;
indvdum@0
   793
indvdum@0
   794
						preg_match_all('#\[((?:/)?(?:[a-z]+))#i', $username, $tags);
indvdum@0
   795
						foreach ($tags[1] as $tag)
indvdum@0
   796
						{
indvdum@0
   797
							if ($tag[0] != '/')
indvdum@0
   798
							{
indvdum@0
   799
								$end_tags[] = '/' . $tag;
indvdum@0
   800
							}
indvdum@0
   801
							else
indvdum@0
   802
							{
indvdum@0
   803
								$end_tag = array_pop($end_tags);
indvdum@0
   804
								$error = ($end_tag != $tag) ? true : false;
indvdum@0
   805
							}
indvdum@0
   806
						}
indvdum@0
   807
indvdum@0
   808
						if ($error)
indvdum@0
   809
						{
indvdum@0
   810
							$username = $m[1];
indvdum@0
   811
						}
indvdum@0
   812
indvdum@0
   813
						$out .= 'quote=&quot;' . $username . '&quot;:' . $this->bbcode_uid . ']';
indvdum@0
   814
					}
indvdum@0
   815
					else
indvdum@0
   816
					{
indvdum@0
   817
						$out .= 'quote:' . $this->bbcode_uid . ']';
indvdum@0
   818
					}
indvdum@0
   819
indvdum@0
   820
					$tok = '[';
indvdum@0
   821
					$buffer = '';
indvdum@0
   822
				}
indvdum@0
   823
				else if (preg_match('#^quote=&quot;(.*?)#is', $buffer, $m))
indvdum@0
   824
				{
indvdum@0
   825
					// the buffer holds an invalid opening tag
indvdum@0
   826
					$buffer .= ']';
indvdum@0
   827
				}
indvdum@0
   828
				else
indvdum@0
   829
				{
indvdum@0
   830
					$out .= $buffer . $tok;
indvdum@0
   831
					$tok = '[]';
indvdum@0
   832
					$buffer = '';
indvdum@0
   833
				}
indvdum@0
   834
			}
indvdum@0
   835
			else
indvdum@0
   836
			{
indvdum@0
   837
/**
indvdum@0
   838
*				Old quote code working fine, but having errors listed in bug #3572
indvdum@0
   839
*
indvdum@0
   840
*				$out .= $buffer . $tok;
indvdum@0
   841
*				$tok = ($tok == '[') ? ']' : '[]';
indvdum@0
   842
*				$buffer = '';
indvdum@0
   843
*/
indvdum@0
   844
indvdum@0
   845
				$out .= $buffer . $tok;
indvdum@0
   846
indvdum@0
   847
				if ($tok == '[')
indvdum@0
   848
				{
indvdum@0
   849
					// Search the text for the next tok... if an ending quote comes first, then change tok to []
indvdum@0
   850
					$pos1 = stripos($in, '[/quote');
indvdum@0
   851
					// If the token ] comes first, we change it to ]
indvdum@0
   852
					$pos2 = strpos($in, ']');
indvdum@0
   853
					// If the token [ comes first, we change it to [
indvdum@0
   854
					$pos3 = strpos($in, '[');
indvdum@0
   855
indvdum@0
   856
					if ($pos1 !== false && ($pos2 === false || $pos1 < $pos2) && ($pos3 === false || $pos1 < $pos3))
indvdum@0
   857
					{
indvdum@0
   858
						$tok = '[]';
indvdum@0
   859
					}
indvdum@0
   860
					else if ($pos3 !== false && ($pos2 === false || $pos3 < $pos2))
indvdum@0
   861
					{
indvdum@0
   862
						$tok = '[';
indvdum@0
   863
					}
indvdum@0
   864
					else
indvdum@0
   865
					{
indvdum@0
   866
						$tok = ']';
indvdum@0
   867
					}
indvdum@0
   868
				}
indvdum@0
   869
				else
indvdum@0
   870
				{
indvdum@0
   871
					$tok = '[]';
indvdum@0
   872
				}
indvdum@0
   873
				$buffer = '';
indvdum@0
   874
			}
indvdum@0
   875
		}
indvdum@0
   876
		while ($in);
indvdum@0
   877
indvdum@0
   878
		$out .= $buffer;
indvdum@0
   879
indvdum@0
   880
		if (sizeof($close_tags))
indvdum@0
   881
		{
indvdum@0
   882
			$out .= '[' . implode('][', $close_tags) . ']';
indvdum@0
   883
		}
indvdum@0
   884
indvdum@0
   885
		foreach ($error_ary as $error_msg)
indvdum@0
   886
		{
indvdum@0
   887
			$this->warn_msg[] = $error_msg;
indvdum@0
   888
		}
indvdum@0
   889
indvdum@0
   890
		return $out;
indvdum@0
   891
	}
indvdum@0
   892
indvdum@0
   893
	/**
indvdum@0
   894
	* Validate email
indvdum@0
   895
	*/
indvdum@0
   896
	function validate_email($var1, $var2)
indvdum@0
   897
	{
indvdum@0
   898
		$var1 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var1)));
indvdum@0
   899
		$var2 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var2)));
indvdum@0
   900
indvdum@0
   901
		$txt = $var2;
indvdum@0
   902
		$email = ($var1) ? $var1 : $var2;
indvdum@0
   903
indvdum@0
   904
		$validated = true;
indvdum@0
   905
indvdum@0
   906
		if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
indvdum@0
   907
		{
indvdum@0
   908
			$validated = false;
indvdum@0
   909
		}
indvdum@0
   910
indvdum@0
   911
		if (!$validated)
indvdum@0
   912
		{
indvdum@0
   913
			return '[email' . (($var1) ? "=$var1" : '') . ']' . $var2 . '[/email]';
indvdum@0
   914
		}
indvdum@0
   915
indvdum@0
   916
		$this->parsed_items['email']++;
indvdum@0
   917
indvdum@0
   918
		if ($var1)
indvdum@0
   919
		{
indvdum@0
   920
			$retval = '[email=' . $this->bbcode_specialchars($email) . ':' . $this->bbcode_uid . ']' . $txt . '[/email:' . $this->bbcode_uid . ']';
indvdum@0
   921
		}
indvdum@0
   922
		else
indvdum@0
   923
		{
indvdum@0
   924
			$retval = '[email:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($email) . '[/email:' . $this->bbcode_uid . ']';
indvdum@0
   925
		}
indvdum@0
   926
indvdum@0
   927
		return $retval;
indvdum@0
   928
	}
indvdum@0
   929
indvdum@0
   930
	/**
indvdum@0
   931
	* Validate url
indvdum@0
   932
	*
indvdum@0
   933
	* @param string $var1 optional url parameter for url bbcode: [url(=$var1)]$var2[/url]
indvdum@0
   934
	* @param string $var2 url bbcode content: [url(=$var1)]$var2[/url]
indvdum@0
   935
	*/
indvdum@0
   936
	function validate_url($var1, $var2)
indvdum@0
   937
	{
indvdum@0
   938
		global $config;
indvdum@0
   939
indvdum@0
   940
		$var1 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var1)));
indvdum@0
   941
		$var2 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var2)));
indvdum@0
   942
indvdum@0
   943
		$url = ($var1) ? $var1 : $var2;
indvdum@0
   944
indvdum@0
   945
		if ($var1 && !$var2)
indvdum@0
   946
		{
indvdum@0
   947
			$var2 = $var1;
indvdum@0
   948
		}
indvdum@0
   949
indvdum@0
   950
		if (!$url)
indvdum@0
   951
		{
indvdum@0
   952
			return '[url' . (($var1) ? '=' . $var1 : '') . ']' . $var2 . '[/url]';
indvdum@0
   953
		}
indvdum@0
   954
indvdum@0
   955
		$valid = false;
indvdum@0
   956
indvdum@0
   957
		$url = str_replace(' ', '%20', $url);
indvdum@0
   958
indvdum@0
   959
		// Checking urls
indvdum@0
   960
		if (preg_match('#^' . get_preg_expression('url') . '$#i', $url) ||
indvdum@0
   961
			preg_match('#^' . get_preg_expression('www_url') . '$#i', $url) ||
indvdum@0
   962
			preg_match('#^' . preg_quote(generate_board_url(), '#') . get_preg_expression('relative_url') . '$#i', $url))
indvdum@0
   963
		{
indvdum@0
   964
			$valid = true;
indvdum@0
   965
		}
indvdum@0
   966
indvdum@0
   967
		if ($valid)
indvdum@0
   968
		{
indvdum@0
   969
			$this->parsed_items['url']++;
indvdum@0
   970
indvdum@0
   971
			// if there is no scheme, then add http schema
indvdum@0
   972
			if (!preg_match('#^[a-z][a-z\d+\-.]*:/{2}#i', $url))
indvdum@0
   973
			{
indvdum@0
   974
				$url = 'http://' . $url;
indvdum@0
   975
			}
indvdum@0
   976
indvdum@0
   977
			// Is this a link to somewhere inside this board? If so then remove the session id from the url
indvdum@0
   978
			if (strpos($url, generate_board_url()) !== false && strpos($url, 'sid=') !== false)
indvdum@0
   979
			{
indvdum@0
   980
				$url = preg_replace('/(&amp;|\?)sid=[0-9a-f]{32}&amp;/', '\1', $url);
indvdum@0
   981
				$url = preg_replace('/(&amp;|\?)sid=[0-9a-f]{32}$/', '', $url);
indvdum@0
   982
				$url = append_sid($url);
indvdum@0
   983
			}
indvdum@0
   984
indvdum@0
   985
			return ($var1) ? '[url=' . $this->bbcode_specialchars($url) . ':' . $this->bbcode_uid . ']' . $var2 . '[/url:' . $this->bbcode_uid . ']' : '[url:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($url) . '[/url:' . $this->bbcode_uid . ']';
indvdum@0
   986
		}
indvdum@0
   987
indvdum@0
   988
		return '[url' . (($var1) ? '=' . $var1 : '') . ']' . $var2 . '[/url]';
indvdum@0
   989
	}
indvdum@0
   990
indvdum@0
   991
	/**
indvdum@0
   992
	* Check if url is pointing to this domain/script_path/php-file
indvdum@0
   993
	*
indvdum@0
   994
	* @param string $url the url to check
indvdum@0
   995
	* @return true if the url is pointing to this domain/script_path/php-file, false if not
indvdum@0
   996
	*
indvdum@0
   997
	* @access private
indvdum@0
   998
	*/
indvdum@0
   999
	function path_in_domain($url)
indvdum@0
  1000
	{
indvdum@0
  1001
		global $config, $phpEx, $user;
indvdum@0
  1002
indvdum@0
  1003
		if ($config['force_server_vars'])
indvdum@0
  1004
		{
indvdum@0
  1005
			$check_path = $config['script_path'];
indvdum@0
  1006
		}
indvdum@0
  1007
		else
indvdum@0
  1008
		{
indvdum@0
  1009
			$check_path = ($user->page['root_script_path'] != '/') ? substr($user->page['root_script_path'], 0, -1) : '/';
indvdum@0
  1010
		}
indvdum@0
  1011
indvdum@0
  1012
		// Is the user trying to link to a php file in this domain and script path?
indvdum@0
  1013
		if (strpos($url, ".{$phpEx}") !== false && strpos($url, $check_path) !== false)
indvdum@0
  1014
		{
indvdum@0
  1015
			$server_name = $user->host;
indvdum@0
  1016
indvdum@0
  1017
			// Forcing server vars is the only way to specify/override the protocol
indvdum@0
  1018
			if ($config['force_server_vars'] || !$server_name)
indvdum@0
  1019
			{
indvdum@0
  1020
				$server_name = $config['server_name'];
indvdum@0
  1021
			}
indvdum@0
  1022
indvdum@0
  1023
			// Check again in correct order...
indvdum@0
  1024
			$pos_ext = strpos($url, ".{$phpEx}");
indvdum@0
  1025
			$pos_path = strpos($url, $check_path);
indvdum@0
  1026
			$pos_domain = strpos($url, $server_name);
indvdum@0
  1027
indvdum@0
  1028
			if ($pos_domain !== false && $pos_path >= $pos_domain && $pos_ext >= $pos_path)
indvdum@0
  1029
			{
indvdum@0
  1030
				// Ok, actually we allow linking to some files (this may be able to be extended in some way later...)
indvdum@0
  1031
				if (strpos($url, '/' . $check_path . '/download/file.' . $phpEx) !== 0)
indvdum@0
  1032
				{
indvdum@0
  1033
					return false;
indvdum@0
  1034
				}
indvdum@0
  1035
indvdum@0
  1036
				return true;
indvdum@0
  1037
			}
indvdum@0
  1038
		}
indvdum@0
  1039
indvdum@0
  1040
		return false;
indvdum@0
  1041
	}
indvdum@0
  1042
}
indvdum@0
  1043
indvdum@0
  1044
/**
indvdum@0
  1045
* Main message parser for posting, pm, etc. takes raw message
indvdum@0
  1046
* and parses it for attachments, bbcode and smilies
indvdum@0
  1047
* @package phpBB3
indvdum@0
  1048
*/
indvdum@0
  1049
class parse_message extends bbcode_firstpass
indvdum@0
  1050
{
indvdum@0
  1051
	var $attachment_data = array();
indvdum@0
  1052
	var $filename_data = array();
indvdum@0
  1053
indvdum@0
  1054
	// Helps ironing out user error
indvdum@0
  1055
	var $message_status = '';
indvdum@0
  1056
indvdum@0
  1057
	var $allow_img_bbcode = true;
indvdum@0
  1058
	var $allow_flash_bbcode = true;
indvdum@0
  1059
	var $allow_quote_bbcode = true;
indvdum@0
  1060
	var $allow_url_bbcode = true;
indvdum@0
  1061
indvdum@0
  1062
	var $mode;
indvdum@0
  1063
indvdum@0
  1064
	/**
indvdum@0
  1065
	* Init - give message here or manually
indvdum@0
  1066
	*/
indvdum@0
  1067
	function parse_message($message = '')
indvdum@0
  1068
	{
indvdum@0
  1069
		// Init BBCode UID
indvdum@0
  1070
		$this->bbcode_uid = substr(base_convert(unique_id(), 16, 36), 0, BBCODE_UID_LEN);
indvdum@0
  1071
		$this->message = $message;
indvdum@0
  1072
	}
indvdum@0
  1073
indvdum@0
  1074
	/**
indvdum@0
  1075
	* Parse Message
indvdum@0
  1076
	*/
indvdum@0
  1077
	function parse($allow_bbcode, $allow_magic_url, $allow_smilies, $allow_img_bbcode = true, $allow_flash_bbcode = true, $allow_quote_bbcode = true, $allow_url_bbcode = true, $update_this_message = true, $mode = 'post')
indvdum@0
  1078
	{
indvdum@0
  1079
		global $config, $db, $user;
indvdum@0
  1080
indvdum@0
  1081
		$this->mode = $mode;
indvdum@0
  1082
indvdum@0
  1083
		foreach (array('chars', 'smilies', 'urls', 'font_size', 'img_height', 'img_width') as $key)
indvdum@0
  1084
		{
indvdum@0
  1085
			if (!isset($config['max_' . $mode . '_' . $key]))
indvdum@0
  1086
			{
indvdum@0
  1087
				$config['max_' . $mode . '_' . $key] = 0;
indvdum@0
  1088
			}
indvdum@0
  1089
		}
indvdum@0
  1090
indvdum@0
  1091
		$this->allow_img_bbcode = $allow_img_bbcode;
indvdum@0
  1092
		$this->allow_flash_bbcode = $allow_flash_bbcode;
indvdum@0
  1093
		$this->allow_quote_bbcode = $allow_quote_bbcode;
indvdum@0
  1094
		$this->allow_url_bbcode = $allow_url_bbcode;
indvdum@0
  1095
indvdum@0
  1096
		// If false, then $this->message won't be altered, the text will be returned instead.
indvdum@0
  1097
		if (!$update_this_message)
indvdum@0
  1098
		{
indvdum@0
  1099
			$tmp_message = $this->message;
indvdum@0
  1100
			$return_message = &$this->message;
indvdum@0
  1101
		}
indvdum@0
  1102
indvdum@0
  1103
		if ($this->message_status == 'display')
indvdum@0
  1104
		{
indvdum@0
  1105
			$this->decode_message();
indvdum@0
  1106
		}
indvdum@0
  1107
indvdum@0
  1108
		// Do some general 'cleanup' first before processing message,
indvdum@0
  1109
		// e.g. remove excessive newlines(?), smilies(?)
indvdum@0
  1110
		$match = array('#(script|about|applet|activex|chrome):#i');
indvdum@0
  1111
		$replace = array("\\1&#058;");
indvdum@0
  1112
		$this->message = preg_replace($match, $replace, trim($this->message));
indvdum@0
  1113
indvdum@0
  1114
		// Store message length...
indvdum@0
  1115
		$message_length = ($mode == 'post') ? utf8_strlen($this->message) : utf8_strlen(preg_replace('#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius', ' ', $this->message));
indvdum@0
  1116
indvdum@0
  1117
		// Maximum message length check. 0 disables this check completely.
indvdum@0
  1118
		if ((int) $config['max_' . $mode . '_chars'] > 0 && $message_length > (int) $config['max_' . $mode . '_chars'])
indvdum@0
  1119
		{
indvdum@0
  1120
			$this->warn_msg[] = sprintf($user->lang['TOO_MANY_CHARS_' . strtoupper($mode)], $message_length, (int) $config['max_' . $mode . '_chars']);
indvdum@0
  1121
			return (!$update_this_message) ? $return_message : $this->warn_msg;
indvdum@0
  1122
		}
indvdum@0
  1123
indvdum@0
  1124
		// Minimum message length check for post only
indvdum@0
  1125
		if ($mode === 'post')
indvdum@0
  1126
		{
indvdum@0
  1127
			if (!$message_length || $message_length < (int) $config['min_post_chars'])
indvdum@0
  1128
			{
indvdum@0
  1129
				$this->warn_msg[] = (!$message_length) ? $user->lang['TOO_FEW_CHARS'] : sprintf($user->lang['TOO_FEW_CHARS_LIMIT'], $message_length, (int) $config['min_post_chars']);
indvdum@0
  1130
				return (!$update_this_message) ? $return_message : $this->warn_msg;
indvdum@0
  1131
			}
indvdum@0
  1132
		}
indvdum@0
  1133
indvdum@0
  1134
		// Prepare BBcode (just prepares some tags for better parsing)
indvdum@0
  1135
		if ($allow_bbcode && strpos($this->message, '[') !== false)
indvdum@0
  1136
		{
indvdum@0
  1137
			$this->bbcode_init();
indvdum@0
  1138
			$disallow = array('img', 'flash', 'quote', 'url');
indvdum@0
  1139
			foreach ($disallow as $bool)
indvdum@0
  1140
			{
indvdum@0
  1141
				if (!${'allow_' . $bool . '_bbcode'})
indvdum@0
  1142
				{
indvdum@0
  1143
					$this->bbcodes[$bool]['disabled'] = true;
indvdum@0
  1144
				}
indvdum@0
  1145
			}
indvdum@0
  1146
indvdum@0
  1147
			$this->prepare_bbcodes();
indvdum@0
  1148
		}
indvdum@0
  1149
indvdum@0
  1150
		// Parse smilies
indvdum@0
  1151
		if ($allow_smilies)
indvdum@0
  1152
		{
indvdum@0
  1153
			$this->smilies($config['max_' . $mode . '_smilies']);
indvdum@0
  1154
		}
indvdum@0
  1155
indvdum@0
  1156
		$num_urls = 0;
indvdum@0
  1157
indvdum@0
  1158
		// Parse BBCode
indvdum@0
  1159
		if ($allow_bbcode && strpos($this->message, '[') !== false)
indvdum@0
  1160
		{
indvdum@0
  1161
			$this->parse_bbcode();
indvdum@0
  1162
			$num_urls += $this->parsed_items['url'];
indvdum@0
  1163
		}
indvdum@0
  1164
indvdum@0
  1165
		// Parse URL's
indvdum@0
  1166
		if ($allow_magic_url)
indvdum@0
  1167
		{
indvdum@0
  1168
			$this->magic_url(generate_board_url());
indvdum@0
  1169
indvdum@0
  1170
			if ($config['max_' . $mode . '_urls'])
indvdum@0
  1171
			{
indvdum@0
  1172
				$num_urls += preg_match_all('#\<!-- ([lmwe]) --\>.*?\<!-- \1 --\>#', $this->message, $matches);
indvdum@0
  1173
			}
indvdum@0
  1174
		}
indvdum@0
  1175
indvdum@0
  1176
		// Check for "empty" message. We do not check here for maximum length, because bbcode, smilies, etc. can add to the length.
indvdum@0
  1177
		// The maximum length check happened before any parsings.
indvdum@0
  1178
		if ($mode === 'post' && utf8_clean_string($this->message) === '')
indvdum@0
  1179
		{
indvdum@0
  1180
			$this->warn_msg[] = $user->lang['TOO_FEW_CHARS'];
indvdum@0
  1181
			return (!$update_this_message) ? $return_message : $this->warn_msg;
indvdum@0
  1182
		}
indvdum@0
  1183
indvdum@0
  1184
		// Check number of links
indvdum@0
  1185
		if ($config['max_' . $mode . '_urls'] && $num_urls > $config['max_' . $mode . '_urls'])
indvdum@0
  1186
		{
indvdum@0
  1187
			$this->warn_msg[] = sprintf($user->lang['TOO_MANY_URLS'], $config['max_' . $mode . '_urls']);
indvdum@0
  1188
			return (!$update_this_message) ? $return_message : $this->warn_msg;
indvdum@0
  1189
		}
indvdum@0
  1190
indvdum@0
  1191
		if (!$update_this_message)
indvdum@0
  1192
		{
indvdum@0
  1193
			unset($this->message);
indvdum@0
  1194
			$this->message = $tmp_message;
indvdum@0
  1195
			return $return_message;
indvdum@0
  1196
		}
indvdum@0
  1197
indvdum@0
  1198
		$this->message_status = 'parsed';
indvdum@0
  1199
		return false;
indvdum@0
  1200
	}
indvdum@0
  1201
indvdum@0
  1202
	/**
indvdum@0
  1203
	* Formatting text for display
indvdum@0
  1204
	*/
indvdum@0
  1205
	function format_display($allow_bbcode, $allow_magic_url, $allow_smilies, $update_this_message = true)
indvdum@0
  1206
	{
indvdum@0
  1207
		// If false, then the parsed message get returned but internal message not processed.
indvdum@0
  1208
		if (!$update_this_message)
indvdum@0
  1209
		{
indvdum@0
  1210
			$tmp_message = $this->message;
indvdum@0
  1211
			$return_message = &$this->message;
indvdum@0
  1212
		}
indvdum@0
  1213
indvdum@0
  1214
		if ($this->message_status == 'plain')
indvdum@0
  1215
		{
indvdum@0
  1216
			// Force updating message - of course.
indvdum@0
  1217
			$this->parse($allow_bbcode, $allow_magic_url, $allow_smilies, $this->allow_img_bbcode, $this->allow_flash_bbcode, $this->allow_quote_bbcode, $this->allow_url_bbcode, true);
indvdum@0
  1218
		}
indvdum@0
  1219
indvdum@0
  1220
		// Replace naughty words such as farty pants
indvdum@0
  1221
		$this->message = censor_text($this->message);
indvdum@0
  1222
indvdum@0
  1223
		// Parse BBcode
indvdum@0
  1224
		if ($allow_bbcode)
indvdum@0
  1225
		{
indvdum@0
  1226
			$this->bbcode_cache_init();
indvdum@0
  1227
indvdum@0
  1228
			// We are giving those parameters to be able to use the bbcode class on its own
indvdum@0
  1229
			$this->bbcode_second_pass($this->message, $this->bbcode_uid);
indvdum@0
  1230
		}
indvdum@0
  1231
indvdum@0
  1232
		$this->message = bbcode_nl2br($this->message);
indvdum@0
  1233
		$this->message = smiley_text($this->message, !$allow_smilies);
indvdum@0
  1234
indvdum@0
  1235
		if (!$update_this_message)
indvdum@0
  1236
		{
indvdum@0
  1237
			unset($this->message);
indvdum@0
  1238
			$this->message = $tmp_message;
indvdum@0
  1239
			return $return_message;
indvdum@0
  1240
		}
indvdum@0
  1241
indvdum@0
  1242
		$this->message_status = 'display';
indvdum@0
  1243
		return false;
indvdum@0
  1244
	}
indvdum@0
  1245
indvdum@0
  1246
	/**
indvdum@0
  1247
	* Decode message to be placed back into form box
indvdum@0
  1248
	*/
indvdum@0
  1249
	function decode_message($custom_bbcode_uid = '', $update_this_message = true)
indvdum@0
  1250
	{
indvdum@0
  1251
		// If false, then the parsed message get returned but internal message not processed.
indvdum@0
  1252
		if (!$update_this_message)
indvdum@0
  1253
		{
indvdum@0
  1254
			$tmp_message = $this->message;
indvdum@0
  1255
			$return_message = &$this->message;
indvdum@0
  1256
		}
indvdum@0
  1257
indvdum@0
  1258
		($custom_bbcode_uid) ? decode_message($this->message, $custom_bbcode_uid) : decode_message($this->message, $this->bbcode_uid);
indvdum@0
  1259
indvdum@0
  1260
		if (!$update_this_message)
indvdum@0
  1261
		{
indvdum@0
  1262
			unset($this->message);
indvdum@0
  1263
			$this->message = $tmp_message;
indvdum@0
  1264
			return $return_message;
indvdum@0
  1265
		}
indvdum@0
  1266
indvdum@0
  1267
		$this->message_status = 'plain';
indvdum@0
  1268
		return false;
indvdum@0
  1269
	}
indvdum@0
  1270
indvdum@0
  1271
	/**
indvdum@0
  1272
	* Replace magic urls of form http://xxx.xxx., www.xxx. and xxx@xxx.xxx.
indvdum@0
  1273
	* Cuts down displayed size of link if over 50 chars, turns absolute links
indvdum@0
  1274
	* into relative versions when the server/script path matches the link
indvdum@0
  1275
	*/
indvdum@0
  1276
	function magic_url($server_url)
indvdum@0
  1277
	{
indvdum@0
  1278
		// We use the global make_clickable function
indvdum@0
  1279
		$this->message = make_clickable($this->message, $server_url);
indvdum@0
  1280
	}
indvdum@0
  1281
indvdum@0
  1282
	/**
indvdum@0
  1283
	* Parse Smilies
indvdum@0
  1284
	*/
indvdum@0
  1285
	function smilies($max_smilies = 0)
indvdum@0
  1286
	{
indvdum@0
  1287
		global $db, $user;
indvdum@0
  1288
		static $match;
indvdum@0
  1289
		static $replace;
indvdum@0
  1290
indvdum@0
  1291
		// See if the static arrays have already been filled on an earlier invocation
indvdum@0
  1292
		if (!is_array($match))
indvdum@0
  1293
		{
indvdum@0
  1294
			$match = $replace = array();
indvdum@0
  1295
indvdum@0
  1296
			// NOTE: obtain_* function? chaching the table contents?
indvdum@0
  1297
indvdum@0
  1298
			// For now setting the ttl to 10 minutes
indvdum@0
  1299
			switch ($db->sql_layer)
indvdum@0
  1300
			{
indvdum@0
  1301
				case 'mssql':
indvdum@0
  1302
				case 'mssql_odbc':
indvdum@0
  1303
				case 'mssqlnative':
indvdum@0
  1304
					$sql = 'SELECT *
indvdum@0
  1305
						FROM ' . SMILIES_TABLE . '
indvdum@0
  1306
						ORDER BY LEN(code) DESC';
indvdum@0
  1307
				break;
indvdum@0
  1308
indvdum@0
  1309
				case 'firebird':
indvdum@0
  1310
					$sql = 'SELECT *
indvdum@0
  1311
						FROM ' . SMILIES_TABLE . '
indvdum@0
  1312
						ORDER BY CHAR_LENGTH(code) DESC';
indvdum@0
  1313
				break;
indvdum@0
  1314
indvdum@0
  1315
				// LENGTH supported by MySQL, IBM DB2, Oracle and Access for sure...
indvdum@0
  1316
				default:
indvdum@0
  1317
					$sql = 'SELECT *
indvdum@0
  1318
						FROM ' . SMILIES_TABLE . '
indvdum@0
  1319
						ORDER BY LENGTH(code) DESC';
indvdum@0
  1320
				break;
indvdum@0
  1321
			}
indvdum@0
  1322
			$result = $db->sql_query($sql, 600);
indvdum@0
  1323
indvdum@0
  1324
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  1325
			{
indvdum@0
  1326
				if (empty($row['code']))
indvdum@0
  1327
				{
indvdum@0
  1328
					continue;
indvdum@0
  1329
				}
indvdum@0
  1330
indvdum@0
  1331
				// (assertion)
indvdum@0
  1332
				$match[] = preg_quote($row['code'], '#');
indvdum@0
  1333
				$replace[] = '<!-- s' . $row['code'] . ' --><img src="{SMILIES_PATH}/' . $row['smiley_url'] . '" alt="' . $row['code'] . '" title="' . $row['emotion'] . '" /><!-- s' . $row['code'] . ' -->';
indvdum@0
  1334
			}
indvdum@0
  1335
			$db->sql_freeresult($result);
indvdum@0
  1336
		}
indvdum@0
  1337
indvdum@0
  1338
		if (sizeof($match))
indvdum@0
  1339
		{
indvdum@0
  1340
			if ($max_smilies)
indvdum@0
  1341
			{
indvdum@0
  1342
				// 'u' modifier has been added to correctly parse smilies within unicode strings
indvdum@0
  1343
				// For details: http://tracker.phpbb.com/browse/PHPBB3-10117
indvdum@0
  1344
				$num_matches = preg_match_all('#(?<=^|[\n .])(?:' . implode('|', $match) . ')(?![^<>]*>)#u', $this->message, $matches);
indvdum@0
  1345
				unset($matches);
indvdum@0
  1346
indvdum@0
  1347
				if ($num_matches !== false && $num_matches > $max_smilies)
indvdum@0
  1348
				{
indvdum@0
  1349
					$this->warn_msg[] = sprintf($user->lang['TOO_MANY_SMILIES'], $max_smilies);
indvdum@0
  1350
					return;
indvdum@0
  1351
				}
indvdum@0
  1352
			}
indvdum@0
  1353
indvdum@0
  1354
			// Make sure the delimiter # is added in front and at the end of every element within $match
indvdum@0
  1355
			// 'u' modifier has been added to correctly parse smilies within unicode strings
indvdum@0
  1356
			// For details: http://tracker.phpbb.com/browse/PHPBB3-10117
indvdum@0
  1357
indvdum@0
  1358
			$this->message = trim(preg_replace(explode(chr(0), '#(?<=^|[\n .])' . implode('(?![^<>]*>)#u' . chr(0) . '#(?<=^|[\n .])', $match) . '(?![^<>]*>)#u'), $replace, $this->message));
indvdum@0
  1359
		}
indvdum@0
  1360
	}
indvdum@0
  1361
indvdum@0
  1362
	/**
indvdum@0
  1363
	* Parse Attachments
indvdum@0
  1364
	*/
indvdum@0
  1365
	function parse_attachments($form_name, $mode, $forum_id, $submit, $preview, $refresh, $is_message = false)
indvdum@0
  1366
	{
indvdum@0
  1367
		global $config, $auth, $user, $phpbb_root_path, $phpEx, $db;
indvdum@0
  1368
indvdum@0
  1369
		$error = array();
indvdum@0
  1370
indvdum@0
  1371
		$num_attachments = sizeof($this->attachment_data);
indvdum@0
  1372
		$this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true));
indvdum@0
  1373
		$upload_file = (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none' && trim($_FILES[$form_name]['name'])) ? true : false;
indvdum@0
  1374
indvdum@0
  1375
		$add_file		= (isset($_POST['add_file'])) ? true : false;
indvdum@0
  1376
		$delete_file	= (isset($_POST['delete_file'])) ? true : false;
indvdum@0
  1377
indvdum@0
  1378
		// First of all adjust comments if changed
indvdum@0
  1379
		$actual_comment_list = utf8_normalize_nfc(request_var('comment_list', array(''), true));
indvdum@0
  1380
indvdum@0
  1381
		foreach ($actual_comment_list as $comment_key => $comment)
indvdum@0
  1382
		{
indvdum@0
  1383
			if (!isset($this->attachment_data[$comment_key]))
indvdum@0
  1384
			{
indvdum@0
  1385
				continue;
indvdum@0
  1386
			}
indvdum@0
  1387
indvdum@0
  1388
			if ($this->attachment_data[$comment_key]['attach_comment'] != $actual_comment_list[$comment_key])
indvdum@0
  1389
			{
indvdum@0
  1390
				$this->attachment_data[$comment_key]['attach_comment'] = $actual_comment_list[$comment_key];
indvdum@0
  1391
			}
indvdum@0
  1392
		}
indvdum@0
  1393
indvdum@0
  1394
		$cfg = array();
indvdum@0
  1395
		$cfg['max_attachments'] = ($is_message) ? $config['max_attachments_pm'] : $config['max_attachments'];
indvdum@0
  1396
		$forum_id = ($is_message) ? 0 : $forum_id;
indvdum@0
  1397
indvdum@0
  1398
		if ($submit && in_array($mode, array('post', 'reply', 'quote', 'edit')) && $upload_file)
indvdum@0
  1399
		{
indvdum@0
  1400
			if ($num_attachments < $cfg['max_attachments'] || $auth->acl_get('a_') || $auth->acl_get('m_', $forum_id))
indvdum@0
  1401
			{
indvdum@0
  1402
				$filedata = upload_attachment($form_name, $forum_id, false, '', $is_message);
indvdum@0
  1403
				$error = $filedata['error'];
indvdum@0
  1404
indvdum@0
  1405
				if ($filedata['post_attach'] && !sizeof($error))
indvdum@0
  1406
				{
indvdum@0
  1407
					$sql_ary = array(
indvdum@0
  1408
						'physical_filename'	=> $filedata['physical_filename'],
indvdum@0
  1409
						'attach_comment'	=> $this->filename_data['filecomment'],
indvdum@0
  1410
						'real_filename'		=> $filedata['real_filename'],
indvdum@0
  1411
						'extension'			=> $filedata['extension'],
indvdum@0
  1412
						'mimetype'			=> $filedata['mimetype'],
indvdum@0
  1413
						'filesize'			=> $filedata['filesize'],
indvdum@0
  1414
						'filetime'			=> $filedata['filetime'],
indvdum@0
  1415
						'thumbnail'			=> $filedata['thumbnail'],
indvdum@0
  1416
						'is_orphan'			=> 1,
indvdum@0
  1417
						'in_message'		=> ($is_message) ? 1 : 0,
indvdum@0
  1418
						'poster_id'			=> $user->data['user_id'],
indvdum@0
  1419
					);
indvdum@0
  1420
indvdum@0
  1421
					$db->sql_query('INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
indvdum@0
  1422
indvdum@0
  1423
					$new_entry = array(
indvdum@0
  1424
						'attach_id'		=> $db->sql_nextid(),
indvdum@0
  1425
						'is_orphan'		=> 1,
indvdum@0
  1426
						'real_filename'	=> $filedata['real_filename'],
indvdum@0
  1427
						'attach_comment'=> $this->filename_data['filecomment'],
indvdum@0
  1428
					);
indvdum@0
  1429
indvdum@0
  1430
					$this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
indvdum@0
  1431
					$this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message);
indvdum@0
  1432
indvdum@0
  1433
					$this->filename_data['filecomment'] = '';
indvdum@0
  1434
indvdum@0
  1435
					// This Variable is set to false here, because Attachments are entered into the
indvdum@0
  1436
					// Database in two modes, one if the id_list is 0 and the second one if post_attach is true
indvdum@0
  1437
					// Since post_attach is automatically switched to true if an Attachment got added to the filesystem,
indvdum@0
  1438
					// but we are assigning an id of 0 here, we have to reset the post_attach variable to false.
indvdum@0
  1439
					//
indvdum@0
  1440
					// This is very relevant, because it could happen that the post got not submitted, but we do not
indvdum@0
  1441
					// know this circumstance here. We could be at the posting page or we could be redirected to the entered
indvdum@0
  1442
					// post. :)
indvdum@0
  1443
					$filedata['post_attach'] = false;
indvdum@0
  1444
				}
indvdum@0
  1445
			}
indvdum@0
  1446
			else
indvdum@0
  1447
			{
indvdum@0
  1448
				$error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $cfg['max_attachments']);
indvdum@0
  1449
			}
indvdum@0
  1450
		}
indvdum@0
  1451
indvdum@0
  1452
		if ($preview || $refresh || sizeof($error))
indvdum@0
  1453
		{
indvdum@0
  1454
			// Perform actions on temporary attachments
indvdum@0
  1455
			if ($delete_file)
indvdum@0
  1456
			{
indvdum@0
  1457
				include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
indvdum@0
  1458
indvdum@0
  1459
				$index = array_keys(request_var('delete_file', array(0 => 0)));
indvdum@0
  1460
				$index = (!empty($index)) ? $index[0] : false;
indvdum@0
  1461
indvdum@0
  1462
				if ($index !== false && !empty($this->attachment_data[$index]))
indvdum@0
  1463
				{
indvdum@0
  1464
					// delete selected attachment
indvdum@0
  1465
					if ($this->attachment_data[$index]['is_orphan'])
indvdum@0
  1466
					{
indvdum@0
  1467
						$sql = 'SELECT attach_id, physical_filename, thumbnail
indvdum@0
  1468
							FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
  1469
							WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id'] . '
indvdum@0
  1470
								AND is_orphan = 1
indvdum@0
  1471
								AND poster_id = ' . $user->data['user_id'];
indvdum@0
  1472
						$result = $db->sql_query($sql);
indvdum@0
  1473
						$row = $db->sql_fetchrow($result);
indvdum@0
  1474
						$db->sql_freeresult($result);
indvdum@0
  1475
indvdum@0
  1476
						if ($row)
indvdum@0
  1477
						{
indvdum@0
  1478
							phpbb_unlink($row['physical_filename'], 'file');
indvdum@0
  1479
indvdum@0
  1480
							if ($row['thumbnail'])
indvdum@0
  1481
							{
indvdum@0
  1482
								phpbb_unlink($row['physical_filename'], 'thumbnail');
indvdum@0
  1483
							}
indvdum@0
  1484
indvdum@0
  1485
							$db->sql_query('DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id']);
indvdum@0
  1486
						}
indvdum@0
  1487
					}
indvdum@0
  1488
					else
indvdum@0
  1489
					{
indvdum@0
  1490
						delete_attachments('attach', array(intval($this->attachment_data[$index]['attach_id'])));
indvdum@0
  1491
					}
indvdum@0
  1492
indvdum@0
  1493
					unset($this->attachment_data[$index]);
indvdum@0
  1494
					$this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "(\\1 == \$index) ? '' : ((\\1 > \$index) ? '[attachment=' . (\\1 - 1) . ']\\2[/attachment]' : '\\0')", $this->message);
indvdum@0
  1495
indvdum@0
  1496
					// Reindex Array
indvdum@0
  1497
					$this->attachment_data = array_values($this->attachment_data);
indvdum@0
  1498
				}
indvdum@0
  1499
			}
indvdum@0
  1500
			else if (($add_file || $preview) && $upload_file)
indvdum@0
  1501
			{
indvdum@0
  1502
				if ($num_attachments < $cfg['max_attachments'] || $auth->acl_gets('m_', 'a_', $forum_id))
indvdum@0
  1503
				{
indvdum@0
  1504
					$filedata = upload_attachment($form_name, $forum_id, false, '', $is_message);
indvdum@0
  1505
					$error = array_merge($error, $filedata['error']);
indvdum@0
  1506
indvdum@0
  1507
					if (!sizeof($error))
indvdum@0
  1508
					{
indvdum@0
  1509
						$sql_ary = array(
indvdum@0
  1510
							'physical_filename'	=> $filedata['physical_filename'],
indvdum@0
  1511
							'attach_comment'	=> $this->filename_data['filecomment'],
indvdum@0
  1512
							'real_filename'		=> $filedata['real_filename'],
indvdum@0
  1513
							'extension'			=> $filedata['extension'],
indvdum@0
  1514
							'mimetype'			=> $filedata['mimetype'],
indvdum@0
  1515
							'filesize'			=> $filedata['filesize'],
indvdum@0
  1516
							'filetime'			=> $filedata['filetime'],
indvdum@0
  1517
							'thumbnail'			=> $filedata['thumbnail'],
indvdum@0
  1518
							'is_orphan'			=> 1,
indvdum@0
  1519
							'in_message'		=> ($is_message) ? 1 : 0,
indvdum@0
  1520
							'poster_id'			=> $user->data['user_id'],
indvdum@0
  1521
						);
indvdum@0
  1522
indvdum@0
  1523
						$db->sql_query('INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
indvdum@0
  1524
indvdum@0
  1525
						$new_entry = array(
indvdum@0
  1526
							'attach_id'		=> $db->sql_nextid(),
indvdum@0
  1527
							'is_orphan'		=> 1,
indvdum@0
  1528
							'real_filename'	=> $filedata['real_filename'],
indvdum@0
  1529
							'attach_comment'=> $this->filename_data['filecomment'],
indvdum@0
  1530
						);
indvdum@0
  1531
indvdum@0
  1532
						$this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
indvdum@0
  1533
						$this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message);
indvdum@0
  1534
						$this->filename_data['filecomment'] = '';
indvdum@0
  1535
					}
indvdum@0
  1536
				}
indvdum@0
  1537
				else
indvdum@0
  1538
				{
indvdum@0
  1539
					$error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $cfg['max_attachments']);
indvdum@0
  1540
				}
indvdum@0
  1541
			}
indvdum@0
  1542
		}
indvdum@0
  1543
indvdum@0
  1544
		foreach ($error as $error_msg)
indvdum@0
  1545
		{
indvdum@0
  1546
			$this->warn_msg[] = $error_msg;
indvdum@0
  1547
		}
indvdum@0
  1548
	}
indvdum@0
  1549
indvdum@0
  1550
	/**
indvdum@0
  1551
	* Get Attachment Data
indvdum@0
  1552
	*/
indvdum@0
  1553
	function get_submitted_attachment_data($check_user_id = false)
indvdum@0
  1554
	{
indvdum@0
  1555
		global $user, $db, $phpbb_root_path, $phpEx, $config;
indvdum@0
  1556
indvdum@0
  1557
		$this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true));
indvdum@0
  1558
		$attachment_data = (isset($_POST['attachment_data'])) ? $_POST['attachment_data'] : array();
indvdum@0
  1559
		$this->attachment_data = array();
indvdum@0
  1560
indvdum@0
  1561
		$check_user_id = ($check_user_id === false) ? $user->data['user_id'] : $check_user_id;
indvdum@0
  1562
indvdum@0
  1563
		if (!sizeof($attachment_data))
indvdum@0
  1564
		{
indvdum@0
  1565
			return;
indvdum@0
  1566
		}
indvdum@0
  1567
indvdum@0
  1568
		$not_orphan = $orphan = array();
indvdum@0
  1569
indvdum@0
  1570
		foreach ($attachment_data as $pos => $var_ary)
indvdum@0
  1571
		{
indvdum@0
  1572
			if ($var_ary['is_orphan'])
indvdum@0
  1573
			{
indvdum@0
  1574
				$orphan[(int) $var_ary['attach_id']] = $pos;
indvdum@0
  1575
			}
indvdum@0
  1576
			else
indvdum@0
  1577
			{
indvdum@0
  1578
				$not_orphan[(int) $var_ary['attach_id']] = $pos;
indvdum@0
  1579
			}
indvdum@0
  1580
		}
indvdum@0
  1581
indvdum@0
  1582
		// Regenerate already posted attachments
indvdum@0
  1583
		if (sizeof($not_orphan))
indvdum@0
  1584
		{
indvdum@0
  1585
			// Get the attachment data, based on the poster id...
indvdum@0
  1586
			$sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment
indvdum@0
  1587
				FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
  1588
				WHERE ' . $db->sql_in_set('attach_id', array_keys($not_orphan)) . '
indvdum@0
  1589
					AND poster_id = ' . $check_user_id;
indvdum@0
  1590
			$result = $db->sql_query($sql);
indvdum@0
  1591
indvdum@0
  1592
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  1593
			{
indvdum@0
  1594
				$pos = $not_orphan[$row['attach_id']];
indvdum@0
  1595
				$this->attachment_data[$pos] = $row;
indvdum@0
  1596
				set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true);
indvdum@0
  1597
indvdum@0
  1598
				unset($not_orphan[$row['attach_id']]);
indvdum@0
  1599
			}
indvdum@0
  1600
			$db->sql_freeresult($result);
indvdum@0
  1601
		}
indvdum@0
  1602
indvdum@0
  1603
		if (sizeof($not_orphan))
indvdum@0
  1604
		{
indvdum@0
  1605
			trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR);
indvdum@0
  1606
		}
indvdum@0
  1607
indvdum@0
  1608
		// Regenerate newly uploaded attachments
indvdum@0
  1609
		if (sizeof($orphan))
indvdum@0
  1610
		{
indvdum@0
  1611
			$sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment
indvdum@0
  1612
				FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
  1613
				WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan)) . '
indvdum@0
  1614
					AND poster_id = ' . $user->data['user_id'] . '
indvdum@0
  1615
					AND is_orphan = 1';
indvdum@0
  1616
			$result = $db->sql_query($sql);
indvdum@0
  1617
indvdum@0
  1618
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  1619
			{
indvdum@0
  1620
				$pos = $orphan[$row['attach_id']];
indvdum@0
  1621
				$this->attachment_data[$pos] = $row;
indvdum@0
  1622
				set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true);
indvdum@0
  1623
indvdum@0
  1624
				unset($orphan[$row['attach_id']]);
indvdum@0
  1625
			}
indvdum@0
  1626
			$db->sql_freeresult($result);
indvdum@0
  1627
		}
indvdum@0
  1628
indvdum@0
  1629
		if (sizeof($orphan))
indvdum@0
  1630
		{
indvdum@0
  1631
			trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR);
indvdum@0
  1632
		}
indvdum@0
  1633
indvdum@0
  1634
		ksort($this->attachment_data);
indvdum@0
  1635
	}
indvdum@0
  1636
indvdum@0
  1637
	/**
indvdum@0
  1638
	* Parse Poll
indvdum@0
  1639
	*/
indvdum@0
  1640
	function parse_poll(&$poll)
indvdum@0
  1641
	{
indvdum@0
  1642
		global $auth, $user, $config;
indvdum@0
  1643
indvdum@0
  1644
		$poll_max_options = $poll['poll_max_options'];
indvdum@0
  1645
indvdum@0
  1646
		// Parse Poll Option text ;)
indvdum@0
  1647
		$tmp_message = $this->message;
indvdum@0
  1648
		$this->message = $poll['poll_option_text'];
indvdum@0
  1649
		$bbcode_bitfield = $this->bbcode_bitfield;
indvdum@0
  1650
indvdum@0
  1651
		$poll['poll_option_text'] = $this->parse($poll['enable_bbcode'], ($config['allow_post_links']) ? $poll['enable_urls'] : false, $poll['enable_smilies'], $poll['img_status'], false, false, $config['allow_post_links'], false, 'poll');
indvdum@0
  1652
indvdum@0
  1653
		$bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield));
indvdum@0
  1654
		$this->message = $tmp_message;
indvdum@0
  1655
indvdum@0
  1656
		// Parse Poll Title
indvdum@0
  1657
		$tmp_message = $this->message;
indvdum@0
  1658
		$this->message = $poll['poll_title'];
indvdum@0
  1659
		$this->bbcode_bitfield = $bbcode_bitfield;
indvdum@0
  1660
indvdum@0
  1661
		$poll['poll_options'] = explode("\n", trim($poll['poll_option_text']));
indvdum@0
  1662
		$poll['poll_options_size'] = sizeof($poll['poll_options']);
indvdum@0
  1663
indvdum@0
  1664
		if (!$poll['poll_title'] && $poll['poll_options_size'])
indvdum@0
  1665
		{
indvdum@0
  1666
			$this->warn_msg[] = $user->lang['NO_POLL_TITLE'];
indvdum@0
  1667
		}
indvdum@0
  1668
		else
indvdum@0
  1669
		{
indvdum@0
  1670
			if (utf8_strlen(preg_replace('#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius', ' ', $this->message)) > 100)
indvdum@0
  1671
			{
indvdum@0
  1672
				$this->warn_msg[] = $user->lang['POLL_TITLE_TOO_LONG'];
indvdum@0
  1673
			}
indvdum@0
  1674
			$poll['poll_title'] = $this->parse($poll['enable_bbcode'], ($config['allow_post_links']) ? $poll['enable_urls'] : false, $poll['enable_smilies'], $poll['img_status'], false, false, $config['allow_post_links'], false, 'poll');
indvdum@0
  1675
			if (strlen($poll['poll_title']) > 255)
indvdum@0
  1676
			{
indvdum@0
  1677
				$this->warn_msg[] = $user->lang['POLL_TITLE_COMP_TOO_LONG'];
indvdum@0
  1678
			}
indvdum@0
  1679
		}
indvdum@0
  1680
indvdum@0
  1681
		$this->bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield));
indvdum@0
  1682
		$this->message = $tmp_message;
indvdum@0
  1683
		unset($tmp_message);
indvdum@0
  1684
indvdum@0
  1685
		if (sizeof($poll['poll_options']) == 1)
indvdum@0
  1686
		{
indvdum@0
  1687
			$this->warn_msg[] = $user->lang['TOO_FEW_POLL_OPTIONS'];
indvdum@0
  1688
		}
indvdum@0
  1689
		else if ($poll['poll_options_size'] > (int) $config['max_poll_options'])
indvdum@0
  1690
		{
indvdum@0
  1691
			$this->warn_msg[] = $user->lang['TOO_MANY_POLL_OPTIONS'];
indvdum@0
  1692
		}
indvdum@0
  1693
		else if ($poll_max_options > $poll['poll_options_size'])
indvdum@0
  1694
		{
indvdum@0
  1695
			$this->warn_msg[] = $user->lang['TOO_MANY_USER_OPTIONS'];
indvdum@0
  1696
		}
indvdum@0
  1697
indvdum@0
  1698
		$poll['poll_max_options'] = ($poll['poll_max_options'] < 1) ? 1 : (($poll['poll_max_options'] > $config['max_poll_options']) ? $config['max_poll_options'] : $poll['poll_max_options']);
indvdum@0
  1699
	}
indvdum@0
  1700
}
indvdum@0
  1701
indvdum@0
  1702
?>