All pastes #835131 Raw Edit

Unnamed

public text v1 · immutable
#835131 ·published 2007-12-28 18:33 UTC
rendered paste body
<?php
/*********************************************
  CPG Dragonflyâ„¢ CMS
  ********************************************
  Copyright © 2004 - 2005 by CPG-Nuke Dev Team
  http://www.dragonflycms.com

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source: /cvs/html/includes/nbbcode.php,v $
  $Revision: 9.18 $
  $Author: djmaze $
  $Date: 2005/06/22 01:34:13 $
**********************************************/
if (!defined('CPG_NUKE')) { exit; }

global $smilies_path, $bbbttns_path, $BASEHREF, $CPG_SESS, $bb_codes, $smilies_close;

$smilies_path = file_exists("themes/$CPG_SESS[theme]/images/smiles/icon_smile.gif") ? "themes/$CPG_SESS[theme]/images/smiles/" : 'images/smiles/';
$bbbttns_path = file_exists("themes/$CPG_SESS[theme]/images/bbcode/b.gif") ? "themes/$CPG_SESS[theme]/images/bbcode/" : 'images/bbcode/';

get_lang('bbcode');

$bb_codes['quote'] = '<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center"><tr>
	<td><span class="genmed"><b>Quote:</b></span></td>
</tr><tr>
	<td class="quote">';
$bb_codes['quote_name'] = '<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center"><tr>
	<td><span class="genmed"><b>\\1 wrote:</b></span></td>
</tr><tr>
	<td class="quote">';
$bb_codes['quote_close'] = '</td></tr></table>';
$bb_codes['code_start'] = '<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center"><tr>
		<td><span class="genmed"><b>Code:</b></span></td>
</tr><tr>
	<td class="code"><code>';
$bb_codes['code_end'] =  '</code></td></tr></table>';
$bb_codes['php_start'] = '<table border="0" align="center" width="90%" cellpadding="3" cellspacing="1"><tr>
	<td><span class="genmed"><b>PHP:</b></span></td>
</tr><tr>
	<td class="code">';
$bb_codes['php_end'] = '</td></tr></table>';
$bb_codes['win_start'] = '<html>
<head>
  <title>Smiley Selection</title>
  <base href="'.$BASEHREF.'" />
  <link rel="stylesheet" href="themes/'.$CPG_SESS['theme'].'/style/style.css" type="text/css" />
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
function emoticon(form, field, text) {
	text = \' \' + text + \' \';
	if (opener.document.forms[form].elements[field].createTextRange && opener.document.forms[form].elements[field].caretPos) {
		var caretPos = opener.document.forms[form].elements[field].caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == \' \' ? text + \' \' : text;
		opener.document.forms[form].elements[field].focus();
	} else {
		opener.document.forms[form].elements[field].value += text;
		opener.document.forms[form].elements[field].focus();
	}
}
//-->
</script>';
$bb_codes['win_end'] = '<br />
<div align="center"><a href="javascript:window.close();" class="genmed">'.$smilies_close.'</a></div>
</body></html>';
if (file_exists('themes/'.$CPG_SESS['theme'].'/bbcode.inc')) {
	include('themes/'.$CPG_SESS['theme'].'/bbcode.inc');
}

function get_code_lang($var, $array) {
	return (isset($array[$var])) ? $array[$var] : $var;
}

function smilies_table($mode, $field='message', $form='post')
{
	global $bb_codes, $db, $prefix, $smilies_path, $MAIN_CFG, $CPG_SESS;
	global $smilies_more, $smilies_desc;
	$url = $MAIN_CFG['server']['path'].getlink("smilies&amp;field=$field&amp;form=$form");

	$inline_cols = 4;
	$inline_rows = 5;
	$window_cols = 8;

	$content = '';
	if ($mode == 'window') {
		$content = $bb_codes['win_start'];
	} else if (!defined('BBCODE_JS_ACTIVE')) {
		$content .= '<script language="JavaScript" src="includes/javascript/bbcode.js" type="text/javascript"></script>';
		define('BBCODE_JS_ACTIVE', 1);
	}
	if ($mode == 'onerow') {
		$content .= '
<table width="450" border="0" cellspacing="0" cellpadding="0" class="forumline">';
	} else {
		$content .= '
<table width="100" border="0" cellspacing="0" cellpadding="5" class="forumline">';
	}
	if ($result = $db->sql_query('SELECT emoticon, code, smile_url FROM '.$prefix.'_bbsmilies ORDER BY smilies_id', false, __FILE__, __LINE__)) {
		$num_smilies = 0;
		$rowset = array();
		while ($row = $db->sql_fetchrow($result)) {
			if (empty($rowset[$row['smile_url']])) {
				$rowset[$row['smile_url']]['code'] = str_replace("'", "\\'", str_replace('\\', '\\\\', $row['code']));
// process the smiley description'
				$rowset[$row['smile_url']]['emoticon'] = get_code_lang($row['emoticon'], $smilies_desc);
				$num_smilies++;
			}
		}

		if ($num_smilies) {
			$smilies_count = ($mode == 'inline') ? min(19, $num_smilies) : $num_smilies;
			$smilies_split_row = ($mode == 'inline') ? $inline_cols - 1 : $window_cols - 1;

			$s_colspan = $row = $col = 0;

			while (list($smile_url, $data) = each($rowset)) {
				if (!$col) {
					$content .= '<tr align="center" valign="middle">';
				}
				$content .= "<td><a href=\"javascript:emoticon('".$form."', '".$field."', '".$data['code']."')\"><img src=\"" . $smilies_path . $smile_url . "\" border=\"0\" alt=\"".$data['emoticon']."\" title=\"".$data['emoticon']."\" /></a></td>";
				$s_colspan = max($s_colspan, $col + 1);

				if ($mode == 'onerow') {
					if ($col >= 15) {
						if ($num_smilies > 15) {
							$content .= "<td colspan=\"$s_colspan\" class=\"nav\"><a href=\"$url\" onclick=\"window.open('$url', '_smilies', 'HEIGHT=200,resizable=yes,scrollbars=yes,WIDTH=230');return false;\" target=\"_smilies\" class=\"nav\">$smilies_more</a></td>";
						}
						break;
					}
					$col++;
				}
				else if ($col == $smilies_split_row) {
					$content .= '</tr>';
					$col = 0;
					if ($mode == 'inline' && $row == $inline_rows - 1) {
						break;
					}
					$row++;
				}
				else { $col++; }
			}
			if ($col > 0) { $content .= '</tr>'; }

			if ($mode == 'inline' && $num_smilies > $inline_rows * $inline_cols) {
				$content .= "<tr align=\"center\">
					<td colspan=\"$s_colspan\" class=\"nav\"><a href=\"$url\" onclick=\"window.open('$url', '_smilies', 'HEIGHT=200,resizable=yes,scrollbars=yes,WIDTH=230');return false;\" target=\"_smilies\" class=\"nav\">$smilies_more</a></td>
				</tr>";
			}
		}
	}
	$content .= "\n</table>\n";
	if ($mode == 'window') { $content .= $bb_codes['win_end']; }
	return $content;
}

