Blame Extras/phpBB/3.0.4/includes/message_parser.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
* @version $Id: message_parser.php 9034 2008-10-24 00:49:30Z toonarmy $
4c79b5
* @copyright (c) 2005 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
if (!class_exists('bbcode'))
4c79b5
{
4c79b5
	include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* BBCODE FIRSTPASS
4c79b5
* BBCODE first pass class (functions for parsing messages for db storage)
4c79b5
* @package phpBB3
4c79b5
*/
4c79b5
class bbcode_firstpass extends bbcode
4c79b5
{
4c79b5
	var $message = '';
4c79b5
	var $warn_msg = array();
4c79b5
	var $parsed_items = array();
4c79b5
4c79b5
	/**
4c79b5
	* Parse BBCode
4c79b5
	*/
4c79b5
	function parse_bbcode()
4c79b5
	{
4c79b5
		if (!$this->bbcodes)
4c79b5
		{
4c79b5
			$this->bbcode_init();
4c79b5
		}
4c79b5
4c79b5
		global $user;
4c79b5
4c79b5
		$this->bbcode_bitfield = '';
4c79b5
		$bitfield = new bitfield();
4c79b5
4c79b5
		foreach ($this->bbcodes as $bbcode_name => $bbcode_data)
4c79b5
		{
4c79b5
			if (isset($bbcode_data['disabled']) && $bbcode_data['disabled'])
4c79b5
			{
4c79b5
				foreach ($bbcode_data['regexp'] as $regexp => $replacement)
4c79b5
				{
4c79b5
					if (preg_match($regexp, $this->message))
4c79b5
					{
4c79b5
						$this->warn_msg[] = sprintf($user->lang['UNAUTHORISED_BBCODE'] , '[' . $bbcode_name . ']');
4c79b5
						continue;
4c79b5
					}
4c79b5
				}
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				foreach ($bbcode_data['regexp'] as $regexp => $replacement)
4c79b5
				{
4c79b5
					// The pattern gets compiled and cached by the PCRE extension,
4c79b5
					// it should not demand recompilation
4c79b5
					if (preg_match($regexp, $this->message))
4c79b5
					{
4c79b5
						$this->message = preg_replace($regexp, $replacement, $this->message);
4c79b5
						$bitfield->set($bbcode_data['bbcode_id']);
4c79b5
					}
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$this->bbcode_bitfield = $bitfield->get_base64();
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Prepare some bbcodes for better parsing
4c79b5
	*/
4c79b5
	function prepare_bbcodes()
4c79b5
	{
4c79b5
		// 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".
4c79b5
		// Fine by me ;) Will ease our live... but do not come back and cry at us, we won't hear you.
4c79b5
4c79b5
		/* Add newline at the end and in front of each quote block to prevent parsing errors (urls, smilies, etc.)
4c79b5
		if (strpos($this->message, '[quote') !== false && strpos($this->message, '[/quote]') !== false)
4c79b5
		{
4c79b5
			$this->message = str_replace("\r\n", "\n", $this->message);
4c79b5
4c79b5
			// We strip newlines and spaces after and before quotes in quotes (trimming) and then add exactly one newline
4c79b5
			$this->message = preg_replace('#\[quote(=".*?")?\]\s*(.*?)\s*\[/quote\]#siu', '[quote\1]' . "\n" . '\2' ."\n[/quote]", $this->message);
4c79b5
		}
4c79b5
		*/
4c79b5
4c79b5
		// Add other checks which needs to be placed before actually parsing anything (be it bbcodes, smilies, urls...)
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Init bbcode data for later parsing
4c79b5
	*/
4c79b5
	function bbcode_init()
4c79b5
	{
4c79b5
		static $rowset;
4c79b5
4c79b5
		// This array holds all bbcode data. BBCodes will be processed in this
4c79b5
		// order, so it is important to keep [code] in first position and
4c79b5
		// [quote] in second position.
4c79b5
		$this->bbcodes = array(
4c79b5
			'code'			=> array('bbcode_id' => 8,	'regexp' => array('#\[code(?:=([a-z]+))?\](.+\[/code\])#ise' => "\$this->bbcode_code('\$1', '\$2')")),
4c79b5
			'quote'			=> array('bbcode_id' => 0,	'regexp' => array('#\[quote(?:="(.*?)")?\](.+)\[/quote\]#ise' => "\$this->bbcode_quote('\$0')")),
4c79b5
			'attachment'	=> array('bbcode_id' => 12,	'regexp' => array('#\[attachment=([0-9]+)\](.*?)\[/attachment\]#ise' => "\$this->bbcode_attachment('\$1', '\$2')")),
4c79b5
			'b'				=> array('bbcode_id' => 1,	'regexp' => array('#\[b\](.*?)\[/b\]#ise' => "\$this->bbcode_strong('\$1')")),
4c79b5
			'i'				=> array('bbcode_id' => 2,	'regexp' => array('#\[i\](.*?)\[/i\]#ise' => "\$this->bbcode_italic('\$1')")),
4c79b5
			'url'			=> array('bbcode_id' => 3,	'regexp' => array('#\[url(=(.*))?\](.*)\[/url\]#iUe' => "\$this->validate_url('\$2', '\$3')")),
4c79b5
			'img'			=> array('bbcode_id' => 4,	'regexp' => array('#\[img\](.*)\[/img\]#iUe' => "\$this->bbcode_img('\$1')")),
4c79b5
			'size'			=> array('bbcode_id' => 5,	'regexp' => array('#\[size=([\-\+]?\d+)\](.*?)\[/size\]#ise' => "\$this->bbcode_size('\$1', '\$2')")),
4c79b5
			'color'			=> array('bbcode_id' => 6,	'regexp' => array('!\[color=(#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!ise' => "\$this->bbcode_color('\$1', '\$2')")),
4c79b5
			'u'				=> array('bbcode_id' => 7,	'regexp' => array('#\[u\](.*?)\[/u\]#ise' => "\$this->bbcode_underline('\$1')")),
4c79b5
			'list'			=> array('bbcode_id' => 9,	'regexp' => array('#\[list(?:=(?:[a-z0-9]|disc|circle|square))?].*\[/list]#ise' => "\$this->bbcode_parse_list('\$0')")),
4c79b5
			'email'			=> array('bbcode_id' => 10,	'regexp' => array('#\[email=?(.*?)?\](.*?)\[/email\]#ise' => "\$this->validate_email('\$1', '\$2')")),
4c79b5
			'flash'			=> array('bbcode_id' => 11,	'regexp' => array('#\[flash=([0-9]+),([0-9]+)\](.*?)\[/flash\]#ie' => "\$this->bbcode_flash('\$1', '\$2', '\$3')"))
4c79b5
		);
4c79b5
4c79b5
		// Zero the parsed items array
4c79b5
		$this->parsed_items = array();
4c79b5
4c79b5
		foreach ($this->bbcodes as $tag => $bbcode_data)
4c79b5
		{
4c79b5
			$this->parsed_items[$tag] = 0;
4c79b5
		}
4c79b5
4c79b5
		if (!is_array($rowset))
4c79b5
		{
4c79b5
			global $db;
4c79b5
			$rowset = array();
4c79b5
4c79b5
			$sql = 'SELECT *
4c79b5
				FROM ' . BBCODES_TABLE;
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$rowset[] = $row;
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		foreach ($rowset as $row)
4c79b5
		{
4c79b5
			$this->bbcodes[$row['bbcode_tag']] = array(
4c79b5
				'bbcode_id'	=> (int) $row['bbcode_id'],
4c79b5
				'regexp'	=> array($row['first_pass_match'] => str_replace('$uid', $this->bbcode_uid, $row['first_pass_replace']))
4c79b5
			);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Making some pre-checks for bbcodes as well as increasing the number of parsed items
4c79b5
	*/
4c79b5
	function check_bbcode($bbcode, &$in)
4c79b5
	{
4c79b5
		// when using the /e modifier, preg_replace slashes double-quotes but does not
4c79b5
		// seem to slash anything else
4c79b5
		$in = str_replace("\r\n", "\n", str_replace('\"', '"', $in));
4c79b5
4c79b5
		// Trimming here to make sure no empty bbcodes are parsed accidently
4c79b5
		if (trim($in) == '')
4c79b5
		{
4c79b5
			return false;
4c79b5
		}
4c79b5
4c79b5
		$this->parsed_items[$bbcode]++;
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Transform some characters in valid bbcodes
4c79b5
	*/
4c79b5
	function bbcode_specialchars($text)
4c79b5
	{
4c79b5
		$str_from = array('<', '>', '[', ']', '.', ':');
4c79b5
		$str_to = array('<', '>', '[', ']', '.', ':');
4c79b5
4c79b5
		return str_replace($str_from, $str_to, $text);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse size tag
4c79b5
	*/
4c79b5
	function bbcode_size($stx, $in)
4c79b5
	{
4c79b5
		global $user, $config;
4c79b5
4c79b5
		if (!$this->check_bbcode('size', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		if ($config['max_' . $this->mode . '_font_size'] && $config['max_' . $this->mode . '_font_size'] < $stx)
4c79b5
		{
4c79b5
			$this->warn_msg[] = sprintf($user->lang['MAX_FONT_SIZE_EXCEEDED'], $config['max_' . $this->mode . '_font_size']);
4c79b5
4c79b5
			return '[size=' . $stx . ']' . $in . '[/size]';
4c79b5
		}
4c79b5
4c79b5
		// Do not allow size=0
4c79b5
		if ($stx <= 0)
4c79b5
		{
4c79b5
			return '[size=' . $stx . ']' . $in . '[/size]';
4c79b5
		}
4c79b5
4c79b5
		return '[size=' . $stx . ':' . $this->bbcode_uid . ']' . $in . '[/size:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse color tag
4c79b5
	*/
4c79b5
	function bbcode_color($stx, $in)
4c79b5
	{
4c79b5
		if (!$this->check_bbcode('color', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		return '[color=' . $stx . ':' . $this->bbcode_uid . ']' . $in . '[/color:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse u tag
4c79b5
	*/
4c79b5
	function bbcode_underline($in)
4c79b5
	{
4c79b5
		if (!$this->check_bbcode('u', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		return '[u:' . $this->bbcode_uid . ']' . $in . '[/u:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse b tag
4c79b5
	*/
4c79b5
	function bbcode_strong($in)
4c79b5
	{
4c79b5
		if (!$this->check_bbcode('b', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		return '[b:' . $this->bbcode_uid . ']' . $in . '[/b:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse i tag
4c79b5
	*/
4c79b5
	function bbcode_italic($in)
4c79b5
	{
4c79b5
		if (!$this->check_bbcode('i', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		return '[i:' . $this->bbcode_uid . ']' . $in . '[/i:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse img tag
4c79b5
	*/
4c79b5
	function bbcode_img($in)
4c79b5
	{
4c79b5
		global $user, $config;
4c79b5
4c79b5
		if (!$this->check_bbcode('img', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		$in = trim($in);
4c79b5
		$error = false;
4c79b5
4c79b5
		$in = str_replace(' ', '%20', $in);
4c79b5
4c79b5
		// Checking urls
4c79b5
		if (!preg_match('#^' . get_preg_expression('url') . '$#i', $in) && !preg_match('#^' . get_preg_expression('www_url') . '$#i', $in))
4c79b5
		{
4c79b5
			return '[img]' . $in . '[/img]';
4c79b5
		}
4c79b5
4c79b5
		// Try to cope with a common user error... not specifying a protocol but only a subdomain
4c79b5
		if (!preg_match('#^[a-z0-9]+://#i', $in))
4c79b5
		{
4c79b5
			$in = 'http://' . $in;
4c79b5
		}
4c79b5
4c79b5
		if ($config['max_' . $this->mode . '_img_height'] || $config['max_' . $this->mode . '_img_width'])
4c79b5
		{
4c79b5
			$stats = @getimagesize($in);
4c79b5
4c79b5
			if ($stats === false)
4c79b5
			{
4c79b5
				$error = true;
4c79b5
				$this->warn_msg[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				if ($config['max_' . $this->mode . '_img_height'] && $config['max_' . $this->mode . '_img_height'] < $stats[1])
4c79b5
				{
4c79b5
					$error = true;
4c79b5
					$this->warn_msg[] = sprintf($user->lang['MAX_IMG_HEIGHT_EXCEEDED'], $config['max_' . $this->mode . '_img_height']);
4c79b5
				}
4c79b5
4c79b5
				if ($config['max_' . $this->mode . '_img_width'] && $config['max_' . $this->mode . '_img_width'] < $stats[0])
4c79b5
				{
4c79b5
					$error = true;
4c79b5
					$this->warn_msg[] = sprintf($user->lang['MAX_IMG_WIDTH_EXCEEDED'], $config['max_' . $this->mode . '_img_width']);
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if ($error || $this->path_in_domain($in))
4c79b5
		{
4c79b5
			return '[img]' . $in . '[/img]';
4c79b5
		}
4c79b5
4c79b5
		return '[img:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($in) . '[/img:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse flash tag
4c79b5
	*/
4c79b5
	function bbcode_flash($width, $height, $in)
4c79b5
	{
4c79b5
		global $user, $config;
4c79b5
4c79b5
		if (!$this->check_bbcode('flash', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		$in = trim($in);
4c79b5
		$error = false;
4c79b5
4c79b5
		// Do not allow 0-sizes generally being entered
4c79b5
		if ($width <= 0 || $height <= 0)
4c79b5
		{
4c79b5
			return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]';
4c79b5
		}
4c79b5
4c79b5
		// Apply the same size checks on flash files as on images
4c79b5
		if ($config['max_' . $this->mode . '_img_height'] || $config['max_' . $this->mode . '_img_width'])
4c79b5
		{
4c79b5
			if ($config['max_' . $this->mode . '_img_height'] && $config['max_' . $this->mode . '_img_height'] < $height)
4c79b5
			{
4c79b5
				$error = true;
4c79b5
				$this->warn_msg[] = sprintf($user->lang['MAX_FLASH_HEIGHT_EXCEEDED'], $config['max_' . $this->mode . '_img_height']);
4c79b5
			}
4c79b5
4c79b5
			if ($config['max_' . $this->mode . '_img_width'] && $config['max_' . $this->mode . '_img_width'] < $width)
4c79b5
			{
4c79b5
				$error = true;
4c79b5
				$this->warn_msg[] = sprintf($user->lang['MAX_FLASH_WIDTH_EXCEEDED'], $config['max_' . $this->mode . '_img_width']);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if ($error || $this->path_in_domain($in))
4c79b5
		{
4c79b5
			return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]';
4c79b5
		}
4c79b5
4c79b5
		return '[flash=' . $width . ',' . $height . ':' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($in) . '[/flash:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse inline attachments [ia]
4c79b5
	*/
4c79b5
	function bbcode_attachment($stx, $in)
4c79b5
	{
4c79b5
		if (!$this->check_bbcode('attachment', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		return '[attachment=' . $stx . ':' . $this->bbcode_uid . ']' . trim($in) . '[/attachment:' . $this->bbcode_uid . ']';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse code text from code tag
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function bbcode_parse_code($stx, &$code)
4c79b5
	{
4c79b5
		switch (strtolower($stx))
4c79b5
		{
4c79b5
			case 'php':
4c79b5
4c79b5
				$remove_tags = false;
4c79b5
4c79b5
				$str_from = array('<', '>', '[', ']', '.', ':', ':');
4c79b5
				$str_to = array('<', '>', '[', ']', '.', ':', ':');
4c79b5
				$code = str_replace($str_from, $str_to, $code);
4c79b5
4c79b5
				if (!preg_match('/\<\?.*?\?\>/is', $code))
4c79b5
				{
4c79b5
					$remove_tags = true;
4c79b5
					$code = "";
4c79b5
				}
4c79b5
4c79b5
				$conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string');
4c79b5
				foreach ($conf as $ini_var)
4c79b5
				{
4c79b5
					@ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var));
4c79b5
				}
4c79b5
4c79b5
				// Because highlight_string is specialcharing the text (but we already did this before), we have to reverse this in order to get correct results
4c79b5
				$code = htmlspecialchars_decode($code);
4c79b5
				$code = highlight_string($code, true);
4c79b5
4c79b5
				$str_from = array('', '', '','[', ']', '.', ':');
4c79b5
				$str_to = array('', '', '', '[', ']', '.', ':');
4c79b5
4c79b5
				if ($remove_tags)
4c79b5
				{
4c79b5
					$str_from[] = '<?php ';
4c79b5
					$str_to[] = '';
4c79b5
					$str_from[] = '<?php ';
4c79b5
					$str_to[] = '';
4c79b5
				}
4c79b5
4c79b5
				$code = str_replace($str_from, $str_to, $code);
4c79b5
				$code = preg_replace('#^()\n?(.*?)\n?()$#is', '$1$2$3', $code);
4c79b5
4c79b5
				if ($remove_tags)
4c79b5
				{
4c79b5
					$code = preg_replace('#()?\?>()#', '$1 $2', $code);
4c79b5
				}
4c79b5
4c79b5
				$code = preg_replace('#^(.*)#s', '$2', $code);
4c79b5
				$code = preg_replace('#(?:\s++| )*+$#u', '', $code);
4c79b5
4c79b5
				// remove newline at the end
4c79b5
				if (!empty($code) && substr($code, -1) == "\n")
4c79b5
				{
4c79b5
					$code = substr($code, 0, -1);
4c79b5
				}
4c79b5
4c79b5
				return "[code=$stx:" . $this->bbcode_uid . ']' . $code . '[/code:' . $this->bbcode_uid . ']';
4c79b5
			break;
4c79b5
4c79b5
			default:
4c79b5
				return '[code:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($code) . '[/code:' . $this->bbcode_uid . ']';
4c79b5
			break;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse code tag
4c79b5
	* Expects the argument to start right after the opening [code] tag and to end with [/code]
4c79b5
	*/
4c79b5
	function bbcode_code($stx, $in)
4c79b5
	{
4c79b5
		if (!$this->check_bbcode('code', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		// We remove the hardcoded elements from the code block here because it is not used in code blocks
4c79b5
		// Having it here saves us one preg_replace per message containing [code] blocks
4c79b5
		// Additionally, magic url parsing should go after parsing bbcodes, but for safety those are stripped out too...
4c79b5
		$htm_match = get_preg_expression('bbcode_htm');
4c79b5
		unset($htm_match[4], $htm_match[5]);
4c79b5
		$htm_replace = array('\1', '\1', '\2', '\1');
4c79b5
4c79b5
		$out = $code_block = '';
4c79b5
		$open = 1;
4c79b5
4c79b5
		while ($in)
4c79b5
		{
4c79b5
			// Determine position and tag length of next code block
4c79b5
			preg_match('#(.*?)(\[code(?:=([a-z]+))?\])(.+)#is', $in, $buffer);
4c79b5
			$pos = (isset($buffer[1])) ? strlen($buffer[1]) : false;
4c79b5
			$tag_length = (isset($buffer[2])) ? strlen($buffer[2]) : false;
4c79b5
4c79b5
			// Determine position of ending code tag
4c79b5
			$pos2 = stripos($in, '[/code]');
4c79b5
4c79b5
			// Which is the next block, ending code or code block
4c79b5
			if ($pos !== false && $pos < $pos2)
4c79b5
			{
4c79b5
				// Open new block
4c79b5
				if (!$open)
4c79b5
				{
4c79b5
					$out .= substr($in, 0, $pos);
4c79b5
					$in = substr($in, $pos);
4c79b5
					$stx = (isset($buffer[3])) ? $buffer[3] : '';
4c79b5
					$code_block = '';
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					// Already opened block, just append to the current block
4c79b5
					$code_block .= substr($in, 0, $pos) . ((isset($buffer[2])) ? $buffer[2] : '');
4c79b5
					$in = substr($in, $pos);
4c79b5
				}
4c79b5
4c79b5
				$in = substr($in, $tag_length);
4c79b5
				$open++;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				// Close the block
4c79b5
				if ($open == 1)
4c79b5
				{
4c79b5
					$code_block .= substr($in, 0, $pos2);
4c79b5
					$code_block = preg_replace($htm_match, $htm_replace, $code_block);
4c79b5
4c79b5
					// Parse this code block
4c79b5
					$out .= $this->bbcode_parse_code($stx, $code_block);
4c79b5
					$code_block = '';
4c79b5
					$open--;
4c79b5
				}
4c79b5
				else if ($open)
4c79b5
				{
4c79b5
					// Close one open tag... add to the current code block
4c79b5
					$code_block .= substr($in, 0, $pos2 + 7);
4c79b5
					$open--;
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					// end code without opening code... will be always outside code block
4c79b5
					$out .= substr($in, 0, $pos2 + 7);
4c79b5
				}
4c79b5
4c79b5
				$in = substr($in, $pos2 + 7);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// if now $code_block has contents we need to parse the remaining code while removing the last closing tag to match up.
4c79b5
		if ($code_block)
4c79b5
		{
4c79b5
			$code_block = substr($code_block, 0, -7);
4c79b5
			$code_block = preg_replace($htm_match, $htm_replace, $code_block);
4c79b5
4c79b5
			$out .= $this->bbcode_parse_code($stx, $code_block);
4c79b5
		}
4c79b5
4c79b5
		return $out;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse list bbcode
4c79b5
	* Expects the argument to start with a tag
4c79b5
	*/
4c79b5
	function bbcode_parse_list($in)
4c79b5
	{
4c79b5
		if (!$this->check_bbcode('list', $in))
4c79b5
		{
4c79b5
			return $in;
4c79b5
		}
4c79b5
4c79b5
		// $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
4c79b5
		$tok = ']';
4c79b5
		$out = '[';
4c79b5
4c79b5
		// First character is [
4c79b5
		$in = substr($in, 1);
4c79b5
		$list_end_tags = $item_end_tags = array();
4c79b5
4c79b5
		do
4c79b5
		{
4c79b5
			$pos = strlen($in);
4c79b5
4c79b5
			for ($i = 0, $tok_len = strlen($tok); $i < $tok_len; ++$i)
4c79b5
			{
4c79b5
				$tmp_pos = strpos($in, $tok[$i]);
4c79b5
4c79b5
				if ($tmp_pos !== false && $tmp_pos < $pos)
4c79b5
				{
4c79b5
					$pos = $tmp_pos;
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			$buffer = substr($in, 0, $pos);
4c79b5
			$tok = $in[$pos];
4c79b5
4c79b5
			$in = substr($in, $pos + 1);
4c79b5
4c79b5
			if ($tok == ']')
4c79b5
			{
4c79b5
				// if $tok is ']' the buffer holds a tag
4c79b5
				if (strtolower($buffer) == '/list' && sizeof($list_end_tags))
4c79b5
				{
4c79b5
					// valid [/list] tag, check nesting so that we don't hit false positives
4c79b5
					if (sizeof($item_end_tags) && sizeof($item_end_tags) >= sizeof($list_end_tags))
4c79b5
					{
4c79b5
						// current li tag has not been closed
4c79b5
						$out = preg_replace('/\n?\[$/', '[', $out) . array_pop($item_end_tags) . '][';
4c79b5
					}
4c79b5
4c79b5
					$out .= array_pop($list_end_tags) . ']';
4c79b5
					$tok = '[';
4c79b5
				}
4c79b5
				else if (preg_match('#^list(=[0-9a-z]+)?$#i', $buffer, $m))
4c79b5
				{
4c79b5
					// sub-list, add a closing tag
4c79b5
					if (empty($m[1]) || preg_match('/^=(?:disc|square|circle)$/i', $m[1]))
4c79b5
					{
4c79b5
						array_push($list_end_tags, '/list:u:' . $this->bbcode_uid);
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						array_push($list_end_tags, '/list:o:' . $this->bbcode_uid);
4c79b5
					}
4c79b5
					$out .= 'list' . substr($buffer, 4) . ':' . $this->bbcode_uid . ']';
4c79b5
					$tok = '[';
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					if (($buffer == '*' || substr($buffer, -2) == '[*') && sizeof($list_end_tags))
4c79b5
					{
4c79b5
						// the buffer holds a bullet tag and we have a [list] tag open
4c79b5
						if (sizeof($item_end_tags) >= sizeof($list_end_tags))
4c79b5
						{
4c79b5
							if (substr($buffer, -2) == '[*')
4c79b5
							{
4c79b5
								$out .= substr($buffer, 0, -2) . '[';
4c79b5
							}
4c79b5
							// current li tag has not been closed
4c79b5
							if (preg_match('/\n\[$/', $out, $m))
4c79b5
							{
4c79b5
								$out = preg_replace('/\n\[$/', '[', $out);
4c79b5
								$buffer = array_pop($item_end_tags) . "]\n[*:" . $this->bbcode_uid;
4c79b5
							}
4c79b5
							else
4c79b5
							{
4c79b5
								$buffer = array_pop($item_end_tags) . '][*:' . $this->bbcode_uid;
4c79b5
							}
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							$buffer = '*:' . $this->bbcode_uid;
4c79b5
						}
4c79b5
4c79b5
						$item_end_tags[] = '/*:m:' . $this->bbcode_uid;
4c79b5
					}
4c79b5
					else if ($buffer == '/*')
4c79b5
					{
4c79b5
						array_pop($item_end_tags);
4c79b5
						$buffer = '/*:' . $this->bbcode_uid;
4c79b5
					}
4c79b5
4c79b5
					$out .= $buffer . $tok;
4c79b5
					$tok = '[]';
4c79b5
				}
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				// Not within a tag, just add buffer to the return string
4c79b5
				$out .= $buffer . $tok;
4c79b5
				$tok = ($tok == '[') ? ']' : '[]';
4c79b5
			}
4c79b5
		}
4c79b5
		while ($in);
4c79b5
4c79b5
		// do we have some tags open? close them now
4c79b5
		if (sizeof($item_end_tags))
4c79b5
		{
4c79b5
			$out .= '[' . implode('][', $item_end_tags) . ']';
4c79b5
		}
4c79b5
		if (sizeof($list_end_tags))
4c79b5
		{
4c79b5
			$out .= '[' . implode('][', $list_end_tags) . ']';
4c79b5
		}
4c79b5
4c79b5
		return $out;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse quote bbcode
4c79b5
	* Expects the argument to start with a tag
4c79b5
	*/
4c79b5
	function bbcode_quote($in)
4c79b5
	{
4c79b5
		global $config, $user;
4c79b5
4c79b5
		/**
4c79b5
		* If you change this code, make sure the cases described within the following reports are still working:
4c79b5
		* #3572 - [quote="[test]test"]test [ test[/quote] - (correct: parsed)
4c79b5
		* #14667 - [quote]test[/quote] test ] and [ test [quote]test[/quote] (correct: parsed)
4c79b5
		* #14770 - [quote="["]test[/quote] (correct: parsed)
4c79b5
		* [quote="[i]test[/i]"]test[/quote] (correct: parsed)
4c79b5
		* [quote="[quote]test[/quote]"]test[/quote] (correct: parsed - Username displayed as [quote]test[/quote])
4c79b5
		* #20735 - [quote]test[/[/b]quote] test [/quote][/quote] test - (correct: quoted: "test[/[/b]quote] test" / non-quoted: "[/quote] test" - also failed if layout distorted)
4c79b5
		*/
4c79b5
4c79b5
		$in = str_replace("\r\n", "\n", str_replace('\"', '"', trim($in)));
4c79b5
4c79b5
		if (!$in)
4c79b5
		{
4c79b5
			return '';
4c79b5
		}
4c79b5
4c79b5
		// To let the parser not catch tokens within quote_username quotes we encode them before we start this...
4c79b5
		$in = preg_replace('#quote="(.*?)"\]#ie', "'quote="' . str_replace(array('[', ']'), array('[', ']'), '\$1') . '"]'", $in);
4c79b5
4c79b5
		$tok = ']';
4c79b5
		$out = '[';
4c79b5
4c79b5
		$in = substr($in, 1);
4c79b5
		$close_tags = $error_ary = array();
4c79b5
		$buffer = '';
4c79b5
4c79b5
		do
4c79b5
		{
4c79b5
			$pos = strlen($in);
4c79b5
			for ($i = 0, $tok_len = strlen($tok); $i < $tok_len; ++$i)
4c79b5
			{
4c79b5
				$tmp_pos = strpos($in, $tok[$i]);
4c79b5
				if ($tmp_pos !== false && $tmp_pos < $pos)
4c79b5
				{
4c79b5
					$pos = $tmp_pos;
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			$buffer .= substr($in, 0, $pos);
4c79b5
			$tok = $in[$pos];
4c79b5
			$in = substr($in, $pos + 1);
4c79b5
4c79b5
			if ($tok == ']')
4c79b5
			{
4c79b5
				if (strtolower($buffer) == '/quote' && sizeof($close_tags) && substr($out, -1, 1) == '[')
4c79b5
				{
4c79b5
					// we have found a closing tag
4c79b5
					$out .= array_pop($close_tags) . ']';
4c79b5
					$tok = '[';
4c79b5
					$buffer = '';
4c79b5
4c79b5
					/* Add space at the end of the closing tag if not happened before to allow following urls/smilies to be parsed correctly
4c79b5
					* 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.
4c79b5
					* Also, we won't have any spaces within $in anyway, only adding up spaces -> #10982
4c79b5
					if (!$in || $in[0] !== ' ')
4c79b5
					{
4c79b5
						$out .= ' ';
4c79b5
					}*/
4c79b5
				}
4c79b5
				else if (preg_match('#^quote(?:="(.*?)")?$#is', $buffer, $m) && substr($out, -1, 1) == '[')
4c79b5
				{
4c79b5
					$this->parsed_items['quote']++;
4c79b5
4c79b5
					// the buffer holds a valid opening tag
4c79b5
					if ($config['max_quote_depth'] && sizeof($close_tags) >= $config['max_quote_depth'])
4c79b5
					{
4c79b5
						// there are too many nested quotes
4c79b5
						$error_ary['quote_depth'] = sprintf($user->lang['QUOTE_DEPTH_EXCEEDED'], $config['max_quote_depth']);
4c79b5
4c79b5
						$out .= $buffer . $tok;
4c79b5
						$tok = '[]';
4c79b5
						$buffer = '';
4c79b5
4c79b5
						continue;
4c79b5
					}
4c79b5
4c79b5
					array_push($close_tags, '/quote:' . $this->bbcode_uid);
4c79b5
4c79b5
					if (isset($m[1]) && $m[1])
4c79b5
					{
4c79b5
						$username = str_replace(array('[', ']'), array('[', ']'), $m[1]);
4c79b5
						$username = preg_replace('#\[(?!b|i|u|color|url|email|/b|/i|/u|/color|/url|/email)#iU', '[$1', $username);
4c79b5
4c79b5
						$end_tags = array();
4c79b5
						$error = false;
4c79b5
4c79b5
						preg_match_all('#\[((?:/)?(?:[a-z]+))#i', $username, $tags);
4c79b5
						foreach ($tags[1] as $tag)
4c79b5
						{
4c79b5
							if ($tag[0] != '/')
4c79b5
							{
4c79b5
								$end_tags[] = '/' . $tag;
4c79b5
							}
4c79b5
							else
4c79b5
							{
4c79b5
								$end_tag = array_pop($end_tags);
4c79b5
								$error = ($end_tag != $tag) ? true : false;
4c79b5
							}
4c79b5
						}
4c79b5
4c79b5
						if ($error)
4c79b5
						{
4c79b5
							$username = $m[1];
4c79b5
						}
4c79b5
4c79b5
						$out .= 'quote="' . $username . '":' . $this->bbcode_uid . ']';
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						$out .= 'quote:' . $this->bbcode_uid . ']';
4c79b5
					}
4c79b5
4c79b5
					$tok = '[';
4c79b5
					$buffer = '';
4c79b5
				}
4c79b5
				else if (preg_match('#^quote="(.*?)#is', $buffer, $m))
4c79b5
				{
4c79b5
					// the buffer holds an invalid opening tag
4c79b5
					$buffer .= ']';
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$out .= $buffer . $tok;
4c79b5
					$tok = '[]';
4c79b5
					$buffer = '';
4c79b5
				}
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
/**
4c79b5
*				Old quote code working fine, but having errors listed in bug #3572
4c79b5
*
4c79b5
*				$out .= $buffer . $tok;
4c79b5
*				$tok = ($tok == '[') ? ']' : '[]';
4c79b5
*				$buffer = '';
4c79b5
*/
4c79b5
4c79b5
				$out .= $buffer . $tok;
4c79b5
4c79b5
				if ($tok == '[')
4c79b5
				{
4c79b5
					// Search the text for the next tok... if an ending quote comes first, then change tok to []
4c79b5
					$pos1 = stripos($in, '[/quote');
4c79b5
					// If the token ] comes first, we change it to ]
4c79b5
					$pos2 = strpos($in, ']');
4c79b5
					// If the token [ comes first, we change it to [
4c79b5
					$pos3 = strpos($in, '[');
4c79b5
4c79b5
					if ($pos1 !== false && ($pos2 === false || $pos1 < $pos2) && ($pos3 === false || $pos1 < $pos3))
4c79b5
					{
4c79b5
						$tok = '[]';
4c79b5
					}
4c79b5
					else if ($pos3 !== false && ($pos2 === false || $pos3 < $pos2))
4c79b5
					{
4c79b5
						$tok = '[';
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						$tok = ']';
4c79b5
					}
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$tok = '[]';
4c79b5
				}
4c79b5
				$buffer = '';
4c79b5
			}
4c79b5
		}
4c79b5
		while ($in);
4c79b5
4c79b5
		if (sizeof($close_tags))
4c79b5
		{
4c79b5
			$out .= '[' . implode('][', $close_tags) . ']';
4c79b5
		}
4c79b5
4c79b5
		foreach ($error_ary as $error_msg)
4c79b5
		{
4c79b5
			$this->warn_msg[] = $error_msg;
4c79b5
		}
4c79b5
4c79b5
		return $out;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Validate email
4c79b5
	*/
4c79b5
	function validate_email($var1, $var2)
4c79b5
	{
4c79b5
		$var1 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var1)));
4c79b5
		$var2 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var2)));
4c79b5
4c79b5
		$txt = $var2;
4c79b5
		$email = ($var1) ? $var1 : $var2;
4c79b5
4c79b5
		$validated = true;
4c79b5
4c79b5
		if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
4c79b5
		{
4c79b5
			$validated = false;
4c79b5
		}
4c79b5
4c79b5
		if (!$validated)
4c79b5
		{
4c79b5
			return '[email' . (($var1) ? "=$var1" : '') . ']' . $var2 . '[/email]';
4c79b5
		}
4c79b5
4c79b5
		$this->parsed_items['email']++;
4c79b5
4c79b5
		if ($var1)
4c79b5
		{
4c79b5
			$retval = '[email=' . $this->bbcode_specialchars($email) . ':' . $this->bbcode_uid . ']' . $txt . '[/email:' . $this->bbcode_uid . ']';
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$retval = '[email:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($email) . '[/email:' . $this->bbcode_uid . ']';
4c79b5
		}
4c79b5
4c79b5
		return $retval;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Validate url
4c79b5
	*
4c79b5
	* @param string $var1 optional url parameter for url bbcode: [url(=$var1)]$var2[/url]
4c79b5
	* @param string $var2 url bbcode content: [url(=$var1)]$var2[/url]
4c79b5
	*/
4c79b5
	function validate_url($var1, $var2)
4c79b5
	{
4c79b5
		global $config;
4c79b5
4c79b5
		$var1 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var1)));
4c79b5
		$var2 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var2)));
4c79b5
4c79b5
		$url = ($var1) ? $var1 : $var2;
4c79b5
4c79b5
		if ($var1 && !$var2)
4c79b5
		{
4c79b5
			$var2 = $var1;
4c79b5
		}
4c79b5
4c79b5
		if (!$url)
4c79b5
		{
4c79b5
			return '[url' . (($var1) ? '=' . $var1 : '') . ']' . $var2 . '[/url]';
4c79b5
		}
4c79b5
4c79b5
		$valid = false;
4c79b5
4c79b5
		$url = str_replace(' ', '%20', $url);
4c79b5
4c79b5
		// Checking urls
4c79b5
		if (preg_match('#^' . get_preg_expression('url') . '$#i', $url) ||
4c79b5
			preg_match('#^' . get_preg_expression('www_url') . '$#i', $url) ||
4c79b5
			preg_match('#^' . preg_quote(generate_board_url(), '#') . get_preg_expression('relative_url') . '$#i', $url))
4c79b5
		{
4c79b5
			$valid = true;
4c79b5
		}
4c79b5
4c79b5
		if ($valid)
4c79b5
		{
4c79b5
			$this->parsed_items['url']++;
4c79b5
4c79b5
			// if there is no scheme, then add http schema
4c79b5
			if (!preg_match('#^[a-z][a-z\d+\-.]*:/{2}#i', $url))
4c79b5
			{
4c79b5
				$url = 'http://' . $url;
4c79b5
			}
4c79b5
4c79b5
			// Is this a link to somewhere inside this board? If so then remove the session id from the url
4c79b5
			if (strpos($url, generate_board_url()) !== false && strpos($url, 'sid=') !== false)
4c79b5
			{
4c79b5
				$url = preg_replace('/(&|\?)sid=[0-9a-f]{32}&/', '\1', $url);
4c79b5
				$url = preg_replace('/(&|\?)sid=[0-9a-f]{32}$/', '', $url);
4c79b5
				$url = append_sid($url);
4c79b5
			}
4c79b5
4c79b5
			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 . ']';
4c79b5
		}
4c79b5
4c79b5
		return '[url' . (($var1) ? '=' . $var1 : '') . ']' . $var2 . '[/url]';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Check if url is pointing to this domain/script_path/php-file
4c79b5
	*
4c79b5
	* @param string $url the url to check
4c79b5
	* @return true if the url is pointing to this domain/script_path/php-file, false if not
4c79b5
	*
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function path_in_domain($url)
4c79b5
	{
4c79b5
		global $config, $phpEx, $user;
4c79b5
4c79b5
		if ($config['force_server_vars'])
4c79b5
		{
4c79b5
			$check_path = $config['script_path'];
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$check_path = ($user->page['root_script_path'] != '/') ? substr($user->page['root_script_path'], 0, -1) : '/';
4c79b5
		}
4c79b5
4c79b5
		// Is the user trying to link to a php file in this domain and script path?
4c79b5
		if (strpos($url, ".{$phpEx}") !== false && strpos($url, $check_path) !== false)
4c79b5
		{
4c79b5
			$server_name = $user->host;
4c79b5
4c79b5
			// Forcing server vars is the only way to specify/override the protocol
4c79b5
			if ($config['force_server_vars'] || !$server_name)
4c79b5
			{
4c79b5
				$server_name = $config['server_name'];
4c79b5
			}
4c79b5
4c79b5
			// Check again in correct order...
4c79b5
			$pos_ext = strpos($url, ".{$phpEx}");
4c79b5
			$pos_path = strpos($url, $check_path);
4c79b5
			$pos_domain = strpos($url, $server_name);
4c79b5
4c79b5
			if ($pos_domain !== false && $pos_path >= $pos_domain && $pos_ext >= $pos_path)
4c79b5
			{
4c79b5
				// Ok, actually we allow linking to some files (this may be able to be extended in some way later...)
4c79b5
				if (strpos($url, '/' . $check_path . '/download/file.' . $phpEx) !== 0)
4c79b5
				{
4c79b5
					return false;
4c79b5
				}
4c79b5
4c79b5
				return true;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return false;
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Main message parser for posting, pm, etc. takes raw message
4c79b5
* and parses it for attachments, bbcode and smilies
4c79b5
* @package phpBB3
4c79b5
*/
4c79b5
class parse_message extends bbcode_firstpass
4c79b5
{
4c79b5
	var $attachment_data = array();
4c79b5
	var $filename_data = array();
4c79b5
4c79b5
	// Helps ironing out user error
4c79b5
	var $message_status = '';
4c79b5
4c79b5
	var $allow_img_bbcode = true;
4c79b5
	var $allow_flash_bbcode = true;
4c79b5
	var $allow_quote_bbcode = true;
4c79b5
	var $allow_url_bbcode = true;
4c79b5
4c79b5
	var $mode;
4c79b5
4c79b5
	/**
4c79b5
	* Init - give message here or manually
4c79b5
	*/
4c79b5
	function parse_message($message = '')
4c79b5
	{
4c79b5
		// Init BBCode UID
4c79b5
		$this->bbcode_uid = substr(base_convert(unique_id(), 16, 36), 0, BBCODE_UID_LEN);
4c79b5
4c79b5
		if ($message)
4c79b5
		{
4c79b5
			$this->message = $message;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse Message
4c79b5
	*/
4c79b5
	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')
4c79b5
	{
4c79b5
		global $config, $db, $user;
4c79b5
4c79b5
		$mode = ($mode != 'post') ? 'sig' : 'post';
4c79b5
4c79b5
		$this->mode = $mode;
4c79b5
4c79b5
		$this->allow_img_bbcode = $allow_img_bbcode;
4c79b5
		$this->allow_flash_bbcode = $allow_flash_bbcode;
4c79b5
		$this->allow_quote_bbcode = $allow_quote_bbcode;
4c79b5
		$this->allow_url_bbcode = $allow_url_bbcode;
4c79b5
4c79b5
		// If false, then $this->message won't be altered, the text will be returned instead.
4c79b5
		if (!$update_this_message)
4c79b5
		{
4c79b5
			$tmp_message = $this->message;
4c79b5
			$return_message = &$this->message;
4c79b5
		}
4c79b5
4c79b5
		if ($this->message_status == 'display')
4c79b5
		{
4c79b5
			$this->decode_message();
4c79b5
		}
4c79b5
4c79b5
		// Do some general 'cleanup' first before processing message,
4c79b5
		// e.g. remove excessive newlines(?), smilies(?)
4c79b5
		$match = array('#(script|about|applet|activex|chrome):#i');
4c79b5
		$replace = array("\\1:");
4c79b5
		$this->message = preg_replace($match, $replace, trim($this->message));
4c79b5
4c79b5
		// Message length check. 0 disables this check completely.
4c79b5
		if ($config['max_' . $mode . '_chars'] > 0)
4c79b5
		{
4c79b5
			$msg_len = ($mode == 'post') ? utf8_strlen($this->message) : utf8_strlen(preg_replace('#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius', ' ', $this->message));
4c79b5
4c79b5
			if ((!$msg_len && $mode !== 'sig') || $config['max_' . $mode . '_chars'] && $msg_len > $config['max_' . $mode . '_chars'])
4c79b5
			{
4c79b5
				$this->warn_msg[] = (!$msg_len) ? $user->lang['TOO_FEW_CHARS'] : sprintf($user->lang['TOO_MANY_CHARS_' . strtoupper($mode)], $msg_len, $config['max_' . $mode . '_chars']);
4c79b5
				return (!$update_this_message) ? $return_message : $this->warn_msg;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Check for "empty" message
4c79b5
		if ($mode !== 'sig' && utf8_clean_string($this->message) === '')
4c79b5
		{
4c79b5
			$this->warn_msg[] = $user->lang['TOO_FEW_CHARS'];
4c79b5
			return (!$update_this_message) ? $return_message : $this->warn_msg;
4c79b5
		}
4c79b5
4c79b5
		// Prepare BBcode (just prepares some tags for better parsing)
4c79b5
		if ($allow_bbcode && strpos($this->message, '[') !== false)
4c79b5
		{
4c79b5
			$this->bbcode_init();
4c79b5
			$disallow = array('img', 'flash', 'quote', 'url');
4c79b5
			foreach ($disallow as $bool)
4c79b5
			{
4c79b5
				if (!${'allow_' . $bool . '_bbcode'})
4c79b5
				{
4c79b5
					$this->bbcodes[$bool]['disabled'] = true;
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			$this->prepare_bbcodes();
4c79b5
		}
4c79b5
4c79b5
		// Parse smilies
4c79b5
		if ($allow_smilies)
4c79b5
		{
4c79b5
			$this->smilies($config['max_' . $mode . '_smilies']);
4c79b5
		}
4c79b5
4c79b5
		$num_urls = 0;
4c79b5
4c79b5
		// Parse BBCode
4c79b5
		if ($allow_bbcode && strpos($this->message, '[') !== false)
4c79b5
		{
4c79b5
			$this->parse_bbcode();
4c79b5
			$num_urls += $this->parsed_items['url'];
4c79b5
		}
4c79b5
4c79b5
		// Parse URL's
4c79b5
		if ($allow_magic_url)
4c79b5
		{
4c79b5
			$this->magic_url(generate_board_url());
4c79b5
4c79b5
			if ($config['max_' . $mode . '_urls'])
4c79b5
			{
4c79b5
				$num_urls += preg_match_all('#\
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Check number of links
4c79b5
		if ($config['max_' . $mode . '_urls'] && $num_urls > $config['max_' . $mode . '_urls'])
4c79b5
		{
4c79b5
			$this->warn_msg[] = sprintf($user->lang['TOO_MANY_URLS'], $config['max_' . $mode . '_urls']);
4c79b5
			return (!$update_this_message) ? $return_message : $this->warn_msg;
4c79b5
		}
4c79b5
4c79b5
		if (!$update_this_message)
4c79b5
		{
4c79b5
			unset($this->message);
4c79b5
			$this->message = $tmp_message;
4c79b5
			return $return_message;
4c79b5
		}
4c79b5
4c79b5
		$this->message_status = 'parsed';
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Formatting text for display
4c79b5
	*/
4c79b5
	function format_display($allow_bbcode, $allow_magic_url, $allow_smilies, $update_this_message = true)
4c79b5
	{
4c79b5
		// If false, then the parsed message get returned but internal message not processed.
4c79b5
		if (!$update_this_message)
4c79b5
		{
4c79b5
			$tmp_message = $this->message;
4c79b5
			$return_message = &$this->message;
4c79b5
		}
4c79b5
4c79b5
		if ($this->message_status == 'plain')
4c79b5
		{
4c79b5
			// Force updating message - of course.
4c79b5
			$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);
4c79b5
		}
4c79b5
4c79b5
		// Replace naughty words such as farty pants
4c79b5
		$this->message = censor_text($this->message);
4c79b5
4c79b5
		// Parse BBcode
4c79b5
		if ($allow_bbcode)
4c79b5
		{
4c79b5
			$this->bbcode_cache_init();
4c79b5
4c79b5
			// We are giving those parameters to be able to use the bbcode class on its own
4c79b5
			$this->bbcode_second_pass($this->message, $this->bbcode_uid);
4c79b5
		}
4c79b5
4c79b5
		$this->message = bbcode_nl2br($this->message);
4c79b5
		$this->message = smiley_text($this->message, !$allow_smilies);
4c79b5
4c79b5
		if (!$update_this_message)
4c79b5
		{
4c79b5
			unset($this->message);
4c79b5
			$this->message = $tmp_message;
4c79b5
			return $return_message;
4c79b5
		}
4c79b5
4c79b5
		$this->message_status = 'display';
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Decode message to be placed back into form box
4c79b5
	*/
4c79b5
	function decode_message($custom_bbcode_uid = '', $update_this_message = true)
4c79b5
	{
4c79b5
		// If false, then the parsed message get returned but internal message not processed.
4c79b5
		if (!$update_this_message)
4c79b5
		{
4c79b5
			$tmp_message = $this->message;
4c79b5
			$return_message = &$this->message;
4c79b5
		}
4c79b5
4c79b5
		($custom_bbcode_uid) ? decode_message($this->message, $custom_bbcode_uid) : decode_message($this->message, $this->bbcode_uid);
4c79b5
4c79b5
		if (!$update_this_message)
4c79b5
		{
4c79b5
			unset($this->message);
4c79b5
			$this->message = $tmp_message;
4c79b5
			return $return_message;
4c79b5
		}
4c79b5
4c79b5
		$this->message_status = 'plain';
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Replace magic urls of form http://xxx.xxx., www.xxx. and xxx@xxx.xxx.
4c79b5
	* Cuts down displayed size of link if over 50 chars, turns absolute links
4c79b5
	* into relative versions when the server/script path matches the link
4c79b5
	*/
4c79b5
	function magic_url($server_url)
4c79b5
	{
4c79b5
		// We use the global make_clickable function
4c79b5
		$this->message = make_clickable($this->message, $server_url);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse Smilies
4c79b5
	*/
4c79b5
	function smilies($max_smilies = 0)
4c79b5
	{
4c79b5
		global $db, $user;
4c79b5
		static $match;
4c79b5
		static $replace;
4c79b5
4c79b5
		// See if the static arrays have already been filled on an earlier invocation
4c79b5
		if (!is_array($match))
4c79b5
		{
4c79b5
			$match = $replace = array();
4c79b5
4c79b5
			// NOTE: obtain_* function? chaching the table contents?
4c79b5
4c79b5
			// For now setting the ttl to 10 minutes
4c79b5
			switch ($db->sql_layer)
4c79b5
			{
4c79b5
				case 'mssql':
4c79b5
				case 'mssql_odbc':
4c79b5
					$sql = 'SELECT *
4c79b5
						FROM ' . SMILIES_TABLE . '
4c79b5
						ORDER BY LEN(code) DESC';
4c79b5
				break;
4c79b5
4c79b5
				case 'firebird':
4c79b5
					$sql = 'SELECT *
4c79b5
						FROM ' . SMILIES_TABLE . '
4c79b5
						ORDER BY CHAR_LENGTH(code) DESC';
4c79b5
				break;
4c79b5
4c79b5
				// LENGTH supported by MySQL, IBM DB2, Oracle and Access for sure...
4c79b5
				default:
4c79b5
					$sql = 'SELECT *
4c79b5
						FROM ' . SMILIES_TABLE . '
4c79b5
						ORDER BY LENGTH(code) DESC';
4c79b5
				break;
4c79b5
			}
4c79b5
			$result = $db->sql_query($sql, 600);
4c79b5
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				if (empty($row['code']))
4c79b5
				{
4c79b5
					continue;
4c79b5
				}
4c79b5
4c79b5
				// (assertion)
4c79b5
				$match[] = '(?<=^|[\n .])' . preg_quote($row['code'], '#') . '(?![^<>]*>)';
4c79b5
				$replace[] = '' . $row['code'] . '';
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($match))
4c79b5
		{
4c79b5
			if ($max_smilies)
4c79b5
			{
4c79b5
				$num_matches = preg_match_all('#' . implode('|', $match) . '#', $this->message, $matches);
4c79b5
				unset($matches);
4c79b5
4c79b5
				if ($num_matches !== false && $num_matches > $max_smilies)
4c79b5
				{
4c79b5
					$this->warn_msg[] = sprintf($user->lang['TOO_MANY_SMILIES'], $max_smilies);
4c79b5
					return;
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			// Make sure the delimiter # is added in front and at the end of every element within $match
4c79b5
			$this->message = trim(preg_replace(explode(chr(0), '#' . implode('#' . chr(0) . '#', $match) . '#'), $replace, $this->message));
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse Attachments
4c79b5
	*/
4c79b5
	function parse_attachments($form_name, $mode, $forum_id, $submit, $preview, $refresh, $is_message = false)
4c79b5
	{
4c79b5
		global $config, $auth, $user, $phpbb_root_path, $phpEx, $db;
4c79b5
4c79b5
		$error = array();
4c79b5
4c79b5
		$num_attachments = sizeof($this->attachment_data);
4c79b5
		$this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true));
4c79b5
		$upload_file = (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none' && trim($_FILES[$form_name]['name'])) ? true : false;
4c79b5
4c79b5
		$add_file		= (isset($_POST['add_file'])) ? true : false;
4c79b5
		$delete_file	= (isset($_POST['delete_file'])) ? true : false;
4c79b5
4c79b5
		// First of all adjust comments if changed
4c79b5
		$actual_comment_list = utf8_normalize_nfc(request_var('comment_list', array(''), true));
4c79b5
4c79b5
		foreach ($actual_comment_list as $comment_key => $comment)
4c79b5
		{
4c79b5
			if (!isset($this->attachment_data[$comment_key]))
4c79b5
			{
4c79b5
				continue;
4c79b5
			}
4c79b5
4c79b5
			if ($this->attachment_data[$comment_key]['attach_comment'] != $actual_comment_list[$comment_key])
4c79b5
			{
4c79b5
				$this->attachment_data[$comment_key]['attach_comment'] = $actual_comment_list[$comment_key];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$cfg = array();
4c79b5
		$cfg['max_attachments'] = ($is_message) ? $config['max_attachments_pm'] : $config['max_attachments'];
4c79b5
		$forum_id = ($is_message) ? 0 : $forum_id;
4c79b5
4c79b5
		if ($submit && in_array($mode, array('post', 'reply', 'quote', 'edit')) && $upload_file)
4c79b5
		{
4c79b5
			if ($num_attachments < $cfg['max_attachments'] || $auth->acl_get('a_') || $auth->acl_get('m_', $forum_id))
4c79b5
			{
4c79b5
				$filedata = upload_attachment($form_name, $forum_id, false, '', $is_message);
4c79b5
				$error = $filedata['error'];
4c79b5
4c79b5
				if ($filedata['post_attach'] && !sizeof($error))
4c79b5
				{
4c79b5
					$sql_ary = array(
4c79b5
						'physical_filename'	=> $filedata['physical_filename'],
4c79b5
						'attach_comment'	=> $this->filename_data['filecomment'],
4c79b5
						'real_filename'		=> $filedata['real_filename'],
4c79b5
						'extension'			=> $filedata['extension'],
4c79b5
						'mimetype'			=> $filedata['mimetype'],
4c79b5
						'filesize'			=> $filedata['filesize'],
4c79b5
						'filetime'			=> $filedata['filetime'],
4c79b5
						'thumbnail'			=> $filedata['thumbnail'],
4c79b5
						'is_orphan'			=> 1,
4c79b5
						'in_message'		=> ($is_message) ? 1 : 0,
4c79b5
						'poster_id'			=> $user->data['user_id'],
4c79b5
					);
4c79b5
4c79b5
					$db->sql_query('INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
4c79b5
4c79b5
					$new_entry = array(
4c79b5
						'attach_id'		=> $db->sql_nextid(),
4c79b5
						'is_orphan'		=> 1,
4c79b5
						'real_filename'	=> $filedata['real_filename'],
4c79b5
						'attach_comment'=> $this->filename_data['filecomment'],
4c79b5
					);
4c79b5
4c79b5
					$this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
4c79b5
					$this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message);
4c79b5
4c79b5
					$this->filename_data['filecomment'] = '';
4c79b5
4c79b5
					// This Variable is set to false here, because Attachments are entered into the
4c79b5
					// Database in two modes, one if the id_list is 0 and the second one if post_attach is true
4c79b5
					// Since post_attach is automatically switched to true if an Attachment got added to the filesystem,
4c79b5
					// but we are assigning an id of 0 here, we have to reset the post_attach variable to false.
4c79b5
					//
4c79b5
					// This is very relevant, because it could happen that the post got not submitted, but we do not
4c79b5
					// know this circumstance here. We could be at the posting page or we could be redirected to the entered
4c79b5
					// post. :)
4c79b5
					$filedata['post_attach'] = false;
4c79b5
				}
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $cfg['max_attachments']);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if ($preview || $refresh || sizeof($error))
4c79b5
		{
4c79b5
			// Perform actions on temporary attachments
4c79b5
			if ($delete_file)
4c79b5
			{
4c79b5
				include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
4c79b5
4c79b5
				$index = array_keys(request_var('delete_file', array(0 => 0)));
4c79b5
				$index = (!empty($index)) ? $index[0] : false;
4c79b5
4c79b5
				if ($index !== false && !empty($this->attachment_data[$index]))
4c79b5
				{
4c79b5
					// delete selected attachment
4c79b5
					if ($this->attachment_data[$index]['is_orphan'])
4c79b5
					{
4c79b5
						$sql = 'SELECT attach_id, physical_filename, thumbnail
4c79b5
							FROM ' . ATTACHMENTS_TABLE . '
4c79b5
							WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id'] . '
4c79b5
								AND is_orphan = 1
4c79b5
								AND poster_id = ' . $user->data['user_id'];
4c79b5
						$result = $db->sql_query($sql);
4c79b5
						$row = $db->sql_fetchrow($result);
4c79b5
						$db->sql_freeresult($result);
4c79b5
4c79b5
						if ($row)
4c79b5
						{
4c79b5
							phpbb_unlink($row['physical_filename'], 'file');
4c79b5
4c79b5
							if ($row['thumbnail'])
4c79b5
							{
4c79b5
								phpbb_unlink($row['physical_filename'], 'thumbnail');
4c79b5
							}
4c79b5
4c79b5
							$db->sql_query('DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id']);
4c79b5
						}
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						delete_attachments('attach', array(intval($this->attachment_data[$index]['attach_id'])));
4c79b5
					}
4c79b5
4c79b5
					unset($this->attachment_data[$index]);
4c79b5
					$this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "(\\1 == \$index) ? '' : ((\\1 > \$index) ? '[attachment=' . (\\1 - 1) . ']\\2[/attachment]' : '\\0')", $this->message);
4c79b5
4c79b5
					// Reindex Array
4c79b5
					$this->attachment_data = array_values($this->attachment_data);
4c79b5
				}
4c79b5
			}
4c79b5
			else if (($add_file || $preview) && $upload_file)
4c79b5
			{
4c79b5
				if ($num_attachments < $cfg['max_attachments'] || $auth->acl_gets('m_', 'a_', $forum_id))
4c79b5
				{
4c79b5
					$filedata = upload_attachment($form_name, $forum_id, false, '', $is_message);
4c79b5
					$error = array_merge($error, $filedata['error']);
4c79b5
4c79b5
					if (!sizeof($error))
4c79b5
					{
4c79b5
						$sql_ary = array(
4c79b5
							'physical_filename'	=> $filedata['physical_filename'],
4c79b5
							'attach_comment'	=> $this->filename_data['filecomment'],
4c79b5
							'real_filename'		=> $filedata['real_filename'],
4c79b5
							'extension'			=> $filedata['extension'],
4c79b5
							'mimetype'			=> $filedata['mimetype'],
4c79b5
							'filesize'			=> $filedata['filesize'],
4c79b5
							'filetime'			=> $filedata['filetime'],
4c79b5
							'thumbnail'			=> $filedata['thumbnail'],
4c79b5
							'is_orphan'			=> 1,
4c79b5
							'in_message'		=> ($is_message) ? 1 : 0,
4c79b5
							'poster_id'			=> $user->data['user_id'],
4c79b5
						);
4c79b5
4c79b5
						$db->sql_query('INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
4c79b5
4c79b5
						$new_entry = array(
4c79b5
							'attach_id'		=> $db->sql_nextid(),
4c79b5
							'is_orphan'		=> 1,
4c79b5
							'real_filename'	=> $filedata['real_filename'],
4c79b5
							'attach_comment'=> $this->filename_data['filecomment'],
4c79b5
						);
4c79b5
4c79b5
						$this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
4c79b5
						$this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message);
4c79b5
						$this->filename_data['filecomment'] = '';
4c79b5
					}
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $cfg['max_attachments']);
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		foreach ($error as $error_msg)
4c79b5
		{
4c79b5
			$this->warn_msg[] = $error_msg;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Get Attachment Data
4c79b5
	*/
4c79b5
	function get_submitted_attachment_data($check_user_id = false)
4c79b5
	{
4c79b5
		global $user, $db, $phpbb_root_path, $phpEx, $config;
4c79b5
4c79b5
		$this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true));
4c79b5
		$attachment_data = (isset($_POST['attachment_data'])) ? $_POST['attachment_data'] : array();
4c79b5
		$this->attachment_data = array();
4c79b5
4c79b5
		$check_user_id = ($check_user_id === false) ? $user->data['user_id'] : $check_user_id;
4c79b5
4c79b5
		if (!sizeof($attachment_data))
4c79b5
		{
4c79b5
			return;
4c79b5
		}
4c79b5
4c79b5
		$not_orphan = $orphan = array();
4c79b5
4c79b5
		foreach ($attachment_data as $pos => $var_ary)
4c79b5
		{
4c79b5
			if ($var_ary['is_orphan'])
4c79b5
			{
4c79b5
				$orphan[(int) $var_ary['attach_id']] = $pos;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$not_orphan[(int) $var_ary['attach_id']] = $pos;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Regenerate already posted attachments
4c79b5
		if (sizeof($not_orphan))
4c79b5
		{
4c79b5
			// Get the attachment data, based on the poster id...
4c79b5
			$sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment
4c79b5
				FROM ' . ATTACHMENTS_TABLE . '
4c79b5
				WHERE ' . $db->sql_in_set('attach_id', array_keys($not_orphan)) . '
4c79b5
					AND poster_id = ' . $check_user_id;
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$pos = $not_orphan[$row['attach_id']];
4c79b5
				$this->attachment_data[$pos] = $row;
4c79b5
				set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true);
4c79b5
4c79b5
				unset($not_orphan[$row['attach_id']]);
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($not_orphan))
4c79b5
		{
4c79b5
			trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR);
4c79b5
		}
4c79b5
4c79b5
		// Regenerate newly uploaded attachments
4c79b5
		if (sizeof($orphan))
4c79b5
		{
4c79b5
			$sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment
4c79b5
				FROM ' . ATTACHMENTS_TABLE . '
4c79b5
				WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan)) . '
4c79b5
					AND poster_id = ' . $user->data['user_id'] . '
4c79b5
					AND is_orphan = 1';
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$pos = $orphan[$row['attach_id']];
4c79b5
				$this->attachment_data[$pos] = $row;
4c79b5
				set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true);
4c79b5
4c79b5
				unset($orphan[$row['attach_id']]);
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($orphan))
4c79b5
		{
4c79b5
			trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR);
4c79b5
		}
4c79b5
4c79b5
		ksort($this->attachment_data);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Parse Poll
4c79b5
	*/
4c79b5
	function parse_poll(&$poll)
4c79b5
	{
4c79b5
		global $auth, $user, $config;
4c79b5
4c79b5
		$poll_max_options = $poll['poll_max_options'];
4c79b5
4c79b5
		// Parse Poll Option text ;)
4c79b5
		$tmp_message = $this->message;
4c79b5
		$this->message = $poll['poll_option_text'];
4c79b5
		$bbcode_bitfield = $this->bbcode_bitfield;
4c79b5
4c79b5
		$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);
4c79b5
4c79b5
		$bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield));
4c79b5
		$this->message = $tmp_message;
4c79b5
4c79b5
		// Parse Poll Title
4c79b5
		$tmp_message = $this->message;
4c79b5
		$this->message = $poll['poll_title'];
4c79b5
		$this->bbcode_bitfield = $bbcode_bitfield;
4c79b5
4c79b5
		$poll['poll_options'] = explode("\n", trim($poll['poll_option_text']));
4c79b5
		$poll['poll_options_size'] = sizeof($poll['poll_options']);
4c79b5
4c79b5
		if (!$poll['poll_title'] && $poll['poll_options_size'])
4c79b5
		{
4c79b5
			$this->warn_msg[] = $user->lang['NO_POLL_TITLE'];
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			if (utf8_strlen(preg_replace('#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius', ' ', $this->message)) > 100)
4c79b5
			{
4c79b5
				$this->warn_msg[] = $user->lang['POLL_TITLE_TOO_LONG'];
4c79b5
			}
4c79b5
			$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);
4c79b5
			if (strlen($poll['poll_title']) > 255)
4c79b5
			{
4c79b5
				$this->warn_msg[] = $user->lang['POLL_TITLE_COMP_TOO_LONG'];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$this->bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield));
4c79b5
		$this->message = $tmp_message;
4c79b5
		unset($tmp_message);
4c79b5
4c79b5
		if (sizeof($poll['poll_options']) == 1)
4c79b5
		{
4c79b5
			$this->warn_msg[] = $user->lang['TOO_FEW_POLL_OPTIONS'];
4c79b5
		}
4c79b5
		else if ($poll['poll_options_size'] > (int) $config['max_poll_options'])
4c79b5
		{
4c79b5
			$this->warn_msg[] = $user->lang['TOO_MANY_POLL_OPTIONS'];
4c79b5
		}
4c79b5
		else if ($poll_max_options > $poll['poll_options_size'])
4c79b5
		{
4c79b5
			$this->warn_msg[] = $user->lang['TOO_MANY_USER_OPTIONS'];
4c79b5
		}
4c79b5
4c79b5
		$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']);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>