function sed_bbcode($text)
{
	global $L, $skin, $sys, $cfg, $sed_groups;

	$text = sed_bbcode_autourls($text);
	$text = " ".$text;

	$bbcodes = array(
	'$' => '&#36;',
	'[b]' => '<strong>',
	'[/b]' => '</strong>',
	'[p]' => '<p>',
	'[/p]' => '</p>',
	'[u]' => '<span style="text-decoration:underline">',
	'[/u]' => '</span>',
	'[s]' => '<span style="text-decoration:line-through">',
	'[/s]' => '</span>',
	'[i]' => '<em>',
	'[/i]' => '</em>',
	'[hr]' => '<hr />',
	'[_]' => '&nbsp;',
	'[__]' => '&nbsp; &nbsp;',
	'[list]' => '<ul>',
	'[/list]' => '</ul>',
	'[ol]' => '<ol>',
	'[/ol]' => '</ol>',
	'[li]' => '<li>',
	'[/li]' => '</li>',
	'[red]' => '<span style="color:#F93737">',
	'[/red]' => '</span>',
	'[white]' => '<span style="color:#FFFFFF">',
	'[/white]' => '</span>',
	'[green]' => '<span style="color:#09DD09">',
	'[/green]' => '</span>',
	'[blue]' => '<span style="color:#018BFF">',
	'[/blue]' => '</span>',
	'[orange]' => '<span style="color:#FF9900">',
	'[/orange]' => '</span>',
	'[yellow]' => '<span style="color:#FFFF00">',
	'[/yellow]' => '</span>',
	'[purple]' => '<span style="color:#A22ADA">',
	'[/purple]' => '</span>',
	'[black]' => '<span style="color:#000000">',
	'[/black]' => '</span>',
	'[grey]' => '<span style="color:#B9B9B9">',
	'[/grey]' => '</span>',
	'[pink]' => '<span style="color:#FFC0FF">',
	'[/pink]' => '</span>',
	'[sky]' => '<span style="color:#D1F4F9">',
	'[/sky]' => '</span>',
	'[sea]' => '<span style="color:#171A97">',
	'[/sea]' => '</span>',
	'[quote]' => '<blockquote><p><strong><em>'.$L['Quote'].'</em></strong>:<br />',
	'[/quote]' => '</p></blockquote>',
	'[br]' => '<br />',
	'[h1]' => '<h1>',
	'[/h1]' => '</h1>',
	'[h2]' => '<h2>',
	'[/h2]' => '</h2>',
	'[h3]' => '<h3>',
	'[/h3]' => '</h3>',
	'[h4]' => '<h4>',
	'[/h4]' => '</h4>',
	'[h5]' => '<h5>',
	'[/h5]' => '</h5>',
	'[size=1]' => '<span style="font-size:xx-small">',
	'[size=2]' => '<span style="font-size:x-small">',
	'[size=3]' => '<span style="font-size:small">',
	'[size=4]' => '<span style="font-size:medium">',
	'[size=5]' => '<span style="font-size:large">',
	'[size=6]' => '<span style="font-size:x-large">',
	'[size=7]' => '<span style="font-size:xx-large">',
	'[/size]' => '</span>',
	'[more]' => ''
	);

	foreach($bbcodes as $bbcode => $bbcodehtml)
	$text = str_replace($bbcode,$bbcodehtml,$text);

	// Tables
	$bbcodes = array(
	'[table]' => '<table>',
	'[/table]' => '</table>',
	'[tr]' => '<tr>',
	'[/tr]' => '</tr>',
	'[td]' => '<td>',
	'[/td]' => '</td>',
	'[th]' => '<th>',
	'[/th]' => '</th>'
	);
	foreach($bbcodes as $bbcode => $bbcodehtml)
	{ $text = str_replace($bbcode,$bbcodehtml,$text); }

	$bbcodes = array(
	'#\[color=([\dA-F]{6})\](.+?)\[/color\]#s' => '<span style="color:#$1">$2</span>',
	'#\[style=([1-9])\](.+?)\[/style\]#s' => '<span class="bbstyle$1">$2</span>',
	'#\[user=(\d+)\](.+?)\[/user\]#' => '<a href="users.php?m=details&id=$1">$2</a>',
	'#\[page=(\d+)\](.+?)\[/page\]#' => '<a href="page.php?id=$1">$2</a>',
	'#\[page\](\d+)\[/page\]#' => '<a href="page.php?id=$1">'.$L['Page'].' #$1</a>',
	'#\[group=(\d+)\](.+?)\[/group\]#' => '<a href="users.php?g=$1">$2</a>',
	'#\[topic\](\d+)\[/topic\]#' => '<a href="forums.php?m=posts&q=$1">'.$L['Topic'].' #$1</a>',
	'#\[post\](\d+)\[/post\]#' => '<a href="forums.php?m=posts&p=$1#$1">'.$L['Post'].' #$1</a>',
	'#\[pm\](\d+)\[/pm\]#' => '<a href="pm.php?m=send&to=$1"><img src="skins/'.$skin.'/img/system/icon-pm.png" alt=""></a>',
	'#\[f\]([a-z][a-z])\[/f\]#' => '<a href="users.php?f=country_$1"><img src="system/img/flags/f-$1.gif" alt="" /></a>',
	'#\[ac=([^\[]+)\](.+?)\[/ac\]#' => '<acronym title="$1">$2</acronym>',
	'#\[del\](.+?)\[/del\]#' => '<del>$1</del>',
	'#\[quote=([^\[]+)\]#' => '<blockquote><p><strong>$1</strong>:<br />',
	'#\[spoiler\]#' => '<div style="margin:0; margin-top:8px"><div style="margin-bottom:4px"><input type="button" value="'.$L['Show'].'" onClick="if(this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display != \'\') { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'\'; this.innerText = \'\'; this.value = \''.$L['Hide'].'\'; } else { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'none\'; this.innerText = \'\'; this.value = \''.$L['Show'].'\'; }"></div><div class="spoiler"><div style="display: none;">',
	'#\[spoiler=([\w_\. \-]+)\]#' => '<div style="margin:0; margin-top:8px"><div style="margin-bottom:4px"><input type="button" value="'.$L['Show'].':$1" onClick="if (this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display != \'\') { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'\'; this.innerText = \'\'; this.value = \''.$L['Hide'].':$1\'; } else { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'none\'; this.innerText = \'\'; this.value = \''.$L['Show'].':$1\'; }"></div><div class="spoiler"><div style="display: none;">',
	'#\[/spoiler\]#' => '</div></div></div>',
	'#\[pfs\]([^\s"\'&;\?\(\[]+)\[/pfs\]#' => '<a href="'.$cfg['pfs_dir'].'$1"><img src="system/img/admin/pfs.gif" alt="" /> $1</a>'
	);

	foreach($bbcodes as $bbcode => $bbcodehtml)
	$text = preg_replace($bbcode, $bbcodehtml, $text);
	
	$bbcodes = array(
	'#\[(img)\]([^\s"\';\?\(\[]+\.(?:jpg|jpeg|gif|png))\[/img\]#',
	'#\[(img)=([^\s"\';\?\(\[]+\.(?:jpg|jpeg|gif|png))\]([^\s"\';\?\(\[]+\.(?:jpg|jpeg|gif|png))\[/img\]#',
	'#\[(thumb)=([^\s"\';\?\(\[]+\.(?:jpg|jpeg|gif|png))\]([^\s"\';\?\(\[]+\.(?:jpg|jpeg|gif|png))\[/thumb\]#',
	'#\[(t)=([^\s"\';\?\(\[]+\.(?:jpg|jpeg|gif|png))\]([^\s"\';\?\(\[]+\.(?:jpg|jpeg|gif|png))\[/t\]#',
	'#\[(url)=([^\s"\';\(\[]+)\](.+?)\[/url\]#',
	'#\[(url)\]([^\s"\'\(\[]+)\[/url\]#',
	'#\[(email)=([._\w\d\-]+@[\w\d\-]+\.[a-z\.]+)\](.+?)\[/email\]#',
	'#\[(email)\]([._\w\d\-]+@[\w\d\-]+\.[a-z\.]+)\[/email\]#'
	);
	
	foreach($bbcodes as $bbcode)
	$text = preg_replace_callback($bbcode, 'sed_bbcode_repl', $text);

	if ($cfg['parser_vid'])
	{
		$bbcodes = array(
		'#\[youtube=([^\s"\';&\?\(\[]+)\]#' => '<object width="425" height="350">
<param name="movie" value="http://www.youtube.com/v/$1"></param>
<embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>',
		'#\[googlevideo=([^\s"\';&\?\(\[]+)\]#' => '<embed style="width:425px; height:326px;" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=$1&hl=en-GB"> </embed>',
		'#\[metacafe=([^\s"\';&\?\(\[]+)\]#' => '<embed style="width:425px; height:345px;" src="http://www.metacafe.com/fplayer/$1" width="400" height="345" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>',
		'#\[flash\]([^\s"\';&\?\(\[]+\.swf)\[/flash\]#' => '<embed src="$1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>',
		'#\[flash w=(\d+) h=(\d+)\]([^\s"\';&\?\(\[]+\.swf)\[/flash\]#' => '<embed src="$3" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="$1" height="$2"></embed>',
		'#\[divx\]([^\s"\';&\?\(\[]+\.divx)\[/divx\]#' => '<embed type="video/divx" src="$1" pluginspage="http://go.divx.com/plugin/download/" showpostplaybackad="false" custommode="Stage6"  object width="450" height="400"></embed>',
		'#\[divx w=(\d+) h=(\d+)\]([^\s"\';&\?\(\[]+\.divx)\[/divx\]#' => '<embed type="video/divx" src="$3" pluginspage="http://go.divx.com/plugin/download/" showpostplaybackad="false" custommode="Stage6"  object width="$1" height="$2"></embed>'
		);

		foreach($bbcodes as $bbcode => $bbcodehtml)
		$text = preg_replace($bbcode,$bbcodehtml,$text);
	}

	$bbcodes = array(
	'#\[colleft\](.+?)\[/colleft\]#s' => '<div class="colleft">$1</div>',
	'#\[colright\](.+?)\[/colright\]#s' => '<div class="colright">$1</div>',
	'#\[center\](.+?)\[/center\]#s' => '<div style="text-align:center;">$1</div>',
	'#\[right\](.+?)\[/right\]#s' => '<div style="text-align:right;">$1</div>',
	'#\[left\](.+?)\[/left\]#s' => '<div style="text-align:left;">$1</div>',
	'#\[c1\:([\d%]+)\](.*?)\[c2\:([\d%]+)\](.*?)\[c3\]#s' => '<table style="margin:0; vertical-align:top; width:100%;"><tr><td style="padding:8px; vertical-align:top; width:$1%;">$2</td><td  style="padding:8px; vertical-align:top; width:$3%;">$4</td></tr></table>'
	);

	foreach($bbcodes as $bbcode => $bbcodehtml)
	$text = preg_replace($bbcode,$bbcodehtml,$text);

	return(substr($text,1));
}