function bbcode_table($field='message', $form='post', $allowed=0)
{
	global $bbbttns_path, $color_desc, $font_desc, $textcolor1, $flag_desc, $flag_code;
	$content = '';
	if (!defined('BBCODE_JS_ACTIVE')) {
		$content .= '<script language="JavaScript" src="includes/javascript/bbcode.js" type="text/javascript"></script>';
		define('BBCODE_JS_ACTIVE', 1);
	}
	$content .= '<table cellpadding="0" cellspacing="0" border="0">
<tr>
	<td>
		<img alt="bold" title="[b]bold[/b]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'b\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="b" src="'.$bbbttns_path.'b.gif" border="0" />
		<img alt="italic" title="[i]italic[/i]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'i\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="i" src="'.$bbbttns_path.'i.gif" border="0" />
		<img alt="underline" title="[u]underline[/u]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'u\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="u" src="'.$bbbttns_path.'u.gif" border="0" />
&nbsp;&nbsp;
		<img alt="Left to Right" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'ltr\')" onclick="BBCdir(\''.$form.'\',\''.$field.'\',\'ltr\')" name="dirltr" src="'.$bbbttns_path.'ltr.gif" border="0" />
		<img alt="Right to Left" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'rtl\')" onclick="BBCdir(\''.$form.'\',\''.$field.'\',\'rtl\')" name="dirrtl" src="'.$bbbttns_path.'rtl.gif" border="0" />
&nbsp;&nbsp;
		<img alt="URL" title="[url]...[/url]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'url\')" onclick="BBCurl(\''.$form.'\',\''.$field.'\')" name="url" src="'.$bbbttns_path.'url.gif" border="0" />
		<img alt="Email" title="[email]...[/email]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'mail\')" onclick="BBCwmi(\''.$form.'\',\''.$field.'\',\'email\')" name="email" src="'.$bbbttns_path.'email.gif" border="0" />';
	if ($allowed) {
		$content .= '
&nbsp;&nbsp;
		<img alt="justify" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'justify\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'align\',this)" name="justify" src="'.$bbbttns_path.'align_justify.gif" border="0" />
		<img alt="left" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'left\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'align\',this)" name="left" src="'.$bbbttns_path.'align_left.gif" border="0" />
		<img alt="center" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'center\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'align\',this)" name="center" src="'.$bbbttns_path.'align_center.gif" border="0" />
		<img alt="right" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'right\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'align\',this)" name="right" src="'.$bbbttns_path.'align_right.gif" border="0" />';
	}
	$content .= '
&nbsp;&nbsp;
		<select onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'fc\')" onchange="BBCfc(\''.$form.'\',\''.$field.'\',this)" title="'.get_code_lang('color', $color_desc).'">
		<option class="genmed" value="'.$textcolor1.'" style="color: black; background-color: rgb(250, 250, 250);">'.get_code_lang('Default', $color_desc).'</option>
		<option class="genmed" value="maroon" style="color: maroon; background-color: rgb(250, 250, 250);">'.get_code_lang('Dark Red', $color_desc).'</option>
		<option class="genmed" value="red" style="color: red; background-color: rgb(250, 250, 250);">'.get_code_lang('Red', $color_desc).'</option>
		<option class="genmed" value="orange" style="color: orange; background-color: rgb(250, 250, 250);">'.get_code_lang('Orange', $color_desc).'</option>
		<option class="genmed" value="brown" style="color: brown; background-color: rgb(250, 250, 250);">'.get_code_lang('Brown', $color_desc).'</option>
		<option class="genmed" value="yellow" style="color: yellow; background-color: rgb(250, 250, 250);">'.get_code_lang('Yellow', $color_desc).'</option>
		<option class="genmed" value="green" style="color: green; background-color: rgb(250, 250, 250);">'.get_code_lang('Green', $color_desc).'</option>
		<option class="genmed" value="olive" style="color: olive; background-color: rgb(250, 250, 250);">'.get_code_lang('Olive', $color_desc).'</option>
		<option class="genmed" value="cyan" style="color: cyan; background-color: rgb(250, 250, 250);">'.get_code_lang('Cyan', $color_desc).'</option>
		<option class="genmed" value="blue" style="color: blue; background-color: rgb(250, 250, 250);">'.get_code_lang('Blue', $color_desc).'</option>
		<option class="genmed" value="darkblue" style="color: darkblue; background-color: rgb(250, 250, 250);">'.get_code_lang('Dark Blue', $color_desc).'</option>
		<option class="genmed" value="indigo" style="color: indigo; background-color: rgb(250, 250, 250);">'.get_code_lang('Indigo', $color_desc).'</option>
		<option class="genmed" value="violet" style="color: violet; background-color: rgb(250, 250, 250);">'.get_code_lang('Violet', $color_desc).'</option>
		<option class="genmed" value="white" style="color: white; background-color: rgb(250, 250, 250);">'.get_code_lang('White', $color_desc).'</option>
		<option class="genmed" value="black" style="color: black; background-color: rgb(250, 250, 250);">'.get_code_lang('Black', $color_desc).'</option>
		</select>';
	if ($allowed) {
		$content .= '
	</td>
</tr><tr>
	<td>
		<img alt="Ordered List" title="[ol]ordered list[/ol]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'ol\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="ol" src="'.$bbbttns_path.'ol.gif" border="0" />
		<img alt="Unordered List" title="[ul]unordered list[/ul]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'ul\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="ul" src="'.$bbbttns_path.'ul.gif" border="0" />
		<img alt="List" title="[li]list[/li]" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'li\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="li" src="'.$bbbttns_path.'li.gif" border="0" />
&nbsp;&nbsp;
		<img alt="Quote" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'quote\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="quote" src="'.$bbbttns_path.'quote.gif" border="0" />
		<img alt="Code" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'code\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="code" src="'.$bbbttns_path.'code.gif" border="0" />
		<img alt="PHP" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'php\')" onclick="BBCcode(\''.$form.'\',\''.$field.'\',this)" name="php" src="'.$bbbttns_path.'php.gif" border="0" />
&nbsp;&nbsp;
		<img alt="H-Line" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'hr\')" onclick="BBChr(\''.$form.'\',\''.$field.'\')" name="hr" src="'.$bbbttns_path.'hr.gif" border="0" />
&nbsp;&nbsp;
		<img alt="Marque to down" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'marqd\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'marq\',this)" name="down" src="'.$bbbttns_path.'marq_down.gif" border="0" />
		<img alt="Marque to up" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'marqu\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'marq\',this)" name="up" src="'.$bbbttns_path.'marq_up.gif" border="0" />
		<img alt="Marque to left" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'marql\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'marq\',this)" name="left" src="'.$bbbttns_path.'marq_left.gif" border="0" />
		<img alt="Marque to right" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'marqr\')" onclick="BBCode(\''.$form.'\',\''.$field.'\',\'marq\',this)" name="right" src="'.$bbbttns_path.'marq_right.gif" border="0" />
&nbsp;&nbsp;
		<select onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'fs\')" onchange="BBCfs(\''.$form.'\',\''.$field.'\',this)" title="'.get_code_lang('size', $font_desc).'">
		<option value="7" class="genmed">'.get_code_lang('Tiny', $font_desc).'</option>
		<option value="9" class="genmed">'.get_code_lang('Small', $font_desc).'</option>
		<option value="12" class="genmed" selected="selected">'.get_code_lang('Normal', $font_desc).'</option>
		<option value="18" class="genmed">'.get_code_lang('Large', $font_desc).'</option>
		<option  value="24" class="genmed">'.get_code_lang('Huge', $font_desc).'</option>
		</select>';
	}
	if ($allowed) {
		$content .= '
	<tr>
	<td>
		<img alt="Image" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'img\')" onclick="BBCwmi(\''.$form.'\',\''.$field.'\',\'img\')" name="img" src="'.$bbbttns_path.'img.gif" border="0" />
		<img alt="Flash" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'flash\')" onclick="BBCflash(\''.$form.'\',\''.$field.'\',\'flash\')" name="flash" src="'.$bbbttns_path.'flash.gif" border="0" />
		<img alt="Video" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'video\')" onclick="BBCmm(\''.$form.'\',\''.$field.'\',\'video\')" name="video" src="'.$bbbttns_path.'video.gif" border="0" />
		&nbsp;&nbsp;
		<img alt="MP3" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'mp3\')" onclick="BBCmp3(\''.$form.'\',\''.$field.'\',\'mp3\')" name="mp3" src="'.$bbbttns_path.'mp3.gif" border="0" />
		<img alt="Quicktime" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'mov\')" onclick="BBCmm(\''.$form.'\',\''.$field.'\',\'mov\')" name="mov" src="'.$bbbttns_path.'mov.gif" border="0" />
		<img alt="MediaPlayer" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'mplayer\')" onclick="BBCmpl(\''.$form.'\',\''.$field.'\',\'mplayer\')" name="mplayer" src="'.$bbbttns_path.'player.gif" border="0" />
    	<img alt="Playlist" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'playlist\')" onclick="BBCmpt(\''.$form.'\',\''.$field.'\',\'playlist\')" name="playlist" src="'.$bbbttns_path.'playlist.gif" border="0" />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    	;
	;
	}
	
	// This section is for additional Media BBCode Buttons
	if ($allowed) {
		$content .= '
	<tr>
	<td>

<img alt="Apple Trailer" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'trailer\')" onclick="BBCapple(\''.$form.'\',\''.$field.'\',\'trailer\')" name="apple" src="'.$bbbttns_path.'apple.gif" border="0" />

<img alt="YouTube" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'youtube\')" onclick="BBCwmi(\''.$form.'\',\''.$field.'\',\'youtube\')" name="youtube" src="'.$bbbttns_path.'youtube.gif" border="0" />

<img alt="MySpace" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'myspace\')" onclick="BBCwmi(\''.$form.'\',\''.$field.'\',\'myspace\')" name="myspace" src="'.$bbbttns_path.'myspace.gif" border="0" />
	
<img alt="iFilm" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'ifilm\')" onclick="BBCwmi(\''.$form.'\',\''.$field.'\',\'ifilm\')" name="ifilm" src="'.$bbbttns_path.'ifilm.gif" border="0" />

<img alt="Divx Video" class="bbcbutton" onmouseover="helpline(\''.$form.'\',\''.$field.'\',\'divx\')" onclick="BBCmm(\''.$form.'\',\''.$field.'\',\'divx\')" name="divx" src="'.$bbbttns_path.'divx.gif" border="0" />
		
	</td>
	</tr>';
	}
	// This ends the section for the additional Media BBCode Buttons
	
	$content .= '
	<tr>
	<td>
		<input type="text" name="help'.$field.'" size="66" maxlength="100" value="Tip: Styles can be applied quickly to selected text" class="helpline" />
	</td>
  </tr>
</table>';

	return $content;
}

