<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

function unhtmlspecialchars($input)
{
	$input = preg_replace("/&gt;/i", ">", $input);
	$input = preg_replace("/&lt;/i", "<", $input);
	$input = preg_replace("/&quot;/i", "\"", $input);
	$input = preg_replace("/&amp;/i", "&", $input);

	return $input;
}

function smileys($text, $specialchars=0) {
	
	if($specialchars) $text=unhtmlspecialchars($text);
	
	$filepath = "./images/smileys/";
	unset($files);
	if ($dh = opendir($filepath)) {
	    while($file = readdir($dh)) {
		    if (ereg("\.gif",$file)) $files[] = $file;
		}
	}
	foreach($files as $file) {
	    $smiley = explode(".", $file);
	    $text = eregi_replace (":".$smiley[0].":", '[IMG]images/smileys/'.$file.'[/IMG]', $text);    
	}

  $ergebnis = safe_query("SELECT * FROM `".PREFIX."smileys`");
  while($ds = mysql_fetch_array($ergebnis)) {
   $text = str_replace($ds['pattern'], '[SMILE='.$ds['alt'].']images/smileys/'.$ds['name'].'[/SMILE]', $text);
  }
  
  if($specialchars) $text=htmlspecialchars($text);

	return $text;
}

function flags($text) {

    $ergebnis = safe_query("SELECT * FROM `".PREFIX."countries`");
    while($ds = mysql_fetch_array($ergebnis)) {
      $text = str_replace ("[flag]".$ds[short]."[/flag]", '<img src="images/flags/'.$ds[short].'.gif" width="18" height="12" border="0" alt="'.$ds[country].'">', $text);
    }

    //entfernen überflüssiger flag-tags
  	$text = str_replace ("[flag][/flag]", '<img src="images/flags/na.gif" width="18" height="12" border="0" alt="not available">', $text);
  	$text = str_replace ("[flag]", '', $text);
  	$text = str_replace ("[/flag]", '', $text);
	
	return $text;
}

//replace [code]-tags

function codereplace($content) {

	$border=BORDER;
	$bg1=BG_1;
	
  $match_count = preg_match_all("#\[code\](.*?)\[\/code\]#si", $content, $code);
	for ($i = 0;$i < $match_count;$i++)
	{
   $before_replace = $code[1][$i];
   $codestring = $code[1][$i];
   $content = str_replace('[CODE]', '[code]', $content);
   $content = str_replace('[/CODE]', '[/code]', $content);
   $code_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#');
	 $code_entities_replace = array('&lt;', '&gt;', '&quot;', '&#58;', '&#91;', '&#93;', '&#40;', '&#41;', '&#123;', '&#125;');
	 $codestring = preg_replace($code_entities_match, $code_entities_replace, $codestring);
	 $content=str_replace('[code]'.$before_replace.'[/code]', '<table width="100%" cellpadding="4" border="0" bgcolor="'.$border.'" cellspacing="1" align="center"><tr><td bgcolor="'.$bg1.'"><div style="overflow:scroll;">code:[HR][FONT=COURIER]'.$codestring.'[/FONT]</div></td></tr></table>', $content);
  }
	return $content;
}

//replace [img]-tags

