<?php

$module["parser"]["name"]="Text parser Class";
$module["parser"]["ver"]="0.1.5";
/*class textparser
{
	private $BBCodesDefault="";
	private $BBCodesExtra="";
	private $rawText;

	public function __construct()
	{
		if(!isset($this->BBCodesDefault) OR !isset($this->BBCodesExtra))
		{
			trigger_error("Es fehlen wichtige Variablen! Bitte kontrollieren Sie die Klasse.");
		}
		$this->addDefaultBBCodes();
	}

	private function addDefaultBBCodes()
	{
		$this->BBCodesDefault = array("b" => array("/\[b\](.*)\[\/b\]/isU" => "<b>$1</b>"),
								      "i" => array("/\[i\](.*)\[\/i\]/isU" => "<i>$1</i>"),
								      "u" => array("/\[u\](.*)\[\/u\]/isU" => "<u>$1</u>"),
								      "hr" => array("/\[hr\]/isU" => "<hr />"),			
								      "br" => array("/\[br\]/isU" => "<br />"),
								      "url" => array("/\[url\=(.*)\](.*)\[\/url\]/isU" => "<a href=\"$1\">$2</a>"),
								      "youtube" => array("/\[youtube\](.*)\[\/youtube\]/isU" => "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://www.youtube.com/v/$1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/$1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>"),
								      "img" => array("/\[img\](.*)\[\/img\]/isU" => "<img src=\"$1\" alt=\"\" title=\"\" />"));
	}

	public function addExtraBBCode($name, $bbCodePattern, $htmlCodePattern)
	{
		if(!isset($this->BBCodesExtra[$name]))
		{
			$this->BBCodesExtra[$name] = array($bbCodePattern => $htmlCodePattern);
		}
		else
		{
			trigger_error("BBCodename existiert bereits");
		}
	}

	private function parseExtraCode()
	{
		if(!is_array($this->BBCodesExtra)) return $this->rawText;

		foreach($this->BBCodesExtra as $BBCode => $array)
		{
			foreach($array as $BBCodePattern => $htmlPattern)
			{
				$this->rawText = preg_replace($BBCodePattern, $htmlPattern, $this->rawText);
			}
		}
		return $this->rawText;
	}

	private function iniParse($rawText)
	{
	$this->rawText = $rawText;
		foreach($this->BBCodesDefault as $BBCode => $array)
		{
			if(stripos($this->rawText, "[".$BBCode."]") !== FALSE && stripos($this->rawText, "[/".$BBCode."]") !== FALSE)
			{
				return true;
			}
			continue;
		}
		if(is_array($this->BBCodesExtra))
		{
			foreach($this->BBCodesExtra as $BBCode => $array)
			{
				if(stripos($this->rawText, "[".$BBCode."]") !== FALSE && stripos($this->rawText, "[/".$BBCode."]") !== FALSE)
				{
					return true;
				}
				continue;
			}
		}

		return false;
	}

	private function parseCode()
	{
		if($this->rawText == "")
		{
			trigger_error("Diese Funktion muss erst durch iniParse() initialisert werden!");
		}

		foreach($this->BBCodesDefault as $BBCode => $array)
		{
			foreach($array as $search => $html)
			{
				$this->rawText = preg_replace($search, $html, $this->rawText);
			}
		}

		return $this->rawText;
	}

	public function parse($rawText)
	{
		//$this->rawText = htmlentities($rawText);
		if(!$this->iniParse($rawText)) return $this->rawText;
		$this->parseCode();
		$this->parseExtraCode();
		return $this->rawText;
	}
}*/

class textparser {

	function __construct($parse_smilies=true) {
		global $config, $db;
		if($config['smilies_table']!=NULL && $config['smilies_table'] != ""){
			$this->parse_smilies=$parse_smilies;
			if($this->parse_smilies){
				$result=$db->query('SELECT * FROM `' . $config['smilies_table']. '`');
				while($row=$db->fetch_array($result)){
				 $find=preg_quote($row['find']);
					$this->smilies[$find]='<img src="' . $config['smilies_url'] . $row['image'].'" style="border:none;" alt="'.$row['name'].'"/>';
				}
			}
		}
	}
	
	 function parseParameters ($stringParameter) {
	  $arrayParameter = array ();

		if (is_string ($stringParameter) === TRUE && empty ($stringParameter) === FALSE){
			if (preg_match_all ('°(^|\w+)\=(\"?)([^\"]*?)\2(?: |$)°', $stringParameter, $arrayMatches, PREG_SET_ORDER) > 0){
				foreach ($arrayMatches AS $integerMatchCount => $arrayMatch){
					if (empty ($arrayMatch[1]) === TRUE){
						$stringKey = '__INIT__';
					}else{
						$stringKey = strtolower ($arrayMatch[1]);
					}
					$arrayParameter[$stringKey] = $arrayMatch[3];
					$arrayParameter[$integerMatchCount] = &$arrayParameter[$stringKey];
				}
			unset ($arrayMatch);
			}
		}

	  return $arrayParameter;
	}
	
	function parse($mixedInfo) {
		$stringCode = '';
		$arrayParameter = array ();
		$booleanMixedInfoIsArray = FALSE;
		
		if (is_array ($mixedInfo) === TRUE && count ($mixedInfo) == 4){
			$stringText = $mixedInfo[3];
			$stringCode = strtolower ($mixedInfo[1]);
			$arrayParameter = $this->parseParameters ($mixedInfo[2]);
			$booleanMixedInfoIsArray = TRUE;
		}elseif (is_string ($mixedInfo) === TRUE){
			$stringText = $mixedInfo;
			$stringText = preg_replace ('°\[(br|hr)]°is','[\1][/\1]',$stringText);
		}else{
			return '';
		}

		if ($stringCode != 'noparse'){
			$stringText = preg_replace_callback ('°\[(\w+)((?:\s|=)[^]]*)?]((?:[^[]|\[(?!/?\1((?:\s|=)[^]]*)?])|(?R))*)\[/\1]°',array($this,'parse'),$stringText);
		}

		if ($booleanMixedInfoIsArray === TRUE){
			switch ($stringCode){
				case 'b':
				case 'i':
				case 'u':
				{
					$stringText = '<'.$mixedInfo[1].'>'.$stringText.'</'.$mixedInfo[1].'>';
					break;
				}

				case 'br':
				case 'hr':
				{
					$stringText = '<'.$mixedInfo[1].' />';
					break;
				}

				case 'url':
				{
					if (count ($arrayParameter) == 0 || array_key_exists ('__INIT__',$arrayParameter) === FALSE){
						$stringText = '<a href="'.$stringText.'">'.$stringText.'</a>';
					}else{
						$stringTitle = '';

						if (array_key_exists ('title',$arrayParameter) === TRUE){
							$stringTitle = ' title="'.$arrayParameter['title'].'"';
						}
						$stringText = '<a href="'.$arrayParameter['__INIT__'].'"'.$stringTitle.'>'.$stringText.'</a>';

						unset ($stringTitle);
					}

					break;
				}
				
				case 'color':
				{
					$stringText = '<span style="color: '.$arrayParameter['__INIT__'].';">'.$stringText.'</span>';
					break;
				}

				default:
				{
					$stringText = $mixedInfo[0];

					break;
				}
				
			}
		}
		if($this->parse_smilies){
			foreach($this->smilies as $find=>$replace){
				$stringText = @preg_replace('#'.$find.'#s', $replace, $stringText);
			}
		}
		return $stringText;
	}
 
}
?>
