<?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                                                   #
#                                                                        #
 ########################################################################
*/

if($_GET['board']) $board = $_GET['board'];
elseif($_POST['board']) $board = $_POST['board'];

$page=$_GET['page'];
$action=$_GET['action'];

function forum_stats() {
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;
	$bg1=BG_1;
	global $wincolor;
	global $loosecolor;
  global $drawcolor;

    // TODAY birthdays
	$ergebnis=safe_query("SELECT nickname, userID, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS(birthday)), '%y') 'age' FROM ".PREFIX."user WHERE DAYOFYEAR(birthday)=DAYOFYEAR(NOW()+1)");
	$n=0;
	while($db=mysql_fetch_array($ergebnis)) {
		$n++;
		$years=$db['age'];
		if($n>1) $birthdays.=', <a href="index.php?site=profile&id='.$db[userID].'"><b>'.$db[nickname].'</b></a> ('.$years.')';
		else $birthdays.='<a href="index.php?site=profile&id='.$db[userID].'"><b>'.$db[nickname].'</b></a> ('.$years.')';
	}
	if(!$n) $birthdays="n/a";
	
	
	// WEEK birthdays
	$ergebnis=safe_query("SELECT nickname, userID, DATE_FORMAT(FROM_DAYS(TO_DAYS(FROM_UNIXTIME(".(time()+(7*24*3600)).")) - TO_DAYS(birthday)), '%y') 'age' FROM ".PREFIX."user WHERE DAYOFYEAR(birthday)<=DAYOFYEAR(FROM_UNIXTIME(".(time()+(7*24*3600)).")) AND DAYOFYEAR(birthday)>DAYOFYEAR(NOW())");	
	$n=0;
	while($db=mysql_fetch_array($ergebnis)) {
		$n++;
		$years=$db['age'];
		if($n>1) $birthweek.=', <a href="index.php?site=profile&id='.$db[userID].'"><b>'.$db[nickname].'</b></a> ('.$years.')';
		else $birthweek.='<a href="index.php?site=profile&id='.$db[userID].'"><b>'.$db[nickname].'</b></a> ('.$years.')';
	}
	if(!$n) $birthweek="n/a";
	
	// WHOISONLINE
	$guests = mysql_num_rows(safe_query("SELECT ip FROM ".PREFIX."whoisonline WHERE userID=''"));
    $user = mysql_num_rows(safe_query("SELECT userID FROM ".PREFIX."whoisonline WHERE ip=''"));
    $useronline = $guests + $user;
    
	if($user==1) $user_on='1 registered user';
	else $user_on=$user.' registered users';	
	
	if($guests==1) $guests_on='1 guest';
	else $guests_on= $guests.' guests';
	
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."whoisonline WHERE ip='' ORDER BY nickname");
	if($user) {
	    $n=1;
		while($ds=mysql_fetch_array($ergebnis)) {
			if(isforumadmin($ds['userID'])) $nickname='<span style="color:'.$loosecolor.'">'.getnickname($ds[userID]).'</span>';
			elseif (isanymoderator($ds['userID'])) $nickname='<span style="color:'.$drawcolor.'">'.getnickname($ds[userID]).'</span>';
      else $nickname=getnickname($ds[userID]);
			if($n>1) $user_names .= ', <a href="index.php?site=profile&id='.$ds[userID].'"><b>'.$nickname.'</b></a>';
			else $user_names = '<a href="index.php?site=profile&id='.$ds[userID].'"><b>'.$nickname.'</b></a>';
			$n++;
		}
	}
	
	$dt=mysql_fetch_array(safe_query("SELECT count(topicID) FROM ".PREFIX."forum_topics WHERE moveID='0'"));
	$topics=$dt[0];
	$dt=mysql_fetch_array(safe_query("SELECT count(postID) FROM ".PREFIX."forum_posts"));
	$posts=$dt[0];
	$posts=$posts-$topics;
	$dt=mysql_fetch_array(safe_query("SELECT count(userID) FROM ".PREFIX."user"));
	$registered=$dt[0];
	$newestuser=safe_query("SELECT userID, nickname FROM ".PREFIX."user ORDER BY registerdate DESC LIMIT 0,1");
	$dn=mysql_fetch_array($newestuser);
	$dm=mysql_fetch_array(safe_query("SELECT maxonline FROM ".PREFIX."counter"));
	$maxonline=$dm['maxonline'];
	
	$newestmember='<a href="index.php?site=profile&id='.$dn[userID].'"><b>'.$dn[nickname].'</b></a>';
	eval ("\$forum_stats = \"".gettemplate("forum_stats")."\";");
	echo $forum_stats;
}

