Location: PHPKode > projects > Spam free PHP GuestBook > Spam free PHP GuestBook-1.2beta/gb/include/bbcode.php
<?php
/*
Copyright (c) 2008 http://ramui.com. All right reserved.
This product is protected by copyright and distributed under licenses restricting copying, distribution. Permission is granted to the public to download and use this script provided that this Notice and any statement of authorship are reproduced in every page on all copies of the script.
*/
class bbcode
{
private $height_max;
private $width_max;
private $autolink;
private $nofollow;
private $priview;
function __construct($height_max,$width_max,$nofollow,$preview)
{
        $this->height_max=$height_max;
		$this->width_max=$width_max;
		$this->nofollow=$nofollow;
		$this->preview=$preview;
}
public function format_text($text,$autolink)
{
        $e=$this->validate_bbcode($text);
		if($e!==true){return ((empty($this->preview))? false : $e);}
		$this->autolink=$autolink;
		$text=fw_remove_smarttag($text);
		$text=htmlspecialchars($text,ENT_NOQUOTES);
		$text=str_replace("\r","",$text);
		$text=str_replace("\n[code]","[code]",str_replace("[/code]\n","[/code]",$text));
		$text=str_replace("\n[quote]","[quote]",str_replace("[/quote]\n","[/quote]",$text));
		$text=str_replace("\n[ul]","[ul]",str_replace("[/ul]\n","[/ul]",$text));
		$text=str_replace("\n[ol]","[ol]",str_replace("[/ol]\n","[/ol]",$text));
		$text=str_replace("\n[li]","[li]",str_replace("[/li]\n","[/li]",$text));
		while(strpos($text,"[url]\n")!==false){$text=str_replace("[url]\n","\n[url]",$text);}
		while(strpos($text,"\n[/url]")!==false){$text=str_replace("\n[/url]","[/url]\n",$text);}
		while(strpos($text,"[img]\n")!==false){$text=str_replace("[img]\n","\n[img]",$text);}
		while(strpos($text,"\n[/img]")!==false){$text=str_replace("\n[/img]","[/img]\n",$text);}
        $this->escape_code($text);
		return $this->convert2_html($text);
}
private function escape_code(&$content)
{
		if(empty($content)){return;}
        $start=strpos($content,'[code]');
        $end=strpos($content,'[/code]');
        if(($start===false)||($end===false)||($start > $end)){$this->url($content);return;}
        $firstpart=substr($content,0,$start);
        $this->url($firstpart);
        $code=substr($content,$start+6,($end-$start-6));
		$code=$this->format_code($code);
        $lastpart=substr($content,$end +7);
		$this->escape_code($lastpart);
        $content=$firstpart.$code.$lastpart;
}
private function url(&$content)
{
		if($this->autolink){$content=$this->convert2_link($content);}
		$content=preg_replace("/\[url\](.+)\[\/url\]/eiUs", "\$this->geturl('\\1')", $content);
		$content=preg_replace('/\[url\=(.+)\](.+)\[\/url\]/eiUs', "\$this->img_link('\\2','\\1')", $content);
		$content=preg_replace('/\[color\=(.+)\](.+)\[\/color\]/iUs', '<span style="color:$1">$2</span>', $content);
		$content=preg_replace("/\[img\](.+)\[\/img\]/eiUs", "\$this->img_link('\\1');", $content);
		$content=nl2br($content);
}
private function geturl($link)
{
		if(strpos($link,']')!==false){
			$x=str_replace('[','<',str_replace(']','>',$link));
			$x=strip_tags($x);
			return '<a target="_blank" '.(($this->nofollow)? 'rel="nofollow" ':'').'href="'.$x.'">'.$link.'</a>';
		}
		else{return '<a target="_blank" '.(($this->nofollow)? 'rel="nofollow" ':'').'href="'.$link.'">'.$link.'</a>';}
}
private function img_link($img,$url='')
{
		if(!empty($url)){
			if(strpos($img,'[img]')===false){return '<a target="_blank" '.(($this->nofollow)? 'rel="nofollow" ':'').'href="'.$url.'">'.$img.'</a>';}
			$img=substr($img,5,-6);}
		list($width, $height) = @getimagesize($img);
		$h=$height;
		$w=$width;
		if(empty($width)||empty($height)){return;}
		if(($width>($this->width_max))||($height>($this->height_max))){
			If(($width/($this->width_max))>($height/($this->height_max))){$w=$this->width_max;$h=$height*(($this->width_max)/$width);}
			else{$h=$this->height_max;$w=$width*(($this->height_max)/$height);}
		}
		if(empty($url)){return '<div class="fw_image"><img style="overflow:auto;width:'.$w.'px;height:'.$h.'px;" src="'.$img.'" /></div>';}
		else{return '<div class="fw_image"><a target="_blank" '.(($this->nofollow)? 'rel="nofollow" ':'').'href="'.$url.'"><img style="overflow:auto;width:'.$w.'px;height:'.$h.'px;" src="'.$img.'" /></a></div>';}
}
private function convert2_link($s)
{
		$exp='/([[:space:]])+(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(\S*)?/';
		$x=preg_replace($exp, ' [url]${0}[/url]', $s);
		while(strpos($x," [url]\n")!==false){$x=str_replace(" [url]\n","\n[url]",$x);}
		while(strpos($x,"[url]\n")!==false){$x=str_replace("[url]\n","\n[url]",$x);}
		while(strpos($x,"[url] ")!==false){$x=str_replace("[url] ","[url]",$x);}
		return $x;
}
private function format_code($code)
{
		if(empty($code)){return;}
        $line_count=substr_count($code,"\n");
        $height=20+($line_count-1)*14;
        if($height<40){$height=40;}
        if($height>300){$height=300;}
        $code='<div class="fw_code_heading">Code</div><pre class="fw_code" style="padding:5px; overflow:auto; height:'.$height.'px;">'.$code.'</pre>';
        return $code;
}
private function convert2_html($text)
{
		if(empty($text)){return;}
		$search=array('[b]','[/b]','[u]','[/u]','[i]','[/i]','[s]','[/s]','[sup]','[/sup]','[sub]','[/sub]','[quote]','[/quote]','[ul]','[/ul]','[ol]','[/ol]','[li]','[/li]');
		$replace=array('<b>','</b>','<u>','</u>','<i>','</i>','<s>','</s>','<sup>','</sup>','<sub>','</sub>','<div class="fw_quote">','</div>','<ul>','</ul>','<ol>','</ol>','<li>','</li>');
		return str_replace($search,$replace,$text);
		return $text;
}
private function validate_bbcode($code)
{
		$tags=array('code','b','u','i','s','sup','sub','quote','ul','ol','li','img');
		$errors='';
		foreach($tags as $tag){$this->nested_tags($code,$errors,$tag,0);}
		if(!empty($errors)){$errors='<h1 style="color:red;">Following errors occer.</h1><ol>'.$errors.'</ol>'; return $errors;}
		return true;
}
private function nested_tags(&$content,&$errors,$tag,$offset)
{
        $start=strpos($content,'['.$tag.']',$offset);
        $end=strpos($content,'[/'.$tag.']',$offset);
		if(($start===false)&&($end===false)){return false;}
		if(($start===false)&&($end!==false)){$errors.="<li>Excess [/$tag] at character($end)!</li>";return false;}
		if(($start!==false)&&($end===false)){$errors.="<li>Unterminated [$tag] at character($start)!</li>";return false;}
		$length=strlen($tag);
        $middle=substr($content,$start+($length+2),($end-$start-($length+2)));
		$b=strpos($middle,'['.$tag.']');
		if($b!==false){$errors.="<li>Nested tag [$tag] not allowed at character($b)!</li>";return false;}
		if(($tag=='quote')||($tag=='ul')||($tag=='ol')||($tag=='li')){
			$b=strpos($middle,'[img]');
			$c=strpos($middle,'[/img]');
			if(($b!==false)||($c!==false)){$errors.="<li>[img] tag inside [$tag] not allowed at character(".(($b===false)? $c:$b).")!</li>";return false;}
		}
		$offset=$end +$length+3;
		$this->nested_tags($content,$errors,$tag,$offset);
}
}
?>
Return current item: Spam free PHP GuestBook