// Replace callback for images and urls (custom XSS protection)
function sed_bbcode_repl($mt)
{
	if($mt[1] == 'img')
		return count($mt) == 3 ? '<img src="'.str_replace('&#', '', $mt[2]).'" alt="" />'
			: '<a href="'.str_replace('&#', '', $mt[2]).'"><img src="'.str_replace('&#', '', $mt[3]).'" alt="" /></a>';
	elseif($mt[1] == 'thumb')
		return '<a href="pfs.php?m=view&v='.str_replace('&#', '', $mt[3]).'"><img src="'.str_replace('&#', '', $mt[2]).'" alt="" /></a>';
	elseif($mt[1] == 't')
		return '<a href="'.str_replace('&#', '', $mt[3]).'"><img src="'.str_replace('&#', '', $mt[2]).'" alt="" /></a>';
	elseif($mt[1] == 'url')
		return count($mt) == 3 ? '<a href="'.str_replace('&#', '', $mt[2]).'">'.$mt[2].'</a>'
			: '<a href="'.str_replace('&#', '', $mt[2]).'">'.$mt[3].'</a>';
	elseif($mt[1] == 'email')
		return count($mt) == 3 ? sed_crypt_mail('<a href="mailto:'.$mt[2].'">'.$mt[2].'</a>')
			: sed_crypt_mail('<a href="mailto:'.$mt[2].'">'.$mt[3].'</a>');
	return '';
}

