<?php
/**
 * @version $Id: html.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 safeHTML($str)
{
	global $pb_editor;
	$str = preg_replace(array("/<(style|script|noscript|select|object)(?:[^\>]?)>.*?<\/\\1>/ims", "/<!--.*?-->/ims", "/on[a-z]{3,16}\s?=\s?(\"|\')?.*?\\1/ims"), '', $str);
	if ($pb_editor == '2')
	{
		$str = preg_replace("/(\r\n|\n|\r)/ims", '', $str);
	}
	$str = str_replace(array('<pre>', '</pre>', '<strike>', '</strike>', '<blockquote>', '</blockquote>', '<address>', '</address>', '<acronym>', '</acronym>', '<b>', '<strong>', '<u>', '<i>', '<em>', '</b>', '</strong>', '</u>', '</i>', '</em>', '</ul>', '</ol>', '</li>', '<center>', '</center>', '</td>', '</tr>', '</table>'), array('[pre]', '[/pre]', '[strike]', '[/strike]', '[quote=]', '[/quote]', '[address]', '[/address]', '[acronym]', '[/acronym]', '[b]', '[b]', '[u]', '[i]', '[i]', '[/b]', '[/b]', '[/u]', '[/i]', '[/i]', '[/ul]', '[/ol]', '[/li]', '[align=center]', '[/align]', '[/td]', '[/tr]', '[/table]'), $str);
	$str = preg_replace("/<code>(.+?)<\/code>/is", "[code]\\1[/code]", $str);
	$str = preg_replace("/<div([^>]*?)>(.+?)<\/div>/ies", "html_div('\\1', '\\2')", $str);
	$str = preg_replace("/<p([^>]*?)>(.+?)<\/p>/ies", "html_p('\\1', '\\2')", $str);
	$str = preg_replace("/<span([^>]*?)>(.+?)<\/span>/ies", "html_span('\\1', '\\2')", $str);
	$str = preg_replace("/<font([^>]*?)>(.+?)<\/font>/ies", "html_font('\\1', '\\2')", $str);
	$str = preg_replace(array("/<ul[^>]*?>/is", "/<ol[^>]*?>/is", "/<li[^>]*?>/is"), array('[ul]', '[ol]', '[li]'), $str);
	$str = preg_replace("/<h([0-9]+)[^>]*?>(.+?)<\/h\\1>/is", "[size=\\1]\\2[/size]", $str);
	$str = preg_replace("/<a([^>]*?href[^>]*?)>(.+?)<\/a>/ies", "html_a('\\1', '\\2')", $str);
	$str = preg_replace("/<img[^>]*?src=([\"\']?)(.+?)\\1[^>]*?>/is", "[img]\\2[/img]", $str);
	$str = preg_replace("/<br[^\>]*?>/is", '[br]', $str);
	$str = preg_replace(array("/<table([^>]*?(width|background|background-color|bgcolor)[^>]*?)>/sie","/<table[^>]*?>/is","/<tr[^>]*?>/is","/<td([^>]*?(width|colspan|rowspan)[^>]*?)>/ise","/<td[^>]*?>/is"), array("html_table('\\1')",'[table]','[tr]',"html_td('\\1')",'[td]'), $str);
	$str = preg_replace("/<\/?[^\>]*?>/is", '', $str);
	return $str;
}

function getattvalue($option, $text)
{
	return preg_match("/$option(\s+?)?\=(\s+?)?[\"']?(.+?)([\"']|$|>)/is", $text, $matches) ? trim($matches[3]) : '';
}

function html_a($href, $text)
{
	$href = stripslashes($href);
	$text = trim(stripslashes($text));
	if(!$text) return '';
	$href = getattvalue('href', $href);
	if (substr($href, 0, 11) == 'javascript:')
	{
		return $text;
	}
	elseif (substr($href, 0, 7) == 'mailto:')
	{
		$tag = 'email';
		$href = substr($href, 7);
	}
	else
	{
		$tag = 'url';
	}
	return '['.$tag.'='.$href.']'.$text.'[/'.$tag.']';
}

function html_table($text)
{
	$text = stripslashes($text);
	$width = $bgcolor = '';
	if (preg_match("/width=([\"\']?)(\d{1,4})(px|em|ex|pt|pc|in|mm|cm|%|)?\\1/is", $text, $matches))
	{
		$width = ($matches[3] == '%') ? (($matches[2] > '98') ? '98%' : $matches[2].'%') : ($matches[2].$matches[3]);
	}
	elseif (preg_match("/width\s?:\s?(\d{1,4})(px|em|ex|pt|pc|in|mm|cm|%|)/is", $text, $matches))
	{
		$width = ($matches[2] == '%') ? (($matches[1] > '98') ? '98%' : $matches[1].'%') : ($matches[1].$matches[2]);
	}
	if(preg_match("/(?:background|background-color|bgcolor)[:=]\s*([\"']?)((rgb\(\d{1,3}%?,\s*\d{1,3}%?,\s*\d{1,3}%?\))|(#[0-9a-fA-F]{3,6})|([a-zA-Z]{1,20}))(\\1)/i", $text, $matches))
	{
		$bgcolor = $matches[2];
	}
	return (!$bgcolor && !$width) ?  '[table]' : "[table=$width,$bgcolor]";
}

function html_td($text)
{
	$text = strtolower(stripslashes($text));
	$width = '';
	$colspan = $rowspan = '1';
	if (strpos($text, 'colspan') !== FALSE)
	{
		if (preg_match("/colspan=([\"\']?)(\d{1,2})\\1/i", $text, $matches))
		{
			$colspan = $matches[2];
		}
	}
	if (strpos($text, 'rowspan') !== FALSE)
	{
		if (preg_match("/rowspan=([\"\']?)(\d{1,2})\\1/i", $text, $matches))
		{
			$rowspan = $matches[2];
		}
	}
	if (strpos($text, 'width') !== FALSE)
	{
		if (preg_match("/width=([\"\']?)(\d{1,4})(px|em|ex|pt|pc|in|mm|cm|%|)?\\1/is", $text, $matches))
		{
			$width = $matches[2].$matches[3];
		}
		elseif (preg_match("/width\s?:\s?(\d{1,4})(px|em|ex|pt|pc|in|mm|cm|%|)/is", $text, $matches))
		{
			$width = $matches[1].$matches[2];
		}
	}
	return !$width ? ($colspan == 1 && $rowspan == 1 ? '[td]' : '[td='.$colspan.','.$rowspan.']') : '[td='.$colspan.','.$rowspan.','.$width.']';
}

function html_div($style, $text)
{
	$style = trim(stripslashes($style));
	$text = stripslashes($text);
	$align = getattvalue('align', $style);
	$prepend = $append = '';
	if (in_array($align, array('center', 'left', 'right')))
	{
		$text = '[align='.$align.']'.$text.'[/align]';
	}
	if (preg_match("/style=([\"\']?)(.+?)\\1/is", $style, $match))
	{
		html_style($match[0], $prepend, $append);
	}
	return '[div]'.$prepend.$text.$append.'[/div]';
}

function html_p($style, $text)
{
	$style = trim(stripslashes($style));
	$text = stripslashes($text);
	$align = getattvalue('align', $style);
	$prepend = $append = '';
	if (in_array($align, array('center', 'left', 'right')))
	{
		$text = '[align='.$align.']'.$text.'[/align]';
	}
	if (preg_match("/style=([\"\']?)(.+?)\\1/is", $style, $match))
	{
		html_style($match[0], $prepend, $append);
	}
	return '[p]'.$prepend.$text.$append.'[/p]';
}

function html_span($style, $text)
{
	$style = trim(stripslashes($style));
	$text = stripslashes($text);
	$align = getattvalue('align', $style);
	$prepend = $append = '';
	if (in_array($align, array('center', 'left', 'right')))
	{
		$text = '[align='.$align.']'.$text.'[/align]';
	}
	if (preg_match("/style=([\"\']?)(.+?)\\1/is", $style, $match))
	{
		html_style($match[0], $prepend, $append);
	}
	return $prepend.$text.$append;
}

function html_font($style, $text)
{
	$style = trim(stripslashes($style));
	$text = stripslashes($text);
	if (!$style) return $text;
	$pre = $end = $prepend = $append = '';
	$searchlist = array('font' => "/face=([\"\']?)(.+?)\\1/is", 'size' => "/size=([\"\']?)(.+?)\\1/is", 'color' => "/color=([\"\']?)(.+?)\\1/is");
	foreach ($searchlist as $key => $value)
	{
		if (preg_match($value, $style, $matchs))
		{
			$pre .= '['.$key.'='.$matchs[2].']';
			$end = '[/'.$key.']'.$end;
		}
	}
	if (preg_match("/style=([\"\']?)(.+?)\\1/is", $style, $match))
	{
		html_style($match[0], $prepend, $append);
	}
	return $prepend.$pre.$text.$end.$append;
}

function html_style($text, &$prepend, &$append)
{
	$text = stripslashes($text);
	$searchlist = array(array('align', "/text-align:\s*(left|center|right);?/is", '1'),
						array('color', "/color:\s*([^;]+);?/is", '1'),
						array('font', "/font-family:\s*([^;]+);?/im", '1'),
						array('size', "/font-size:\s*(\d+(\.\d+)?(px|em|ex|pt|pc|in|mm|cm|%|));?/is", '1'),
						array('b', "/font-weight:\s*(bold);?/is", '0'),
						array('i', "/font-style:\s*(italic);?/is", '0'),
						array('u', "/text-decoration:\s*(underline);?/is", '0'));
	$style = getattvalue('style', $text);
	$style = preg_replace("/^color:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)(;?)/ies", "'color:#'.dechex('\\1').dechex('\\2').dechex('\\3')", $style);
	foreach ($searchlist as $value)
	{
		if (preg_match($value[1], $style, $matchs))
		{
			$prepend .= '['.$value[0].($value[2] ? '='.$matchs[1].']' : ']');
			$append = '[/'.$value[0].']'.$append;
		}
	}
}

?>