Your IP : 216.73.216.201


Current Path : /home/balossw/www/temp/common/includes/
Upload File :
Current File : /home/balossw/www/temp/common/includes/bbcode.inc.php

<?php
/*---------------------------------------------------------------------------
		File name ............ common/includes/bbcode.inc.php
		Author ............... Antoine Lemoine
		Creation date ........ 02.09.2009
		Modification date .... 29.01.2010
---------------------------------------------------------------------------*/


function bbdecode($text, $images_folder='')
{
	$text = trim($text);

	if (substr($images_folder,strlen($images_folder)-1,1)!='/') { $images_folder .= '/'; }

	//Remplace les tags Dcode par leur equivalent en HTML

	$tags = array('b','i','u','s','left','center','right','justify');
	foreach ($tags as $tag)
	{
		$counter[$tag] = preg_match_all('/\['.$tag.'\]/U',$text,$out,PREG_PATTERN_ORDER)
			- preg_match_all('/\[\/'.$tag.'\]/U',$text,$out,PREG_PATTERN_ORDER);
	}

	$counter['link'] = preg_match_all('/\[link\]/U',$text,$out,PREG_PATTERN_ORDER);
	$counter['link'] += preg_match_all('/\[link=([^]]*)\]/U',$text,$out,PREG_PATTERN_ORDER);
	$counter['link'] -= preg_match_all('/\[\/link\]/U',$text,$out,PREG_PATTERN_ORDER);

	$counter['img'] = preg_match_all('/\[img(?: align=(center|left|right))?\]/U',$text,$out,PREG_PATTERN_ORDER);
	$counter['img'] -= preg_match_all('/\[\/img\]/U',$text,$out,PREG_PATTERN_ORDER);
	
	$counter['size'] = preg_match_all('/\[size=[1-7]\]/U',$text,$out,PREG_PATTERN_ORDER);
	$counter['size'] -= preg_match_all('/\[\/size\]/U',$text,$out,PREG_PATTERN_ORDER);

	$ok = true;
	foreach ($counter as $tag => $n)
	{
		if ($n!=0) { $ok = false; }
	}

	if ($ok)
	{
		// Convertit les nouvelles lignes en <br>
		$text = preg_replace("#\r?\n#",'<br />',$text);
		$text = concatLines($text);

		$text = preg_replace('#\[([bius])\]#i','<\\1>', $text);
		$text = preg_replace('#\[/([bius])\]#i','</\\1>', $text);

		$text = preg_replace('#\[(left|center|right|justify)\]#i','<div style="text-align: \\1;">', $text);
		$text = preg_replace('#\[/(left|center|right|justify)\]#i','</div>', $text);

		$text = preg_replace('#\[indent\]#i','<blockquote>', $text);
		$text = preg_replace('#\[/indent\]#i','</blockquote>', $text);

		$text = preg_replace('#\[size=([1-7])\]#i', '<font size="\\1">', $text);
		$text = preg_replace('#\[/size\]#i', '</font>', $text);

		$text = splitLines('/(\[link=([^]]*)\])/U','/(\[\/link\])/U',$text);
		$text = preg_replace('/\[link\](.*)\[\/link\]/Umi', '<a href="\\1" target="_blank">\\1</a>', $text);
		$text = preg_replace('/\[link=([^]]*)\](.*)\[\/link\]/Umi', '<a href="\\1" target="_blank">\\2</a>', $text);
		$text = concatLines($text);

		$text = splitLines('/(\[\*\])/U',null,$text);
		$text = splitLines('/(\[\/list\])/U',null,$text);
		$text = preg_replace('/^\[\*\](.*)$/Umi', '<li>$1</li>', $text);
		$text = concatLines($text);

		$text = splitLines('/(\[list(=1)?\])/U','/(\[\/list\])/U',$text);
		$text = preg_replace('/\[list=1\](.*)\[\/list\]/Umi', '<ol>$1</ol>', $text);
		$text = preg_replace('/\[list\](.*)\[\/list\]/Umi', '<ul>$1</ul>', $text);
		$text = concatLines($text);

		// Images
		$patterns = array(
			'/\[img\]([^]]*)\[\/img\]/i' =>
				'<img class="image" src="%filename%" style="width:%width%px;height:%height%px;" %title% />',
			'/\[img align=(center|left|right)\]([^]]*)\[\/img\]/i' =>
				'<div class="img_$1"><img class="image" src="%filename%" style="width:%width%px;height:%height%px;" %title% /></div>',
			'/\[img\]([^]]*)\|([^]]*)\[\/img\]/i' =>
				'<div class="img_center"><img class="image" src="%filename%" style="width:%width%px;height:%height%px;" %title% /><br /><span class="legend">$2</span></div>',
			'/\[img align=(center|left|right)\]([^]]*)\|([^]]*)\[\/img\]/i' =>
				'<div class="img_$1"><img class="image" src="%filename%" style="width:%width%px;height:%height%px;" %title% /><br /><span class="legend">$3</span></div>',
		);

		foreach ($patterns as $pattern => $replacement)
		{
			while (preg_match($pattern,$text,$result))
			{
				$filename = $result[1];
				$title = '';
				if (substr($filename,0,7)!='http://')
				{
					$filename = cleanPath($images_folder.$filename);
					if (!file_exists($filename))
					{
						$title = 'title="'.getHtmlValue($filename).'"';
						$filename = 'canvas/images/no-image.png';
					}
				}
				else
				{
					$filename = cleanPath($filename);
					if (!@fopen($filename,'r'))
					{
						$title = 'title="'.getHtmlValue($filename).'"';
						$filename = 'canvas/images/no-image.png';
					}
				}
				$image_size = @getimagesize($filename);
				$width = $image_size[0];
				$height = $image_size[1];
				$text = preg_replace($pattern,
					str_replace(array('%filename%','%width%','%height%','%title%'),array($filename,$width,$height,$title),$replacement),
					$text,1);
			}
		}
	}
	else
	{
		$text .= "<p class=\"error\">Erreur de d&eacute;codage du bbcode.</p>";
	}

	return $text;
}

function splitLines($regex1, $regex2, $text)
{
	$text = preg_replace($regex1,"\n$1",$text);
	if ($regex2!=null) { $text = preg_replace($regex2,"$1\n",$text); }
	return $text;
}

function concatLines($text)
{
	return preg_replace("/[\r\n]*/i",'',$text);
}

?>