function imgreplace($content) {

  global $picsize_l;
  global $picsize_h;
  
  $autoresize=0;
  
  if($autoresize==1) {
  
	  preg_match_all("|(\[img\])(.*)(\[\/img\])|i", $content, $imgtags, PREG_SET_ORDER);
	  foreach($imgtags as $teil) {
	   $content = str_replace('[IMG]', '[img]', $content);
	   $content = str_replace('[/IMG]', '[/img]', $content);
	   $picinfo = getimagesize($teil[2]);
	   switch($picinfo[2]) {
	   case 1: $format = "gif"; break;
	   case 2: $format = "jpeg"; break;
	   }
	   if(!$picsize_l) $size_l = "9999"; else $size_l=$picsize_l;
	   if(!$picsize_h) $size_h = "9999"; else $size_h=$picsize_h;
	   if($picinfo[0] > $size_l OR $picinfo[1] > $size_h) $content = str_replace('[img]'.$teil[2].'[/img]', '[url='.$teil[2].']<img src="'.$teil[2].'" border="0" width="'.$picsize_l.'" alt="show original"><br>([i]auto-resize: '.$picinfo[1].'x'.$picinfo[0].'px, '.$format.'[/i])[/url]', $content);
	   elseif($picinfo[0] > (2*$size_l) OR $picinfo[1] > (2*$size_h)) $content = str_replace('[img]'.$teil[2].'[/img]', '[url='.$teil[2].'][b]Large picture[/b]<br>('.$picinfo[1].'x'.$picinfo[0].'px, '.$format.')[/url]', $content);
	   else {
	    $content = str_replace('[img]'.$teil[2].'[/img]', '<img src="'.$teil[2].'" border="0">', $content);
	   }
	  }
	 } 
	 else $content = preg_replace("#\[img\](.*?)\[/img\]#si", "<img src=\"\\1\" border=\"0\">", $content);

	return $content;

}

//replace [quote]-tags

function quotereplace($content) {

	$border=BORDER;
	$bg1=BG_1;
	
	$content = preg_replace("#\[quote=(.*?)\]#si", "[quote][b]\\1 wrote:[/b][br][hr]",$content); 
	
	//prepare: how often start- and end-tag occurrs
	$starttags = substr_count($content, '[quote]');
	$endtags = substr_count($content, '[/quote]');
	
	$overflow=abs($starttags-$endtags);
	
	for($i=0;$i<$overflow;$i++) {
		if($starttags>$endtags) $content=$content.'[/quote]';
		elseif($endtags>$starttags) $content='[quote]'.$content;
	}
		
 	$content = preg_replace("#\[quote\]#si", '<table width="100%"	cellpadding="2" cellspacing="1" bgcolor="'.$border.'"	align="center"><tr><td bgcolor="'.$bg1.'" class="small">', $content, 10);
  $content = preg_replace("#\[/quote\]#si", '</td></tr></table>',$content, 10); 
  
  //remove overflowed quote-tags
  
  $content=eregi_replace('\[quote\]','',$content);
  $content=eregi_replace('\[/quote\]','',$content);

	return $content;	

}

function insertlinks($content) {

  //insert member links
  $ergebnis = safe_query("SELECT us.userID, us.nickname, us.country FROM ".PREFIX."squads_members AS sq, ".PREFIX."user AS us WHERE sq.userID=us.userID GROUP BY us.userID");
  while($ds = mysql_fetch_array($ergebnis)) {
   $content = str_replace($ds[nickname].' ', '[flag]'.$ds[country].'[/flag] <a href="index.php?site=profile&id='.$ds[userID].'">'.$ds[nickname].'</a>&nbsp;', $content);
  }
  return $content;

}

