<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
// Based on:								     //
// myPHPNUKE Web Portal System - http://myphpnuke.com/	  		     //
// PHP-NUKE Web Portal System - http://phpnuke.org/	  		     //
// Thatware - http://thatware.org/					     //
// ------------------------------------------------------------------------- //
//  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 //
// ------------------------------------------------------------------------- //
// Author of File: Kazumi Ono (http://www.mywebaddons.com/)                  //
// ------------------------------------------------------------------------- //
include("header.php");

if ( empty($forum) || !is_numeric($forum) ) {
	redirect_header("index.php",2,_MD_ERRORFORUM);
	exit();
} elseif ( empty($topic_id) || !is_numeric($topic_id) ) {
	redirect_header("viewforum.php?forum=$forum",2,_MD_ERRORTOPIC);
	exit();
}
$forum = intval($forum);
$topic_id = intval($topic_id);
include(XOOPS_ROOT_PATH."/header.php");
include_once("class/class.forumposts.php");

$pagetype = "viewtopic";
$myts =& MyTextSanitizer::getInstance();
$sql = "SELECT t.topic_title, t.topic_status, t.topic_sticky, f.forum_name, f.forum_access, f.forum_type, f.allow_html, f.allow_sig, f.posts_per_page, f.hot_threshold, f.topics_per_page FROM ".$xoopsDB->prefix("bb_topics")." t LEFT JOIN ".$xoopsDB->prefix("bb_forums")." f ON f.forum_id = t.forum_id WHERE t.topic_id = $topic_id AND t.forum_id = $forum";

if ( !$result = $xoopsDB->query($sql) ) {
	error_die("<h4>"._MD_ERROROCCURED."</h4><hr>"._MD_COULDNOTQUERY."<br />$sql");
}

if ( !$forumdata = $xoopsDB->fetchArray($result) ) {
	error_die(_MD_FORUMNOEXIST);
}
if ( $forumdata['forum_type'] == 1 ) {
	// To get here, we have a logged-in user. So, check whether that user is allowed to view
	// this private forum.
	$accesserror = 0;
	if ( $xoopsUser ) {
		if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
			if ( !check_priv_forum_auth($xoopsUser->uid(), $forum, FALSE) ) {
				$accesserror = 1;
			}
		}
	} else {
		$accesserror = 1;
	}
	if ( $accesserror == 1 ) {
		redirect_header("index.php",2,_MD_NORIGHTTOACCESS);
		exit();
	}
		// Ok, looks like we're good.
}
$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->uid()) ) {
			$can_post = 1;
		}
	}
}

$total = get_total_posts($topic_id, "topic");
if ( $total > $forumdata['posts_per_page'] ) {
	$times = 0;
	for ($x = 0; $x < $total; $x += $forumdata['posts_per_page'] ) {
		$times++;
	}
   	$pages = $times;
}

$forumdata['topic_title'] = $myts->makeTboxData4Show($forumdata['topic_title']);
OpenTable();

include("page_header.php");

if ( isset($post_id) && $post_id != "" ) {
	$forumpost = new ForumPosts($post_id);
} else {
	$forumpost = new ForumPosts();
}

if ( $xoopsUser ) {
	if ( !isset($order) ) {
		$order = $xoopsUser->getVar("uorder");
	}
	if ( !isset($viewmode) || $viewmode=="" ) {
		if ($xoopsUser->getVar("umode") != "nocomments") {
			$viewmode = $xoopsUser->getVar("umode");
		}
	}
}
if ( isset($order) && $order == 1 ) {
	$qorder = "post_time DESC";
} else {
	$order = 0;
	$qorder = "post_time ASC";
}
$forumpost->setOrder($qorder);

if ( !isset($post_id) || $post_id == "" ) {
	$pid=0;
	$forumpost->setTopicId($topic_id);
	$forumpost->setParent($pid);
}

if ( isset($viewmode) && $viewmode=="thread" ) {
	$postsArray = $forumpost->getTopPosts();
} else {
	$viewmode = "flat";
	if ( isset($start) ) {
		$postsArray = $forumpost->getAllPosts($forumdata['posts_per_page'], $start);
	} else {
		$postsArray = $forumpost->getAllPosts($forumdata['posts_per_page']);
	}
}

//if ( !$forumpost->parent() ) {
	include("include/navbar.php");