// smilies_pass(
function set_smilies($message, $url='') {
	static $orig, $repl;
	if (!isset($orig)) {
		global $db, $prefix, $smilies_path, $smilies_desc;
		$orig = $repl = array();
		$smilies = $db->sql_ufetchrowset('SELECT * FROM '.$prefix.'_bbsmilies', SQL_BOTH, __FILE__, __LINE__);
		if (count($smilies)) {
			usort($smilies, 'sort_smiley');
		}
		if ($url != '') {
			if (substr($url, -1) != '/') { $url .= '/'; }
		}
		for ($i = 0; $i < count($smilies); $i++) {
			$smilies[$i]['code'] = str_replace('/', '\/', preg_quote($smilies[$i]['code']));
			$orig[] = "/(?<=.\W|\W.|^\W)" . $smilies[$i]['code'] . "(?=.\W|\W.|\W$)/s";
			$repl[] = '<img src="' . $url . $smilies_path . $smilies[$i]['smile_url'] . '" alt="' . get_code_lang($smilies[$i]['emoticon'], $smilies_desc) . '" title="' . get_code_lang($smilies[$i]['emoticon'], $smilies_desc) . '" border="0" />';
		}
	}
	if (count($orig)) {
		$message = preg_replace($orig, $repl, " $message ");
		$message = substr($message, 1, -1);
	}
	return $message;
}