function boardmain() {
	global $maxposts;
	global $userID;
	global $action;
	global $loggedin;
	global $hideinternboards;
	
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;
	
	eval ("\$title_messageboard = \"".gettemplate("title_messageboard")."\";");
	echo $title_messageboard;

	if($action=="markall") {
		$alltopics=safe_query("SELECT topicID FROM ".PREFIX."forum_topics");
		while($mark=mysql_fetch_array($alltopics)) {
		    $array[]=$mark[topicID];
		    $array[]=time();
		}		
		$string_new=implode(":", $array);
		safe_query("UPDATE ".PREFIX."user SET topics='$string_new' WHERE userID='$userID'");
	}
	
	eval ("\$forum_main_head = \"".gettemplate("forum_main_head")."\";");
	echo $forum_main_head;

	// KATEGORIEN

	if(isinternboarduser($userID) OR !$hideinternboards) $intern=1;
	else $intern=0;

	$kath=safe_query("SELECT catID, name, info FROM ".PREFIX."forum_categories WHERE intern<=".$intern." ORDER BY sort");
	while($dk=mysql_fetch_array($kath)) {
    $kathname=$dk[name];
		$info='';
		if($dk[info]) $info=$dk[info];
				
		eval ("\$forum_main_kath = \"".gettemplate("forum_main_kath")."\";");
		echo $forum_main_kath;
			
		// BOARDS MIT KATEGORIE
		$boards=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE category='$dk[catID]' AND intern<=".$intern." ORDER BY sort");
        $i=1;
		while($db=mysql_fetch_array($boards)) {
	        if($i%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
			
			$board=$db['boardID'];
			$anztopics=$db['topics'];
			$anzposts=$db['posts'];
			$boardname = $db['name'];
			$boardname='&#8226; <a href="index.php?site=forum&board='.$board.'"><b>'.$boardname.'</b></a>';
			
			$boardinfo='';
			if($db[info]) $boardinfo=$db[info];
			$moderators=getmoderators($db[boardID]);
			if($moderators) $moderators='moderated by: '.$moderators;
			
			$lp=mysql_fetch_array(safe_query("SELECT topicID, lastdate, lastposter, replys FROM ".PREFIX."forum_topics WHERE boardID='$db[boardID]' AND moveID='0' ORDER BY lastdate DESC LIMIT 0,1"));
			$date='';
			$time='';
			$poster='';
			$member='';
			if(is_array($lp)) {
	    		$date=date("d.m.Y", $lp[lastdate]);
				$today=date("d.m.Y", time());
                $yesterday = date("d.m.Y", time()-3600*24);
		
                if($date==$today) $date="today";
                elseif($date==$yesterday && $date<$today) $date="yesterday";
                else $date=$date;
							
				$time=date("- H:i", $lp[lastdate]);
				$poster='<a href="index.php?site=profile&id='.$lp[lastposter].'">'.getnickname($lp[lastposter]).'</a>';
				if(isclanmember($lp[lastposter])) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
				else $member='';
  			$topic=$lp[topicID];
  			$post=getlastpost($topicID);
  			$postlink='index.php?site=forum_topic&topic='.$topic.'&type=ASC&page='.ceil(($lp['replys']+1)/$maxposts);
			}
			
			// viewed topics
			$getviewedtopics=safe_query("SELECT topics FROM ".PREFIX."user WHERE userID='$userID'");
			$gv=mysql_fetch_array($getviewedtopics);
			if($gv[topics]!="") {  
   			$string=$gv[topics];
				$array=explode(":", $string);
        $boardtopics=safe_query("SELECT topicID, lastdate, moveID FROM ".PREFIX."forum_topics WHERE boardID='$board' AND lastdate>".(time()-(30*24*3600))." ORDER BY lastdate DESC LIMIT 0,100");
				$found=0;
				while($bt=mysql_fetch_array($boardtopics) AND ($found<1)) {
					if(!$bt[moveID]) {
						if (in_array($bt[topicID], $array)){			
		  				    $x=array_search($bt[topicID], $array);
		  				    if($array[$x+1]<$bt[lastdate]) $found++;
						}
						else $found++;					
					}
				}
				if($found) $icon='<img src="images/icons/boardicons/on.gif" alt="new posts">';	
				else $icon='<img src="images/icons/boardicons/off.gif" alt="no new posts">';
			}
			else $icon='<img src="images/icons/boardicons/on.gif" alt="new posts">';
			if(!$loggedin) $icon='<img src="images/icons/boardicons/off.gif">';
			// end viewed topics	

			if($db[intern]) {
				if(isinternboarduser($userID)) {
					eval ("\$forum_main_board = \"".gettemplate("forum_main_board")."\";");
					echo $forum_main_board;
				}
				elseif(!$hideinternboards) {
					if(!isinternboarduser($userID)) $icon='<img src="images/icons/boardicons/lock.gif">';
					eval ("\$forum_main_board = \"".gettemplate("forum_main_board")."\";");
					echo $forum_main_board;
				}	
			}
			else {
				eval ("\$forum_main_board = \"".gettemplate("forum_main_board")."\";");
				echo $forum_main_board;
			}
			$i++;
		}
	}
	
	// BOARDS OHNE KATEGORIE
	$boards=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE category='0' ORDER BY sort");
    $i=1;
	while($db=mysql_fetch_array($boards)) {
        if($i%2) {
			$bg1=BG_1;
			$bg2=BG_2;
		}
		else {
			$bg1=BG_3;
			$bg2=BG_4;
		}
		
		$board=$db[boardID];
		$anztopics=$db['topics'];
		$anzposts=$db['posts'];
		
		$boardname = $db[name];
		$boardname='&#8226; <a href="index.php?site=forum&board='.$db[boardID].'"><b>'.$boardname.'</b></a>';
			
		$boardinfo='';
		if($db[info]) $boardinfo=$db[info];
		$moderators=getmoderators($db[boardID]);
		if($moderators) $moderators='moderated by: '.$moderators;
					
		$lp=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."forum_topics WHERE boardID='$db[boardID]' AND moveID='0' ORDER BY lastdate DESC LIMIT 0,1"));
		$date='';
		$time='';
		$poster='';
		$member='';
		if(is_array($lp)) {
    		$date=date("d.m.Y", $lp[lastdate]);
			$today=date("d.m.Y", time());
            $yesterday = date("d.m.Y", time()-3600*24);
		
            if($date==$today) $date="today";
            elseif($date==$yesterday && $date<$today) $date="yesterday";
            else $date=$date;
							
			$time=date("- H:i", $lp[lastdate]);
			$poster='<a href="index.php?site=profile&id='.$lp[lastposter].'">'.getnickname($lp[lastposter]).'</a>';
			if(isclanmember($lp[lastposter])) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
			else $member='';
			$topic=$lp[topicID];
		}
		
					
		// viewed topics
		$getviewedtopics=safe_query("SELECT topics FROM ".PREFIX."user WHERE userID='$userID'");
		$gv=mysql_fetch_array($getviewedtopics);
		if($gv[topics]!="") {  
			$string=$gv[topics];
			$array=explode(":", $string);
			$anzarray=count($array);

            $boardtopics=safe_query("SELECT topicID, lastdate, moveID FROM ".PREFIX."forum_topics WHERE boardID='$board'");					
			$found=0;
			while($bt=mysql_fetch_array($boardtopics)) {
				if(!$bt[moveID]) {
					if (in_array($bt[topicID], $array)){			
	  				    $x=array_search($bt[topicID], $array);
	  				    if($array[$x+1]<$bt[lastdate]) $found++;
					}
					else $found++;
				}
			}
			if($found) $icon='<img src="images/icons/boardicons/on.gif" alt="new posts">';	
			else $icon='<img src="images/icons/boardicons/off.gif" alt="no new posts">';
		}
		else $icon='<img src="images/icons/boardicons/on.gif" alt="new posts">';
		if(!$loggedin) $icon='<img src="images/icons/boardicons/off.gif">';
		// end viewed topics	

		if($db[intern]) {
			if(isinternboarduser($userID)) {
				eval ("\$forum_main_board = \"".gettemplate("forum_main_board")."\";");
				echo $forum_main_board;
			}
			elseif(!$hideinternboards) {
				if(!isinternboarduser($userID)) $icon='<img src="images/icons/boardicons/lock.gif">';
				eval ("\$forum_main_board = \"".gettemplate("forum_main_board")."\";");
				echo $forum_main_board;
			}	
		}
		else {
			eval ("\$forum_main_board = \"".gettemplate("forum_main_board")."\";");
			echo $forum_main_board;
		}
		$i++;
	}
	
	eval ("\$forum_main_foot = \"".gettemplate("forum_main_foot")."\";");
	echo $forum_main_foot;
	
	if($loggedin) {
		eval ("\$forum_main_legend = \"".gettemplate("forum_main_legend")."\";");
		echo $forum_main_legend;
	}
	
	
	forum_stats();
}