function replacement($content, $bbcode=true) {
	$pagebg=PAGEBG;
	$border=BORDER;
	$bg1=BG_1;
	$bghead=BGHEAD;
	$bgcat=BGCAT;

	if($bbcode) {

	  $content = codereplace($content);
	  $content = imgreplace($content);
		$content = quotereplace($content);  
		$content = preg_replace("#\[url\]http://(.*?)\[img\]http://(.*?)\[/img\]\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>", $content);
		$content = preg_replace("#\[url\]www.(.*?)\[img\]www.(.*?)\[/img\]\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\"><img src=\"http://www.\\2\" border=\"0\"></a>", $content);    
		$content = preg_replace("#\[url=http://(.*?)\]\[img\]http://(.*?)\[/img\]\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>", $content);
		$content = preg_replace("#\[url=www.(.*?)\]\[img\]www.(.*?)\[/img\]\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>", $content);
	  $content = preg_replace("#\[url\]http://(.*?)\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">\\1</a>", $content);            // [url]http://.......[/url]
		$content = preg_replace("#\[url\]www.(.*?)\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">http://www.\\1</a>", $content);    // [url]www...........[/url]
		$content = preg_replace("#\[url=http://(.*?)\](.*?)\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">\\2</a>", $content);      // [url=http://..]link[/url]
		$content = preg_replace("#\[url=www.(.*?)\](.*?)\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>", $content);     // [url=www......]link[/url]
		$content = preg_replace("#\[url=(.*?)\](.*?)\[/url\]#si", "<a href=\"\\1\">\\2</a>", $content);      // relativer link
		$content = preg_replace("#\[url\](.*?)\[/url\]#si", "<a href=\"\\1\">\\1</a>", $content);      // relativer link
		$content = preg_replace("#(^|[^\"=]{1})(http://|https://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm","\\1<a href=\"\\2\\3\" target=\"_blank\">\\3</a>\\4",$content);
		$content = preg_replace("#\[email\](.*?)\[/email\]#si", "<a href=\"mailto:\\1\">\\1</a>", $content);
		$content = preg_replace("#\[email=(.*?)\](.*?)\[/email\]#si", "<a href=\"mailto:\\1\">\\2</a>", $content);
		$content = preg_replace("#\[size=(.*?)\](.*?)\[/size\]#si", "<font size=\"\\1\">\\2</font>", $content);
		$content = preg_replace("#\[color=(.*?)\](.*?)\[/color\]#si", "<font color=\"\\1\">\\2</font>", $content);
		$content = preg_replace("#\[font=(.*?)\](.*?)\[/font\]#si", "<font face=\"\\1\">\\2</font>", $content);
		$content = preg_replace("#\[align=(.*?)\](.*?)\[/align\]#si", "<div align=\"\\1\">\\2</div>", $content);
	  $content = preg_replace("#\[b\](.*?)\[/b\]#si", "<b>\\1</b>",$content); 
	  $content = preg_replace("#\[i\](.*?)\[/i\]#si", "<i>\\1</i>",$content); 
	  $content = preg_replace("#\[u\](.*?)\[/u\]#si", "<u>\\1</u>",$content); 
	  $content = preg_replace("#\[s\](.*?)\[/s\]#si", "<s>\\1</s>",$content); 
	  $content = preg_replace("#\[pre\](.*?)\[/pre\]#si","<pre>\\1</pre>", $content); 
	  $content = preg_replace("#\[list\](.*?)\[/list\]#si", "<ul>\\1</ul>", $content); 
	  $content = preg_replace("#\[list=1\](.*?)\[/list=1\]#si", "<ol>\\1</ol>", $content);
	  $content = preg_replace("#\[list=a\](.*?)\[/list=a\]#si", "<ol type=\"a\">\\1</ol>", $content);
		
		$content = preg_replace("#\[\*\](.*?)\[/\*\]#si", "<li>\\1</li>", $content);
		$content = preg_replace("#\[br]#si", "<br>", $content);
		$content = preg_replace("#\[hr]#si", "<hr noshade color=\"$border\" size=\"1\">", $content);
		$content = preg_replace("#\[center]#si", "<center>", $content);
		$content = preg_replace("#\[/center]#si", "</center>", $content);
		
	}
	$content = preg_replace("#\[SMILE=(.*?)\](.*?)\[/SMILE\]#si", '<img src="\\2" alt="\\1" border="0">', $content);

	return $content;
}

function toggle($content, $id) {
	$replace1='<table width="100%">
			    <tr><td><a href="javascript:Toggle(\''.$id.'_\\1\')"><img alt="read more" src="images/icons/expand.gif" border="0" id="ToggleImg_'.$id.'_\\1"> \\2</a></td></tr>
				<tr>
				  <td style="padding-left: 16px;"><span id="ToggleRow_'.$id.'_\\1" style="display:none">';
	$replace2='</span></td></tr></table>';
				
	$n=1;
	while($pos = strpos(strtoupper($content), "[TOGGLE=")) {
		$start = substr($content, 0, $pos);
		$end= substr($content, $pos+7);
		$middle='[TOGGLE_'.$n;
		
		$content = $start.$middle.$end;
		$n++;
	}
	
	$content = preg_replace("#\[TOGGLE_(.*?)=(.*?)\]#si", $replace1, $content);
	$content = preg_replace("#\[/TOGGLE\]#si", $replace2, $content);
	return $content;
}
?>