function sort_smiley($a, $b)
{
	if (strlen($a['code']) == strlen($b['code'])) { return 0; }
	return (strlen($a['code']) > strlen($b['code'])) ? -1 : 1;
}

// bbencode_first_pass() prepare bbcode for db insert
function encode_bbcode($text)
{
	return BBCode::encode($text);
}
function decode_bb_all($text, $allowed=0, $allow_html=false, $url='') {
	return set_smilies(decode_bbcode($text, $allowed, $allow_html), $url);
}
function decode_bbcode($text, $allowed=0, $allow_html=false, $user_sig_bbcode_uid='')
{
	return BBCode::decode($text, $allowed, $allow_html, $user_sig_bbcode_uid);
}

function shrink_url($url) {
	$url = preg_replace("#(^[\w]+?://)#", '', $url);
	return (strlen($url) > 35) ? substr($url,0,22).'...'.substr($url,-10) : $url;
}

function make_clickable($text)
{
	$ret = ' ' . $text;
	$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" rel=\"nofollow\" title=\"\\2\" target=\"_blank\">'.shrink_url('\\2').'</a>'", $ret);
	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" rel=\"nofollow\" target=\"_blank\" title=\"\\2\">'.shrink_url('\\2').'</a>'", $ret);
	$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1 \\2 &#64; \\3", $ret);
	$ret = substr($ret, 1);
	return($ret);
}

// prepare_message(
function message_prepare($message, $html_on, $bbcode_on)
{
	global $board_config;
	//
	// Clean up the message
	//
	$message = trim($message);
	if ($html_on) {
		$allowed_html_tags = split(',', $board_config['allow_html_tags']);
		$end_html = 0;
		$start_html = 1;
		$tmp_message = '';
		$message = ' ' . $message . ' ';
		while ($start_html = strpos($message, '<', $start_html)) {
			$tmp_message .= BBCode::encode_html(substr($message, $end_html + 1, ($start_html - $end_html - 1)));
			if ($end_html = strpos($message, '>', $start_html)) {
				$length = $end_html - $start_html + 1;
				$hold_string = substr($message, $start_html, $length);
				if (($unclosed_open = strrpos(' ' . $hold_string, '<')) != 1) {
					$tmp_message .= BBCode::encode_html(substr($hold_string, 0, $unclosed_open - 1));
					$hold_string = substr($hold_string, $unclosed_open - 1);
				}
				$tagallowed = false;
				for ($i = 0; $i < sizeof($allowed_html_tags); $i++) {
					$match_tag = trim($allowed_html_tags[$i]);
					if (preg_match('#^<\/?' . $match_tag . '[> ]#i', $hold_string)) {
						$tagallowed = (preg_match('#^<\/?' . $match_tag . ' .*?(style[ ]*?=|on[\w]+[ ]*?=)#i', $hold_string)) ? false : true;
					}
				}
				$tmp_message .= ($length && !$tagallowed) ? BBCode::encode_html($hold_string) : $hold_string;
				$start_html += $length;
			} else {
				$tmp_message .= BBCode::encode_html(substr($message, $start_html));
				$start_html = strlen($message);
				$end_html = $start_html;
			}
		}
		if ($end_html != strlen($message) && $tmp_message != '') {
			$tmp_message .= BBCode::encode_html(substr($message, $end_html + 1));
		}
		$message = ($tmp_message != '') ? trim($tmp_message) : trim($message);
	} else {
		$message = BBCode::encode_html($message);
	}
	if ($bbcode_on) {
		$message = BBCode::encode($message);
	}
	return $message;
}

