<?php
// $Id: index.php,v 1.2 2004/07/19 20:34:53 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS
//
//	 Relaible - Unique - Nocost &  Simplicity & ease off use
//                       < http://www.runcms.org >
// -------------------------------------------------------------------------//
// Based on:
// phpBB: http://www.phpBB.com
// License type: GPL
// ------------------------------------------------------------------------- //

include_once("header.php");

$viewcat = intval($viewcat);
$showver = intval($showver);

if ($newbbConfig['wol_enabled'])
{
	include_once($bbPath['path']."class/class.whosonline.php");
	$whosonline = new WhosOnline(0);
}

$myts->setType("admin");

$sql = "
	SELECT c.* FROM
	".$bbTable['categories']." c,
	".$bbTable['forums']." f
	WHERE
	f.cat_id=c.cat_id
	GROUP BY
	c.cat_id, c.cat_title, c.cat_order
	ORDER BY
	c.cat_order";

if (!$result = $db->query($sql)) {
	redirect_header("../../index.php", 2, _MD_COULDNOTQUERY);
	exit();
}

$total_categories = $db->num_rows($result);

if ($xoopsConfig['startpage'] == "newbb_plus") {
	$xoopsOption['show_rblock'] = 1;
	include_once(XOOPS_ROOT_PATH."/header.php");
	make_cblock();
	echo "<br />";
	} else {
		$xoopsOption['show_rblock'] = 0;
		include_once(XOOPS_ROOT_PATH."/header.php");
	}

OpenTable();
?>

<table cellpadding="2" cellspacing="0" border="0" width="<?php echo $bbWidth?>"  align="center"><tr>
<td colspan="2">
<b><?php printf(_MD_WELCOME, $meta['title']);?></b><br />
<small><?php echo _MD_TOSTART;?></small><hr />
</td>
</tr><tr valign="bottom">
<td><small><?php echo _MD_TOTALTOPICSC;?><b><?php echo get_total_topics();?></b> | <?php echo _MD_TOTALPOSTSC;?><b><?php echo get_total_posts("0", "all");?></b></small></td>

<td align="right"><small>
<?php
	$currenttime = formatTimestamp(time(), "m");
	printf(_MD_TIMENOW, $currenttime);
	echo "<br />";
	$yourlastvisit = formatTimestamp($last_visit, "m");
	printf(_MD_LASTVISIT, $yourlastvisit)
;?>
</small></td>

</tr></table>
<table border="0" width="<?php echo $bbWidth?>" cellpadding="0" cellspacing="0" align="center" valign="top"><tr>
<td class="bg2">
<table border="0" cellpadding="4" cellspacing="1" width="100%"><tr class="bg3" align="center">
<td>&nbsp;</td>
<td nowrap="nowrap" align="left"><b><?php echo _MD_FORUM;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_TOPICS;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_POSTS;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_LASTPOST;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_MODERATOR;?></b></td>
</tr>

<?php
$viewable_forums = get_viewable_forums();