function showboard($board) {
    global $userID;
    global $loggedin;
	global $maxtopics;
	global $maxposts;
	global $page;
	global $action;
	
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;

	eval ("\$title_messageboard = \"".gettemplate("title_messageboard")."\";");
	echo $title_messageboard;
	
	$alle=safe_query("SELECT topicID FROM ".PREFIX."forum_topics WHERE boardID='$board'");
	$gesamt=mysql_num_rows($alle);
	
	if($action=="markall") {
		$getviewedtopics=safe_query("SELECT topics FROM ".PREFIX."user WHERE userID='$userID'");
    	$gv=mysql_fetch_array($getviewedtopics);
		$time=time();
		$string=$gv[topics];
		$array=explode(":", $string);
		while($ma=mysql_fetch_array($alle)) {
			if($gv[topics]!="") {  
				if(in_array($ma[topicID], $array)) {
				    $x=array_search($ma[topicID], $array);
					$y=$x+1;
					$array[$y]=$time;
				}
				else {
			        $array[]=$ma[topicID];
				    $array[]=$time;
			    }
			}
			else {
			    $array[]=$ma[topicID];
				$array[]=$time;
			}
		}
		$string_new=implode(":", $array);
		safe_query("UPDATE ".PREFIX."user SET topics='$string_new' WHERE userID='$userID'");
	}
	
	$pages=1;
	if(!isset($page) || $page=='') $page=1;
	$max=$maxtopics;

				
    for ($n=$max; $n<=$gesamt; $n+=$max) {
	    if($gesamt>$n) $pages++;
	}
	
	if($pages>1) $page_link = makepagelink("index.php?site=forum&board=$board", $page, $pages);
	
	if($page==1) $start=0;
	if($page>1) $start=$page*$max-$max;
	
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE boardID='$board' ");
	$db = mysql_fetch_array($ergebnis);
	$boardname = $db[name];
	
	if($db[intern]) {
	    $check=safe_query("SELECT userID FROM ".PREFIX."user_groups WHERE internboards='1' AND userID='$userID'");
		if(! mysql_NUM_ROWS($check)) die('You have no permission to view intern boards!<br><br>
		                                  <meta http-equiv="refresh" content="3;URL=index.php?site=forum">');
	}
	
	$moderators=getmoderators($board);
	if($moderators) $moderators='(moderated by: '.$moderators.')';
	
	eval ("\$forum_head = \"".gettemplate("forum_head")."\";");
    echo $forum_head;
	
	if(isforumadmin($userID)) $actions='<a href="index.php?site=forum&board='.$board.'&action=admin-action&admaction=announce"><img src="images/icons/announce.gif" border="0"></a> ';
	$actions.='<a href="index.php?site=search"><img src="images/icons/search.gif" border="0"></a> ';
	if($loggedin) {
		$mark='&#8226; <a href="index.php?site=forum&board='.$board.'&action=markall">mark all topics read</a>';
		$actions.='<a href="index.php?site=forum&addtopic=true&board='.$board.'"><img src="images/icons/newtopic.gif" border="0"></a>';
	}
      
	$i=1;
	// ANNOUNCEMENTS
	$announce=safe_query("SELECT * FROM ".PREFIX."forum_announcements WHERE boardID='$board' ORDER BY date DESC");
	$anz=mysql_num_rows(safe_query("SELECT announceID FROM ".PREFIX."forum_announcements WHERE boardID='$board'"));
	if($anz) {
		eval ("\$forum_announcement_head = \"".gettemplate("forum_announcement_head")."\";");
	    echo $forum_announcement_head;

		while($da=mysql_fetch_array($announce)) {
			if($i%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
			
			$date=date("d.m.Y", $da[date]);
			$time=date("H:i", $da[date]);
			$topicname=cleartext($da[topic]);
			$today=date("d.m.Y", time());
		    $yesterday = date("d.m.Y", time()-3600*24);
		
		    if($date==$today) $date="today";
		    elseif($date==$yesterday && $date<$today) $date="yesterday";
		    else $date=$date;
			
      $gesamt=1;
			$folder='<img src="images/icons/foldericons/announce.gif" alt="Announcement">';
	    $topictitle=cleartext($da[topic]);
			$poster='<a href="index.php?site=profile&id='.$da[userID].'">'.getnickname($da[userID]).'</a>';
			if(isclanmember($da[userID])) $member1=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
			else $member1='';
	    $lastposter='<a href="index.php?site=profile&id='.$da[userID].'">'.getnickname($da[userID]).'</a>';
			if(isclanmember($da[userID])) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
			else $member='';
	    $link='<a href="index.php?site=forum_topic&announcement='.$da[announceID].'"><b>'.$topictitle.'</b></a>';
			$replys=0;
			$views=$da[views];
			
			eval ("\$forum_announcement_content = \"".gettemplate("forum_announcement_content")."\";");
	        echo $forum_announcement_content;
   		    $i++;
		}
		eval ("\$forum_announcement_foot = \"".gettemplate("forum_announcement_foot")."\";");
	    echo $forum_announcement_foot;
	}
	
	eval ("\$forum_actions = \"".gettemplate("forum_actions")."\";");
    echo $forum_actions;
	
	// TOPICS

	$topics = safe_query("SELECT * FROM ".PREFIX."forum_topics WHERE boardID='$board' ORDER BY sticky DESC, lastdate DESC LIMIT $start,$max");
	$anztopics = mysql_num_rows(safe_query("SELECT boardID FROM ".PREFIX."forum_topics WHERE boardID='$board'"));
	
	$i=1;
	unset($link);
	if($anztopics) {
		eval ("\$forum_topics_head = \"".gettemplate("forum_topics_head")."\";");
		echo $forum_topics_head;
		while($dt=mysql_fetch_array($topics)) {
			if($i%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
			
	        if($dt[moveID]) $gesamt=0;
			else $gesamt=$dt[replys]+1;
			
	        $topicpages=1;
			
            $max=$maxposts;
				
    		for ($n=$max; $n<=$gesamt; $n+=$max) {
	    		if($gesamt>$n) $topicpages++;
			}
	        if($topicpages>1) $topicpage_link = makepagelink("index.php?site=forum_topic&topic=$dt[topicID]", 1, $topicpages);
			
			if($dt[icon]) $icon='<img src="images/icons/topicicons/'.$dt[icon].'">';
		    else $icon='';
			
			// viewed topics
			if($dt[closed]) $folder='<img src="images/icons/foldericons/lockfolder.gif" alt="closed">';
			elseif($dt[moveID]) $folder='<img src="images/icons/topicicons/pfeil.gif" alt="moved">';
			elseif($dt[sticky]) $folder='<img src="images/icons/foldericons/sticky.gif" alt="sticky">';
			else {
			    $getviewedtopics=safe_query("SELECT topics FROM ".PREFIX."user WHERE userID='$userID'");
    			$gv=mysql_fetch_array($getviewedtopics);
				if($gv[topics]!="") {  
	    			$string=$gv[topics];
					$array=explode(":", $string);
    				$anzarray=count($array);

    				if (in_array($dt[topicID], $array)) {			
				  		$x=array_search($dt[topicID], $array);
				  		if($array[$x+1]<$dt[lastdate]) {
						    if($dt[replys]>15 || $dt[views]>150) $folder='<img src="images/icons/foldericons/newhotfolder.gif" alt="new posts [popular]">';
							else $folder='<img src="images/icons/foldericons/newfolder.gif" alt="new posts">';
						}	
				  		else {
						    if($dt[replys]>15 || $dt[views]>150) $folder='<img src="images/icons/foldericons/hotfolder.gif" alt="no new posts [popular]">';
							else $folder='<img src="images/icons/foldericons/folder.gif" alt="no new posts">';
						}	
					}
					else {
					    if($dt[replys]>15 || $dt[views]>150) $folder='<img src="images/icons/foldericons/newhotfolder.gif" alt="new posts [popular]">';			
						else $folder='<img src="images/icons/foldericons/newfolder.gif" alt="new posts">';			
					}	
				}
				else {
				    if($dt[replys]>15 || $dt[views]>150) $folder='<img src="images/icons/foldericons/newhotfolder.gif" alt="new posts [popular]">';
					else $folder='<img src="images/icons/foldericons/newfolder.gif" alt="new posts">';
				}	
			}	
			// end viewed topics
			
			if(!$loggedin) {
			    if($gesamt>15) $folder='<img src="images/icons/foldericons/hotfolder.gif">';
				else $folder='<img src="images/icons/foldericons/folder.gif">';
			}
			if($dt[sticky]) $folder='<img src="images/icons/foldericons/sticky.gif" alt="sticky">';

			$topictitle=getinput($dt[topic]);
			$topictitle=str_break($topictitle, 60);
			
			$poster='<a href="index.php?site=profile&id='.$dt[userID].'">'.getnickname($dt[userID]).'</a>';
			if(isclanmember($posterID)) $member1=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
			else $member1='';
			
			$replys='0';
			$views='0';
			
			if($dt[moveID]) { // MOVED TOPIC
				$move=safe_query("SELECT * FROM ".PREFIX."forum_topics WHERE topicID='$dt[moveID]'");
				$dm=mysql_fetch_array($move);
				
				if($dm[replys]) $replys=$dm[replys];
				if($dm[views]) $views=$dm[views];

 			    $date=date("d.m.y", $dm[lastdate]);
					$time=date("H:i", $dm[lastdate]);
					$today=date("d.m.y", time());
			    $yesterday = date("d.m.y", time()-3600*24);
			    if($date==$today) $date="today, ".$time;
			    elseif($date==$yesterday && $date<$today) $date="yesterday, ".$time;
			    else $date=$date.", ".$time;
					$lastposter='<a href="index.php?site=profile&id='.$dm[lastposter].'">'.getnickname($dm[lastposter]).'</a>';
					if(isclanmember($dm[lastposter])) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
					else $member='';
					$link='<a href="index.php?site=forum_topic&topic='.$dt[moveID].'"><b>MOVED: '.$topictitle.'</b></a>';

			}
      else {	// NO MOVED TOPIC
				if($dt[replys]) $replys=$dt[replys];
				if($dt[views]) $views=$dt[views];

		    $date=date("d.m.y", $dt[lastdate]);
				$time=date("H:i", $dt[lastdate]);
				$today=date("d.m.y", time());
		    $yesterday = date("d.m.y", time()-3600*24);
  	    if($date==$today) $date="today, ".$time;
		    elseif($date==$yesterday && $date<$today) $date="yesterday, ".$time;
		    else $date=$date.", ".$time;
				$lastposter='<a href="index.php?site=profile&id='.$dt[lastposter].'">'.getnickname($dt[lastposter]).'</a>';
				if(isclanmember($dt[lastposter])) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
				else $member='';
			  $link='<a href="index.php?site=forum_topic&topic='.$dt[topicID].'"><b>'.$topictitle.'</b></a>';
			}

			eval ("\$forum_topics_content = \"".gettemplate("forum_topics_content")."\";");
	        echo $forum_topics_content;
   		    $i++;
			unset($topicpage_link);	 
			unset($lastposter);
			unset($member);
			unset($member1);
			unset($date);
			unset($time);
			unset($link);
		
		}
		eval ("\$forum_topics_foot = \"".gettemplate("forum_topics_foot")."\";");
		echo $forum_topics_foot;
			   
		eval ("\$forum_actions = \"".gettemplate("forum_actions")."\";");
		echo $forum_actions;
	}
	
	if($loggedin) {
		eval ("\$forum_topics_legend = \"".gettemplate("forum_topics_legend")."\";");
	    echo $forum_topics_legend;
	}	
			 
	if(!$loggedin) echo'<br><br>To start a topic you have to be registered and logged in!<br><br>
	                    &#8226; <b><a href="index.php?site=register">register now</a></b><br>
                      &#8226; <b><a href="index.php?site=login">login</a></b>';
	
	unset($page_link);	 
}

if($_POST['submit'] || $_POST['movetopic'] || $_GET['addtopic'] || $_POST['addtopic'] || $_GET['action'] == "admin-action") {

if($_POST['admaction']=="closetopic") {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');

  $topicID = $_POST['topicID'];
  $board = $_POST['board'];

	if(!isforumadmin($userID) AND !ismoderator($userID, $board)) die('No access');

  safe_query("UPDATE ".PREFIX."forum_topics SET closed='1' WHERE topicID='$topicID' ");
	header("Location: index.php?site=forum&board=$board");
}
elseif($_POST['admaction']=="opentopic") {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');

  $topicID = $_POST['topicID'];
  $board = $_POST['board'];

	if(!isforumadmin($userID) AND !ismoderator($userID, $board)) die('No access');

 	safe_query("UPDATE ".PREFIX."forum_topics SET closed='0' WHERE topicID='$topicID' ");
	header("Location: index.php?site=forum&board=$board");	
}
elseif($_POST['admaction']=="deletetopic") {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');

  $topicID = $_POST['topicID'];
  $board = $_POST['board'];

	if(!isforumadmin($userID) AND !ismoderator($userID, $board)) die('No access');

  safe_query("DELETE FROM ".PREFIX."forum_topics WHERE topicID='$topicID' ");
	safe_query("DELETE FROM ".PREFIX."forum_topics WHERE moveID='$topicID' ");
	safe_query("DELETE FROM ".PREFIX."forum_posts WHERE topicID='$topicID' ");
	header("Location: index.php?site=forum&board=$board");
}
elseif($_POST['admaction']=="stickytopic") {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');

  $topicID = $_POST['topicID'];
  $board = $_POST['board'];

	if(!isforumadmin($userID) AND !ismoderator($userID, $board)) die('No access');

 	safe_query("UPDATE ".PREFIX."forum_topics SET sticky='1' WHERE topicID='$topicID' ");
	header("Location: index.php?site=forum&board=$board");	
}
elseif($_POST['admaction']=="unstickytopic") {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');

  $topicID = $_POST['topicID'];
  $board = $_POST['board'];

	if(!isforumadmin($userID) AND !ismoderator($userID, $board)) die('No access');

 	safe_query("UPDATE ".PREFIX."forum_topics SET sticky='0' WHERE topicID='$topicID' ");
	header("Location: index.php?site=forum&board=$board");	
}
elseif($_POST['admaction']=="delposts") {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');

  $topicID = $_POST['topicID'];
  $postID = $_POST['postID'];
  $board = $_POST['board'];

	if(!isforumadmin($userID) AND !ismoderator($userID, $board)) die('No access');

	foreach($postID as $id) {
	    $last = safe_query("SELECT * FROM ".PREFIX."forum_posts WHERE topicID = '$topicID' ");
		$anz = mysql_num_rows($last);
		if($anz > 1) {
		    safe_query("DELETE FROM ".PREFIX."forum_posts WHERE postID='$id' ");
			$last = safe_query("SELECT * FROM ".PREFIX."forum_posts WHERE topicID = '$topicID' ORDER BY date DESC LIMIT 0,1 ");
			$dl = mysql_fetch_array($last);
			safe_query("UPDATE ".PREFIX."forum_topics SET lastdate='$dl[date]', lastposter='$dl[poster]', replys=replys-1 WHERE topicID='$topicID' ");
			$deleted=false;
		}
		else {
		    safe_query("DELETE FROM ".PREFIX."forum_posts WHERE postID='$id' ");
			safe_query("DELETE FROM ".PREFIX."forum_topics WHERE topicID='$topicID' ");
			safe_query("DELETE FROM ".PREFIX."forum_topics WHERE moveID='$topicID' ");
			$deleted=true;
	    }
	}
	if($deleted) header("Location: index.php?site=forum&board=$board");
	else header("Location: index.php?site=forum&board=$board&topic=$topicID");
}
elseif($_GET['admaction']=="announce") {
  $board = $_GET['board'];
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE boardID='$board' ");
	$db = mysql_fetch_array($ergebnis);
	$boardname = $db[name];

	$bg1=BG_1;
	$bg2=BG_2;
	$bg3=BG_3;
	$bg4=BG_4;

	$moderators=getmoderators($board);

	eval ("\$forum_head = \"".gettemplate("forum_head")."\";");
	echo $forum_head;
		
	eval ("\$addbbcode = \"".gettemplate("addbbcode")."\";");
	eval ("\$forum_announcement_post = \"".gettemplate("forum_announcement_post")."\";");
    echo $forum_announcement_post;
}
elseif($_POST['movetopic']) {
	include("_mysql.php");
	include("_settings.php");
  include('_functions.php');

	$toboard = $_POST['toboard'];
	$topicID = $_POST['topicID'];
	
	if(!isanyadmin($userID) AND !ismoderator($userID, getboardid($topicID))) die('No access');

	$di=mysql_fetch_array(safe_query("SELECT intern FROM ".PREFIX."forum_boards WHERE boardID='$toboard'"));
	
	$ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_topics WHERE topicID='$topicID'");
	$ds=mysql_fetch_array($ergebnis);
				
	// EINTAG DES moveID
	if($_POST['movelink']) safe_query("INSERT INTO ".PREFIX."forum_topics (boardID, icon, intern, userID, date, topic, lastdate, lastposter, replys, views, closed, moveID)
	             values ('$ds[boardID]', '', '', '$ds[userID]', '$ds[date]', '$ds[topic]', '$ds[lastdate]', '', '', '', '', '$topicID') ");
				
	// ÄNDERN DES EINTRAGES
	safe_query("UPDATE ".PREFIX."forum_topics SET boardID='$toboard', intern='$ds[intern]' WHERE topicID='$topicID'");
	safe_query("UPDATE ".PREFIX."forum_posts SET boardID='$toboard' WHERE topicID='$topicID'");
	$post_num = mysql_affected_rows();
	safe_query("UPDATE ".PREFIX."forum_boards SET topics=topics+1 WHERE boardID='$toboard'");
	safe_query("UPDATE ".PREFIX."forum_boards SET topics=topics-1 WHERE boardID='".$ds['boardID']."'");	
	safe_query("UPDATE ".PREFIX."forum_boards SET posts=posts+".$post_num." WHERE boardID='".$toboard."'");	
	safe_query("UPDATE ".PREFIX."forum_boards SET posts=posts-".$post_num." WHERE boardID='".$ds['boardID']."'");	

	header("Location: index.php?site=forum&board=$toboard");
}
elseif($_POST['admaction']=="movetopic") {
	include("_mysql.php");
	include("_settings.php");
  include('_functions.php');
	if(!isanyadmin($userID) AND !ismoderator($userID, getboardid($_POST['topicID']))) die('No access');

	echo'<html>
			<head>
			<title>'.PAGETITLE.'</title>
			<link href="_stylesheet.css" rel="stylesheet" type="text/css">
			<script src="js/bbcode.js" language="jscript" type="text/javascript"></script>
			</head>
			<body bgcolor="'.PAGEBG.'">';

		 	
	$kath=safe_query("SELECT * FROM ".PREFIX."forum_categories ORDER BY sort");
    while($dk=mysql_fetch_array($kath)) {
		$ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE category='$dk[catID]' ORDER BY sort");
	    while($db=mysql_fetch_array($ergebnis)) {
		    $boards.='<option value="'.$db[boardID].'">'.$dk[name].' - '.$db[name].'</option>';
		}
	}	

    $ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE category='0' ORDER BY sort");
    while($ds=mysql_fetch_array($ergebnis)) {
		$boards.='<option value="'.$ds[boardID].'">'.$ds[name].'</option>';
	}
			
	echo'<br><br><form action="forum.php?board='.$_POST['board'].'" method="post">
		 <table width="800" cellspacing="1" cellpadding="2" align="center" bgcolor="'.BORDER.'">
		  <tr>
		    <td class="title" bgcolor="'.BGHEAD.'" align="center">MOVE TOPIC</td>
		  </tr>
		  <tr>
		    <td bgcolor="'.BG_1.'"></td>
		  </tr>
		  <tr>
		    <td bgcolor="'.BG_1.'" align="center" height="100">
				move this topic to board: <select name="toboard">'.$boards.'</select><br>
				<input type="checkbox" name="movelink" value="1" checked> Leave shadowtopic?<br><br>
			   <input type="hidden" name="admaction" value="movetopic">
			   <input type="hidden" name="topicID" value="'.$_POST['topicID'].'">
			   <input type="hidden" name="board" value="'.$_POST['board'].'">
			   <input type="submit" name="movetopic" value="move topic">
			</td>
		  </tr>
		</table></form></body></html>';
}
elseif($_POST['newtopic'] && $_POST['preview']!=1) {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');
  if(!$userID) die('Not logged in.');
	
	$board = $_POST['board'];
	$intern = $_POST['intern'];
  $icon = $_POST['icon'];
  $topicname = $_POST['topicname']; if(!$topicname) $topicname = "(no title)";
  $message = $_POST['message'];
  $topic_sticky = (isset($_POST['sticky'])) ? '1' : '0';
	
    $ds=mysql_fetch_array(safe_query("SELECT intern FROM ".PREFIX."forum_boards WHERE boardID='$board'"));
	$intern=$ds[intern];
	
	$date=time();
	safe_query("INSERT INTO ".PREFIX."forum_topics ( boardID, intern, userID, date, icon, topic, lastdate, lastposter, replys, views, closed, sticky )
                             values ( '$board', '$intern', '$userID', '$date', '$icon', '".mysql_escape_string($topicname)."', '$date', '$userID', '0', '0', '0', '$topic_sticky' ) ");
	$id=mysql_insert_id();
	safe_query("UPDATE ".PREFIX."forum_boards SET topics=topics+1 WHERE boardID='".$board."' ");                             
	safe_query("INSERT INTO ".PREFIX."forum_posts ( boardID, topicID, date, poster, message )
                             values( '$board', '$id', '$date', '$userID', '".mysql_escape_string($message)."' ) ");
							 
	if($notify) safe_query("INSERT INTO ".PREFIX."forum_notify (topicID, userID) values('$topic', '$userID') ");
	header("Location: index.php?site=forum&board=".$board."");
}
elseif($_GET['addtopic'] OR $_POST['addtopic']) {
	eval ("\$title_messageboard = \"".gettemplate("title_messageboard")."\";");
	echo $title_messageboard;

	$ergebnis = safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE boardID='$board' ");
	$db = mysql_fetch_array($ergebnis);
	$boardname = $db[name];
	
	$moderators=getmoderators($board);

	eval ("\$forum_head = \"".gettemplate("forum_head")."\";");
	echo $forum_head;

	$bg1=BG_1;

	if($loggedin) {
		if($_POST['preview']) {
				
			$bg1=BG_1;
			$bg2=BG_2;
				
			$message_preview = $_POST['message'];
			$time=date("H:i", time());
			$date="today";
			$message = cleartext($_POST['message']);
			$message = toggle($message, 'xx');
			$username='<a href="index.php?site=profile&id='.$userID.'"><b>'.getnickname($userID).'</b></a>';
			
			$board = $_POST['board'];
			$topicname = $_POST['topicname'];
			
			if(isclanmember($userID)) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
			else $member='';
			if(getavatar($userID)) $avatar='<img src="images/avatars/'.getavatar($userID).'">';
			else $avatar='';
			if(getsignatur($userID)) $signatur='<hr width="200" noshade color="'.BORDER.'" align="left" size="1">'.cleartext(getsignatur($userID));
			else $signatur='';
			if (getemail($userID)) $email = '<a href="mailto:'.getemail($userID).'"><img src="images/icons/email.gif" border="0" width="15" height="11" alt="email"></a>';
			else $email='';
			$pm='';
			$buddy='';
			$statuspic='<img src="images/icons/online.gif" width="7" height="7" alt="online">';
			$sem = '^[http://]+[a-z0-9_\.-]+[a-z0-9_-]+$';
			if(!(eregi($sem, gethomepage($userID)))) $hp='';
			else $hp='<a href="'.gethomepage($userID).'" target="_blank"><img src="images/icons/hp.gif" border="0" width="14" height="14" alt="homepage"></a>';
			$registered = getregistered($userID);
			$posts = getuserforumposts($userID);
			if(isforumadmin($userID) || ismoderator($userID, $board)) {
			    if(ismoderator($userID, $board)) {
				    $usertype="Moderator";
				    $rang='<img src="images/icons/ranks/moderator.gif">';
				    if(isset($_POST['sticky'])){
              $_sticky = 'checked="checked"';
            }
				}
				if(isforumadmin($userID)) {
				  $usertype="Administrator";
					$rang='<img src="images/icons/ranks/admin.gif">';
					if(isset($_POST['sticky'])){
            $_sticky = 'checked="checked"';
          }
				}
			}	
			else {
			    $ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_ranks WHERE $posts >= postmin AND $posts <= postmax");
				$ds=mysql_fetch_array($ergebnis);
				$usertype=$ds[rank];
				$rang='<img src="images/icons/ranks/'.$ds[pic].'">';
			}
			$action='';
				
			echo'<table width="100%" cellspacing="1" cellpadding="2" bgcolor="'.BORDER.'">
				 <tr bgcolor="'.BGHEAD.'"><td colspan="2" class="title" align="center">'.cleartext($topicname).'</td></tr>
				 <tr bgcolor="'.PAGEBG.'"><td colspan="2"></td></tr>';
				 
			eval ("\$forum_topic_content = \"".gettemplate("forum_topic_content")."\";");
		    echo $forum_topic_content;
			echo'</table>';
		}
		
		eval ("\$addbbcode = \"".gettemplate("addbbcode")."\";");
		
		$message=$message_preview;
		if(isforumadmin($userID) || ismoderator($userID, $board)) {
      if(isset($_sticky)){
			  $chk_sticky = '<br />'."\n".' <input class="input" type="checkbox" name="sticky" value="1" '.$_sticky.'> Make this topic sticky';
      }
      else{
        $chk_sticky = '<br />'."\n".' <input class="input" type="checkbox" name="sticky" value="1"> Make this topic sticky';
      }
		}
		else {
      $chk_sticky = '';
		}
		eval ("\$forum_newtopic = \"".gettemplate("forum_newtopic")."\";");
		  echo $forum_newtopic;	   
	}
	else {
    	echo'<br><br>To start a topic you have to be registered and logged in!<br><br>
	     	&#8226; <b><a href="index.php?site=register">register now</a></b><br>
        &#8226; <b><a href="index.php?site=login">login</a></b>';
	}
} elseif(!$_POST['admaction']) {

header("Location: index.php?site=forum");

}

} elseif(!isset($board)) {
    boardmain();
}
else showboard($board);

?>