class BBCode {

	function encode_html($text) {
		return (ereg('<', $text)) ? htmlprepare($text, false, ENT_NOQUOTES) : $text;
	}

	function encode($text)
	{
		// Split all bbcodes.
		$text_parts = BBCode::split_bbcodes($text);
		// Merge all bbcodes and do special actions depending on the type of code.
		$text = '';
		while ($part = array_shift($text_parts)) {
			if (isset($part['code'])) {
				if ($part['code'] == 'list' && $part['text'][5] == '=') {
					// [list=x] for ordered lists.
					$part['text'] = substr($part['text'], 0, -1).':o]';
				}
				if ($part['code'] != 'code' && $part['code'] != 'php' && $part['subc']) {
					$part['text'] = '['.encode_bbcode(substr($part['text'], 1, -1)).']';
				}
			}
			$text .= $part['text'];
		}
		return trim($text);
	}

	function decode($text, $allowed=0, $allow_html=false, $user_sig_bbcode_uid='')
	{
		global $bb_codes;
		// First: If there isn't a "[" and a "]" in the message, don't bother.
		if (!(strpos($text, '[') !== false && strpos($text, ']'))) {
			return ($allow_html ? (ereg('<', $text) ? $text : nl2br($text)) : nl2br(strip_tags($text)));
		}

		if ($user_sig_bbcode_uid != '') $text = preg_replace("/:(([a-z0-9]+:)?)$user_sig_bbcode_uid(=|\])/si", '\\3', $text);

		// pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
		$text = BBCode::split_on_bbcodes($text, $allowed, $allow_html, $user_sig_bbcode_uid);

		// Patterns and replacements for URL, email tags etc.
		$patterns = $replacements = array();
$text = str_replace('/watch?v=', '/v/', $text);
$text = str_replace('/videoplay?docid=', '/googleplayer.swf?docId=', $text);
		// [list] and [list=x] for (un)ordered lists.
		// unordered lists
		$text = str_replace('[list]', '<ul>', $text);
		$text = str_replace('[/list:u]', '</ul>', $text);
		$text = str_replace('[/list]', '</ul>', $text);
//		$patterns[] = '#\[list\](.*?)\[/list:u\]#si';
//		$replacements[] = '<ul>\\1</ul>';
		// Ordered lists
		$text = preg_replace('/\[list=([ai1])\]/si', '<ol type="\\1">', $text);
		$text = str_replace('[/list:o]', '</ol>', $text);
//		$patterns[] = '#\[list=([ai1])\](.*?)\[/list:o\]#si';
//		$replacements[] = '<ol type="\\1">\\2</ol>';
		// li tags
		$text = str_replace('[*]', '<li>', $text);

		// colours
		$patterns[] = '#\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*?)\[/color\]#si';
		$replacements[] = '<span style="color: \\1">\\2</span>';

		// size
		$patterns[] = '#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si';
		$replacements[] = '<span style="font-size: \\1px; line-height: normal">\\2</span>';

		// [b] and [/b] for bolding text.
		$patterns[] = '#\[b\](.*?)\[/b\]#si';
		$replacements[] = '<span style="font-weight: bold">\\1</span>';

		// [u] and [/u] for underlining text.
		$patterns[] = '#\[u\](.*?)\[/u\]#si';
		$replacements[] = '<span style="text-decoration: underline">\\1</span>';

		// [i] and [/i] for italicizing text.
		$patterns[] = '#\[i\](.*?)\[/i\]#si';
		$replacements[] = '<span style="font-style: italic">\\1</span>';

		// align
		$patterns[] = '#\[align=(left|right|center|justify)\](.*?)\[/align\]#si';
		$replacements[] = '<div style="text-align:\\1">\\2</div>';

		// [url]xxxx://www.cpgnuke.com[/url]
		$patterns[] = "#\[url\]([\w]+?://[^ \"\n\r\t<]*?)\[/url\]#ise";
		$replacements[] = "'<a href=\"\\1\" target=\"_blank\" title=\"\\1\" class=\"postlink\" rel=\"nofollow\">'.shrink_url('\\1').'</a>'";
		// [url]www.cpgnuke.com[/url] (no xxxx:// prefix).
		$patterns[] = "#\[url\]((www|ftp)\.[^ \"\n\r\t<]*?)\[/url\]#ise";
		$replacements[] = "'<a href=\"http://\\1\" target=\"_blank\" title=\"\\1\" class=\"postlink\" rel=\"nofollow\">'.shrink_url('\\1').'</a>'";
		// [url=www.cpgnuke.com]cpgnuke[/url] (no xxxx:// prefix).
		$patterns[] = "#\[url=((www|ftp)\.[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is";
		$replacements[] = "<a href=\"http://\\1\" target=\"_blank\" title=\"\\1\" class=\"postlink\" rel=\"nofollow\">\\3</a>";
		// [url=xxxx://www.cpgnuke.com]cpgnuke[/url]
		// [url=index.php]cpgnuke[/url] (local).
		$patterns[] = "#\[url=([\w]+(://|\.|/)[^ (\"\n\r\t<]*?)\](.*?)\[/url\]#is";
		$replacements[] = "<a href=\"\\1\" target=\"_blank\" title=\"\\1\" class=\"postlink\" rel=\"nofollow\">\\3</a>";

		// [email]user@domain.tld[/email] code..
		$patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
		$replacements[] = "<a href=\"mailto:\\1\">\\1</a>";

		if ($allowed) {
			// [hr]
			$patterns[] = "#\[hr\]#si";
			$replacements[] = '<hr />';

			// marquee
			$patterns[] = "#\[marq=(left|right|up|down)\](.*?)\[/marq\]#si";
			$replacements[] = '<marquee direction="\\1" scrolldelay="60" scrollamount="1" onmouseover="this.stop()" onmouseout="this.start()">\\2</marquee>';
			
			// [ol] and [/ol] for ordered list.
		$patterns[] = '#\[ol\](.*?)\[/ol\]#si';
		$replacements[] = '<ol>\\1</ol>';
		
			// [ul] and [/ul] for unordered list.
		$patterns[] = '#\[ul\](.*?)\[/ul\]#si';
		$replacements[] = '<ul>\\1</ul>';
			
			// [li] and [/li] for bolding text.
		$patterns[] = '#\[li\](.*?)\[/li\]#si';
		$replacements[] = '<li>\\1</li>';

			// [img]image_url_here[/img] code..
			$patterns[] = "#\[img\]([\w]+(://|\.|/)[^ (\"\n\r\t<]*?)\[/img\]#si";
			$replacements[] = "<img src=\"\\1\" border=\"0\" alt=\"\" />";

			// [flash width= height= bgcolor= ] and [/flash] code..
			$patterns[] = "#\[flash width=([0-9]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9]) bgcolor=([^ \"\n\r\t<]*?)\]((ht|f)tp://)([^ \?&=\"\n\r\t<]*?(\.(swf|fla)))\[/flash\]#si";
			$replacements[] = '<center><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="\\1" height="\\2">
			<param name="movie" value="\\4\\6">
			<param name="quality" value="high">
			<param name="scale" value="noborder">
			<param name="bgcolor" value="#\\3">
			<embed src="\\4\\6" quality="high" scale="noborder" bgcolor="#\\3" width="\\1" height="\\2" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
			</embed></object></center>';

			// [video width= height= loop= ] and [/video] code..
			$patterns[] = "#\[video width=([0-6]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9])\]([\w]+?://[^ \?&=\"\n\r\t<]*?(\.(avi|mpg|mpeg|wmv)))\[/video\]#si";
			$replacements[] = '<center><object id="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
			standby="Loading Microsoft Windows® Media Player components..." type="application/x-oleobject">
			<param name="fileName" value="\\3">
			<param name="animationatStart" value="true">
			<param name="transparentatStart" value="true">
			<param name="autoStart" value="false">
			<param name="showControls" value="true">
			<embed type="application/x-mplayer2" src="\\3" width=\\1 height=\\2 autostart="0" showstatusbar="1" allowScriptAccess="never"></embed></object></center>';
			
			// [mp3] and [/mp3] code..
			$patterns[] = "#\[mp3 width=([0-6]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9])\]([\w]+?://[^ \?&=\"\n\r\t<]*?(\.(mp3)))\[/mp3\]#si";
			$replacements[] = '<center><embed allowScriptAccess="never" allowNetworking="internal" enableJavaScript="false" src="player/mediaplayer.swf" width="\\1" height="\\2" bgcolor="#FFFFFF" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="file=\\3&showdigits=true&repeat=false&lightcolor=0xCC9900&backcolor=0x000000&frontcolor=0xCCCCCC&bufferlength=10&image=\\6&overstretch=true" /></embed></center>';

			// [mov width= height= ] and [/mov] code..
			$patterns[] = "#\[mov width=([0-6]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9])\]([\w]+?://[^ \?&=\"\n\r\t<]*?(\.(mov)))\[/mov\]#si";
			$replacements[] = '<center><embed src="\\3" width=\\1 height=\\2 controller="\\true" autostart="\\false" cache="\\true"
			pluginspage="\\http://www.apple.com/quicktime/download" target="\\quicktimeplayer" bgcolor="\\black" showstatusbar="\\true"></center>';
	
			// [mplayer] and [/mplayer] code..
			$patterns[] = "#\[mplayer width=([0-6]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9]) lightcolor=([^ \"\n\r\t<]*?) backcolor=([^ \"\n\r\t<]*?) frontcolor=([^ \"\n\r\t<]*?) image=([^ \"\n\r\t<]*?)\]([\w]+?://[^ \?&=\"\n\r\t<]*?(\.(flv)))\[/mplayer\]#si";
			$replacements[] = '<center><embed allowScriptAccess="never" allowNetworking="internal" enableJavaScript="false" src="player/mediaplayer.swf" width="\\1" height="\\2" bgcolor="#FFFFFF" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="file=\\7&showdigits=true&repeat=false&lightcolor=0x\\3&backcolor=0x\\4&frontcolor=0x\\5&bufferlength=10&image=\\6&overstretch=true&logo=images/mediaplayer/df-logo.png" /></embed></center>';

			// [playlist] and [/playlist] code..
			$patterns[] = "#\[playlist width=([0-6]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9]) disphght=([0-4]?[0-9]?[0-9]) lightcolor=([^ \"\n\r\t<]*?) backcolor=([^ \"\n\r\t<]*?) frontcolor=([^ \"\n\r\t<]*?)\]([^ \"\n\r\t<]*?)\[/playlist\]#si";
			$replacements[] = '<center><embed allowScriptAccess="never" allowNetworking="internal" enableJavaScript="false" src="player/mediaplayer.swf" width="\\1" height="\\2" bgcolor="#FFFFFF" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="file=player/\\7&showdigits=true&displayheight=\\3&repeat=true&shuffle=false&lightcolor=0x\\4&backcolor=0x\\5&frontcolor=0x\\6&bufferlength=10&overstretch=true&logo=images/mediaplayer/df-logo.png" /></embed></center>';
			
			// [flag] code..
			$patterns[] = "#\[flag=(.*?)\]#si";
			$replacements[] = '<img src="images/flags/\\1.gif" border="0" alt="" />';

			// This section is for additional Media BBCode Buttons
			
			// [trailer width= height= ] and [/trailer] code..
	$patterns[] = "#\[trailer width=([0-9]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9])\]([\w]+?://[^ \?&=\"\n\r\t<]*?(\.(mov)))\[/trailer\]#si";
	$replacements[] = '<center><embed src="\\3" width=\\1 height=\\2 controller="\\true" autostart="\\false" cache="\\true"
	pluginspage="\\http://www.apple.com/quicktime/download" target="\\quicktimeplayer" bgcolor="\\black" showstatusbar="\\true"></center>';
	
// [youtube] youtube video url [/youtube] code..
	$patterns[] = "#\[youtube\]([\w]+(://|\.|/)[^ (\"\n\r\t<]*?)\[/youtube\]#si";
	$replacements[] = '<center><object width="425" height="350"><param name="movie" value="\\1"></param><embed src="\\1" type="application/x-shockwave-flash" width="425" height="350"></embed></object></center>';
	
// [myspace] myspace video id [/myspace] code..
	$patterns[] = "#\[myspace\]([^ \"\n\r\t<]*?)\[/myspace\]#si";
	$replacements[] = '<center><embed allowScriptAccess="never" enableJavaScript="false" src="http://lads.myspace.com/videos/vplayer.swf" flashvars="m=\\1&type=video" type="application/x-shockwave-flash" width="430" height="346"></embed></center>';
	
// [google] google video id [/google] code..
	$patterns[] = "#\[google\]([\w]+(://|\.|/)[^ (\"\n\r\t<]*?)\[/google\]#si";
	$replacements[] = '<center><object width="400" height="326"><param name="movie" value="\\1"></param><param name="wmode" value="transparent"></param><embed src="\\1" type="application/x-shockwave-flash" wmode="transparent" width="400" height="326"></embed></object></center>';
	
// [ifilm] ifilm video id [/ifilm] code..
	$patterns[] = "#\[ifilm\]([^ \"\n\r\t<]*?)\[/ifilm\]#si";
	$replacements[] = '<center><embed width="448" height="365" src="http://www.ifilm.com/efp" quality="high" bgcolor="000000" name="efp" align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="flvbaseclip=\\1&"> </embed></center>';
	
// [metacafe] metacafe video [/metacafe] code..
	$patterns[] = "#\[metacafe\]([^ \"\n\r\t<]*?)\[/metacafe\]#si";
	$replacements[] = '<center><embed width="400" height="345" src="http://www.metacafe.com/fplayer/\\1.swf" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></center>';
	
// [divx] divx video url [/divx] code..
	$patterns[] = "#\[divx width=([0-6]?[0-9]?[0-9]) height=([0-4]?[0-9]?[0-9])\]([\w]+?://[^ \?&=\"\n\r\t<]*?(\.(avi|divx)))\[/divx\]#si";
	$replacements[] = '<center><object classid="clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616" width="\\1" height="\\2" codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab">
	<param name="autoPlay" value="false" />
	<param name="allowContextMenu" value="false" />
	<param name="src" value="\\3" />
	<embed type="video/divx" src="\\3" width="\\1" height="\\2" mode="full"  autoPlay="false"  allowContextMenu="false"  pluginspage="http://go.divx.com/plugin/download/"></embed></object></center>';
	
// [item] and [itemico] - Parse text for ItemStats mod
	if( is_active('item_stats') )
	{
	require_once(BASEDIR.'modules/item_stats/df_itemstats.php');
	$text = itemstats_parse($text);
	
	// [youtube] youtube video id [/youtube] code..
	$patterns[] = "#\[youtube\]([^ \"\n\r\t<]*?)\[/youtube\]#si";
	$replacements[] = '<center><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/\\1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/\\1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object></center>';
			
			// This ends the section for the additional Media BBCode Buttons
			}
            }

		$text = preg_replace($patterns, $replacements, $text);

		// Fix linebreaks on important items
		$text = preg_replace("/<br>/si", "<br \/>", $text);
		$text = preg_replace("/<ul><br \/>/si", "<ul>", $text);
		$text = preg_replace("/<\/ul><br \/>/si", "</ul>", $text);
		$text = preg_replace("/<\/ol><br \/>/si", "</ol>", $text);
		$text = preg_replace("/<\/table><br \/>/si", "</table>", $text);
		$text = preg_replace("/<\/div><br \/>/si", "</div>", $text);
		$text = preg_replace("/<br \/><table/si", "<table", $text);
		
		// Remove our padding from the string..
		return trim($text);
	}

