<?php
/**
 * @version $Id: ubb.func.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

function UBB2HTML($str, $type = 'content')
{
	global $pb_tubbtype, $pb_cubbtype, $pb_signubbtype, $pb_tubbsize, $_PBENV;
	switch ($type)
	{
		case 'content':
			$ubbtype = explode("\t", $pb_tubbtype);
			break;

		case 'comment':
			$ubbtype = explode("\t", $pb_cubbtype);
			break;

		case 'signature':
			$ubbtype = explode("\t", $pb_signubbtype);
			break;
	}
	$search = array('[strike]', '[/strike]', '[br]', '[pre]', '[/pre]', '[acronym]', '[/acronym]', '[address]', '[/address]', '[div]', '[/div]', '[p]', '[/p]', '[span]', '[/span]', '[ul]', '[ol]', '[li]', '[/ul]', '[/ol]', '[/li]', '[tr]', '[/tr]', '[td]', '[/td]', '[table]', '[/table]');
	$replace = array('<strike>', '</strike>', '<br />', '<pre>', '</pre>', '<acronym>', '</acronym>', '<address>', '</address>', '<div>', '</div>', '<p>', '</p>', '<span>', '</span>', '<ul>', '<ol>', '<li>', '</ul>', '</ol>', '</li>', '<tr>', '</tr>', '<td>', '</td>', '<table>', '</table>');
	if (in_array('b', $ubbtype))
	{
		$search[] = '[b]';
		$search[] = '[/b]';
		$replace[] = '<strong>';
		$replace[] = '</strong>';
	}
	if (in_array('i', $ubbtype))
	{
		$search[] = '[i]';
		$search[] = '[/i]';
		$replace[] = '<em>';
		$replace[] = '</em>';
	}
	if (in_array('u', $ubbtype))
	{
		$search[] = '[u]';
		$search[] = '[/u]';
		$replace[] = '<u>';
		$replace[] = '</u>';
	}
	$str = str_replace($search, $replace, $str);

	$reg_search = array("#\[align=([^\[\<]+?)\](.*?)\[/align]#is",
						"/\[table=(\d{1,4}(px|em|ex|pt|pc|in|mm|cm|%|)?)(,([\(\)%,#\w ]+)?)?\]/ies",
						"#\[td=(\d{1,2}),(\d{1,2})(,(\d{1,3}(px|em|ex|pt|pc|in|mm|cm|%|)?))?\]#is");
	$reg_replace = array("<p style=\"text-align:\\1\">\\2</p>",
						"tableCode('\\1','\\4')",
						"<td colspan=\"\\1\" rowspan=\"\\2\" width=\"\\4\">");
	if (in_array('font', $ubbtype))
	{
		$reg_search[] = "#\[font=([^\[\<]+?)\](.*?)\[/font\]#is";
		$reg_replace[] = "<span style=\"font-family:\\1\">\\2</span>";
	}
	if (in_array('size', $ubbtype))
	{
		$reg_search[] = "#\[size=(\d+(\.\d+)?(px|em|ex|pt|pc|in|mm|cm|%|)?)\](.*?)\[/size\]#ies";
		$reg_replace[] = "fontsize('\\1', '\\4', '$type')";
	}
	if (in_array('color', $ubbtype))
	{
		$reg_search[] = "#\[color=([^\[\<]+?)\](.*?)\[/color\]#is";
		$reg_replace[] = "<span style=\"color:\\1\">\\2</span>";
	}
	$str = preg_replace($reg_search, $reg_replace, $str);

	if ((strpos($str, '[code]') !== FALSE) && (strpos($str, '[/code]') !== FALSE) && in_array('code', $ubbtype))
	{
		$str = preg_replace("#\[code\](.+?)\[/code\]#ies", "pCode('\\1')", $str);
	}
	//link
	if ((strpos($str, '[url=') !== FALSE) && (strpos($str, '[/url]') !== FALSE) && in_array('url', $ubbtype))
	{
		$str = preg_replace("#\[url=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|ed2k){1}:\/\/([^\[\"']+?)\](.+?)\[\/url\]#is", "<a href=\"\\1://\\2\" target=\"_blank\">\\3</a>", $str);
	}
	//email
	if ((strpos($str, '[email=') !== FALSE) && (strpos($str, '[/email]') !== FALSE) && in_array('email', $ubbtype))
	{
		$str = preg_replace("#\[email=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\](.+?)\[\/email\]#is", "<a href=\"mailto:\\1@\\2\">\\3</a>", $str);
	}
	//imgage
	if ((strpos($str, '[img]') !== FALSE) && (strpos($str, '[/img]') !== FALSE) && in_array('img', $ubbtype))
	{
		$str = preg_replace("#\[img\]\s*([^\[\<\r\n]+?)\s*\[/img\]#ies", "pIMG('\\1')", $str);
	}
	//quote
	if ((strpos($str, '[quote') !== FALSE) && (strpos($str, '[/quote]') !== FALSE) && in_array('quote', $ubbtype))
	{
		$str = preg_replace("#\[quote(=([^\[\>]+?))?\][\n\r]*(.+?)[\n\r]*\[\/quote\]#eis", "Pqoute('\\2', '\\3')", $str);
	}
	//media
	if ((strpos($str, '[media') !== FALSE) && (strpos($str, '[/media]') !== FALSE) && in_array('media', $ubbtype))
	{
		$str = preg_replace("/\[media(=(\d*?),(\d*?))?\]\s*([^\[\<\r\n]+?)\s*\[\/media\]/eis", "Pmedia('\\2', '\\3', '\\4')", $str);
	}
	//flash
	if ((strpos($str, '[flash') !== FALSE) && (strpos($str, '[/flash]') !== FALSE) && in_array('flash', $ubbtype))
	{
		$str = preg_replace("/\[flash(=(\d*?),(\d*?))?\]\s*([^\[\<\r\n]+?)\s*\[\/flash\]/eis", "Pflash('\\2', '\\3', '\\4')", $str);
	}
	//emote
	if ((strpos($str, '[emote]') !== FALSE) && (strpos($str, '[/emote]') !== FALSE))
	{
		$str = preg_replace("/\[emote\]\s*([^\[\<\r\n\.]+?)\s*\[\/emote\]/is", "<img src=\"".$_PBENV['PB_URL']."images/emoticons/\\1.gif\" alt=\"\\1\" class=\"emote\" />", $str);
	}
	return $str;
}

/**
 * 表格
 */