if ($total_categories) {
        if ($viewcat < 1) {
		$viewcat = -1;
	}
	while ($cat_row = $db->fetch_array($result)) {
		$categories[] = $cat_row;
	}

	$where = "WHERE f.parent_forum = 0";
	if ($viewcat != -1) {
		$where .= " AND f.cat_id = $viewcat";
	}

	$sql = "
		SELECT f.*, u.uname, u.uid, p.topic_id, p.post_time, p.subject, p.icon
		FROM ".$bbTable['forums']." f
		LEFT JOIN ".$bbTable['posts']." p
		ON p.post_id = f.forum_last_post_id
		LEFT JOIN ".$db->prefix("users")." u
		ON u.uid = p.uid $where
		ORDER BY f.cat_id, f.forum_order";

	if (!$f_res = $db->query($sql)) {
		die("Error <br />$sql<br>$db->error()</br>");
	}

	while ($forum_data = $db->fetch_array($f_res)) {
		$forum_row[] = $forum_data;
	}

	for ($i=0; $i<$total_categories; $i++) {
		if ($viewcat != -1) {
			if ($categories[$i]['cat_id'] != $viewcat) {
				$title = $myts->makeTboxData4Show($categories[$i]['cat_title']);
				echo "
				<tr align='left' valign='top'>
				<td colspan='6' class='bg4'>
				<b><a href='./index.php?viewcat=".$categories[$i]['cat_id']."'>$title</a></b>
				</td></tr>";
				continue;
			}
		}

$title = $myts->makeTboxData4Show($categories[$i]['cat_title']);

echo "
<tr align='left' valign='top'>
<td colspan='6' class='bg4'>
<a href='./index.php?viewcat=".$categories[$i]['cat_id']."'><b>$title</b></a>
</td></tr>";

@reset($forum_row);
$size = count($forum_row);
for ($x=0; $x<$size; $x++) {
	unset($last_post);
	if ($forum_row[$x]['cat_id'] == $categories[$i]['cat_id']) {

		$f_id = $forum_row[$x]['forum_id'];
		if (!isset($viewable_forums[$f_id])  || $viewable_forums[$f_id] != 1 )
		{
				continue;
		}

	if ($forum_row[$x]['post_time']) {
		$forum_row[$x]['subject'] = $myts->makeTboxData4Show($forum_row[$x]['subject']);
	 	$last_post  = formatTimestamp($forum_row[$x]['post_time'], "m") . "<br />";
		$last_post .= "<a href='".$bbPath['url']."viewtopic.php?post_id=".$forum_row[$x]['forum_last_post_id']."&amp;topic_id=".$forum_row[$x]['topic_id']."&amp;forum=".$forum_row[$x]['forum_id']."#".$forum_row[$x]['forum_last_post_id']."'>";
		if ($forum_row[$x]['icon']) {
			$last_post .= "<img src='".XOOPS_URL."/images/subject/".$forum_row[$x]['icon']."' border='0' alt='' />";
			} else {
				$last_post .= "<img src='".XOOPS_URL."/images/subject/icon1.gif' border='0' alt='' />";
			}
		$last_post .= "</a><br />"._MD_BY."&nbsp;";
		if ($forum_row[$x]['uid'] != 0 && $forum_row[$x]['uname']) {
			$last_post .= "<a href='".XOOPS_URL."/userinfo.php?uid=".$forum_row[$x]['uid']."'>" . $myts->makeTboxData4Show($forum_row[$x]['uname'])."</a>";
			} else {
				$last_post .= $xoopsConfig['anonymous'];
			}
	}
	$last_post_datetime = $forum_row[$x]['post_time'];
	if (empty($last_post)) {
		$last_post = _MD_NOPOSTS;
	}
	echo "<tr align='left' valign='top'>";

	if ( ( ($last_post_datetime > $last_visit) && ($last_post != _MD_NOPOSTS)  ) &&
		!isset($HTTP_COOKIE_VARS['bbtopic_time'][$forum_row[$x]['topic_id']]) ) {
		echo "<td class='bg3' align='center' valign='middle' width='5%'><img src='".$bbImage['newposts']."' alt='' /></td>";
		} else {
			echo "<td class='bg3' align='center' valign='middle' width='5%'><img src='".$bbImage['folder']."' alt='' /></td>";
		}
	$name         = $myts->makeTboxData4Show($forum_row[$x]['forum_name']);
	$total_posts  = $forum_row[$x]["forum_posts"];
	$total_topics = $forum_row[$x]["forum_topics"];
	$myts->setType('admin');
	$desc         = $myts->makeTareaData4Show($forum_row[$x]['forum_desc'], 1, 1, 1);
	$forum_link   = $bbPath['url']."viewforum.php?forum=".$forum_row[$x]["forum_id"];

	$sf_links	  = '';
	if($forum_row[$x]['subforum_count'] > 0)
	{
		$sf_query = "
			SELECT f.*, u.uname, u.uid, p.topic_id, p.post_time, p.subject, p.icon
			FROM ".$bbTable['forums']." f
			LEFT JOIN ".$bbTable['posts']." p
			ON p.post_id = f.forum_last_post_id
			LEFT JOIN ".$db->prefix("users")." u
			ON u.uid = p.uid WHERE parent_forum=".$forum_row[$x]['forum_id']." 
			ORDER BY f.cat_id, f.forum_order";

		if ($res = $db->query($sf_query))
		{
			$bSFAdded = false;
			while ($r = $db->fetch_object($res))
			{
				$perm = get_forum_auth($r->forum_id);
				if ($perm['can_view'])
				{
					if (!$bSFAdded)
					{
						$sf_links .= "<br><br>"._MD_SUBFORUMS."&nbsp;&nbsp;";
						$bSFAdded = true;
					}
					$sf_links .= "<a href='./viewforum.php?forum=".$r->forum_id."' title='".$r->forum_desc."'>".$r->forum_name."</a>&nbsp;&nbsp;";
				}

				$total_posts  += $r->forum_posts;
				$total_topics += $r->forum_topics;

				if ($r->post_time > $last_post_datetime)
				{
					$last_post_datetime = $r->post_time;
					$r->subject = $myts->makeTboxData4Show($r->subject);
				 	$last_post  = formatTimestamp($r->post_time, "m") . "<br />";
					$last_post .= "<a href='".$bbPath['url']."viewtopic.php?post_id=".$r->forum_last_post_id."&amp;topic_id=".$r->topic_id."&amp;forum=".$r->forum_id."#".$r->forum_last_post_id."'>";
					if ($r->icon) {
						$last_post .= "<img src='".XOOPS_URL."/images/subject/".$r->icon."' border='0' alt='' />";
					} else {
						$last_post .= "<img src='".XOOPS_URL."/images/subject/icon1.gif' border='0' alt='' />";
					}
					$last_post .= "</a><br />"._MD_BY."&nbsp;";
					if ($r->uid != 0 && $r->uname) {
						$last_post .= "<a href='".XOOPS_URL."/userinfo.php?uid=".$r->uid."'>" . $myts->makeTboxData4Show($r->uname)."</a>";
					} else {
						$last_post .= $xoopsConfig['anonymous'];
					}
			}
		}
	}
	}
	if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
		$admin_link = "<a href='admin/forum_manager.php?op=edit_forum&forum_id=".$forum_row[$x]["forum_id"]."'><img src='".$bbImage['editicon']."' alt='"._EDIT."' border='0'></a>";
	}
	echo "
	<td class='bg1' onmouseover='this.className=\"bg3\"; this.style.cursor=\"hand\";' onmouseout='this.className=\"bg1\";' onclick='window.location.href=\"$forum_link\";'>
	$admin_link <a href='$forum_link'><b>$name</b></a>
	<br />$desc $sf_links</td>
	<td class='bg3' width='5%' align='center' valign='middle'>$total_topics</td>
	<td class='bg1' width='5%' align='center' valign='middle'>$total_posts</td>
	<td class='bg3' width='15%' align='center' valign='middle' onmouseover='this.className=\"bg1\";' onmouseout='this.className=\"bg3\";'>$last_post</td>
	<td class='bg1' width='5%' align='center' valign='middle' onmouseover='this.className=\"bg3\";' onmouseout='this.className=\"bg1\";'>";
	list_moderators($forum_row[$x]['forum_id']);
	echo "</td></tr>";
			}
		}
	}
}