	function split_bbcodes($text)
	{
		$curr_pos = 0;
		$str_len = strlen($text);
		$text_parts = array();
		while ($curr_pos < $str_len) {
			// Find bbcode start tag, if not found end the loop.
			$curr_pos = strpos($text, '[', $curr_pos);
			if ($curr_pos === false) { break; }
			$end = strpos($text, ']', $curr_pos);
			if ($end === false) { break; }

			$code_start = substr($text, $curr_pos, $end-$curr_pos+1);
			$code = strtolower(preg_replace('/\[([a-z]+).*]/i', '\\1', $code_start));
			$code_len = strlen($code);

			$end_pos = empty($code) ? false : $end;
			$depth = 0;
			$sub = false;
			while ($end_pos) {
				// Find bbcode end tag, if not found end the loop.
				$end_pos = strpos($text, '[', $end_pos);
				if ($end_pos === false) { break; }
				$end = strpos($text, ']', $end_pos);
				if ($end === false) { break; }
				$code_end = strtolower(substr($text, $end_pos, $end-$end_pos+1));
				if ($code_end == "[/$code]") {
					if ($depth > 0) {
						$depth--;
						$end_pos++;
						$sub = true;
					} else {
						if ($curr_pos > 0) {
							$text_parts[]['text'] = substr($text, 0, $curr_pos);
						}
						$text_parts[] = array(
							'text' => substr($text, $curr_pos, $end-$curr_pos+1),
							'code' => $code,
							'subc' => $sub);
						$text = substr($text, $end+1);
						$str_len = strlen($text);
						$curr_pos = 0;
						break;
					}
				} else {
					if (substr($code_end, 0, 1+$code_len) == "[$code") { $depth++; }
					$end_pos++;
				}
			}
			$curr_pos++;
		}
		if ($str_len > 0) { $text_parts[]['text'] = $text; }
		return $text_parts;
	}

