Location: PHPKode > projects > Private Message System > pmsys-2.3.0/includes/functions_parse.php
<?php

/******************************************************************
* $Id: functions_parse.php,v 1.10 2003/08/22 20:57:39 allowee Exp $
*
* Copyright (C) 2001-2003 PMS Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA  02111-1307, USA.
*
* The "GNU General Public License" (GPL) is available at
* http://www.gnu.org/copyleft/gpl.html.
******************************************************************/


function parseCensorship ($text) {
    $text = " ".$text." ";
    $badwords = array();
// Adding more words to the censor:
//
//    $badwords[] = array(' badword ', ' goodword ');
//
// duplicate these lines as needed, changing the good
// and bad words as necessary.
//
// Putting a space either side of both badword and goodword will only change
// a whole word... ie 'ass' != 'assessment'
//
//
//  Most of these words taken from <http://www.4cm.com/badwordlist/index.php>
//   - Cheers JohnAbelaB
//
//  If you get a REG_EPAREN error,  try replacing symbols such as ( and ) with
//  \( and \) ...

    $badwords[] = array(' \@\$\$ ', ' butt ');
    $badwords[] = array(' a\$\$ ', ' butt ');
    $badwords[] = array(' anton ', ' [censored] ');
    $badwords[] = array(' arse ', ' butt ');
    $badwords[] = array(' arsehole ', ' [censored] ');
    $badwords[] = array(' ass ', ' butt ');
    $badwords[] = array(' ass muncher ', ' [censored] ');
    $badwords[] = array(' asshole ', ' [censored] ');
    $badwords[] = array(' asstooling ', ' [censored] ');
    $badwords[] = array(' asswipe ', ' [censored] ');
    $badwords[] = array(' b\!tch ', ' [censored] ');
    $badwords[] = array(' b17ch ', ' [censored] ');
    $badwords[] = array(' b1tch ', ' [censored] ');
    $badwords[] = array(' bastard ', ' [censored] ');
    $badwords[] = array(' beefcurtins ', ' [censored] ');
    $badwords[] = array(' bi7ch ', ' [censored] ');
    $badwords[] = array(' bitch ', ' [censored] ');
    $badwords[] = array(' bitchy ', ' [censored] ');
    $badwords[] = array(' boiolas ', ' [censored] ');
    $badwords[] = array(' bollocks ', ' [censored] ');
    $badwords[] = array(' breasts ', ' [censored] ');
    $badwords[] = array(' brown nose ', ' [censored] ');
    $badwords[] = array(' bugger ', ' damn ');
    $badwords[] = array(' butt pirate ', ' [censored] ');
    $badwords[] = array(' c0ck ', ' [censored] ');
    $badwords[] = array(' cawk ', ' [censored] ');
    $badwords[] = array(' chink ', ' [censored] ');
    $badwords[] = array(' clitsaq ', ' [censored] ');
    $badwords[] = array(' cock ', ' [censored] ');
    $badwords[] = array(' cockbite ', ' [censored] ');
    $badwords[] = array(' cockgobbler ', ' [censored] ');
    $badwords[] = array(' cocksucker ', ' [censored] ');
    $badwords[] = array(' cum ', ' [censored] ');
    $badwords[] = array(' cunt ', ' [censored] ');
    $badwords[] = array(' dago ', ' [censored] ');
    $badwords[] = array(' daygo ', ' [censored] ');
    $badwords[] = array(' dego ', ' [censored] ');
    $badwords[] = array(' dick ', ' [censored] ');
    $badwords[] = array(' dick wad ', ' [censored] ');
    $badwords[] = array(' dickhead ', ' [censored] ');
    $badwords[] = array(' dickweed ', ' [censored] ');
    $badwords[] = array(' douchebag ', ' [censored] ');
    $badwords[] = array(' dziwka ', ' [censored] ');
    $badwords[] = array(' ekto ', ' [censored] ');
    $badwords[] = array(' enculer ', ' [censored] ');
    $badwords[] = array(' faen ', ' [censored] ');
    $badwords[] = array(' fag ', ' [censored] ');
    $badwords[] = array(' faggot ', ' [censored] ');
    $badwords[] = array(' fart ', ' [censored] ');
    $badwords[] = array(' fatass ', ' [censored] ');
    $badwords[] = array(' feg ', ' [censored] ');
    $badwords[] = array(' felch ', ' [censored] ');
    $badwords[] = array(' ficken ', ' [censored] ');
    $badwords[] = array(' fitta ', ' [censored] ');
    $badwords[] = array(' fitte ', ' [censored] ');
    $badwords[] = array(' flikker ', ' [censored] ');
    $badwords[] = array(' fok ', ' $#%! ');
    $badwords[] = array(' fuck ', ' $#%! ');
    $badwords[] = array(' fuck', ' $#%! ');
    $badwords[] = array(' Fuck ', ' $#%! ');
    $badwords[] = array(' FUCK ', ' $#%! ');
    $badwords[] = array(' fu\(k ', ' $#%! ');
    $badwords[] = array(' fucker ', ' [censored] ');
    $badwords[] = array(' fucking ', ' [censored] ');
    $badwords[] = array(' fuckwit ', ' [censored] ');
    $badwords[] = array(' fuk ', ' [censored] ');
    $badwords[] = array(' fuking ', ' [censored] ');
    $badwords[] = array(' futkretzn ', ' [censored] ');
    $badwords[] = array(' fux0r ', ' $#%! ');
    $badwords[] = array(' gook ', ' [censored] ');
    $badwords[] = array(' h0r ', ' [censored] ');
    $badwords[] = array(' handjob ', ' [censored] ');
    $badwords[] = array(' helvete ', ' [censored] ');
    $badwords[] = array(' honkey ', ' [censored] ');
    $badwords[] = array(' hore ', ' [censored] ');
    $badwords[] = array(' hump ', ' [censored] ');
    $badwords[] = array(' injun ', ' [censored] ');
    $badwords[] = array(' kawk ', ' [censored] ');
    $badwords[] = array(' kike ', ' [censored] ');
    $badwords[] = array(' knulle ', ' [censored] ');
    $badwords[] = array(' kraut ', ' [censored] ');
    $badwords[] = array(' kuk ', ' [censored] ');
    $badwords[] = array(' kuksuger ', ' [censored] ');
    $badwords[] = array(' kurac ', ' [censored] ');
    $badwords[] = array(' kurwa ', ' [censored] ');
    $badwords[] = array(' langer ', ' [censored] ');
    $badwords[] = array(' masturbation ', ' [censored] ');
    $badwords[] = array(' merd ', ' [censored] ');
    $badwords[] = array(' motherfucker ', ' [censored] ');
    $badwords[] = array(' motherfuckingcocksucker ', ' [censored] ');
    $badwords[] = array(' motherfuckingcocksucker', ' [censored] ');
    $badwords[] = array(' mutherfucker ', ' [censored] ');
    $badwords[] = array(' nepesaurio ', ' [censored] ');
    $badwords[] = array(' nigga ', ' [censored] ');
    $badwords[] = array(' nigger ', ' [censored] ');
    $badwords[] = array(' nonce ', ' [censored] ');
    $badwords[] = array(' nutsack ', ' [censored] ');
    $badwords[] = array(' one\-eyed\-trouser\-snake ', ' [censored] ');
    $badwords[] = array(' penis ', ' [censored] ');
    $badwords[] = array(' picka ', ' [censored] ');
    $badwords[] = array(' pissant ', ' [censored] ');
    $badwords[] = array(' pizda ', ' [censored] ');
    $badwords[] = array(' politician ', ' [censored] ');
    $badwords[] = array(' prick ', ' [censored] ');
    $badwords[] = array(' puckface ', ' [censored] ');
    $badwords[] = array(' pule ', ' [censored] ');
    $badwords[] = array(' pussy ', ' [censored] ');
    $badwords[] = array(' puta ', ' [censored] ');
    $badwords[] = array(' puto ', ' [censored] ');
    $badwords[] = array(' rimjob ', ' [censored] ');
    $badwords[] = array(' rubber ', ' [censored] ');
    $badwords[] = array(' scheisse ', ' [censored] ');
    $badwords[] = array(' schlampe ', ' [censored] ');
    $badwords[] = array(' schlong ', ' [censored] ');
    $badwords[] = array(' screw ', ' [censored] ');
    $badwords[] = array(' shit ', ' **** ');
    $badwords[] = array(' shiteater ', ' [censored] ');
    $badwords[] = array(' shiz ', ' [censored] ');
    $badwords[] = array(' skribz ', ' [censored] ');
    $badwords[] = array(' skurwysyn ', ' [censored] ');
    $badwords[] = array(' slut ', ' [censored] ');
    $badwords[] = array(' spermburper ', ' [censored] ');
    $badwords[] = array(' spic ', ' [censored] ');
    $badwords[] = array(' spierdalaj ', ' [censored] ');
    $badwords[] = array(' splooge ', ' [censored] ');
    $badwords[] = array(' spunk ', ' [censored] ');
    $badwords[] = array(' tatas ', ' [censored] ');
    $badwords[] = array(' tits ', ' [censored] ');
    $badwords[] = array(' toss the salad ', ' [censored] ');
    $badwords[] = array(' twat ', ' [censored] ');
    $badwords[] = array(' unclefucker ', ' [censored] ');
    $badwords[] = array(' vagina ', ' [censored] ');
    $badwords[] = array(' vittu ', ' [censored] ');
    $badwords[] = array(' votze ', ' [censored] ');
    $badwords[] = array(' wank ', ' [censored] ');
    $badwords[] = array(' wanka ', ' [censored] ');
    $badwords[] = array(' wanker ', ' [censored] ');
    $badwords[] = array(' wankers ', ' [censored] ');
    $badwords[] = array(' wankstain ', ' [censored] ');
    $badwords[] = array(' whore ', ' [censored] ');
    $badwords[] = array(' wichser ', ' [censored] ');
    $badwords[] = array(' wop ', ' [censored] ');
    $badwords[] = array(' yed ', ' [censored] ');

    foreach ($badwords as $badword) {
	    $text = eregi_replace($badword[0],$badword[1],$text);
    }
    return trim($text);
}


