<?
/**
 *	NP_BBCode plugin for Nucleus CMS
 *	from Kai Greve (http://kgblog.de)  
 * 
 *	History:
 *	0.1 - initial release (only in the nucleus forum)
 *	0.2 - more BBCode tags and additional options
 *	0.3  (2007-11-15):
 *		- allow also [quote name=""] (name is printed bold)
 *		- bug fixed: the option for the img tag in comments has also
 *		             switched of imgages in items
 *		- add a [youtube][/youtube] tag for videos from YouTube
 *	0.4  (2007-11-27):
 *		- bug removed: resolve [quote name=""] also in comments
 *		- bug removed: resolve youtube urls also if they doesn't include 'www.'
 *		- code styling: use tabs instead of spaces
 * 	0.5  (2008-09-14):
 *		- new options: 'Allow BBCode in items?' and 'Allow BBCode in comments?'
 *		- enhancement: video id only can be used in the youtube markup  
 * 	0.6  (2009-01-03):
 *		- added: BBCode [s][/s] for strike respectively line through text
 */
 
class NP_BBCode extends NucleusPlugin {
 
	function getName() { return 'BBCode'; }
	function getAuthor()  { return 'Kai Greve'; }
	function getURL()  { return 'http://kgblog.de/'; }
	function getVersion() { return '0.6'; }
	function getDescription() {
		return 'Replaces BBCode in items and comments with HTML markup';
	}
 
	function getEventList() {
		return array('PreItem', 'PreComment', 'FormExtra');
	}
 
	function install() {
		$this->createOption('bbcode_items','Allow BBCode in items?','yesno','yes');
		$this->createOption('bbcode_comments','Allow BBCode in comments?','yesno','yes');
		$this->createOption('ImageLinks','Allow image links in comments?','yesno','no');
		$this->createOption('nofollow','Use rel="nofollow"','yesno','yes');
		$this->createOption('qh','Quote header','text','<blockquote><p>');
		$this->createOption('qf','Quote footer','text','</p></blockquote>');
		$this->createOption('ch','Code header','text','<pre>');
		$this->createOption('cf','Code footer','text','</pre>');
		$this->createOption('infourl','Information about BBCode','text','http://www.phpbb.com/phpBB/faq.php?mode=bbcode');
		$this->createOption('YoutubeLinks','Allow YouTube links in comments?','yesno','no');
		$this->createOption('yt_width','Youtube video width','text','425');
		$this->createOption('yt_height','Youtube video height','text','355');
	}
 
	function event_FormExtra ($data) {
		if($data['type']=='commentform-notloggedin' || $data['type']=='commentform-loggedin') {
			echo "<br /><strong>Allowed <a href=\"".$this->getOption('infourl')."\" onclick=\"javascript:window.open(this.href, '_blank'); return false;\">BBCode:</a></strong>";
			echo "[b] [i] [u] [s] [color=] [size=] [quote] [code] [email]";
			if ($this->getOption('ImageLinks') == 'yes') {
				echo " [img]";
			}
			if ($this->getOption('YoutubeLinks') == 'yes') {
				echo " [youtube]";
			}
		}
	}
 
	function youtube_code ($data) {
		// get the id from the string bracketed with youtube
		$url_parts = parse_url($data[1]);
		if ($url_parts['host']!='') {
			// full url is used (query includes appended parameters from the url)
			$youtube_id = str_replace('v=', '', $url_parts['query']);
		} else {
			// only video id in brackets
			$youtube_id = $url_parts['path'];
		}
		// built HTML code for youtube video
		$code='<object width="'.$this->getOption('yt_width').'" height="'.$this->getOption('yt_height').'"><param name="movie" value="http://www.youtube.com/v/'.$youtube_id.'&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$youtube_id.'&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="'.$this->getOption('yt_width').'" height="'.$this->getOption('yt_height').'"></embed></object>';
 
		return $code;
	}
 