	// split the bbcodes and use nl2br on everything except [php]
	function split_on_bbcodes($text, $allowed=0, $allow_html=false, $user_sig_bbcode_uid='')
	{
		global $bb_codes;
		// Split all bbcodes.
		$text_parts = BBCode::split_bbcodes($text);
		// Merge all bbcodes and do special actions depending on the type of code.
		$text = '';
		while ($part = array_shift($text_parts)) {
			if (isset($part['code'])) {
				if ($part['code'] == 'php') {
					// [PHP]
					$text .= ($allowed) ? BBCode::decode_php($part['text']) : nl2br(htmlspecialchars($part['text']));
				} elseif ($part['code'] == 'code') {
					// [CODE]
					if (!$allowed && ereg('<', $part['text'])) {
						$part['text'] = nl2br(htmlspecialchars($part['text']));
					}
					$text .= $allowed ? BBCode::decode_code($part['text']) : $part['text'];
				} elseif ($part['code'] == 'quote') {
					// [QUOTE] and [QUOTE=""]
					if ($part['text'][6] == ']') {
						$text .= $bb_codes['quote'].BBCode::split_on_bbcodes(substr($part['text'], 7, -8), $allowed, $allow_html).$bb_codes['quote_close'];
					} else {
						$part['text'] = preg_replace('/\[quote="(.*?)"\]/si', $bb_codes['quote_name'], BBCode::split_on_bbcodes(substr($part['text'], 0, -8), $allowed, $allow_html), 1);
						$text .= $part['text'].$bb_codes['quote_close'];
					}
				} elseif ($part['subc']) {
					$text .= '['.BBCode::split_on_bbcodes(substr($part['text'], 1, -1)).']';
				} elseif ($allow_html) {
					$text .= (!ereg('<', $part['text']) ? nl2br($part['text']) : $part['text']);
				} else {
					$text .= nl2br(BBCode::encode_html($part['text']));
				}
			} elseif ($allow_html) {
				$text .= (!ereg('<', $part['text']) ? nl2br($part['text']) : $part['text']);
			} else {
				$text .= nl2br(BBCode::encode_html($part['text']));
			}
		}
		return $text;
	}

	function decode_code($text)
	{
		global $bb_codes;
		$text = substr($text, 6, -7);
		$code_entities_match   = array('#<#',  '#>#',  '#"#',	'#:#',   '#\[#',  '#\]#',  '#\(#',  '#\)#',  '#\{#',   '#\}#');
		$code_entities_replace = array('&lt;', '&gt;', '&quot;', '&#58;', '&#91;', '&#93;', '&#40;', '&#41;', '&#123;', '&#125;');
		$text = preg_replace($code_entities_match, $code_entities_replace, $text);
		// Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.
		$text = str_replace('  ', '&nbsp; ', $text);
		// now Replace 2 spaces with ' &nbsp;' to catch odd #s of spaces.
		$text = str_replace('  ', ' &nbsp;', $text);
		// Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.
		$text = str_replace("\t", '&nbsp; &nbsp;', $text);
		// now Replace space occurring at the beginning of a line
		$text = preg_replace('/^ {1}/m', '&nbsp;', $text);
		return $bb_codes['code_start'].nl2br($text).$bb_codes['code_end'];
	}

	function decode_php($text)
	{
		global $bb_codes;
		$text = substr($text, 5, -6);
		$text = str_replace("\r\n", "\n", $text);
		$text = htmlunprepare($text, true);
		$added = FALSE;
		if (preg_match('/^<\?.*/', $text) <= 0) {
			$text = "<?php\n$text\n";
			$added = TRUE;
		}
		if (PHPVERS < 42) {
			ob_start();
			highlight_string($text);
			$text = ob_get_contents();
			ob_end_clean();
		} else {
			$text = highlight_string($text, TRUE);
		}
		if (PHPVERS < 50) {
			$text = preg_replace('/<font color="(.*?)">/si', '<span style="color: \\1;">', $text);
			$text = str_replace('</font>', '</span>', $text);
		}
		if ($added == TRUE) {
			if (PHPVERS < 50) {
				$text = preg_replace('/^(.*)\n.*?<\/span>(.*)php<br \/>/i', "\\1\n\\2?php<br />", $text, 1);
			}
			$text = preg_replace('/^(.*)\n.*php<br \/><\/span>/i', "\\1\n", $text, 1);
			$text = preg_replace('/^(.*)\n(.*)>.*php<br \/>/i', "\\1\n\\2>", $text, 1);
		}
		$text = str_replace('[', '&#91;', $text);
		return $bb_codes['php_start'].$text.$bb_codes['php_end'];
	}
}