function highlight($bericht) {
    $bericht = str_replace("<br />", "", $bericht);
    $bericht = str_replace("&gt;", ">", $bericht);
    $bericht = str_replace("&lt;", "<", $bericht);
    $bericht = str_replace("&#36;", "\$", $bericht);
    $bericht = str_replace("&quot;", "\"", $bericht);
    $bericht = trim($bericht);
    ob_start();
    $bericht = stripslashes($bericht);
    $bericht = highlight_string($bericht);
    $bericht = ob_get_contents();
    ob_end_clean();
    $bericht2 = "<div align=\"left\"><font face='verdana' size='1'><b>PHP Code:</b></font><table border=\"0\" width=\"48%\" bgcolor=\"black\" cellspacing=\"1\" cellpadding=\"1\"><tr><td width=\"100%\" bgcolor=\"#EEEEEE\">$bericht</td></tr></table></div>";
    return $bericht2;
}



function plain2ubb($msg){
	global $ubbcode;

$msg = str_replace("{", "&#123;", $msg);

  // --- Image
if(!empty($ubbcode['img'])){
  $msg = preg_replace("/\[img\](\r\n|\r|\n)*((http|https):\/\/([^;<>\(\)\"".iif(0,"","!\*\?\&")."]+)|[a-z0-9\/\\\._\- ]+)\[\/img\]/siU", "<img src=\"\\2\" border=\"0\" alt=\"\">", $msg);

  $msg = preg_replace("/\[img\](\r\n|\r|\n)*((http|https):\/\/([^;<>\*\(\)\"]+)|[a-z0-9\/\\\._\- ]+)\[\/img\]/siU", "<a href=\"\\2\" target=\"_blank\">\\2</a>", $msg);

  $msg = preg_replace("/\[img\](.+?)\[\/img\]/is",'<img src="\1">', $msg);
  $msg = preg_replace("/\[img w=(.+?) h=(.+?)\](.+?)\[\/img\]/is",'<img src="\3" height="\1" width="\2">', $msg);
}
  // --- Email
  $msg = preg_replace("/\[email\](.+?)\[\/email\]/is",'<a href="mailto:\1">\1</a>', $msg);
  $msg = preg_replace("/\[email=(.+?)\](.+?)\[\/email\]/is",'<a href="mailto:\1">\2</a>', $msg);

  // --- Url


  $patterns = array();
		$replacements = array();
		$patterns[0] = "#\[url\]([a-z]+?://){1}(.*?)\[/url\]#si";
		$replacements[0] = '<a href="\1\2" target="_blank">\1\2</a>';
		$patterns[1] = "#\[url\](.*?)\[/url\]#si";
		$replacements[1] = '<a href="http://\1" target="_blank">\1</a>';
		$patterns[2] = "#\[url=([a-z]+?://){1}(.*?)\](.*?)\[/url\]#si";
		$replacements[2] = '<a href="\1\2" target="_blank">\3</a>';
		$patterns[3] = "#\[url=(.*?)\](.*?)\[/url\]#si";
		$replacements[3] = '<a href="http://\1" target="_blank">\2</a>';
		$msg = preg_replace($patterns, $replacements, $msg);


if(!empty($ubbcode['style'])){
  // --- Font-weight 
  $msg = preg_replace("/\[b\](.+?)\[\/b\]/is",'<b>\1</b>', $msg);  
  $msg = preg_replace("/\[u\](.+?)\[\/u\]/is",'<u>\1</u>', $msg);
  $msg = preg_replace("/\[i\](.+?)\[\/i\]/is",'<i>\1</i>', $msg);  
  $msg = preg_replace("/\[s\](.+?)\[\/s\]/is",'<s>\1</s>', $msg);
  $msg = preg_replace("/\[center\](.+?)\[\/center\]/is",'<center>\1</center>', $msg);
  $msg = preg_replace("/\[ul\](.+?)\[\/ul\]/is",'<ul>\1</ul>', $msg);
  $msg = preg_replace("/\[li\](.+?)\[\/li\]/is",'<li>\1</li>', $msg);
}
if(!empty($ubbcode['color'])){
  // --- Replace color-tags
  $msg = preg_replace("/\[red\](.+?)\[\/red\]/is",'<font color=red>\1</font>', $msg);  
  $msg = preg_replace("/\[blue\](.+?)\[\/blue\]/is",'<font color=blue>\1</font>', $msg);
  $msg = preg_replace("/\[green\](.+?)\[\/green\]/is",'<font color=green>\1</font>', $msg);  
  $msg = preg_replace("/\[yellow\](.+?)\[\/yellow\]/is",'<font color=yellow>\1</font>', $msg);
}

  // --- Quote replace
$msg = str_replace("[quote]","<blockquote><span class=\"12px\"><b>Quote:</b></span><hr size='1'>",$msg);
$msg = str_replace("[/quote]","<hr size='1'></blockquote>",$msg);


if(!empty($ubbcode['php'])){
$msg = preg_replace('_\[php\](.*?)\[/php\]_ise', "highlight(' \\1 ')", $msg);
$msg = preg_replace('_\<\?php(.*?)\?\>_ise', "highlight('<?php \\1 ?>')", $msg);
$msg = preg_replace('_\<\?(.*?)\?\>_ise', "highlight('<? \\1 ?>')", $msg);
}


  $ACCEPT_CHARS = "a-z0-9_~#&;=./?+-";     
  $msg = eregi_replace(" (http|https|ftp|gopher|news)://([$ACCEPT_CHARS]+)", "<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>", $msg);



  return $msg;
}