//}
$adminview = 0;
if ( $xoopsUser ) {
	if ( $xoopsUser->isAdmin($xoopsModule->mid()) || is_moderator( $forum, $xoopsUser->uid()) ){
		$adminview = 1;
	}
}
if ( $viewmode == "flat" ) {
$count = 0;
openThread($width="95%");
foreach ( $postsArray as $obj ) {
	if ( !($count % 2) ) {
		$row_color = 1;
	} else {
		$row_color = 2;
	}
	$obj->showPost($viewmode, $order, $can_post, $forumdata['topic_status'], $forumdata['allow_sig'], $adminview, $row_color);
	$count++;
}
closeThread();
}

//if the mode is thread, show thread
if ( $viewmode=="thread" ) {
	foreach ( $postsArray as $obj ) {
		openThread();
		$obj->showPost($viewmode, $order, $can_post, $forumdata['topic_status'], $forumdata['allow_sig'], $adminview);
		closeThread();
		
		//if not in the top page, show links
		if ( $forumpost->parent() ) {
			echo "<div style='text-align:left'>";
			echo "&nbsp;<a href='".$PHP_SELF."?forum=$forum&amp;topic_id=$topic_id&amp;viewmode=$viewmode&amp;order=$order'>"._MD_TOP."</a>&nbsp;|&nbsp;<a href='$PHP_SELF?forum=$forum&amp;topic_id=$topic_id&amp;post_id=".$forumpost->parent()."&amp;viewmode=".$viewmode."&amp;order=".$order."#".$forumpost->parent()."'>"._MD_PARENT."</a>";
			echo "</div>";
		}
		echo "<br />";
		$treeArray = $forumpost->getPostTree($obj->postid());
		if ( count($treeArray) >0 ) {
			$obj->showTreeHead();
			$count = 0;
			foreach ( $treeArray as $treeItem ) {
				if ( !($count % 2) ) {
						$color_num = 1;
				} else {
					$color_num = 2;
				}
				$treeItem->showTreeItem($viewmode, $order, $color_num);
				$count++;
			}
			$obj->showTreeFoot();
		}
		echo "<br />";
	}
}

if ( !isset($refresh) && $viewmode != "thread" ) {
	$sql = "UPDATE ".$xoopsDB->prefix("bb_topics")." SET topic_views = topic_views + 1 WHERE topic_id = $topic_id";
	$xoopsDB->queryF($sql);
}

if ( !$forumpost->parent() ) {
	include("include/navbar.php");
}

echo "<br /><table width='$bbWidth'><tr><td align='left' valign='top'>";
if ( $can_post == 1 ) {
	echo "<input type='button' value='"._MD_POSTNEW."' onclick='location.href=\"newtopic.php?forum=".$forum."\"' />";
}
echo "</td><td align='right' valign='top'>";

make_jumpbox();
echo "</td></tr></table>";

if ( $xoopsUser ) {
	if ( $xoopsUser->isAdmin($xoopsModule->mid()) || is_moderator($forum,$xoopsUser->uid()) ) {
		echo "<div>";
		if ( $forumdata['topic_status'] != 1 ) {
			echo "<a href='".$bbUrl['root']."topicmanager.php?mode=lock&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['locktopic']."' alt='"._MD_LOCKTOPIC."' /></a> ";
		} else {
			echo "<a href='".$bbUrl['root']."topicmanager.php?mode=unlock&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['unlocktopic']."' alt='"._MD_UNLOCKTOPIC."' /></a> ";
		}

		echo "<a href='".$bbUrl['root']."topicmanager.php?mode=move&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['movetopic']."' alt='"._MD_MOVETOPIC."' /></a> ";
		echo "<a href='".$bbUrl['root']."topicmanager.php?mode=del&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['deltopic']."' alt='"._MD_DELETETOPIC."' /></a>";
		//add sticky
		if ( $forumdata['topic_sticky'] == 0 ) {
			echo "<a href='".$bbUrl['root']."topicmanager.php?mode=sticky&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['sticky']."' alt='"._MD_STICKYTOPIC."' /></a></div>\n";
		} else {
			echo "<a href='".$bbUrl['root']."topicmanager.php?mode=unsticky&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['unsticky']."' alt='"._MD_UNSTICKYTOPIC."' /></a></div>\n";
		}
		//
	}
}
//echo $bbtopic_time[$topic_id];
setcookie("bbtopic_time[$topic_id]", time(), 0);
CloseTable();
include(XOOPS_ROOT_PATH."/footer.php");
?>