?>
</table></td></tr></table>
<br>
<?php
if ($newbbConfig['wol_enabled'])
	echo $whosonline->show_online_list();
?>


<form name="search" action="search.php" method="post">
<table align="center" border="0" width="<?php echo $bbWidth;?>"><tr>

<td valign="middle">
<img src="<?php echo $bbImage['newposts'];?>" alt="" align=middle> = <?php echo _MD_NEWPOSTS;?>.
<br /><img src="<?php echo $bbImage['folder'];?>" alt="" align=middle> = <?php echo _MD_NONEWPOSTS;?>.
</td>

<td align="right" valign="bottom">
<b><?php echo _MD_SEARCH;?></b> <input type="text" class="text" name="term"  size="20">
<input type="hidden" name="forum" value="all">
<input type="hidden" name="sortby" value="p.post_time desc">
<input type="hidden" name="searchboth" value="both">
<input type="hidden" name="submit" value="<?php echo _MD_SEARCH;?>">
<br />[ <a href="<?php $bbPath['url'];?>search.php"><?php echo _MD_ADVSEARCH;?></a> ]
</td>

</tr></table></form>

<?php
if ($newbbConfig['rss_enable'] == 1) {
	echo "<div align='right'><a href='./cache/forum.xml' target='_blank'><img src='./images/xml.gif' border='0' vspace='2'></a></div>";
}

if($showver == 1)
{
	
	echo "<div align='right'>NewBB Plus v".get_current_version()."</div>";
}
CloseTable();
include_once(XOOPS_ROOT_PATH."/footer.php");
?>
