HEX
Server: Apache/2.4.59 (Debian)
System: Linux skycube.cz 4.19.0-25-amd64 #1 SMP Debian 4.19.289-2 (2023-08-08) x86_64
User: ilya (534)
PHP: 7.3.31-1~deb10u7
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: /var/www/ilya/data/www/kamforum.ru/sources/classes/bbcode/class_bbcode_core.php
<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board v2.1.5
|   =============================================
|   by Matthew Mecham
|   (c) 2001 - 2005 Invision Power Services, Inc.
|   http://www.invisionpower.com
|   =============================================
|   Web: http://www.invisionboard.com
|   Time: Tue, 18 Oct 2005 19:30:11 GMT
|   Release: 95f5a3c9ea538e4ebb097b9464fc22d2
|   Licence Info: http://www.invisionboard.com/?license
+---------------------------------------------------------------------------
|   > $Date: 2005-10-10 14:03:20 +0100 (Mon, 10 Oct 2005) $
|   > $Revision: 22 $
|   > $Author: matt $
+---------------------------------------------------------------------------
|
|   > BB Code Core Module
|   > Module written by Matt Mecham
|   > Date started: Wednesday 9th March 2005 11:27
|
+--------------------------------------------------------------------------
*/

/**
* BBCode Parsing Core Class
*
* This class contains all the main class functions
* EXAMPLE USAGE
* <code>
* $parser           =  new parse_bbcode();
* $parser->ipsclass =& $this->ipsclass;
* 
* # If you wish convert posted text into BBCode
* $parser->parse_smilies = 1;
* $parser->parse_bbcode  = 1;
* 
* $bbcode_text = $parser->pre_db_parse( $_POST['text'] );
* 
* # If you wish to display this parsed BBCode, we've still got
* # to parse HTML (where allowed) and parse user defined BBCode.
* $parser->parse_html    = 0;
* $parser->parse_nl2br   = 1;
* $ready_to_print        = $parser->pre_display_parse(  $bbcode_text  );
* 
* # Sometimes, you may wish to just save the raw POST text and convert on-the-fly.
* # IPB does this with private messages, calendar events and announcements. In this case, you'd use the following:
* $parser->parse_html    = 0;
* $parser->parse_nl2br   = 1;
* $parser->parse_smilies = 1;
* $parser->parse_bbcode  = 1;
* $bbcode_text           = $parser->pre_db_parse( $_POST['text'] );
* $ready_to_print        = $parser->pre_display_parse( $bbcode_text );
* 
* # If you wish to convert already converted BBCode back into the raw format
* # (for use in an editing screen, for example) use this:
* $raw_post = $parser->pre_edit_parse( $parsed_text );
* 
* # Of course, if you're using the rich text editor (WYSIWYG) then you don't want to uncovert the HTML
* # otherwise the rich text editor will show unparsed BBCode tags, and not formatted HTML. In this case use this:
* $raw_post = $parser->convert_ipb_html_to_html( $parsed_text );
* </code>
*
* @package		InvisionPowerBoard
* @subpackage	BBCodeParser
* @author		Matt Mecham
* @copyright	Invision Power Services, Inc.
* @version		2.1
*/

/**
*
*/

/**
* BBCode Parsing Core Class
*
* Main object class
*
* @package		InvisionPowerBoard
* @subpackage	BBCodeParser
* @author  	 	Matt Mecham
* @version		2.1
* @since		2.1.0
*/
class class_bbcode_core
{
	/**
	* IPS Class Object
	*
	* @var object
	*/
	var $ipsclass;
	
	/**#@+
	* User defined setting
	* @var integer 
	*/
	var $parse_smilies    = 0;
	var $parse_html       = 0;
	var $parse_bbcode     = 0;
	var $parse_wordwrap   = 0;
	var $parse_nl2br      = 1;
	var $strip_quotes     = 0;
	var $allow_unicode    = 1;
	var $bypass_badwords  = 0;
	var $load_custom_tags = 0;
	var $max_embed_quotes = 15;
	
	// Do not change this unless you have a VERY good reason...
	var $strip_hex_entity = 1;
	/**#@-*/
	
	/**#@+
	* Internally defined setting
	* @var integer 
	*/
	var $image_count      = 0;
	var $emoticon_count   = 0;
	var $quote_open       = 0;
	var $quote_closed     = 0;
	var $quote_error      = 0;
	/**#@-*/
	
	/**#@+
	* Internally defined setting
	* @var string 
	*/
	var $error            = "";
	var $emoticons        = "";
	var $badwords         = "";
	var $in_sig           = "";
	/**#@-*/
	
	/**#@+
	* Internally defined array
	* @var string 
	*/
	var $quote_html     = array();
	var $rev_font_sizes = array();
	var $font_sizes     = array( 1 => '8',
								 2 => '10',
								 3 => '12',
								 4 => '14',
								 5 => '18',
								 6 => '24',
								 7 => '36' );
	/**#@-*/
	
	/*-------------------------------------------------------------------------*/
	// Global init
	/*-------------------------------------------------------------------------*/
	