function fix_stuff($msg){
	 $searcharray = array("/javascript:/si", "/about:/si", "/vbscript:/si" );

 $replacearray = array("java, not allowed, script:", "about :", "vb, not allowed, script :");
  $msg = preg_replace($searcharray, $replacearray, $msg);

 return $msg;
}

function cross($msg){

	 $searcharray = array("/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/esiU",
						"/(\[)(url)(])([^\"]*)(\[\/url\])/esiU");

 $replacearray = array(
          "checkurl('\\5', '\\7')",
          "checkurl('\\4')",
        );
  $msg = preg_replace($searcharray, $replacearray, $msg);

 return $msg;
}


function parseEmoticons ($text) {

    $emoticons = array();
    $emoticons[] = array(":EEK:", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":off-topic:", "<img src='./images/smileys/icon_offtopic.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":ROLLEYES:", "<img src='./images/smileys/rolleyes.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":MAD:", "<img src='./images/smileys/mad.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":CONFUSED:", "<img src='./images/smileys/confused.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":SIGH:", "<img src='./images/smileys/sigh.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":YES:", "<img src='./images/smileys/yes.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":NO:", "<img src='./images/smileys/no.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":SLEEP:", "<img src='./images/smileys/sleep.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":UPSET:", "<img src='./images/smileys/upset.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":SHY:", "<img src='./images/smileys/shy.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":NONE:", "<img src='./images/smileys/none.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":LAUGH:", "<img src='./images/smileys/laugh.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":DEAD:", "<img src='./images/smileys/dead.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":CRY:", "<img src='./images/smileys/cry.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":eek:", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":rolleyes:", "<img src='./images/smileys/rolleyes.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":mad:", "<img src='./images/smileys/mad.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":confused:", "<img src='./images/smileys/confused.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":sigh:", "<img src='./images/smileys/sigh.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":yes:", "<img src='./images/smileys/yes.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":no:", "<img src='./images/smileys/no.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":sleep:", "<img src='./images/smileys/sleep.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":upset:", "<img src='./images/smileys/upset.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":shy:", "<img src='./images/smileys/shy.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":laugh:", "<img src='./images/smileys/laugh.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":dead:", "<img src='./images/smileys/dead.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":cry:", "<img src='./images/smileys/cry.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":)", "<img src='./images/smileys/happy.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":(", "<img src='./images/smileys/sad.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(";)", "<img src='./images/smileys/smilewinkgrin.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":|", "<img src='./images/smileys/none.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-)", "<img src='./images/smileys/smile.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-(", "<img src='./images/smileys/sad.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(";-)", "<img src='./images/smileys/smilewinkgrin.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-|", "<img src='./images/smileys/none.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":0", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("B)", "<img src='./images/smileys/cool.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":D", "<img src='./images/smileys/biggrin.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":P", "<img src='./images/smileys/bigrazz.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":B", "<img src='./images/smileys/bigrazz.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("B-)", "<img src='./images/smileys/cool.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-D", "<img src='./images/smileys/biggrin.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-P", "<img src='./images/smileys/bigrazz.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":O", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("b)", "<img src='./images/smileys/cool.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":d", "<img src='./images/smileys/biggrin.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":p", "<img src='./images/smileys/bigrazz.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":b", "<img src='./images/smileys/bigrazz.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("b-)", "<img src='./images/smileys/cool.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-d", "<img src='./images/smileys/biggrin.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-p", "<img src='./images/smileys/bigrazz.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":-b", "<img src='./images/smileys/bigrazz.gif' alt='' align='absmiddle'>");
    $emoticons[] = array(":o", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("o_O", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("O_o", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("o_o", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");
    $emoticons[] = array("O_O", "<img src='./images/smileys/bigeek.gif' alt='' align='absmiddle'>");

    foreach ($emoticons as $emoticon) {
	    $text = str_replace($emoticon[0],$emoticon[1],$text);
    }
    return $text;
}

function checkurl($url, $hyperlink="") {
  $righturl = $url;
  if(!preg_match("![a-z]://!si", $url)) {
    $righturl = "http://$righturl";
  }
  // remove threat of users including javascript in url
  /*$righturl = preg_replace("/javascript:/si", "java script:", $righturl);
  $righturl = preg_replace("/about:/si", "about :", $righturl);*/
  $hyperlink = iif(trim($hyperlink)=="" or $hyperlink==$url, iif(strlen($url)>55,substr($url,0,35)."...".substr($url,-15),$url) ,$hyperlink);
  return "<a href=\"".$righturl."\" target=\"_blank\">".str_replace('\"', '"', $hyperlink)."</a>";
}

// ###################### Start iif #######################
function iif ($expression,$returntrue,$returnfalse) {

  if ($expression==0) {
    return $returnfalse;
  } else {
    return $returntrue;
  }

}


/**
 * Preparses a message, puts [url] tags around urls etc.
 * @param $message the message to parse the code in
 * @return the preparsed code
 */

function preparsecode($message, $realname, $username = '') {

	if(strstr($realname, "[") || strstr($realname, "]") || strstr($realname, "'") || strstr($realname, "\"")) {
		$realname = $username;
	}

	$codes = array(
		'/(\/me) (.*)([\r\n]?)/i'
	);

	// 		'\1[url=\2]\2[/url]',
	$codesto = array(
		"[me=".$realname."]\\2[/me]\\3"
	);

	$message = preg_replace($codes, $codesto, $message);
	$message = str_replace("\r","", $message);

	// Check if all quotes are closed
	preg_match_all("/(\[quote author=(.+?) link=(.+?) date=(.+?)\])|(\[quote\])/", $message, $regs);
	$quoteopen =  count($regs[0]);
	preg_match_all("/(\[\/quote\])/", $message, $regs);
	$quoteclose =  count($regs[0]);

	if($quoteopen > $quoteclose) {
		$toclose = $quoteopen - $quoteclose;
		for($i = 0 ; $i < $toclose ; $i++) {
			$message .= "[/quote]";
		}
	} elseif($quoteclose > $quoteopen) {
		$toopen = $quoteclose - $quoteopen;
		for($i = 0 ; $i < $toopen ; $i++) {
			$message = "[quote]$message";
		}
	}

    // Check if all code tags are closed
	preg_match_all("/(\[code\])/", $message, $regs);
	$codeopen =  count($regs[0]);
	preg_match_all("/(\[\/code\])/", $message, $regs);
	$codeclose =  count($regs[0]);

	if($codeopen > $codeclose) {
		$toclose = $codeopen - $codeclose;
		for($i = 0 ; $i < $toclose ; $i++) {
			$message .= "[/code]";
		}
	} elseif($codeclose > $codeopen) {
		$toopen = $codeclose - $codeopen;
		for($i = 0 ; $i < $toopen ; $i++) {
			$message = "[code]$message";
		}
	}

	// now that we've fixed all the code tags, let's fix the IMG and URL tags
	$parts = split('\[\/?code\]', ' ' . $message);

	for($i = 0 ; $i < count($parts) ; $i++)
	{
		if($i%2==0)
		{
			$parts[$i] = fixTags($parts[$i]);
			if($i > 0)
				$parts[$i] = '[/code]' . $parts[$i];
		}
		else
		{
			$parts[$i] = '[code]' . $parts[$i];
		}
	}
	$message = substr(implode('',$parts), 1);
	return $message;

}

function fixTags ($message) {

	// First, we fix the [img] Tags

	while (preg_match("/\[img\](.+?)\[\/img\]/i", $message, $matches)) {

		$searchfor = $matches[1];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"http://")) {
			if (!stristr($replace,"https://")) {
				$replace = "http://".$replace;
			} else {
				$replace = stristr($replace,"https://");
			}
		} else {
			$replace = stristr($replace,"http://");
		}
  $message = str_replace("[img]".$searchfor."[/img]","{img}".$replace."{/img}",$message);
  $message = str_replace("[IMG]".$searchfor."[/IMG]","{IMG}".$replace."{/IMG}",$message);

	}

	$message = str_replace("{img}","[img]",$message);
	$message = str_replace("{/img}","[/img]",$message);
    $message = str_replace("{IMG}","[IMG]",$message);
	$message = str_replace("{/IMG}","[/IMG]",$message);

	// Now the [url]'s

	while (preg_match("/\[url\](.+?)\[\/url\]/si", $message, $matches)) {

		$searchfor = $matches[1];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"http://")) {
			if (!stristr($replace,"https://")) {
				$replace = "http://".$replace;
			} else {
				$replace = stristr($replace,"https://");
			}
		} else {
			$replace = stristr($replace,"http://");
		}
		$message = str_replace("[url]".$searchfor."[/url]","{url=".$replace."]".$matches[1]."{/url}",$message);
        $message = str_replace("[URL]".$searchfor."[/URL]","{URL=".$replace."]".$matches[1]."{/URL}",$message);

 }

	$message = str_replace("{url=","[url=",$message);
	$message = str_replace("{/url}","[/url]",$message);
    $message = str_replace("{URL=","[URL=",$message);
	$message = str_replace("{/URL}","[/URL]",$message);

	while (preg_match("/\[url=(.+?)\](.+?)\[\/url\]/si", $message, $matches)) {

		$searchfor = $matches[1];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"http://")) {
			if (!stristr($replace,"https://")) {
				$replace = "http://".$replace;
			} else {
				$replace = stristr($replace,"https://");
			}
		} else {
			$replace = stristr($replace,"http://");
		}
		$message = str_replace("[url=".$searchfor."]".$matches[2]."[/url]","{url=".$replace."]".$matches[2]."{/url}",$message);
        $message = str_replace("[URL=".$searchfor."]".$matches[2]."[/URL]","{URL=".$replace."]".$matches[2]."{/URL}",$message);

	}

	$message = str_replace("{url=","[url=",$message);
	$message = str_replace("{/url}","[/url]",$message);
    $message = str_replace("{URL=","[URL=",$message);
	$message = str_replace("{/URL}","[/URL]",$message);

	// Now the [iurl]'s

	while (preg_match("/\[iurl\](.+?)\[\/iurl\]/si", $message, $matches)) {

		$searchfor = $matches[1];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"http://")) {
			if (!stristr($replace,"https://")) {
				$replace = "http://".$replace;
			} else {
				$replace = stristr($replace,"https://");
			}
		} else {
			$replace = stristr($replace,"http://");
		}
		$message = str_replace("[iurl]".$searchfor."[/iurl]","{iurl=".$replace."]".$matches[1]."{/iurl}",$message);
        $message = str_replace("[IURL]".$searchfor."[/IURL]","{IURL=".$replace."]".$matches[1]."{/IURL}",$message);

	}

	$message = str_replace("{iurl=","[iurl=",$message);
	$message = str_replace("{/iurl}","[/iurl]",$message);
    $message = str_replace("{IURL=","[IURL=",$message);
	$message = str_replace("{/IURL}","[/IURL]",$message);

	while (preg_match("/\[iurl=(.+?)\](.+?)\[\/iurl\]/si", $message, $matches)) {

		$searchfor = $matches[1];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"http://")) {
			if (!stristr($replace,"https://")) {
				$replace = "http://".$replace;
			} else {
				$replace = stristr($replace,"https://");
			}
		} else {
			$replace = stristr($replace,"http://");
		}
		$message = str_replace("[iurl=".$searchfor."]".$matches[2]."[/iurl]","{iurl=".$replace."]".$matches[2]."{/iurl}",$message);
        $message = str_replace("[IURL=".$searchfor."]".$matches[2]."[/IURL]","{IURL=".$replace."]".$matches[2]."{/IURL}",$message);

	}

	$message = str_replace("{iurl=","[iurl=",$message);
	$message = str_replace("{/iurl}","[/iurl]",$message);
    $message = str_replace("{IURL=","[IURL=",$message);
	$message = str_replace("{/IURL}","[/IURL]",$message);

	// Now the [ftp]'s

	while (preg_match("/\[ftp\](.+?)\[\/ftp\]/si", $message, $matches)) {

		$searchfor = $matches[1];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"ftp://")) {
			$replace = "ftp://".$replace;
		} else {
			$replace = stristr($replace,"ftp://");
		}
		$message = str_replace("[ftp]".$searchfor."[/ftp]","{ftp=".$replace."]".$matches[1]."{/ftp}",$message);
        $message = str_replace("[FTP]".$searchfor."[/FTP]","{FTP=".$replace."]".$matches[1]."{/FTP}",$message);

	}

	$message = str_replace("{ftp=","[ftp=",$message);
	$message = str_replace("{/ftp}","[/ftp]",$message);
    $message = str_replace("{FTP=","[FTP=",$message);
	$message = str_replace("{/FTP}","[/FTP]",$message);

	while (preg_match("/\[ftp=(.+?)\](.+?)\[\/ftp\]/si", $message, $matches)) {

		$searchfor = $matches[1];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"ftp://")) {
			$replace = "ftp://".$replace;
		} else {
			$replace = stristr($replace,"ftp://");
		}
		$message = str_replace("[ftp=".$searchfor."]".$matches[2]."[/ftp]","{ftp=".$replace."]".$matches[2]."{/ftp}",$message);
        $message = str_replace("[FTP=".$searchfor."]".$matches[2]."[/FTP]","{FTP=".$replace."]".$matches[2]."{/FTP}",$message);

	}

	$message = str_replace("{ftp=","[ftp=",$message);
	$message = str_replace("{/ftp}","[/ftp]",$message);
    $message = str_replace("{FTP=","[FTP=",$message);
	$message = str_replace("{/FTP}","[/FTP]",$message);

	// new: [flash]
	while (preg_match("/\[flash=(.+?)\](.+?)\[\/flash\]/si", $message, $matches)) {

		$searchfor = $matches[2];
		$replace = $searchfor;
		$replace = trim($replace);	// remove all leading and trailing whitespaces
		if (!stristr($replace,"http://")) {
			if (!stristr($replace,"https://")) {
				$replace = "http://".$replace;
			} else {
				$replace = stristr($replace,"https://");
			}
		} else {
			$replace = stristr($replace,"http://");
		}
		$message = str_replace("[flash=".$matches[1]."]".$searchfor."[/flash]","{flash=".$matches[1]."]".$replace."{/flash}",$message);
        $message = str_replace("[FLASH=".$matches[1]."]".$searchfor."[/FLASH]","{FLASH=".$matches[1]."]".$replace."{/FLASH}",$message);
	}

	$message = str_replace("{flash=","[flash=",$message);
	$message = str_replace("{/flash}","[/flash]",$message);
    $message = str_replace("{FLASH=","[FLASH=",$message);
	$message = str_replace("{/FLASH}","[/FLASH]",$message);

	return $message;

}
 