	function Treatment($_text, $_nofo='', $_type) {
		global $CONF, $blog;
 
		/* [b][/b] [i][/i] [u][/u] */
		$_text=preg_replace('/\[b\](.*)\[\/b\]/Usi','<span style="font-weight: bold;">\1</span>',$_text);
		$_text=preg_replace('/\[i\](.*)\[\/i\]/Usi','<span style="font-style: italic;">\1</span>',$_text);
		$_text=preg_replace('/\[u\](.*)\[\/u\]/Usi','<span style="text-decoration: underline">\1</span>',$_text);
 
		/* [s][/s] */
		$_text=preg_replace('/\[s\](.*)\[\/s\]/Usi','<span style="text-decoration:line-through;">\1</span>',$_text);
 
		/* [color=][/color] [size=][/size] */
		$_text=preg_replace('/\[color=(.*)\](.*)\[\/color\]/Usi','<span style="color:\1;">\2</span>',$_text);
		$_text=preg_replace('/\[size=(.*)\](.*)\[\/size\]/Usi','<span style="font-size:\1px; line-hight:normal;">\2</span>',$_text);
 
		/* [quote][/quote] [quote name=][/quote]*/
		$_text=preg_replace('/\[quote\](.*)\[\/quote\]/Usi',''.$this->getOption('qh').'\1'.$this->getOption('qf').'',$_text);
		if ($_type=='item') {
			$_text=preg_replace('/\[quote name="(.*)"\](.*)\[\/quote\]/Usi',''.$this->getOption('qh').'<strong>\1</strong>:&nbsp;\2'.$this->getOption('qf').'',$_text);
		} else {
			$_text=preg_replace('/\[quote name=&quot;(.*)&quot;\](.*)\[\/quote\]/Usi',''.$this->getOption('qh').'<strong>\1</strong>:&nbsp;\2'.$this->getOption('qf').'',$_text);
		}
 
		/* [code][/code] */
		preg_match('/\[code\](.*)\[\/code\]/Usi', $_text, $matches);
		$code=preg_replace('/<br \/>/Ui','', $matches[1]);
		$_text=preg_replace('/\[code\](.*)\[\/code\]/Usi',''.$this->getOption('ch').$code.$this->getOption('cf').'',$_text);
 
		/* [url][/url] [url=][/url]*/
		$_text=preg_replace('/\[url\](.*)\[\/url\]/Ui','<a href="\1"'.$_nofo.'>\1</a>',$_text);
		$_text=preg_replace('/\[url=(.*)\](.*)\[\/url\]/Ui','<a href="\1"'.$_nofo.'>\2</a>',$_text);
 
		/* [email][/email] [email=][/email]*/
		$_text=preg_replace('/\[email\](.*)\[\/email\]/Ui','<a href="mailto:\1">\1</a>',$_text);
		$_text=preg_replace('/\[email=(.*)\](.*)\[\/email\]/Ui','<a href="mailto:\1">\2</a>',$_text);
 
		/* [img][/img] */
		if (!($this->getOption('ImageLinks') == 'no' && $_type=='comment')) {
			$_text=preg_replace('/\[img\](.*)\[\/img\]/Ui','<img src="\1" />',$_text);
		}
 
		/* [youtube][/youtube] */
		if (!($this->getOption('YoutubeLinks') == 'no' && $_type=='comment')) {
			$_text=preg_replace_callback('/\[youtube\](.*)\[\/youtube\]/Ui',array($this,'youtube_code'),$_text);
		}
 
		return $_text;
	}
 
	function DeleteLinks ($_text) {
		// disable core links
		return preg_replace('/\<a href=\"(.*)\" rel=\"nofollow\">(.*)\<\/a\>/i','\1',$_text);
	}
 
	function AddLinks ($_text, $_nofo='') {
		// add links
		return preg_replace('/(\s)([http|https|ftp|file]+:\/\/[a-zA-Z0-9_?=&%;+-.\/]*)/si','\1<a href="\2"'.$_nofo.'>\2</a>',$_text);
	}
 
	function event_PreItem($_data) {
		if ($this->getOption('bbcode_items')=='yes') {
			$_data[item]->body = $this->Treatment($_data[item]->body, '', 'item');
			$_data[item]->more = $this->Treatment($_data[item]->more, '', 'item');
		}
	}
 
	function event_PreComment($_data) {
		if ($this->getOption('bbcode_comments')=='yes') {
			if ($this->getOption('nofollow')=='yes'){ $nofo=' rel="nofollow"'; }
			$_data['comment']['body'] = $this->DeleteLinks($_data['comment']['body']);
			$_data['comment']['body'] = $this->Treatment($_data['comment']['body'], $nofo, 'comment');
			$_data['comment']['body'] = $this->AddLinks($_data['comment']['body'], $nofo);
		}
	}
 
	function supportsFeature ($what) {
		switch ($what)
		{
			case 'SqlTablePrefix':
				return 1;
			default:
				return 0;
		}
	}
}
?>
np_bbcode_code.txt · Last modified: 2009/01/03 14:50 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki