<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
//v1.9 www.exoops.info 2006/03 
// $Id: viewforum.php,v 1.8 2003/03/03 17:11:22 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Based on:
// phpBB: http://www.phpBB.com
// License type: GPL
// ------------------------------------------------------------------------- //
include_once("header.php");
$forum = intval($forum);
if ($forum == -1 || !isset($forum)) {
	redirect_header("index.php", 2, _MD_ERRORFORUM);
	exit();
}
$sql = "SELECT forum_type, forum_name, forum_access, allow_html, allow_sig, posts_per_page, hot_threshold, topics_per_page FROM ".$db->prefix("bb_forums")." WHERE forum_id=$forum";
if (!$result = $db->query($sql)) {
	redirect_header("index.php", 2, _MD_ERRORCONNECT);
	exit();
}
if (!$forumdata = $db->fetchArray($result)) {
	redirect_header("index.php", 2, _ALREADYINDATABASE);
	exit();
}
$forum_name = $myts->makeTboxData4Show($forumdata['forum_name']);
if ($forumdata['forum_type'] == 1) {
	$accesserror = 0;
	if ( $xoopsUser ) {
		if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
			if ( !check_priv_forum_auth($xoopsUser->getVar("uid"), $forum, FALSE) ) {
				$accesserror = 1;
			}
		}
	} else {
		$accesserror = 1;
	}
	if ( $accesserror == 1 ) {
		redirect_header("index.php", 2, _MD_NORIGHTTOACCESS);
		exit();
	}
}
$can_post = 0;
if ($forumdata['forum_access'] == 1) {
	if ($xoopsUser) {
		$can_post = 1;
	}
	} elseif ($forumdata['forum_access'] == 2) {
		$can_post = 1;
		} else {
			if ($xoopsUser) {
				if ( $xoopsUser->isAdmin || is_moderator($forum, $xoopsUser->getVar("uid")) ) {
					$can_post = 1;
				}
			}
		}
$bcoosConfig['title'] .= " - ".$forum_name;
include_once(XOOPS_ROOT_PATH."/header.php");
OpenTable();
echo "<table border='0' width='$bbWidth' cellpadding='5' align='center'>";
echo "<tr><td align='left'><img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='".$bbUrl['root']."index.php'>";
echo _MD_FORUMINDEX;
echo "</a><br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_close']."' alt='/' />&nbsp;&nbsp;<b>".$myts->makeTboxData4Show($forumdata['forum_name'])."</b><br />( ". _MD_MODERATEDBY .":";
list_moderators($forum);
echo " )</td><td align='right'>";
if ( $can_post ) {
	echo "<input type='button' class='button' value='"._MD_POSTNEW."' onclick='location.href=\"newtopic.php?forum=".$forum."\"' />";
	} else {
		echo "<a href='".XOOPS_URL."/register.php'>"._MD_REGTOPOST."</a>";
	}