/**
 * Parses both smilies and kB code as required
 * @param $message the message to parse the code in
 * @param $boarddata the data array of the current board
 * @param $code whether or not to parse kB codes
 * @param $smilies same for smilies :-)
 */

function doUBBC($message,$enableSmilies = 0) {

	global $text, $txt;

	$parts = split('\[\/?code\]', ' ' . $message);

	for($i = 0 ; $i < count($parts) ; $i++)
	{
		if($i%2==0)
		{
			$parts[$i] = str_replace(array('$','[[',']]'), array('&#36;','{<{','}>}'), $parts[$i]);
			if($i > 0)
				$parts[$i] = "</font></td></tr></table>" . $parts[$i];
			$parts[$i] = preg_replace ("/([\n >\(])([\w\-_]+?):\/\/([\w\-_]+)((\.[\w\-_]+)+(:[\d]+)?((\/[\w\-_%]+(\.[\w\-_%]+)*)|(\/[~]?[\w\-_%]*))*(\/?(\?[&;=\w\+%]+)*)?(#[\w\-_]*)?)/", "\\1[url=\\2://\\3\\4]\\2://\\3\\4[/url]", $parts[$i]);
			$parts[$i] = preg_replace ("/([\n >\(])www((\.[\w\-_]+)+(:[\d]+)?((\/[\w\-_%]+(\.[\w\-_%]+)*)|(\/[~]?[\w\-_%]*))*(\/?(\?[&;=\w\+%]+)*)?(#[\w\-_]*)?)/", "\\1[url=http://www\\2]www\\2[/url]", $parts[$i]);
			$parts[$i] = doparsecodesmilies($parts[$i], $enableSmilies);
		}
		elseif($i <= count($parts)-1)
			$parts[$i] = "<font size=1><b>Code:</b></font><table border=0 cellspacing=1 cellpadding=2 width=\"100%\"><tr><td class=quote><font face=\"Courier new\">" . $parts[$i];
	}
	$message = substr(implode('', $parts), 1);

	$message = str_replace('{<{','[',$message);
	$message = str_replace('}>}',']',$message);
	// $message = stripslashes($message); // Maybe you'll have to uncomment this
	$message = str_replace("  ","&nbsp; ",$message);
	//$message = str_replace("\t","&nbsp; &nbsp; ",$message);
	//$message = str_replace("\n\r","<br>",$message);
	//$message = str_replace("\r","<br>",$message);
	//$message = str_replace("\n","<br>",$message);
	return $message;

}

/**
 * This function is being called by the above function (in a regexp) and will parse
 * smilies and tags as desired outside [code] and [php] tags.
 * @param $message the message to parse the code in
 */

function doparsecodesmilies($message,$enableSmilies = 1) {

	global $codefromcache, $codetocache, $smileyfromcache, $smileytocache, $text, $scripturl,$imagesdir,$txt, $Row5;

	if(gettype($codefromcache) != "array" || gettype($smileyfromcache) != "array") {

		$codefromcache = array(
			'/([a-z0-9_-][a-z0-9\._-]*@[a-z0-9_-]+(\.[a-z0-9_-]+)+)/is',
			'/\[url\](.+?)\[\/url\]/is',
			'/\[url=(.+?)\](.+?)\[\/url\]/is',
			'/\[iurl\](.+?)\[\/iurl\]/is',
			'/\[iurl=(.+?)\](.+?)\[\/iurl\]/is',
			'/\[b\](.+?)\[\/b\]/is',
			'/\[i\](.+?)\[\/i\]/is',
			'/\[u\](.+?)\[\/u\]/is',
			'/\[s\](.+?)\[\/s\]/is',
			'/\[move\](.+?)\[\/move\]/is',
			'/\n?\[quote author=(.+?) link=(.+?) date=(.+?)\]\n*/ei',
			'/\[\/quote\]/i',
			'/\n?\[quote\]\n*/i',
			'/\[me=([^\]]+)\](.+?)\[\/me\]/is',
			'/\[img\](.+?)\[\/img\]/i',
			'/\[img width=([0-9]+) height=([0-9]+)\s*\](.+?)\[\/img\]/i',
			'/\[img height=([0-9]+) width=([0-9]+)\s*\](.+?)\[\/img\]/i',
			'/\[color=([\w#]+)\](.*?)\[\/color\]/is',
			'/\[black\](.+?)\[\/black\]/is',
			'/\[white\](.+?)\[\/white\]/is',
			'/\[red\](.+?)\[\/red\]/is',
			'/\[green\](.+?)\[\/green\]/is',
			'/\[blue\](.+?)\[\/blue\]/is',
			'/\[font=(.+?)\](.+?)\[\/font\]/is',
			'/\[size=(.+?)\](.+?)\[\/size\]/is',
			'/\[pre\](.+?)\[\/pre\]/is',
			'/\[left\](.+?)\[\/left\]/is',
			'/\[right\](.+?)\[\/right\]/is',
			'/\[center\](.+?)\[\/center\]/is',
			'/\[sub\](.+?)\[\/sub\]/is',
			'/\[sup\](.+?)\[\/sup\]/is',
			'/\[tt\](.+?)\[\/tt\]/is',
			'/\[table\]\s*(.+?)\s*\[\/table\]\s?/is',
			'/\s*\[tr\]\s*(.*?)\s*\[\/tr\]\s*/is',
			'/\s*\[td\]\s*(.*?)\s*\[\/td\]\s*/is',
			'/\[ftp\](.+?)\[\/ftp\]/is',
			'/\[ftp=(.+?)\](.+?)\[\/ftp\]/is',
			'/\[glow=(.+?),(.+?),(.+?)\](.+?)\[\/glow\]/is',
			'/\[shadow=(.+?),(.+?),(.+?)\](.+?)\[\/shadow\]/is',
			'/\[email\](.+?)\[\/email\]/is',
			'/\[hr\]/i',
			'/\[flash=(\S+?),(\S+?)\](\S+?)\[\/flash\]/is',
			'/\[list\]/',
			'/\[\/list\]/',
			'/(<\/?table>|<\/?tr>|<\/td>)<br>/'
		);

		$codetocache = array(
			'[url=mailto:\1]\1[/url]',
			"<a href=\"\\1\" target=_blank>\\1</a>",
			"<a href=\"\\1\" target=_blank>\\2</a>",
			"<a href=\"\\1\">\\1</a>",
			"<a href=\"\\1\">\\2</a>",
			"<b>\\1</b>",
			"<i>\\1</i>",
			"<u>\\1</u>",
			"<s>\\1</s>",
			"<marquee>\\1</marquee>",
			"'<br><font size=1><b><a href=\"$scripturl?action=display;\\2\">$txt[yse239]: \\1 $txt[176] '.senttime('\\3').'</a>	</b></font><table border=0 cellspacing=1 cellpadding=2 width=\"100%\"><tr><td class=quote>'",
			"</td></tr></table>",
			"<br><font size=1><b>quote:</b></font><table border=0 cellspacing=1 cellpadding=2 width=\"100%\"><tr><td class=quote>",
			"<font class=meaction>* \\1 \\2</font>",
			"<img src=\"\\1\" alt=\"\" border=\"0\">",
			"<img src=\"\\3\" alt=\"\" border=\"0\" width=\"\\1\" height=\"\\2\">",
			"<img src=\"\\3\" alt=\"\" border=\"0\" width=\"\\2\" height=\"\\1\">",
			"<font color=\"\\1\">\\2</font>",
			"<font color=\"#000000\">\\1</font>",
			"<font color=\"#FFFFFF\">\\1</font>",
			"<font color=\"#FF0000\">\\1</font>",
			"<font color=\"#00FF00\">\\1</font>",
			"<font color=\"#0000FF\">\\1</font>",
			"<font face=\"\\1\">\\2</font>",
			"<font size='\\1'>\\2</font>",
			"<pre>\\1</pre>",
			"<div align=\"left\">\\1</div>",
			"<div align=\"right\">\\1</div>",
			"<div align=\"center\">\\1</div>",
			"<sub>\\1</sub>",
			"<sup>\\1</sup>",
			"<tt>\\1</tt>",
			"<table>\\1</table>",
			"<tr>\\1</tr>",
			"<td>\\1</td>",
			"<a href=\"\\1\" target=_blank>\\1</a>",
			"<a href=\"\\1\" target=_blank>\\2</a>",
			"<table style=\"Filter: Glow(Color=\\1, Strength='.(('\\2'<400)?'\\2':400).')\" width='.(('\\3'<400)?'\\3':400).'\>\\4</table>",
			"<span style=\"Filter: Shadow(Color=\\1, Direction=\\2); width: 0 px; \">\\4</span>",
			"\\1",
			"<hr>",
			"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\\1 height=\\2><param name=movie value=\\3><param name=play value=true><param name=loop value=true><param name=quality value=high><embed src=\\3 width=\\1 height=\\2 play=true loop=true quality=high></embed></object>",
			"<ul>",
			"</ul>",
			"\\1"
		);

		// Instead we use:
			$smiliesfrom = array('::)', '>:(', '>:D', ':)', ';)', ':D', ';D', ':(', ':o', '8)', ':P', '???', ':-[', ':-X', ':-*', ":'(",':-\\','^-^','O0');
       //  :-/ :-* :'( 

		$smiliesto = array('rolleyes', 'angry', "evil", "smile", "wink", 'cheesy', 'grin', 'sad', 'shocked', 'cool', 'tongue', 'huh', 'embarassed', 'lipsrsealed', 'kiss', 'cry','undecided','azn','afro'); // You'll get the idea

		// This smiley regex makes sure it doesn't parse smilies within code tags (so [url=mailto:hide@address.com] doesn't parse the :D smiley)
		for($i=0 ; $i < count($smiliesfrom); $i++) {

			$smileyfromcache[] ='/(((>[^<]*)|(^[^<]*))([\s\n\r]|^))('.str_replace('|','\|', quotemeta(str_replace("<", "&lt;", str_replace(">", "&gt;", str_replace("\/", "\\\/", $smiliesfrom[$i]))))).')/s';

			$smileytocache[] = "\\1<img src=\"./images/smileys/$smiliesto[$i].gif\" alt=\"\" border=\"0\">";
		}

	}

	if ($enableSmilies) {
		$message = parsesmilies($message);
	}
	$message = parsecode($message);

	// List items
	$itemcode = array(
		'*' => '',
		'@' => ' type="disc"',
		'+' => ' type="square"',
		'x' => ' type="square"',
		'#' => ' type="square"',
		'o' => ' type="circle"',
		'0' => ' type="circle"'
	);

	$message = preg_replace("!\\[([*@+x#o0])\\]!Uie", '\'<li\'.$itemcode[\'\\1\'].\'>\'', $message);

	return $message;

} 

/**
 * Parses the special KeyBulletin code (also being used in UBB, YaBB, iB and other boards)
 * and returns it parsed (duh).
 * @param $message the message to parse the code in
 * @return the parsed code
 */

function parsecode($message) {
	global $codefromcache, $codetocache, $ubbcode;

	if(!strstr($message, '[') && !strstr($message, '://') && !strstr($message, '@') && !strstr($message, '/me')) {
		return $message;
	}

	if(gettype($codefromcache) != "array") {
		return $message;
	}

	$message = preg_replace($codefromcache, $codetocache, $message);

    //start resize/restrict posted images mod by Mostmaster
	$maxwidth = $ubbcode['maxwidth'];
	$maxheight = $ubbcode['maxheight'];
	if(!($maxwidth=="0" && $maxheight=="0")){
		preg_match_all('/<img src="(http:\/\/.+?)" border="0" alt="">/is', $message, $imgmatches, PREG_PATTERN_ORDER);
		for($i=0; $i<count($imgmatches[1]); $i++){
			$imagesize = @getimagesize($imgmatches[1][$i]);
			$width = $imagesize[0];
			$height = $imagesize[1];
			if($width>$maxwidth || $height>$maxheight){
				if($width>$maxwidth && $maxwidth!="0"){
					$height = floor($maxwidth/$width*$height);
					$width = $maxwidth;
					if($height>$maxheight && $maxheight!="0"){
						$width = floor($maxheight/$height*$width);
						$height = $maxheight;
					}
				}else{
					if($height>$maxheight && $maxheight!="0"){
						$width = floor($maxheight/$height*$width);
						$height = $maxheight;
					}
				}
			}
			$imgnew[$i] = "<img src=\"" . $imgmatches[1][$i] . "\" alt=\"\" border=\"0\">";
		}
		if(isset($imgnew)){
		$message = str_replace($imgmatches[0], $imgnew, $message);
		}
	}
	//end resize/restrict posted images mod by Mostmaster

	return $message;

}

