<?
class parser
{

	//html = true
	//autorise de metre du html
	//html = false
	//interdit l'usage de html

	function parse($txt, $html=false)
	{
		
		$txt=htmlentities($txt, ENT_QUOTES);

		
		$img = array(
			':)'		=> 's01.gif',
			':-)'		=> 's01.gif',	
                    	':p'		=> 's02.gif',
                    	'??'		=> 's03.gif',
                    	'???'		=> 's03.gif',
                    	'!!'		=> 's04.gif',
                    	'!!!'		=> 's04.gif',
                    	':garde:' 	=> 's05.gif',
                    	'^_^'		=> 's06.gif',
                    	'^^'		=> 's06.gif',
                    	':><:'		=> 's07.gif',
                    	':chevalier:'	=> 's08.gif',
                    	';)' 		=> 's09.gif',
                    	';o)'		=> 's09.gif',
                        ':|' 		=> 's10.gif',
                        ':-°'		=> 's11.gif',
                        ':-*'		=> 's11.gif',
                    	':(' 		=> 's12.gif',
                    	':|' 		=> 's10.gif',
                    	);
            
		$txt=preg_replace("/\[b\](.*?)\[\/b\]/si","<strong>$1</strong>",$txt);
		$txt=preg_replace("/\[i\](.*?)\[\/i\]/si","<em>$1</em>",$txt);
		$txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie"                       			, "parser::regex_build_url(array('html' => '\\1', 'show' => '\\1'))", $txt );
		$txt = preg_replace( "#\[url\s*=\s*\&quot\;\s*(\S+?)\s*\&quot\;\s*\](.*?)\[\/url\]#ie" , "parser::regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
		$txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie"                       , "parser::regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
		
		while( preg_match( "#\n?\[list\](.+?)\[/list\]\n?#ies" , $txt ) )
		{
			$txt = preg_replace( "#\n?\[list\](.+?)\[/list\]\n?#ies", "parser::regex_list('\\1')" , $txt );
		}
		
		while( preg_match( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies" , $txt ) )
		{
			$txt = preg_replace( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies", "parser::regex_list('\\2','\\1')" , $txt );
		}
                foreach ( $img as $key=>$contenu )
		{
			$txt = str_replace($key,'<img alt="'.$key.'" src="img/smile/'.$contenu.'" />',$txt);
		}
		$txt=nl2br($txt);
		
		return $txt;
	}
	
	function unparse($txt)
	{
		$txt=preg_replace("/<strong>(.*)\<\/strong>/si","[b]$1[/b]",$txt);
		$txt=preg_replace("/<em>(.*)\<\/em>/si","[i]$1[/i]",$txt);
		$txt = preg_replace( "#(\n){0,}<ul>#" , "\\1\[LIST\]"  , $txt );
		$txt = preg_replace( "#(\n){0,}<ol type='(a|A|i|I|1)'>#" , "\\1\[LIST=\\2\]\n"  , $txt );
		$txt = preg_replace( "#(\n){0,}<li>#" , "\n\[*\]"     , $txt );
		$txt = preg_replace( "#(\n){0,}</ul>(\n){0,}#", "\n\[/LIST\]\\2" , $txt );
		$txt = preg_replace( "#(\n){0,}</ol>(\n){0,}#", "\n\[/LIST\]\\2" , $txt );
		$txt = preg_replace( "#<a href=[\"'](http://|https://|ftp://|news://)?(\S+?)['\"].+?".">(.+?)</a>#" , "\[URL=\\1\\2\]\\3\[/URL\]"  , $txt );
		$txt = str_replace( "</li>", "", $txt );
		
		$txt=str_replace("<br />","",$txt);
		$txt=str_replace("\n\n","",$txt);
		//$txt=str_replace("\\'","'",$txt);
		
		$img = array(
			':)'		=> 's01.gif',
			':-)'		=> 's01.gif',	
                    	':p'		=> 's02.gif',
                    	'??'		=> 's03.gif',
                    	'???'		=> 's03.gif',
                    	'!!'		=> 's04.gif',
                    	'!!!'		=> 's04.gif',
                    	':garde:' 	=> 's05.gif',
                    	'^_^'		=> 's06.gif',
                    	'^^'		=> 's06.gif',
                    	':><:'		=> 's07.gif',
                    	':chevalier:'	=> 's08.gif',
                    	';)' 		=> 's09.gif',
                    	';o)'		=> 's09.gif',
                        ':|' 		=> 's10.gif',
                        ':-°'		=> 's11.gif',
                        ':-*'		=> 's11.gif',
                    	':|' 		=> 's12.gif',
                    	);
                
                foreach ( $img as $key=>$contenu )
		{
			$txt = str_replace('<img alt="'.$key.'" src="img/smile/'.$contenu.'" />',$key,$txt);
		}
		
		return trim(stripslashes($txt));
	}
	
	function regex_build_url($url=array()) {
	
		$skip_it = 0;
		
		// Make sure the last character isn't punctuation.. if it is, remove it and add it to the
		// end array
		
		if ( preg_match( "/([\.,\?]|&#33;)$/", $url['html'], $match) )
		{
			$url['end'] .= $match[1];
			$url['html'] = preg_replace( "/([\.,\?]|&#33;)$/", "", $url['html'] );
			$url['show'] = preg_replace( "/([\.,\?]|&#33;)$/", "", $url['show'] );
		}
		
		// Make sure it's not being used in a closing code/quote/html or sql block
		
		if (preg_match( "/\[\/(html|quote|code|sql)/i", $url['html']) )
		{
			return $url['html'];
		}
		
		// clean up the ampersands
		$url['html'] = preg_replace( "/&amp;/" , "&" , $url['html'] );
		
		// Make sure we don't have a JS link
		$url['html'] = preg_replace( "/javascript:/i", "java script&#58; ", $url['html'] );
		
		// Do we have http:// at the front?
		
		if ( ! preg_match("#^(http|news|https|ftp|aim)://#", $url['html'] ) )
		{
			$url['html'] = 'http://'.$url['html'];
		}
		
		//-------------------------
		// Tidy up the viewable URL
		//-------------------------

		if (preg_match( "/^<img src/i", $url['show'] )) $skip_it = 1;

		$url['show'] = preg_replace( "/&amp;/" , "&" , $url['show'] );
		$url['show'] = preg_replace( "/javascript:/i", "javascript&#58; ", $url['show'] );
		
		if ( (strlen($url['show']) -58 ) < 3 )  $skip_it = 1;
		
		// Make sure it's a "proper" url
		
		if (!preg_match( "/^(http|ftp|https|news):\/\//i", $url['show'] )) $skip_it = 1;
		
		$show     = $url['show'];
		
		if ($skip_it != 1) {
			$stripped = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\2", $url['show'] );
			$uri_type = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\1", $url['show'] );
			
			$show = $uri_type.'://'.substr( $stripped , 0, 35 ).'...'.substr( $stripped , -15   );
		}
		
		$url['html'] = preg_replace("/&/","&amp;",$url['html']);
		
		return $url['st'] . "<a href=\"".$url['html']."\" target=\"_blank\">".$show."</a>" . $url['end'];
		
	}
	
	function regex_list( $txt="", $type="" )
	{
		if ($txt == "")
		{
			return;
		}
		
		//$txt = str_replace( "\n", "", str_replace( "\r\n", "\n", $txt ) );
		
		if ( $type == "" )
		{
			// Unordered list.
			
			return "<ul>".parser::regex_list_item($txt)."</ul>";
		}
		else
		{
			return "<ol type=\"$type\">".parser::regex_list_item($txt)."</ol>";
		}
	}
	
	function regex_list_item($txt)
	{
		$txt = preg_replace( "#\[\*\]#", "</li><li>" , trim($txt) );
		
		$txt = preg_replace( "#^</?li>#"  , "", $txt );
		
		return str_replace( "\n</li>", "</li>", $txt."</li>" );
	}
	
}
?>