	/**
	* Builds up font arrays
	*
	* @return void;
	*/
	function global_init()
	{
		//-------------------------------
		// Remap font sizes
		//-------------------------------
		
		foreach( $this->font_sizes as $bbcode => $real )
		{
			$this->rev_font_sizes[ $bbcode ] = $real;
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// Get real font size
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert pt size to BBCode size
	*
	* @param	integer	Real size
	* @return   integer BBCode size
	*/
	function convert_realsize_to_bbsize( $real )
	{
		$real = intval( $real );
		
		if ( $this->rev_font_sizes[ $real ] )
		{
			return $this->rev_font_sizes[ $real ];
		}
		else
		{
			return 3;
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// Get BBcode font size
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert BBCode size to px size
	*
	* @param	integer BBCode size
	* @return	integer Real size
	*/
	function convert_bbsize_to_realsize( $bb )
	{
		$bb = intval( $bb );
		
		if ( $this->font_sizes[ $bb ] )
		{
			return $this->font_sizes[ $bb ];
		}
		else
		{
			return 12;
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// Clean up IPB html
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert special IPB HTML to normal HTML
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function clean_ipb_html( $t="" )
	{
		//-----------------------------------------
		// left, right, center
		//-----------------------------------------
		
		$t = preg_replace( "#\[(left|right|center)\](.+?)\[/\\1\]#is"  , "<div align=\"\\1\">\\2</div>", $t );
		
		//-----------------------------------------
		// Indent => Block quote
		//-----------------------------------------
		
		while( preg_match( "#\[indent\](.+?)\[/indent\]#is" , $t ) )
		{
			$t = preg_replace( "#\[indent\](.+?)\[/indent\]#is"  , "<blockquote>\\1</blockquote>", $t );
		}
		
		//-----------------------------------------
		// Quotes
		//-----------------------------------------
		
		$t = preg_replace( "#<!--QuoteBegin-->(.+?)<!--QuoteEBegin-->#"                        , '[quote]'         , $t );
		$t = preg_replace( "#<!--QuoteBegin-{1,2}([^>]+?)\+([^>]+?)-->(.+?)<!--QuoteEBegin-->#", "[quote=\\1,\\2]" , $t );
		$t = preg_replace( "#<!--QuoteBegin-{1,2}([^>]+?)\+-->(.+?)<!--QuoteEBegin-->#"        , "[quote=\\1]"     , $t );
		$t = preg_replace( "#<!--QuoteEnd-->(.+?)<!--QuoteEEnd-->#"                            , '[/quote]'        , $t );
		
		//-----------------------------------------
		// New quote
		//-----------------------------------------
		
		$t = preg_replace( "#<!--quoteo([^>]+?)?-->(.+?)<!--quotec-->#sie", "\$this->_parse_new_quote('\\1', '\\2' )" , $t );
			
		//-----------------------------------------
		// SQL
		//-----------------------------------------
		
		$t = preg_replace( "#<!--sql-->(.+?)<!--sql1-->(.+?)<!--sql2-->(.+?)<!--sql3-->#eis", "\$this->unconvert_sql(\"\\2\")", $t);
		
		//-----------------------------------------
		// HTML
		//-----------------------------------------
		
		$t = preg_replace( "#<!--html-->(.+?)<!--html1-->(.+?)<!--html2-->(.+?)<!--html3-->#e", "\$this->unconvert_htm(\"\\2\")", $t);
		
		//-----------------------------------------
		// CODE
		//-----------------------------------------
		
		$t = preg_replace( "#<!--c1-->(.+?)<!--ec1-->#", '[code]' , $t );
		$t = preg_replace( "#<!--c2-->(.+?)<!--ec2-->#", '[/code]', $t );
			
		//-----------------------------------------
		// Remove all comments
		//-----------------------------------------
		
		# Leave this to the editor to show?
		# If we strip comments here, the <!--size(..)--> tags won't be converted back
		//$t = preg_replace( "#\<\!\-\-(.+?)\-\-\>#is", "", $t );
		
		$t = str_replace( '&#39;'  , "'", $t );
		$t = str_replace( '&#33;'  , "!", $t );
		$t = str_replace( '&#039;' , "'", $t );
		$t = str_replace( '&apos;' , "'", $t );
		
		//-----------------------------------------
		// Clean up nbsp
		//-----------------------------------------
		
		//$t = str_replace( '&nbsp;&nbsp;&nbsp;&nbsp;', "\t", $t );
		//$t = str_replace( '&nbsp;&nbsp;'            , "  ", $t );
		
		//-----------------------------------------
		// Remove snap back macro
		//-----------------------------------------
		
		$t = preg_replace("#<a href=['\"]index.php?act=findpost&(amp;)?pid=.+?['\"]><\{.+?\}></a>#", "", $t );
		
		//-----------------------------------------
		// Remove all macros
		//-----------------------------------------
		
		$t = preg_replace( "#<\{.+?\}>#", "", $t );
		
		return $t;
	}
	
	/*-------------------------------------------------------------------------*/
	// Strip quote tags
	/*-------------------------------------------------------------------------*/
	
	/**
	* Remove quote tags
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function strip_quote_tags( $txt="" )
	{
		return preg_replace( "#\[QUOTE(=.+?,.+?)?\].+?\[/QUOTE\]#is", "", $txt );
	}
	
	/*-------------------------------------------------------------------------*/
	// strip all tags
	/*-------------------------------------------------------------------------*/
	
	/**
	* Remove ALL tags
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function strip_all_tags( $txt="" )
	{
		$txt = $this->strip_quote_tags( $this->pre_edit_parse( $txt ) );
		
		while( preg_match( "#\[.+?\](.+?)\[/.+?\]#is", $txt ) )
		{
			$txt = preg_replace( "#\[.+?\](.+?)\[/.+?\]#is", "\\1", $txt );
		}
		
		$txt = preg_replace( "#\[attach.+?\]#is"       , ""   , $txt );
		
		return $txt;
	}
	
	/*-------------------------------------------------------------------------*/
	// strip all tags to formatted HTML
	/*-------------------------------------------------------------------------*/
	
	/**
	* Remove all tags, but format neatly
	*
	* @param	string	Raw text
	* @return	string	Converted text
	* @deprecated 2.1.0
	*/
	function strip_all_tags_to_formatted( $txt="" )
	{
		//$txt = $this->strip_quote_tags( $this->unconvert( $txt ) );
		//$txt = preg_replace( "#\[CODE\](.+?)\[/CODE\]#is", "<pre>\\1</pre>", $txt );
		//$txt = preg_replace( "#\[LIST\](.+?)\[/LIST\]#eis", "'<ul>' .str_replace( '[*]', '<li>', nl2br('\\1') ).'</ul>';", $txt );
		//$txt = preg_replace( "#\[LIST=.+?\](.+?)\[/LIST\]#eis", "'<ul>' .str_replace( '[*]', '<li>', nl2br('\\1') ).'</ul>';", $txt );
		//$txt = preg_replace( "#\[.+?\](.+?)\[/.+?\]#is", "\\1", $txt );
		
		return $txt;
	}
	
	/*-------------------------------------------------------------------------*/
	// PARSE POLL TAGS
	// Converts certain code tags for polling
	/*-------------------------------------------------------------------------*/
	
	/**
	* Parse poll tags
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function parse_poll_tags($txt)
	{
		$txt = preg_replace( "#\[img\](.+?)\[/img\]#ie" , "\$this->regex_check_image('\\1')", $txt );
		$txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie"                                       , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\1'))", $txt );
		$txt = preg_replace( "#\[url\s*=\s*\&quot\;\s*(\S+?)\s*\&quot\;\s*\](.*?)\[\/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
		$txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie"                       , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
	
		return $txt;
	}
	
	/*-------------------------------------------------------------------------*/
	// My strip-tags. Converts HTML entities back before strippin' em
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert HTML entities before stripping them
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function my_strip_tags($t="")
	{
		$t = str_replace( '&gt;', '>', $t );
		$t = str_replace( '&lt;', '<', $t );
		
		$t = strip_tags($t);
		
		//-----------------------------------------
		// Make sure nothing naughty is left...
		//-----------------------------------------
		
		$t = str_replace( '<', '&lt;', $t );
		$t = str_replace( '>', '&gt;', $t );
		
		return $t;
	}
	
	/*-------------------------------------------------------------------------*/
	// Checks opening and closing bbtags - never pre-parsed
	/*-------------------------------------------------------------------------*/
	
	/**
	* Check for custom BBcode tags
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function bbcode_check($t="")
	{
		$count = array();
		
		foreach( $this->ipsclass->cache['bbcode'] as $i => $r )
		{
			if ( $r['bbcode_useoption'] )
			{
				$count[ $r['bbcode_id'] ]['open']      = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).'=' );
				$count[ $r['bbcode_id'] ]['wrongopen'] = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).']' );
			}
			else
			{
				$count[ $r['bbcode_id'] ]['open']      = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).']' );
				$count[ $r['bbcode_id'] ]['wrongopen'] = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).'=' );
			}
			
			$count[ $r['bbcode_id'] ]['closed'] = substr_count( strtolower($t), '[/'.strtolower($r['bbcode_tag']).']' );
			
			//-----------------------------------------
			// check...
			//-----------------------------------------
			
			if ( $count[ $r['bbcode_id'] ]['open'] != $count[ $r['bbcode_id'] ]['closed'] )
			{
				if ( $count[ $r['bbcode_id'] ]['wrongopen'] == $count[ $r['bbcode_id'] ]['closed'] )
				{
					$this->error = 'custom_tags_incorrect2';
				}
				else
				{
					$this->error = 'custom_tags_incorrect';
				}
			}
		}
		
		return $t;
	}
	
	/*-------------------------------------------------------------------------*/
	// Post DB parse BBCode
	/*-------------------------------------------------------------------------*/
	
	/**
	* Pre-display parse custom BBCode
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function post_db_parse_bbcode($t="")
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$snapback = 0;
		
		//-----------------------------------------
		// Check...
		//-----------------------------------------
		
		if ( is_array( $this->ipsclass->cache['bbcode'] ) and count( $this->ipsclass->cache['bbcode'] ) )
		{
			foreach( $this->ipsclass->cache['bbcode'] as $i => $row )
			{
				if ( strtolower($row['bbcode_tag']) == 'snapback' )
				{
					$snapback = 1;
				}
				
				$preg_tag = preg_quote($row['bbcode_tag'], '#' );
				
				if ( substr_count( $row['bbcode_replace'], '{content}' ) >= 1 )
				{
					//-----------------------------------------
					// Slightly slower
					//-----------------------------------------
					
					if ( $row['bbcode_useoption'] )
					{
						preg_match_all( "#(\[".preg_quote($row['bbcode_tag'], '#' )."=(?:&quot;|&\#39;)?(.+?)(?:&quot;|&\#39;)?\])(.+?)(\[/".preg_quote($row['bbcode_tag'], '#' )."\])#si", $t, $match );
						
						for ($i=0; $i < count($match[0]); $i++)
						{
							# XSS Check: Bug ID: 980
							if ( $row['bbcode_tag'] == 'post' OR $row['bbcode_tag'] == 'topic' )
							{
								$match[2][$i] = intval( $match[2][$i] );
							}
							
							$tmp = $row['bbcode_replace'];
							$tmp = str_replace( '{option}' , $match[2][$i], $tmp );
							$tmp = str_replace( '{content}', $match[3][$i], $tmp );
							$t   = str_replace( $match[0][$i], $tmp, $t );
						}
					}
					else
					{
						# Tricky.. match anything that's not a closing tag, or nothing
						preg_match_all( "#(\[$preg_tag\])((?!\[/$preg_tag\]).+?)?(\[/$preg_tag\])#si", $t, $match );
						
						for ($i=0; $i < count($match[0]); $i++)
						{
							# XSS Check: Bug ID: 980
							if ( $row['bbcode_tag'] == 'post' OR $row['bbcode_tag'] == 'topic' )
							{
								$match[2][$i] = intval( $match[2][$i] );
							}
							
							$tmp = $row['bbcode_replace'];
							$tmp = str_replace( '{content}', $match[2][$i], $tmp );
							$t   = str_replace( $match[0][$i], $tmp, $t );
						}
					}
				}
				else
				{
					$replace = explode( '{content}', $row['bbcode_replace'] );
					
					if ( $row['bbcode_useoption'] )
					{
						$t = preg_replace( "#\[".$row['bbcode_tag']."=(?:&quot;|&\#39;)?(.+?)(?:&quot;|&\#39;)?\]#si", str_replace( '{option}', "\\1", $replace[0] ), $t );
					}
					else
					{
						$t = preg_replace( '#\['.$row['bbcode_tag'].'\]#i' , $replace[0], $t );
					}
					
					$t = preg_replace( '#\[/'.$row['bbcode_tag'].'\]#i', $replace[1], $t );
				}
			}
		}
		
		//-----------------------------------------
		// Snapback used?
		//-----------------------------------------
		
		if ( ! $snapback )
		{
			$t = preg_replace( "#\[snapback\](\d+?)\[/snapback\]is#", "<a href='index.php?act=findpost&amp;pid=\\1'><{POST_SNAPBACK}></a>", $t );
		}
		
		return $t;
	}
	
	//-----------------------------------------
	// Word wrap, wraps 'da word innit
	//-----------------------------------------
	
	/**
	* Custom word wrap
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function my_wordwrap($t="", $chrs=0, $replace="<br />")
	{
		if ( $t == "" )
		{
			return $t;
		}
		
		if ( $chrs < 1 )
		{
			return $t;
		}
		
		$t = preg_replace("#([^\s<>'\"/\.\\-\?&\n\r\%]{".$chrs."})#i", " \\1".$replace ,$t);
		
		return $t;
	}
	
	//-----------------------------------------
	// parse_html
	// Converts the doHTML tag
	//-----------------------------------------
	
	/**
	* Pre-display convert HTML entities for use
	* when HTML is enabled
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function post_db_parse_html($t="")
	{
		if ( $t == "" )
		{
			return $t;
		}
		
		//-----------------------------------------
		// Remove <br>s 'cos we know they can't
		// be user inputted, 'cos they are still
		// &lt;br&gt; at this point :)
		//-----------------------------------------
		
		if ( $this->parse_nl2br != 1 )
		{
			$t = str_replace( "<br>"    , "\n" , $t );
			$t = str_replace( "<br />"  , "\n" , $t );
		}
		
		$t = str_replace( "&#39;"   , "'", $t );
		$t = str_replace( "&#33;"   , "!", $t );
		$t = str_replace( "&#036;"   , "$", $t );
		$t = str_replace( "&#124;"  , "|", $t );
		$t = str_replace( "&amp;"   , "&", $t );
		$t = str_replace( "&gt;"    , ">", $t );
		$t = str_replace( "&lt;"    , "<", $t );
		$t = str_replace( "&quot;"  , '"', $t );
		
		//-----------------------------------------
		// Take a crack at parsing some of the nasties
		// NOTE: THIS IS NOT DESIGNED AS A FOOLPROOF METHOD
		// AND SHOULD NOT BE RELIED UPON!
		//-----------------------------------------
		
		$t = preg_replace( "/javascript/i" , "j&#097;v&#097;script", $t );
		$t = preg_replace( "/alert/i"      , "&#097;lert"          , $t );
		$t = preg_replace( "/about:/i"     , "&#097;bout:"         , $t );
		$t = preg_replace( "/onmouseover/i", "&#111;nmouseover"    , $t );
		$t = preg_replace( "/onclick/i"    , "&#111;nclick"        , $t );
		$t = preg_replace( "/onload/i"     , "&#111;nload"         , $t );
		$t = preg_replace( "/onsubmit/i"   , "&#111;nsubmit"       , $t );
		
		return $t;
	}
	
	//-----------------------------------------
	// Badwords:
	// Swops naughty, naugty words and stuff
	//-----------------------------------------
	
	/**
	* Replace bad words
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function bad_words($text = "")
	{
		if ($text == "")
		{
			return "";
		}
		
		if ( $this->bypass_badwords == 1 )
		{
			return $text;
		}
		
		//-----------------------------------------
		// Go all loopy
		//-----------------------------------------
		
		if ( is_array( $this->ipsclass->cache['badwords'] ) )
		{
			usort( $this->ipsclass->cache['badwords'] , array( 'class_bbcode_core', 'word_length_sort' ) );
			
			if ( count($this->ipsclass->cache['badwords']) > 0 )
			{
				foreach($this->ipsclass->cache['badwords'] as $idx => $r)
				{
				
					if ($r['swop'] == "")
					{
						$replace = '######';
					}
					else
					{
						$replace = $r['swop'];
					}
					
					$r['type'] = preg_quote($r['type'], "/");
					
					if ($r['m_exact'] == 1)
					{
						$text = preg_replace( "/(^|\b)".$r['type']."(\b|!|\?|\.|,|$)/i", "$replace", $text );
					}
					else
					{
						$text = preg_replace( "/".$r['type']."/i", "$replace", $text );
					}
				}
			}
		}
		
		return $text;
	}
	
	/*-------------------------------------------------------------------------*/
	// wrap style: code and quote table HTML generator
	/*-------------------------------------------------------------------------*/
	
	/**
	* Wrap quote / code / html / sql divs
	*
	* @param	string	Type
	* @param	string	Extra vars
	* @return	array	Converted text
	*/
	function wrap_style( $type='quote', $extra="" )
	{
		$used = array(
					   'quote' => array( 'title' => "{$this->ipsclass->lang['bbcode_wrap_quote']}", 'css_top' => 'quotetop' , 'css_main' => 'quotemain' ),
					   'code'  => array( 'title' => "{$this->ipsclass->lang['bbcode_wrap_code']}" , 'css_top' => 'codetop'  , 'css_main' => 'codemain'  ),
					   'sql'   => array( 'title' => "{$this->ipsclass->lang['bbcode_wrap_sql']}"  , 'css_top' => 'sqltop'   , 'css_main' => 'sqlmain'   ),
					   'html'  => array( 'title' => "{$this->ipsclass->lang['bbcode_wrap_html']}" , 'css_top' => 'htmltop'  , 'css_main' => 'htmlmain'  )
					 );
		
		$this->wrap_top    = "<div class='{$used[ $type ]['css_top']}'>{$used[ $type ]['title']}{$extra}</div><div class='{$used[ $type ]['css_main']}'>";
	    $this->wrap_bottom = "</div>";
			
		/*if ( ! $this->wrap_top )
		{
			
		
			if ( $this->ipsclass->compiled_templates['skin_global'] )
			{
				$this->wrap_top    = $this->ipsclass->compiled_templates['skin_global']->bbcode_wrap_start();
				$this->wrap_bottom = $this->ipsclass->compiled_templates['skin_global']->bbcode_wrap_end();
				
				$this->wrap_top = str_replace( '<!--css.top-->' , "{$used[ $type ]['css_top']}" , $this->wrap_top );
				$this->wrap_top = str_replace( '<!--css.main-->', "{$used[ $type ]['css_main']}", $this->wrap_top );
				$this->wrap_top = str_replace( '<!--title-->'   , "{$used[ $type ]['title']}"   , $this->wrap_top );
				$this->wrap_top = str_replace( '<!--extra-->'   , $extra                        , $this->wrap_top );
			}
		}*/
		
		return array( 'START' => $this->wrap_top, 'END' => $this->wrap_bottom );
	}

	/*-------------------------------------------------------------------------*/
	// regex_html_tag: HTML syntax highlighting
	/*-------------------------------------------------------------------------*/
	
	/**
	* Custom HTML syntax highlighting
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function regex_html_tag($html="")
	{
		if ($html == "")
		{
			return;
		}
		
		//-----------------------------------------
		// Take a stab at removing most of the common
		// smilie characters.
		//-----------------------------------------
		
		$html = str_replace( ":"     , "&#58;", $html );
		$html = str_replace( "["     , "&#91;", $html );
		$html = str_replace( "]"     , "&#93;", $html );
		$html = str_replace( ")"     , "&#41;", $html );
		$html = str_replace( "("     , "&#40;", $html );
		$html = str_replace( "{"	 , "&#123;", $html );
		$html = str_replace( "}"	 , "&#125;", $html );
		$html = str_replace( "$"	 , "&#36;", $html );
		
		$html = preg_replace( "/^<br>/"  , "", $html );
		$html = preg_replace( "#^<br />#", "", $html );
		$html = preg_replace( "/^\s+/"   , "", $html );
		
		$html = preg_replace( "#&lt;([^&<>]+)&gt;#"                            , "&lt;<span style='color:blue'>\\1</span>&gt;"        , $html );   //Matches <tag>
		$html = preg_replace( "#&lt;([^&<>]+)=#"                               , "&lt;<span style='color:blue'>\\1</span>="           , $html );   //Matches <tag
		$html = preg_replace( "#&lt;/([^&]+)&gt;#"                             , "&lt;/<span style='color:blue'>\\1</span>&gt;"       , $html );   //Matches </tag>
		$html = preg_replace( "!=(&quot;|&#39;)(.+?)?(&quot;|&#39;)(\s|&gt;)!" , "=\\1<span style='color:orange'>\\2</span>\\3\\4"    , $html );   //Matches ='this'
		$html = preg_replace( "!&#60;&#33;--(.+?)--&#62;!"                     , "&lt;&#33;<span style='color:red'>--\\1--</span>&gt;", $html );
		
		$wrap = $this->wrap_style( 'html' );
		
		return "<!--html-->{$wrap['START']}<!--html1-->$html<!--html2-->{$wrap['END']}<!--html3-->";
	}
	
	/*-------------------------------------------------------------------------*/
	// regex_sql_tag: SQL syntax highlighting
	/*-------------------------------------------------------------------------*/
	
	/**
	* Custom SQL syntax highlighting
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function regex_sql_tag($sql="")
	{
		if ($sql == "")
		{
			return;
		}
		
		//-----------------------------------------	
		// Knock off any preceeding newlines (which have
		// since been converted into <br>)
		//-----------------------------------------
		
		$sql = preg_replace( "/^<br>/"  , "", $sql );
		$sql = preg_replace( "#^<br />#", "", $sql );
		$sql = preg_replace( "/^\s+/"   , "", $sql );
		
		//-----------------------------------------
		// Make certain regex work..
		//-----------------------------------------
		
		if (! preg_match( "/\s+$/" , $sql) )
		{
			$sql = $sql.' ';
		}
		
		$sql = preg_replace( "#(=|\+|\-|&gt;|&lt;|~|==|\!=|LIKE|NOT LIKE|REGEXP)#i"            , "<span style='color:orange'>\\1</span>", $sql );
		$sql = preg_replace( "#(MAX|AVG|SUM|COUNT|MIN)\(#i"                                    , "<span style='color:blue'>\\1</span>("    , $sql );
		$sql = preg_replace( "#(FROM|INTO)\s{1,}(\S+?)\s{1,}#i"                                , "<span style='color:green'>\\1</span> <span style='color:orange'>\\2</span> ", $sql );
	    $sql = preg_replace( "!(&quot;|&#39;|&#039;)(.+?)(&quot;|&#39;|&#039;)!i"              , "<span style='color:red'>\\1\\2\\3</span>" , $sql );
	    $sql = preg_replace( "#\s{1,}(AND|OR)\s{1,}#i"                                         , " <span style='color:blue'>\\1</span> "    , $sql );
	    $sql = preg_replace( "#(LEFT|JOIN|WHERE|MODIFY|CHANGE|AS|DISTINCT|IN|ASC|DESC|ORDER BY)\s{1,}#i" , "<span style='color:green'>\\1</span> "   , $sql );
	    $sql = preg_replace( "#LIMIT\s*(\d+)\s*,\s*(\d+)#i"                                    , "<span style='color:green'>LIMIT</span> <span style='color:orange'>\\1, \\2</span>" , $sql );
	    $sql = preg_replace( "#(SELECT|INSERT|UPDATE|DELETE|ALTER TABLE|DROP)#i"               , "<span style='color:blue;font-weight:bold'>\\1</span>" , $sql );
	    
	    $html = $this->wrap_style( 'sql' );
	    
	    return "<!--sql-->{$html['START']}<!--sql1-->{$sql}<!--sql2-->{$html['END']}<!--sql3-->";
	}
	
	/*-------------------------------------------------------------------------*/
	// regex_code_tag: Builds this code tag HTML
	/*-------------------------------------------------------------------------*/
	
	/**
	* Build code tag, make contents safe
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function regex_code_tag($txt="")
	{
		$default = "\[code\]$txt\[/code\]";
		
		if ( $txt == "" )
		{
			return;
		}
		
		//-----------------------------------------
		// Take a stab at removing most of the common
		// smilie characters.
		//-----------------------------------------
		
		//$txt = str_replace( "&" , "&amp;", $txt );
		$txt = str_replace( "&lt;"      , "&#60;" , $txt );
		$txt = str_replace( "&gt;"      , "&#62;" , $txt );
		$txt = str_replace( "&quot;"    , "&#34;" , $txt );
		$txt = str_replace( ":"         , "&#58;" , $txt );
		$txt = str_replace( "["         , "&#91;" , $txt );
		$txt = str_replace( "]"         , "&#93;" , $txt );
		$txt = str_replace( ")"         , "&#41;" , $txt );
		$txt = str_replace( "("         , "&#40;" , $txt );
		$txt = str_replace( "\r"        , "<br />", $txt );
		$txt = str_replace( "\n"        , "<br />", $txt );
		$txt = preg_replace( "#\s{1};#" , "&#59;" , $txt );
		
		//-----------------------------------------
		// Ensure that spacing is preserved
		//-----------------------------------------
		
		$txt = preg_replace( "#\t#"   , "&nbsp;&nbsp;&nbsp;&nbsp;", $txt );
		$txt = preg_replace( "#\s{2}#", "&nbsp;&nbsp;"            , $txt );
		
		$html = $this->wrap_style( 'code' );
		
		return "<!--c1-->{$html['START']}<!--ec1-->$txt<!--c2-->{$html['END']}<!--ec2-->";
	}
	
	/*-------------------------------------------------------------------------*/
	// regex_check_image: Checks, and builds the <img>
	/*-------------------------------------------------------------------------*/
	
	/**
	* Check image URL and return HTML
	*
	* @param	string	URL
	* @return	string	IMG tag HTML
	*/
	function regex_check_image($url="")
	{
		if (! $url )
		{
			return;
		}
		
		$url = trim($url);
		
		$default = "[img]".$url."[/img]";
		
		$this->image_count++;
		
		//-----------------------------------------
		// Make sure we've not overriden the set image # limit
		//-----------------------------------------
		
		if ($this->ipsclass->vars['max_images'])
		{
			if ($this->image_count > $this->ipsclass->vars['max_images'])
			{
				$this->error = 'too_many_img';
				return $default;
			}
		}
		
		//-----------------------------------------
		// XSS check
		//-----------------------------------------
		
		$url = urldecode( $url );
		$url = str_replace( "document.cookie", "", $url );
		
		//-----------------------------------------
		// Are they attempting to post a dynamic image, or JS?
		//-----------------------------------------
		
		if ( $this->ipsclass->vars['allow_dynamic_img'] != 1 )
		{
			if ( preg_match( "/[?&;]/", $url) )
			{
				$this->error = 'no_dynamic';
				return $default;
			}
			
			if ( preg_match( "/java(\s+?)?script(\:|\s)/is", $url ) )
			{
				$this->error = 'no_dynamic';
				return $default;
			}
		}
		
		//-----------------------------------------
		// Is the img extension allowed to be posted?
		//-----------------------------------------
		
		if ( $this->ipsclass->vars['img_ext'] )
		{
			$extension = preg_replace( "#^.*\.(\w+)(\?.*$|$)#", "\\1", $url );
			
			$extension = strtolower($extension);
			
			if ( (! $extension) OR ( preg_match( "#/#", $extension ) ) )
			{
				$this->error = 'invalid_ext';
				return $default;
			}
			
			$this->ipsclass->vars['img_ext'] = strtolower($this->ipsclass->vars['img_ext']);
			
			if ( ! preg_match( "/".preg_quote($extension, '/')."(,|$)/", $this->ipsclass->vars['img_ext'] ))
			{
				$this->error = 'invalid_ext';
				return $default;
			}
		}
		
		//-----------------------------------------
		// Is it a legitimate image?
		//-----------------------------------------
		
		/*if ( ! preg_match( "/^(http|https|ftp):\/\//i", $url ) )
		{
			$this->error = 'no_dynamic';
			return $default;
		}*/
		
		//-----------------------------------------
		// If we are still here....
		//-----------------------------------------
		
		$url = str_replace( " ", "%20", $url );
		
		return "<img src=\"$url\" border=\"0\" alt=\"{$this->ipsclass->lang['bbcode_img_alt']}\" />";
	}
	
    /*-------------------------------------------------------------------------*/
	// regex_font_attr:
	// Returns a string for an /e regexp based on the input
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert size / color / font BBCode tags
	*
	* @param	array	Vars
	* @return	string	Converted text
	*/
	function regex_font_attr( $IN )
	{
		if ( ! is_array($IN) )
		{
			return;
		}
		
		//-----------------------------------------
		// Make safe
		//-----------------------------------------
		
		$IN['1'] = preg_replace( "/[&\(\)\.\%\[\]<>]/", "", preg_replace( "#^(.+?)(?:;|$)#", "\\1", $IN['1'] ) );
		
		//-----------------------------------------
		// Size
		//-----------------------------------------
		
		if ($IN['s'] == 'size')
		{
			$IN['1'] = intval($IN['1']) + 7;
			
			if ($IN['1'] > 30)
			{
				$IN['1'] = 30;
			}
			
			return "<span style='font-size:".$IN['1']."pt;line-height:100%'>".$IN['2']."</span>";
		}
		
		//-----------------------------------------
		// BACKGROUND
		//-----------------------------------------
		
		else if ($IN['s'] == 'background')
		{
			$IN[1] = preg_replace( "/[^\d\w\#\s]/s", "", $IN[1] );
			return "<span style='background-color:".$IN[1]."'>".$IN['2']."</span>";
		}
		
		//-----------------------------------------
		// COLOR
		//-----------------------------------------
		
		else if ($IN['s'] == 'col')
		{
			$IN[1] = preg_replace( "/[^\d\w\#\s]/s", "", $IN[1] );
			return "<span style='color:".$IN[1]."'>".$IN['2']."</span>";
		}
		
		//-----------------------------------------
		// FONT
		//-----------------------------------------
		
		else if ($IN['s'] == 'font')
		{
			$IN['1'] = preg_replace( "/[^\d\w\#\-\_\s]/s", "", $IN['1'] );
			return "<span style='font-family:".$IN['1']."'>".$IN['2']."</span>";
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// regex_list: List generation
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert list BBCode
	*
	* @param	string	Raw text
	* @param	string	List type
	* @return	string	Converted text
	*/
	function regex_list( $txt="", $type="" )
	{
		if ($txt == "")
		{
			return;
		}
		
		if ( $type == "" )
		{
			return "<ul>".$this->regex_list_item($txt)."</ul>";
		}
		else
		{
			return "<ol type='$type'>".$this->regex_list_item($txt)."</ol>";
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// Regex list item
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert list item
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function regex_list_item($txt)
	{
		$txt = preg_replace( "#\[\*\]#", "</li><li>" , trim($txt) );
		
		$txt = preg_replace( "#^</?li>#"  , "", $txt );
		
		return str_replace( "\n</li>", "</li>", $txt."</li>" );
	}
			
	/*-------------------------------------------------------------------------*/
	// regex_parse_quotes: Builds this quote tag HTML
	/*-------------------------------------------------------------------------*/
	
	/**
	* Parse quotes: main
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function regex_parse_quotes($the_txt="")
	{
		$this->quote_open   = 0;
		$this->quote_closed = 0;
		$this->quote_error  = 0;
		
		if ($the_txt == "") return;
		
		$txt = $the_txt;
		
		if ( substr_count( strtolower($txt), '[quote' ) > $this->max_embed_quotes )
		{
			return $txt;
		}
		
		$txt = str_replace( chr(173).']', '&#93;', $txt );
		
		$this->quote_html = $this->wrap_style('quote');
		
		$txt = preg_replace( "#\[/quote\]#ie"                     , "\$this->regex_close_quote()"                  , $txt );
		$txt = preg_replace( "#\[quote=([^\],]+?),([^\]]+?)\]#ie" , "\$this->regex_quote_tag('\\1', '\\2', '\\3')" , $txt );
		$txt = preg_replace( "#\[quote=([^\]]+?)\]#ie"            , "\$this->regex_quote_tag('\\1', '')"           , $txt );
		$txt = preg_replace( "#\[quote([^\]]+?)?\]#ie"            , "\$this->regex_simple_quote_tag('\\1')"        , $txt );
		
		
		$txt = str_replace( "\n", "<br />", $txt );
		
		if ( ($this->quote_open == $this->quote_closed) and ($this->quote_error == 0) )
		{
			return $txt;
		}
		else
		{
			//print "{$this->quote_open} == {$this->quote_closed}";
			return $the_txt;
		}
	}
	
	
	/*-------------------------------------------------------------------------*/
	// regex_simple_quote_tag: Builds this quote tag HTML
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert simple quote tag
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function regex_simple_quote_tag( $extra='' )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$extra   = $extra;
		$post_id = 0;
		$date    = '';
		$name    = '';
		
		//-----------------------------------------
		// Inc..
		//-----------------------------------------
		
		$this->quote_open++;
		
		//-----------------------------------------
		// Post?
		//-----------------------------------------
		
		preg_match( "#post=([\"']|&quot;|&\#039;|&\#39;)?(\d+)([\"']|&quot;|&\#039;|&\#39;)?#", $extra, $match );
		
		if ( intval( $match[2] ) )
		{
			$post_id = intval( $match[2] );
		}
		
		//-----------------------------------------
		// Name?
		//-----------------------------------------
		
		preg_match( "#name=([\"']|&quot;|&\#039;|&\#39;)(.+?)([\"']|&quot;|&\#039;|&\#39;)\s?(date|post)#is", $extra, $match );
		
		if ( $match[2] )
		{
			$name = $this->_make_quote_safe($match[2]);
		}
		
		//-----------------------------------------
		// Date?
		//-----------------------------------------
		
		preg_match( "#date=([\"']|&quot;|&\#039;|&\#39;)(.+?)([\"']|&quot;|&\#039;|&\#39;)#", $extra, $match );
		
		if ( $match[2] )
		{
			$date = $this->_make_quote_safe($match[2]);
		}
		
		//-----------------------------------------
		// Anything?
		//-----------------------------------------
		
		if ( ! $post_id AND ! $date AND ! $name )
		{
			return "<!--quoteo-->{$this->quote_html['START']}<!--quotec-->";
		}
		else
		{
			//-----------------------------------------
			// Name...
			//-----------------------------------------
			
			if ( $name or $date )
			{
				$textra = '(';
			}
			
			if ( $name )
			{
				$textra .= $name;
			}
			
			//-----------------------------------------
			// Date...
			//-----------------------------------------
			
			if ( $date )
			{
				$textra .= ' &#064; '.$date;
			}
			
			if ( $name or $date )
			{
				$textra .= ')';
			}
			
			//-----------------------------------------
			// Post...
			//-----------------------------------------
			
			if ( $post_id )
			{
				$textra .= " [snapback]{$post_id}[/snapback]";
			}
			
			$html = $this->wrap_style( 'quote', $textra );
			
			//-----------------------------------------
			// Return...
			//-----------------------------------------
			
			return "<!--quoteo(post={$post_id}:date={$date}:name={$name})-->{$html['START']}<!--quotec-->";
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// regex_close_quote: closes a quote tag
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert quote close tag
	*
	* @return	string	Converted text
	*/
	function regex_close_quote()
	{
		$this->quote_closed++;
		
		return "<!--QuoteEnd-->{$this->quote_html['END']}<!--QuoteEEnd-->";
	}
	
	/*-------------------------------------------------------------------------*/
	// regex_quote_tag: Builds this quote tag HTML (OLD)
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert quote tag: Main
	*
	* @param	string	Poster's name
	* @param	string	Date String
	* @param	string	Post content
	* @return	string	Converted text
	*/
	function regex_quote_tag($name="", $date="", $post="")
	{ 
		//-----------------------------------------
		// Defaults
		//-----------------------------------------
		
		$name = $name;
		$date = $date;
		$post = $post;
		
		if ( $date != "" )
		{
			$default = "[quote=$name,$date{$post}]";
		}
		else
		{
			$default = "[quote=$name{$post}]";
		}
		
		if ( strstr( $name, '<!--c1-->' ) or strstr( $date, '<!--c1-->' ) )
		{
			//-----------------------------------------
			// Code tag detected...
			//-----------------------------------------
			
			$this->quote_error++;
		 	return $default;
		}
		
		//-----------------------------------------
		// Sort name
		//-----------------------------------------
		
		$name = str_replace( "+", "&#043;", $name );
		$name = str_replace( "-", "&#045;", $name );
		$name = str_replace( '[', "&#091;", $name );
		$name = str_replace( ']', "&#093;", $name );
		
		//-----------------------------------------
		// Inc..
		//-----------------------------------------
		
		$this->quote_open++;
		
		//-----------------------------------------
		// Quote header
		//-----------------------------------------
		
		if ($date == "")
		{
			$hextra = "($name)";
		}
		else
		{
			$hextra = "($name &#064; $date)";
		}
		
		$html = $this->wrap_style( 'quote', $hextra );
		
		//-----------------------------------------
		// Sort out extra
		//-----------------------------------------
		
		$extra = "-".$name.'+'.$date;
		
		//-----------------------------------------
		// Return...
		//-----------------------------------------
		
		return "<!--QuoteBegin".$extra."-->{$html['START']}<!--QuoteEBegin-->";
	}		
	
	/*-------------------------------------------------------------------------*/
	// regex_build_url: Checks, and builds the a href
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert URLs
	*
	* @param	array	Input vars
	* @return	string	Converted text
	*/
	function regex_build_url( $url=array() )
	{
		$skip_it = 0;
		
		//-----------------------------------------
		// Make sure the last character isn't punctuation..
		// if it is, remove it and add it to the
		// end array
		//-----------------------------------------
		
		if ( preg_match( "/([\.,\?]|&#33;)$/", $url['html'], $match) )
		{
			$url['end'] .= $match[1];
			$url['html'] = preg_replace( "/([\.,\?]|&#33;)$/", "", $url['html'] );
			$url['show'] = preg_replace( "/([\.,\?]|&#33;)$/", "", $url['show'] );
		}
		
		//-----------------------------------------
		// Make sure it's not being used in a
		// closing code/quote/html or sql block
		//-----------------------------------------
		
		if (preg_match( "/\[\/(html|quote|code|sql)/i", $url['html']) )
		{
			return $url['html'];
		}
		
		//-----------------------------------------
		// clean up the ampersands / brackets
		//-----------------------------------------
		
		$url['html'] = str_replace( "&amp;" , "&"   , $url['html'] );
		$url['html'] = str_replace( "["     , "%5b" , $url['html'] );
		$url['html'] = str_replace( "]"     , "%5d" , $url['html'] );
		
		//-----------------------------------------
		// Make sure we don't have a JS link
		//-----------------------------------------
		
		$url['html'] = preg_replace( "/javascript:/i", "java script&#58; ", $url['html'] );
		
		//-----------------------------------------
		// Do we have http:// at the front?
		//-----------------------------------------
		
		if ( ! preg_match("#^(http|news|https|ftp|aim)://#", $url['html'] ) )
		{
			$url['html'] = 'http://'.$url['html'];
		}
		
		//-----------------------------------------
		// Tidy up the viewable URL
		//-----------------------------------------

		if ( preg_match( "/^<img src/i", $url['show'] ) )
		{
			$skip_it     = 1;
			$url['show'] = stripslashes($url['show']);
		}

		$url['show'] = preg_replace( "/&amp;/" , "&" , $url['show'] );
		$url['show'] = preg_replace( "/javascript:/i", "javascript&#58; ", $url['show'] );
		
		if ( (strlen($url['show']) -58 ) < 3 )  $skip_it = 1;
		
		//-----------------------------------------
		// Make sure it's a "proper" url
		//-----------------------------------------
		
		if ( ! preg_match( "/^(http|ftp|https|news):\/\//i", $url['show'] )) $skip_it = 1;
		
		$show = $url['show'];
		
		if ($skip_it != 1)
		{
			$stripped = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\2", $url['show'] );
			$uri_type = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\1", $url['show'] );
			
			$show = $uri_type.'://'.substr( $stripped , 0, 35 ).'...'.substr( $stripped , -15   );
		}
		
		return $url['st'] . "<a href=\"".$url['html']."\" target=\"_blank\">".$show."</a>" . $url['end'];
	}
	
	/*-------------------------------------------------------------------------*/
	// Remove sessions in a nice way
	/*-------------------------------------------------------------------------*/
	
	/**
	* Remove session keys from URLs
	*
	* @param	string	Start token
	* @param	string	End token
	* @return	string	Converted text
	*/
	function regex_bash_session($start_tok, $end_tok)
	{
		//-----------------------------------------
		// Case 1: index.php?s=0000        :: Return nothing (parses: index.php)
		// Case 2: index.php?s=0000&this=1 :: Return ?       (parses: index.php?this=1)
		// Case 3: index.php?this=1&s=0000 :: Return nothing (parses: index.php?this=1)
		// Case 4: index.php?t=1&s=00&y=2  :: Return &       (parses: index.php?t=1&y=2)
		//-----------------------------------------
		
		$start_tok = str_replace( '&amp;', '&', $start_tok );
		$end_tok   = str_replace( '&amp;', '&', $end_tok   );
		
		//1:
		if ($start_tok == '?' and $end_tok == '')
		{
			return "";
		}
		//2:
		else if ($start_tok == '?' and $end_tok == '&')
		{
			return '?';
		}
		//3:
		else if ($start_tok == '&' and $end_tok == '')
		{
			return "";
		}
		else if ($start_tok == '&' and $end_tok == '&')
		{
			return "&";
		}
		else
		{
			return $start_tok.$end_tok;
		}
	}
	
	
	/*-------------------------------------------------------------------------*/
	// Converts hex entity code to character
	/*-------------------------------------------------------------------------*/
	
	/**
	* Converts hex entity code to character
	*
	* @param	string	hex entity
	* @return	string	decimal entity
	*/
	function regex_bash_hex($hex_entity)
	{
		return html_entity_decode( "&#".hexdec( $hex_entity ).";" );
	}	
	
	/*-------------------------------------------------------------------------*/
	// regex_check_flash: Checks, and builds the <object>
	// html.
	/*-------------------------------------------------------------------------*/
	
	/**
	* Check and convert flash BBcode tags
	*
	* @param	integer	Movie width
	* @param	integer	Movie height
	* @param	string	Movie URL
	* @return	string	Converted text
	*/
	function regex_check_flash($width="", $height="", $url="")
	{
		$default = "\[flash=$width,$height\]$url\[/flash\]";
		
		if ( ! $this->ipsclass->vars['allow_flash'] )
		{
			return $default;
		}
		
		if ( $width > $this->ipsclass->vars['max_w_flash'] )
		{
			$this->error = 'flash_too_big';
			return $default;
		}
		
		if ( $height > $this->ipsclass->vars['max_h_flash'] )
		{
			$this->error = 'flash_too_big';
			return $default;
		}
		
		if ( ! preg_match( "/^http:\/\/(\S+)\.swf$/i", $url) )
		{
			$this->error = 'flash_url';
			return $default;
		}
		
		return "<!--Flash $width+$height+$url--><OBJECT CLASSID='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' WIDTH=$width HEIGHT=$height><PARAM NAME=MOVIE VALUE=$url><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><EMBED SRC=$url WIDTH=$width HEIGHT=$height PLAY=TRUE LOOP=TRUE QUALITY=HIGH></EMBED></OBJECT><!--End Flash-->";
	}
	
	/*-------------------------------------------------------------------------*/
	// Unconvert size
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert font-size HTML back into BBCode
	*
	* @param	integer	Core size
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function unconvert_size($size="", $text="")
	{
		$size -= 7;
		
		return '[size='.$size.']'.$text.'[/size]';
	}
	
	/*-------------------------------------------------------------------------*/
	// Unconvert flash
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert flash HTML back into BBCode
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function unconvert_flash($flash="")
	{
		$f_arr = explode( "+", $flash );
		
		return '[flash='.$f_arr[0].','.$f_arr[1].']'.$f_arr[2].'[/flash]';
	}
	
	/*-------------------------------------------------------------------------*/
	// Unconvert SQL
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert SQL HTML back into BBCode
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function unconvert_sql($sql="")
	{
		$sql = stripslashes($sql);
		
		$sql = preg_replace( "#<span style='.+?'>#is", "", $sql );
		$sql = str_replace( "</span>"                , "", $sql );
		$sql = preg_replace( "#\s*$#"                , "", $sql );
		
		return '[sql]'.$sql.'[/sql]';
	}

	/*-------------------------------------------------------------------------*/
	// Unconvert HTML
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert HTML TAG HTML back into BBCode
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function unconvert_htm($html="")
	{
		$html = stripslashes($html);
		
		$html = preg_replace( "#<span style='.+?'>#is", "", $html );
		$html = str_replace( "</span>"                , "", $html );
		$html = preg_replace( "#\s*$#"                , "", $html );
		
		return '[html]'.$html.'[/html]';
	}
	
	/*-------------------------------------------------------------------------*/
	// convert_emoticon:
	// replaces the text with the emoticon image
	/*-------------------------------------------------------------------------*/
	
	/**
	* Convert emoticon to image HTML
	*
	* @param	string	Emo code :)
	* @param	string	Image URL
	* @return	string	Converted text
	*/
	function convert_emoticon($code="", $image="")
	{
		if (!$code or !$image) return;
		
		//-----------------------------------------
		// Remove slashes added by preg_quote
		//-----------------------------------------
		
		$code = stripslashes($code);
		
		$this->emoticon_count++;
		
		return "<!--emo&".trim($code)."--><img src='{$this->ipsclass->vars['EMOTICONS_URL']}/$image' border='0' style='vertical-align:middle' alt='$image' /><!--endemo-->";
	}
	
	/*-------------------------------------------------------------------------*/
	// Custom array sort
	/*-------------------------------------------------------------------------*/
	
	/**
	* Custom sort operation
	*
	* @param	string	A
	* @param	string	B
	* @return	integer
	*/
	function smilie_length_sort($a, $b)
	{
		if ( strlen($a['typed']) == strlen($b['typed']) )
		{
			return 0;
		}
		return ( strlen($a['typed']) > strlen($b['typed']) ) ? -1 : 1;
	}
	
	/*-------------------------------------------------------------------------*/
	// Custom array sort
	/*-------------------------------------------------------------------------*/
	
	/**
	* Custom sort operation
	*
	* @param	string	A
	* @param	string	B
	* @return	integer
	*/
	function word_length_sort($a, $b)
	{
		if ( strlen($a['type']) == strlen($b['type']) )
		{
			return 0;
		}
		return ( strlen($a['type']) > strlen($b['type']) ) ? -1 : 1;
	}
	
	/*-------------------------------------------------------------------------*/
	// Make quote safe
	/*-------------------------------------------------------------------------*/
	
	/**
	* Make quotes safe
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function _make_quote_safe( $txt='' )
	{
		//-----------------------------------------
		// Sort name
		//-----------------------------------------
		
		$txt = str_replace( "+", "&#043;" , $txt );
		$txt = str_replace( "-", "&#045;" , $txt );
		$txt = str_replace( ":", "&#58;"  , $txt );
		$txt = str_replace( "[", "&#91;"  , $txt );
		$txt = str_replace( "]", "&#93;"  , $txt );
		$txt = str_replace( ")", "&#41;"  , $txt );
		$txt = str_replace( "(", "&#40;"  , $txt );
		$txt = str_replace( "'", "&#039;" , $txt );
		
		return $txt;
	}
	
	/*-------------------------------------------------------------------------*/
	// Parse new quotes
	/*-------------------------------------------------------------------------*/
	
	/**
	* Parse new quotes
	*
	* @param	string	Quote data
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function _parse_new_quote( $quote_data='', $quote_text='' )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$return = array();
		
		//-----------------------------------------
		// No data?
		//-----------------------------------------
		
		if ( ! $quote_data )
		{
			return '[quote]';
		}
		else
		{
			preg_match( "#\(post=(.+?)?:date=(.+?)?:name=(.+?)?\)#", $quote_data, $match );
			
			if ( $match[3] )
			{
				$return[] = " name='{$match[3]}'";
			}
			
			if ( $match[1] )
			{
				$return[] = " post='".intval($match[1])."'";
			}
			
			if ( $match[2] )
			{
				$return[] = " date='{$match[2]}'";
			}
						
			return str_replace( '  ', ' ', '[quote' . implode( ' ', $return ).']' );
		}
	}
	
}



?>