/**
 * Parses smilies in the specified $message
 * @param $message the message to parse
 * @return the parsed code
 */

function parsesmilies($message) {

	global $smileyfromcache, $smileytocache;

	$oldmessage = "";
	while($oldmessage != $message) {
		$oldmessage = $message;
		$message = preg_replace($smileyfromcache, $smileytocache, $message);
	}

	return $message;

}


/**
 * Highlights passed $code (only works in PHP4+)
 * @param $code code to be parsed
 * @return the php code parsed code
 */
function phphighlight($code)
{
   if (floor(phpversion())<4)
      $buffer=$code;
   else
   {
      $code = stripslashes($code);
      $code = stripslashes($code);
      $code = str_replace("&gt;", ">", $code);
      $code = str_replace("&lt;", "<", $code);
      $code = str_replace("&#36;", "\$", $code);
      $code = str_replace("&quot;", "\"", $code);
      if (!strstr($code,'<?')) {
         $code="<?php".trim($code)."?>";
         $addedtags=1;
      }
      ob_start();
      $oldlevel=error_reporting(0);
      highlight_string($code);
      error_reporting($oldlevel);
      $buffer = ob_get_contents();
      ob_end_clean();
      $buffer = str_replace("&quot;", "\"", $buffer);
     }

   return "<font size=1><b>PHP:</b></font><table border=0 cellspacing=1 cellpadding=2 width=\"100%\"><tr><td class=quote>".addslashes($buffer)."</td></tr></table>";
}
Return current item: Private Message System