echo "&nbsp;&nbsp;</td></tr></table>";
if (!isset($sortname) || $sortname == "") $sortname = "p.post_time";
if (!isset($sortorder) || $sortorder == "") $sortorder = "DESC";
if (!isset($sortdays) || $sortdays == "") {$sortdays = "10000"; } else { $sortdays = $newbbConfig['sort_days'];}
?>
<form action="viewforum.php" method="get">
<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="<?php echo $bbWidth;?>">
<tr><td align="center">
<b><?php echo _MD_SORTEDBY;?></b>
<select class="select" name="sortname">
<option value="t.topic_title"<?php if($sortname=="t.topic_title") echo " selected";?>><?php echo _MD_TOPICTITLE;?></option>
<option value="p.post_time"<?php if($sortname=="p.post_time") echo " selected";?>><?php echo _MD_LASTPOSTTIME;?></option>
<option value="t.topic_replies"<?php if($sortname=="t.topic_replies") echo " selected";?>><?php echo _MD_NUMBERREPLIES;?></option>
<option value="t.topic_views"<?php if($sortname=="t.topic_views") echo " selected";?>><?php echo _MD_NUMBERVIEWS;?></option>
<option value="u.uname"<?php if($sortname=="u.uname") echo " selected";?>><?php echo _MD_TOPICPOSTER;?></option>
</select>
<select class="select" name="sortorder">
<option value="ASC"<?php if($sortorder=="ASC") echo " selected";?>><?php echo _MD_ASCENDING;?></option>
<option value="DESC"<?php if($sortorder=="DESC") echo " selected";?>><?php echo _MD_DESCENDING;?></option>
</select>
<select class="select" name="sortdays">
<option value="1"<?php if($sortdays=="1") echo " selected";?>><?php echo _MD_LASTDAY;?></option>
<option value="2"<?php if($sortdays=="2") echo " selected";?>><?php printf(_FROMLASTDAYS,2);?></option>
<option value="5"<?php if($sortdays=="5") echo " selected";?>><?php printf(_FROMLASTDAYS,5);?></option>
<option value="10"<?php if($sortdays=="10") echo " selected";?>><?php printf(_FROMLASTDAYS,10);?></option>
<option value="20"<?php if($sortdays=="20") echo " selected";?>><?php printf(_FROMLASTDAYS,20);?></option>
<option value="30"<?php if($sortdays=="30") echo " selected";?>><?php printf(_FROMLASTDAYS,30);?></option>
<option value="45"<?php if($sortdays=="45") echo " selected";?>><?php printf(_FROMLASTDAYS,45);?></option>
<option value="60"<?php if($sortdays=="60") echo " selected";?>><?php printf(_FROMLASTDAYS,60);?></option>
<option value="75"<?php if($sortdays=="75") echo " selected";?>><?php printf(_FROMLASTDAYS,75);?></option>
<option value="100"<?php if($sortdays=="100") echo " selected";?>><?php printf(_FROMLASTDAYS,100);?></option>
<option value="365"<?php if($sortdays=="365") echo " selected";?>><?php echo _MD_THELASTYEAR;?></option>
<option value="1000"<?php if($sortdays=="10000") echo " selected";?>><?php echo _MD_BEGINNING;?></option>
</select>
<input type="hidden" name="forum" value="<?php echo $forum;?>">
<input type="submit" class="button" name="refresh" value="<?php echo _GO;?>">
</nobr>
</td></tr>
</table>
</form>
<?php
$startdate = ( time() - (86400 * $sortdays) );
?>
<table border="0" cellpadding="0" cellspacing="0" align="center" valign="top" width="<?php echo $bbWidth;?>"><tr><td class="bg2">
<table border="0" cellpadding="4" cellspacing="1" width="100%">
<tr class="bg3" align="left">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=t.topic_title&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="t.topic_title" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_TOPIC;?></a></B></TD>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=t.topic_replies&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="t.topic_replies" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_REPLIES?></a></b></td>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=u.uname&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="u.uname" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_POSTER;?></a></b></td>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=t.topic_views&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="t.topic_views" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_VIEWS;?></a></b></td>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=p.post_time&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="p.post_time" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _DATE;?></a></b></td>
</tr>
<?php
if ( !isset($start) || $start=="" ) $start = 0;
$start = intval($start);
$sql = "
	SELECT
	t.*,
	u.uname,
	u2.uname AS last_poster,
	u2.uid AS last_poster_uid,
	p.post_time AS last_post_time,
	p.icon
	FROM
	".$db->prefix("bb_topics")." t
	LEFT JOIN ".$db->prefix("users")." u
	ON u.uid=t.topic_poster
	LEFT JOIN ".$db->prefix("bb_posts")." p
	ON p.post_id=t.topic_last_post_id
	LEFT JOIN ".$db->prefix("users")." u2
	ON u2.uid=p.uid
	WHERE t.forum_id=$forum
	AND (p.post_time > '$startdate' OR t.topic_sticky=1)
	ORDER BY
	topic_sticky DESC, ".$sortname." ".$sortorder."";
