Location: PHPKode > projects > WB News > includes/Webcode.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 7th Feb 2007                            #||
||#     Filename: Webcode.php                            #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: Webcode.php,v 1.1.2.2 2008/06/29 05:54:43 pmcilwaine Exp $
 */

class Webcode
{

	var $string = "";
	var $stringLimit = FALSE;
	
	var $config = array();
	var $emoticons = array();

	var $custom_bbcode = array();
	
	function Webcode( &$string="", $stringLimit=FALSE )
	{
		$this->string =& $string;
		$this->stringLimit = $stringLimit;

		/** Load Webcode Configuration **/
		$this->Load();

		if ( "" != $string )
		{
			$this->ParseText();
		}

		return;
	}

	function GetWebcode()
	{
		return $this->string;
	}

	function ParseText( &$string="", $stringLimit=FALSE )
	{
		if ( "" != $string )
		{
			$this->string =& $string;
			$this->stringLimit = $stringLimit;
		}

		/** filter bad words in string **/
// 		$this->FilterWords();
		/** HTMLDecode **/
		$this->HTMLDecode();
		/** parse new lines **/
		$this->NewLines();
		/** find & parse lists **/
		$this->ParseLists();
		/** parse paragraphs **/
		$this->ParseParagraph();
		/** BBcode parsing **/
		$this->ParseBBCode();
		$this->ParseCustomBBCode();
		/** parse emoticons **/
		$this->ParseEmoticons();
		/** Wrap text **/
// 		$this->WrapText();

		return;
	}

	/**
	 *	Load Configuration
	 */
	function Load()
	{
		global $config, $DB;
		$this->config =& $config;

		/** load emoticons **/
		$this->emoticons = $DB->ListBy( TBL_EMOTICON, NULL, array("code","image", "name") );
		
		include_once( "bbcode_plugins.php" );
		if ( count($bbcode) == count($format) && count($bbcode) > 0 )
		{
			foreach ( $bbcode as $key => $value )
			{
				$this->custom_bbcode[$key] = array(
					$value,
					$format[$key]
					);
			}
		}
	}

	function HTMLDecode()
	{
		if (function_exists('html_entity_decode'))
		{
			$this->string = html_entity_decode( $this->string );
    	}
		
    	$this->string = str_replace( array('<', '>'), array('&lt;', '&gt;'), $this->string );
	}

	function NewLines()
	{
		$this->string = nl2br( $this->string );
	}

	function ParseParagraph()
	{
		$this->string = preg_replace( "/(.+?)<br \/>\s*<br \/>/im", "<p>$1</p>", $this->string );
		if ( !preg_match( "/^<p>.+?<\\/p>/im", $this->string ) )
		{
			$this->string = "<p>" . $this->string . "</p>";
		}
	}

	function ParseLists()
	{
		if ( preg_match("/\[list\]<br \/>\\n(.+?)\[\/list\]/ism", $this->string ) )
		{
			$this->string = preg_replace("/\[list\]<br \/>\\n(.+?)\[\/list\]/ism", "<ul>\\\n$1</ul>\r\n", $this->string );
		}
		
		if ( preg_match( "/\[list=numbered\]<br \/>\\n(.+?)\[\/list\]/ism", $this->string ) )
		{
			$this->string = preg_replace("/\[list=numbered\]<br \/>\\n(.+?)\[\/list\]/ism", "<ol>\r\n$1</ol>\r\n", $this->string );
		}
		
		if ( preg_match( "/\[\*\](.+?)<br \/>\\n/ism", $this->string ) )
		{
			$this->string = preg_replace("/\[\*\](.+?)<br \/>\\n/ism", "<li>$1</li>\r\n", $this->string );
		}
	}