function tableCode($width, $bgcolor)
{
	if (substr($width, -1) == '%')
	{
		$width = (substr($width, 0, -1) < 98) ? (int)$width.'%' : '98%';
	}
	else
	{
		$width = (int)$width;
		$width = ($width <= 560) ? $width : '98%';
	}
	return '<table'.($width ? ' width="'.$width.'" ' : ' ').'align="center" class="t_table"'.($bgcolor ? ' bgcolor="'.$bgcolor.'" ' : '').'>';
}

/**
 * 文字size
 */
function fontsize($size, $content, $type)
{
	global $pb_tubbsize;
	if (is_numeric($size))
	{
		if ($type == 'content' || $type == 'comment')
		{
			if ($size > $pb_tubbsize || $size < 1) $size = 3;
		}
		else
		{
			$size = 3;
		}
		return '<font size="'.$size.'">'.$content.'</font>';
	}
	else
	{
		return '<span style="font-size:'.$size.'">'.$content.'</span>';
	}
}

/**
 * 代码
 */
function pCode($code)
{
	$code = str_replace('\\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code));
	return '<div class="code"><code>'.$code.'</code></div>';
}

/**
 * 图片自动显示
 */
function pIMG($var)
{
	global $pb_previewsize, $_PBENV;
	list($pb_previewh, $pb_previeww) = explode("\t", $pb_previewsize);
	$onload = '';
	if(strtolower(substr($var, 0, 4)) != 'http')
	{
		$var = $_PBENV['PB_URL'].$var;
	}
	if ($pb_previewh || $pb_previeww)
	{
		$onload = ' onload="';
		if ($pb_previeww)
		{
			$onload .= "if(this.width>'".$pb_previeww."')this.width='".$pb_previeww."';";
		}
		if ($pb_previewh)
		{
			$onload .= "if(this.height>'".$pb_previewh."')this.height='".$pb_previewh."';";
		}
		$onload .= '"';
	}
	return "<a href=\"$var\" title=\"\" rel=\"gallery\" class=\"thickbox\"><img src=\"$var\" alt=\"\"".$onload." /></a>";;
}

/**
 * 视频媒体
 */
function Pmedia($height, $width, $mediaurl)
{
	global $pb_mplayersize, $pb_mautoplay;
	list($pb_mplayerh, $pb_mplayerw) = explode("\t", $pb_mplayersize);
	$exticon = strtolower(Fext($mediaurl));
	$pb_mplayerh = ($height > $pb_mplayerh || !$height) ? $pb_mplayerh : $height;
	$pb_mplayerw = ($width > $pb_mplayerw || !$width) ? $pb_mplayerh : $width;
	if (preg_match("/^(wav|asf|asx|vqf|mpg|mpeg|avi|wmv)$/", $exticon))
	{
		return '<div><object id="MediaPlayer" width="'.$pb_mplayerw.'" height="'.$pb_mplayerh.'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112" align="baseline" border="0" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject"><param name="URL" value="'.$mediaurl.'"><param name="autoStart" value="'.$pb_mautoplay.'"><param name="invokeURLs" value="false"><param name="playCount" value="100"><param name="defaultFrame" value="datawindow"><embed src="'.$mediaurl.'" align="baseline" border="0" width="'.$pb_mplayerw.'" height="'.$pb_mplayerh.'"	type="application/x-mplayer2" pluginspage="" name="MediaPlayer" showcontrols="1" showpositioncontrols="0" showaudiocontrols="1" showtracker="1" showdisplay="0"	showstatusbar="1" autosize="0" showgotobar="0" showcaptioning="0" autostart="'.$pb_mautoplay.'" autorewind="0" animationatstart="0" transparentatstart="0" allowscan="1" enablecontextmenu="1" clicktoplay="0" defaultframe="datawindow" invokeurls="0"></embed></object></div>';
	}
	elseif (preg_match("/^(mid|mp3|m3u|wma)$/", $exticon))
	{
		return '<div><object id="MediaPlayer1" width="350" height="68" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112" align="baseline" border="0" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject"> <param name="URL" value="'.$mediaurl.'"> <param name="autoStart" value="'.($pb_mautoplay ? 'true' : 'false').'"> <param name="invokeURLs" value="false"> <param name="playCount" value="100"> <param name="defaultFrame" value="datawindow"> <embed src="'.$mediaurl.'" align="baseline" border="0" width="350" height="68" type="application/x-mplayer2" pluginspage="" name="MediaPlayer1" showcontrols="1" showpositioncontrols="0" showaudiocontrols="1" showtracker="1" showdisplay="0" showstatusbar="1" autosize="0" showgotobar="0" showcaptioning="0" autostart="'.$pb_mautoplay.'" autorewind="0" animationatstart="0" transparentatstart="0" allowscan="1" enablecontextmenu="1" clicktoplay="0" defaultframe="datawindow" invokeurls="0"></embed></object></div>';
	}
	elseif ('ra' == $exticon)
	{
		return '<embed id="RealPlayer1" autogotourl=false type="audio/x-pn-realaudio-plugin" src="'.$mediaurl.'" controls="ControlPanel,StatusBar" width=350 height=68 border=0 autostart='.($pb_mautoplay ? 'true' : 'false').' loop=true></embed>';
	}
	elseif (preg_match("/^(rmvb|rm|rv)$/", $exticon))
	{
		return '<embed id="RealPlayer" autogotourl="false" type="audio/x-pn-realaudio-plugin" src="'.$mediaurl.'" controls="ControlPanel,StatusBar" width="'.$pb_mplayerw.'" height="'.$pb_mplayerh.'" border="0" autostart="'.($pb_mautoplay ? 'true' : 'false').'" loop="true"></embed>';
	}
	return $mediaurl;
}
/**
 * Flash播放
 */
function Pflash($height, $width, $flashurl)
{
	global $pb_mplayersize, $pb_fautoplay, $_PBENV;
	list($pb_mplayerh, $pb_mplayerw) = explode("\t", $pb_mplayersize);
	$pb_mplayerh = ($height > $pb_mplayerh || !$height) ? $pb_mplayerh : $height;
	$pb_mplayerw = ($width > $pb_mplayerw || !$width) ? $pb_mplayerh : $width;
	if ($pb_fautoplay)
	{
		return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'.$pb_mplayerw.'" height="'.$pb_mplayerh.'"><param name="movie" value="'.$flashurl.'" /><param name="quality" value="high" /><embed src="'.$flashurl.'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'.$pb_mplayerw.'" height="'.$pb_mplayerh.'"></embed></object>';
	}
	else
	{
		return '<img src="'.$_PBENV['PB_URL'].'images/file/flash.gif" border="0" class="attachicon" alt="flash" />&nbsp;<a href="'.$flashurl.'" target="_blank">'.$flashurl.'</a><br />';		
	}
}

/**
 * 引用
 */
function Pqoute($user, $content)
{
	global $func_message;
	if ($user)
	{
		$user = sprintf($func_message['ubb_post_topic'], $user);
	}
	else
	{
		$user = $func_message['ubb_qoute_empty'];
	}
	return '<div class="quote"><div class="quote-title">'.$user.' </div><blockquote>' . str_replace('\\"', '"', $content) . '</blockquote></div>';
}
?>