// Protects your mailto links from spamming bots.
// The algorithm is taken from PEAR::HTML_Crypt class.
function sed_crypt_mail($text)
{
	static $calls = 0;
	$enc_string = '';
	$length = strlen($text);
	for ($i=0; $i < $length; $i++) {
		$current_chr = substr($text, $i, 1);
		$inter = ord($current_chr)+3;
		$enc_char =  chr($inter);
		$enc_string .= ($enc_char == '\\' ? '\\\\' : $enc_char);
	}
	// get a random string to use as a function name
	srand((float) microtime() * 10000000);
	$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
	$rnd = $letters[array_rand($letters)] . md5(time());
	// the actual js (in one line to confuse)
	if($calls++ == 0) $var = 'var a,s,n;';
	$script = "<script language=\"JavaScript\" type=\"text/javascript\">{$var}function $rnd(s){r='';for(i=0;i<s.length;i++){n=s.charCodeAt(i);if(n>=8364){n=128;}r+=String.fromCharCode(n-3);}return r;}a='".$enc_string."';document.write ($rnd(a));</script>";
	return $script;

}

/* ------------------ */



function sed_bbcode_autourls($text)

{

	$text = ' '.$text;

	$text = preg_replace('#([\n ])(\w+?)://([^\t \n\r]+)#', '$1[url]$2://$3[/url]', $text);

	$text = preg_replace('#([\n ])([\w\-_\.]+?@[\w\-]+\.[^,\t \n\r]+)#', '$1[email]$2[/email]', $text);
	$text = preg_replace('#(\s)(www\.[\w\-\.]+\.\w{2,4}[^\s\r\n]*)#', '$1[url]http://$2[/url]', $text);

	return(substr($text,1));

}



/* ------------------ */



function sed_bbcode_urls($text)

{

	global $cfg;

	$bbcodes = array(

	'#\[img\]([^\'\;\?\(\[]+)\.(jpg|jpeg|gif|png)\[/img\]#' => '$1.$2',

	'#\[thumb=([^\'\;\?\(\[]+)\.(jpg|jpeg|gif|png)\]([^\[]+)\.(jpg|jpeg|gif|png)\[/thumb\]#' => '$1.$2',

	'#\[pfs\]([^\[]+)\[/pfs\]#' => $cfg['pfs_dir'].'$1',

	);



	foreach($bbcodes as $bbcode => $bbcodehtml)

	$text = preg_replace($bbcode,$bbcodehtml,$text);



	return($text);

}