	function ParseBBCode()
	{
		$bbcode = array(
			"B" => array(
				"/(\[b\])(.+?)(\[\/b\])/is",
				"<strong>$2</strong>"
				),
			"U" => array(
				"/(\[u\])(.+?)(\[\/u\])/is",
					"<u>$2</u>",
				),
			"I" => array(
				"/(\[i\])(.+?)(\[\/i\])/is",
				"<em>$2</em>"
				),
			"L" => array(
				"/\[url=(.+?)\](.+?)\[\/url\]/ise",
				"\$this->sanitizeBBcode('$1', '$2', '<a href=\"%s\">%s</a>')"
				),
			"L2" => array(
				"/(\[url])(.+?)(\[\/url\])/is",
				'<a href="$2">$2</a>'
				),
			"T" => array(
				"/\[type=(.+?)\](.+?)\[\/type\]/ise",
				"\$this->sanitizeBBcode('$1', '$2', '<span style=\"font-family: %s;\">%s</span>')"
				),
			"S" => array(
				"/\[size=(.+?)\](.+?)\[\/size\]/ise",
				"\$this->sanitizeBBcode('$1', '$2', '<span style=\"font-size: %sem;\">%s</span>')"
				),
			"C" => array(
				"/\[color=(.+?)\](.+?)\[\/color\]/ise",
				"\$this->sanitizeBBcode('$1', '$2', '<span style=\"color: %s;\">%s</span>')"
				),
			"A" => array(
				"/(\[align=(.+?)\])(.+?)(\[\/align\])/is",
				$this->TextAlign('$3', '$2'),
				),
			"P" => array(
				"#\[php\](.+?)\[/php\]#msie",
				"\$this->HightlightPHP('$1')"
				)
			);

		foreach ( $bbcode as $code )
		{
			$this->string = preg_replace( $code[0], $code[1], $this->string );
		}
	}

	/**
	 *  Sanitizes BBcode Tags
	 *
	 *  @param $attr    string  BBcode attribute
	 *  @param $value   string  The value to be seen by the public <div>test</div> test would be the value
	 *  @param $sprintf string  To be parsed of the attribute and value
	 *	@since 1.2.2
	 */
	function SanitizeBBcode( $attr, $value, $sprintf )
	{
		$attr = str_replace( array( "\r\n", "\r", "\n", "javascript:" ), "", $attr );
		return sprintf( $sprintf, $attr, $value );
	}
	
	/**
	 *  Returns a string parsed for text alignment
	 *
	 *  @param $string string - Text to be aligned
	 *  @param $align string - String Alignment either left, center, right
	 *  @return string
	 */
	function TextAlign( $string, $align )
	{
		return "<div style=\"text-align:" . $align . ";\">$string</div>";
	}

	/** TODO
		- make sure Emoticons dont get displayed
		- fix newline characters
		- make sure we do not get start tags e.g. <p>
		**/
	function HightlightPHP( $php )
	{
		$php = str_replace( array( "&lt;", "&gt;", "<br />" ), array( '<', '>', '', "", "" ), $php );
		$php = stripslashes( $php );
		$php = html_entity_decode( $php );
		$php = trim($php);

		if ( preg_match( "/^\\s*<\\?/si", $php ) )
		{
			$php = highlight_string( $php, TRUE );
		}
		else
		{
			$php = highlight_string( "<?php\n$php", TRUE );
		}
		
		$php = str_replace( "\r", "", $php );

		return $php;
	}

	function ParseCustomBBCode()
	{
		if ( !$this->custom_bbcode )
		{
			return;
		}

		foreach ( $this->custom_bbcode as $custom_bbcode )
		{
			$this->string = preg_replace( $custom_bbcode[0], $custom_bbcode[1], $this->string );
		}
	}

	function ParseEmoticons()
	{
		if ( !$this->emoticons )
		{
			return ;
		}

		foreach ( $this->emoticons as $emoticon )
		{
			$this->string = str_replace( $emoticon["code"], "<img src=\"" . $emoticon["image"] . "\" alt=\"" . $emoticon["name"] . "\"/>", $this->string );
		}
	}

	function WrapText()
	{
		$width = 80; /** TODO remove this should be in LoadConfig **/
		$this->string = preg_replace( "/([^\\s]{". $width ."})/i", "$1" . LINE_BREAK, $this->string);
	}

}

return;

?>
Return current item: WB News