if ( !$result = $db->query($sql, $forumdata['topics_per_page'], $start) ) {
	error_die("</table></td></tr></table><h4>"._ERROROCCURED."</h4><hr />"._MD_COULDNOTQUERY."");
}
$topics_start = $start;
while ($myrow = $db->fetchArray($result)) {
	echo"<tr>";
	if (!$myrow["last_poster"]) { $myrow["last_poster"] = $bcoosConfig['anonymous'];}
	$timenow = formatTimestamp(time(), "s");
	$timepost = formatTimestamp($myrow['last_post_time'], "s");
	if ( $timenow == $timepost) { $last_post  = formatTimestamp($myrow['last_post_time'], "t");
	} else { $last_post  = formatTimestamp($myrow['last_post_time'], "m"); }
	
	$last_post .= "<br />"._MD_BY;
	if ( !empty($myrow['last_poster_uid']) ) {
		$last_post .= " <a href='".XOOPS_URL."/userinfo.php?uid=".$myrow['last_poster_uid']."'>".$myts->makeTboxData4Show($myrow['last_poster'])."</a>";
		} else {
			$last_post .= " ".$myts->makeTboxData4Show($myrow['last_poster']);
		}
	if ($myrow['topic_sticky'] == 1) {
		$image = $bbImage['sticky'];
		} elseif ($myrow['topic_status'] == 1) {
			$image = $bbImage['locked'];
			} else {
		if ( $myrow['topic_replies'] >= $forumdata['hot_threshold']) {
			if ( $myrow['last_post_time'] > $last_visit ) {
				if ( !isset($bbtopic_time[$myrow['topic_id']]) || ($bbtopic_time[$myrow['topic_id']] < $myrow['last_post_time'] ) ) {
					$image = $bbImage['hot_newposts'];
					} else {
						$image = $bbImage['hot_folder'];
					}
		 			} else {
						$image = $bbImage['hot_folder'];
					}
			} else {
			if( $myrow['last_post_time'] > $last_visit ) {
				if ( !isset($bbtopic_time[$myrow['topic_id']]) || ($bbtopic_time[$myrow['topic_id']] < $myrow['last_post_time'] ) ) {
					$image = $bbImage['newposts'];
				} else {
					$image = $bbImage['folder'];
				}
		 	} else {
				$image = $bbImage['folder'];
			}
		}
	}
	echo "<td class='bg3' align='center'><img src='$image' /></td>";
	$topic_title = $myts->makeTboxData4Show($myrow['topic_title']);
	$pagination = "";
	$start = "";
	$topiclink = "viewtopic.php?topic_id=".$myrow['topic_id']."&amp;forum=".$forum."";
	if ( $myrow['topic_replies'] + 1 > $forumdata['posts_per_page']) {
		$pagination .= "&nbsp;&nbsp;&nbsp;(<img src=\"".$bbImage['posticon']."\" /> ";
		$pagenr      = 1;
		$skippages   = 0;
		for ($x = 0; $x < $myrow['topic_replies'] + 1; $x += $forumdata['posts_per_page']) {
			$lastpage = (($x + $forumdata['posts_per_page']) >= $myrow['topic_replies'] + 1);
			if ($lastpage) {
				$start = "&start=$x";
				} else {
					if ( $x != 0 ) {
						$start = "&start=$x";
					}
				}
			if ( $pagenr > 3 && $skippages != 1 && !$lastpage ) {
				$pagination .= ", ... ";
				$skippages = 1;
			}
			if ($skippages != 1 || $lastpage) {
				if ($x!=0) $pagination .= ", ";
				$pagination .= "<a href='$topiclink$start'>$pagenr</a>";
			}
			$pagenr++;
		}
		$pagination .= ")";
	}
	if ($myrow['icon']) {
		echo "<td class='bg1' align='center'><img src=\"".XOOPS_URL."/images/subject/".$myrow['icon']."\" /></td>\n";
	} else {
		echo "<td class='bg1' align='center'>&nbsp;</td>";
	}
	echo "<td id='topic".$myrow['topic_id']."' class='bg3'";
	echo " onmouseover='this.className=\"bg1\"; this.style.cursor=\"hand\";' onclick='window.location.href=\"" . $topiclink . "\"' onmouseout='this.className=\"bg3\";'";
	echo "> <a href='$topiclink'>$topic_title</a>$pagination";
	echo "</td>
	<td class='bg1' align='center' valign='middle'>".$myrow['topic_replies']."</td>
	<td class='bg3' align='center' valign='middle' nowrap='nowrap' onmouseover='this.className=\"bg1\";' onmouseout='this.className=\"bg3\";'>";
	if ( $myrow['topic_poster'] != 0 && $myrow['uname'] ) {
		echo "<a href='".XOOPS_URL."/userinfo.php?uid=".$myrow['topic_poster']."'>".$myrow['uname']."</a>";
	} else {
		echo $bcoosConfig['anonymous'];
	}
	echo "
	</td>
	<td class='bg1' align='center' valign='middle'>".$myrow['topic_views']."</td>
	<td class='bg3' align='right' valign='middle' nowrap='nowrap'>$last_post</td></tr>";
}
?>
</table></td></tr></table>
<table align="center" border="0" width="<?php echo $bbWidth;?>"><tr>
<td valign="top">
<img src="<?php echo $bbImage['newposts'];?>"> = <?php echo _MD_NEWPOSTS;?> <img src="<?php echo $bbImage['hot_newposts'];?>" /> = <?php printf(_MD_MORETHAN,$forumdata['hot_threshold']);?>
<br /><img src="<?php echo $bbImage['folder'];?>"> = <?php echo _MD_NONEWPOSTS?> <img src="<?php echo $bbImage['hot_folder'];?>" /> = <?php printf(_MD_MORETHAN,$forumdata['hot_threshold']);?>
<br /><img src="<?php echo $bbImage['locked'];?>"> = <?php echo _MD_TOPICLOCKED;?><br /><img src="<?php echo $bbImage['sticky'];?>"> = <?php echo _MD_TOPICSTICKY;?>
</td>
<td align="right">
<?php
$sql = "SELECT COUNT(*) FROM ".$db->prefix("bb_topics")." WHERE forum_id=$forum AND (topic_time > $startdate OR topic_sticky = 1)";
if ( !$r = $db->query($sql) ) {
	error_die(_MD_COULDNOTQUERY."</table></td></tr></table>");
}
list($all_topics) = $db->fetchRow($r);
$count = 1;
$next = $topics_start + $forumdata['topics_per_page'];
if ( $all_topics > $forumdata['topics_per_page'] ) {
	if ( $next < $all_topics ) {
		echo "<a href=\"viewforum.php?forum=$forum&start=$next&sortname=$sortname&sortorder=$sortorder&sortdays=$sortdays\">"._MD_NEXTPAGE."</a> | ";
	}
	for ( $x = 0; $x < $all_topics; $x++ ) {
		if ( !($x % $forumdata['topics_per_page']) ) {
			if( $x == $topics_start ) {
				echo "$count\n";
			} else {
				echo "<a href=\"viewforum.php?forum=$forum&start=$x&sortname=$sortname&sortorder=$sortorder&sortdays=$sortdays\">$count</a>\n";
	 			if ( !($count % 10)) {
					echo "<br />";
				}
			}
			$count++;
		}
	}
}
echo "<br />";
make_jumpbox();
?>
</td>
</tr></table>
<?php
CloseTable();
include_once(XOOPS_ROOT_PATH."/footer.php");
?>
