<?php
// $Id: viewtopic.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");
include_once(XOOPS_ROOT_PATH.'/class/xoopsform/formdhtmltextarea.php');

$forum    = intval($forum);
$topic_id = intval($topic_id);

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

if ( empty($forum) ) {
	redirect_header('index.php', 2, _MD_ERRORFORUM);
	exit();
	} elseif ( empty($topic_id) ) {
		redirect_header('viewforum.php?forum='.$forum.'', 2, _MD_ERRORTOPIC);
		exit();
	}

//Début modif notification par mail
if ( $xoopsUser ) {
   $user_id = $xoopsUser->uid();
   $result = $db->query("SELECT email_notify FROM ".$bbTable['topics_mail']." WHERE topic_id = $topic_id AND usernotif_id = $user_id");
   $topicnotif = $db->fetch_array($result);
   if ( $flag_mail == 1 ) {
	  $notif_table = $bbTable['topics_mail'];
	  $sql    = "INSERT INTO ".$notif_table." ";
	  $sql    .= "(email_notify,topic_id,usernotif_id) ";
	  $sql    .= "VALUES ('1','$topic_id','$user_id')";
	  $db->query($sql);
	  $flag_reload = 1;
  } 
   if ( $flag_mail == 2 ) {
 	  $db->query("DELETE FROM ".$bbTable['topics_mail']." WHERE topic_id='$topic_id' AND usernotif_id='$user_id'");
	  $flag_reload = 1;
  }
   unset ($flag_mail);   
   
   if ( $flag_reload ) {
      $flag_reload = 0;
      redirect_header("viewtopic.php?topic_id=$topic_id&amp;forum=$forum",1,_MD_EMAILDEMAND);
      exit();
   }   
}  
//Fin modif notification par mail

include_once($bbPath['path']."class/class.forumposts.php");

$sql = "
	SELECT
	t.topic_title,
	t.topic_status,
	t.topic_sticky,
	f.forum_name,
	f.allow_html,
	f.allow_sig,
	f.posts_per_page,
	f.hot_threshold,
	f.topics_per_page,
	f.allow_attachments,
	f.attach_maxkb,
	f.attach_ext,
	f.allow_polls,
	f.parent_forum
	FROM
	".$bbTable['topics']." t
	LEFT JOIN ".$bbTable['forums']." f ON f.forum_id = t.forum_id
	WHERE
	t.topic_id = $topic_id
	AND
	t.forum_id = $forum";

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

if ( !$forumdata = $db->fetch_array($result) ) {
	redirect_header("index.php", 2, _MD_FORUMNOEXIST);
	exit();
}

$permissions = get_forum_auth($forum);
if ($permissions['can_view'] == 0)
{
	redirect_header("index.php", 2, _MD_NORIGHTTOACCESS);
	exit();
}

$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']);

cookie("bbtopic_time[$topic_id]", time());

include_once(XOOPS_ROOT_PATH."/header.php");
?>
<script>
function toggle(id)
{
	if(document.getElementById(id).style.display=="none")
	  document.getElementById(id).style.display="";
	else
	  document.getElementById(id).style.display="none";
}
</script>
<?php
OpenTable();

echo "<table border='0' width='$bbWidth' cellpadding='5' align='center'>";
$total_forum = get_total_posts($forum, 'forum');
echo "<tr><td align='left'><img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='".$bbPath['url']."index.php'>";
echo _MD_FORUMINDEX.'</a>';
if($forumdata['parent_forum'] == 0)
{
echo "</a><br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='". $bbPath['url']."viewforum.php?forum=$forum&amp;$total_forum'>".$myts->makeTboxData4Show($forumdata['forum_name'])."</a>";
echo "<br /><img src='".$bbImage['f_content']."' alt='/' />&nbsp;&nbsp;<b></a>";
echo $forumdata['topic_title']."</b>";
}
else
{
	$q = "select forum_name from ".$bbTable['forums']." WHERE forum_id=".$forumdata['parent_forum'];
	$row = $db->fetch_array($db->query($q));
	echo "<br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='".$bbPath['url']."viewforum.php?forum=".$forumdata['parent_forum']."'>".$myts->makeTboxData4Show($row['forum_name'])."</a>";
	echo "</a><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='". $bbPath['url']."viewforum.php?forum=$forum&amp;$total_forum'>".$myts->makeTboxData4Show($forumdata['forum_name'])."</a>";
	echo "<br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_content']."' alt='/' />&nbsp;&nbsp;<b>";
	echo $forumdata['topic_title']."</b>";
}
echo "</td></tr>";
echo "</table>";

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) && $xoopsUser->getVar("umode") != "0") {
		$viewmode = $xoopsUser->getVar("umode");
	}
	} else {
		if ( !isset($order) ) {
			$order = $xoopsConfig['com_order'];
		}
		if ( !isset($viewmode) && ($xoopsConfig['com_mode'] != "0") ) {
			$viewmode = $xoopsConfig['com_mode'];
		}
	}

if ($order == 1) {
	$qorder = "post_time DESC";
	} else {
		$qorder = "post_time ASC";
	}
$forumpost->setOrder($qorder);

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

if ($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']);
			}
	}

echo "<table width='$bbWidth'><tr>";
echo "<td>";
// Début notification par mail
if ( $xoopsUser ) {
  if ( $topicnotif ['email_notify'] == 1 ) {
   	 echo "&nbsp;<img border='0' src='".$bbImage['notifyoff']."' width='18' height='16'>&nbsp;&nbsp;&nbsp;<a class='titre1' href='viewtopic.php?topic_id=$topic_id&amp;flag_mail=2&amp;forum=$forum'><b>"._MD_DESACTIVEMAIL."</b></a>";
  } else {
  	echo "&nbsp;<img border='0' src='".$bbImage['notifyon']."' width='16' height='15'>&nbsp;&nbsp;&nbsp;<a class='titre1' href='viewtopic.php?topic_id=$topic_id&amp;flag_mail=1&amp;forum=$forum'><b>"._MD_ACTIVEMAIL."</b></a>";  
  } 
}
// Fin modif notification par mail
echo "</td>";
echo "<td align='right'>";
echo "<a href='print.php?forum=$forum&amp;topic_id=$topic_id' target='_blank'><img src='".$bbImage['topic_print']."' alt='"._MD_PRINT_TOPIC."' /></a>&nbsp;";

if ( $permissions['can_post'] == 1 )
{
	echo "<a href='newtopic.php?forum=$forum'><img src='".$bbImage['newthread']."'></a>&nbsp;";
}

if ($forumdata['topic_status']!=1 && $permissions['can_reply'] )
{
	echo "<a href='reply.php?forum=$forum&amp;post_id=".$postsArray[count($postsArray)-1]->postid()."&amp;topic_id=$topic_id&amp;viewmode=".$viewmode."&amp;order=".$order."'><img src='".$bbImage['reply']."' alt='"._MD_REPLY."' /></a>&nbsp;";
}
else if($forumdata['topic_status']==1)
{
	echo "<img src='".$bbImage['reply_locked']."'>";
}
echo "</td>";
echo "</tr></table>";

include("include/navbar.php");

$adminview = 0;
if ($xoopsUser) {
	if ( $xoopsUser->isAdmin($xoopsModule->mid()) || is_moderator($forum, $xoopsUser->uid())) {
		$adminview = 1;
	}
}

// ADD By xtremdj to show the polls if there are one
$sql="SELECT * from ".$bbTable['poll_desc']." where topic_id=".$topic_id;

if(!$result = $db->query($sql)){
		error_die("Problem reuesting polls");
	}
	$polldata = $db->fetch_array($result);

if (($polldata["poll_id"]!="")&&($poll_id==""))
	{
	// show the polls
	echo "<table align='center' width='$bbWidth' border='0'><tr><td>";
	include_once($bbPath['path']."class/forumpoll.php");
	include_once($bbPath['path']."class/forumpolloption.php");
	include_once($bbPath['path']."class/forumpolllog.php");
	include_once($bbPath['path']."class/forumpollrenderer.php");
	$poll = new ForumPoll($polldata["poll_id"]);
	$renderer = new ForumPollRenderer($poll);

	if($xoopsUser)
	{
		$voted_polls = ForumPollLog::hasVoted($polldata["poll_id"], $REMOTE_ADDR, $xoopsUser->getVar("uid"));
	}
	else
	{
		$voted_polls = ForumPollLog::hasVoted($polldata["poll_id"], $REMOTE_ADDR);
	}
	if ( $voted_polls  || $permissions['can_vote']==0 || $poll->hasExpired()) echo $renderer->renderResults();
	else echo $renderer->renderForm();
	echo "</td></tr></table>";
	
	}
	else
		if ($poll_id!="")
		{
		//Show result of the polls
		echo "<table align='center' width='$bbWidth' border='0'><tr><td>";
		include_once($bbPath['path']."class/forumpoll.php");
		include_once($bbPath['path']."class/forumpolloption.php");
		include_once($bbPath['path']."class/forumpolllog.php");
		include_once($bbPath['path']."class/forumpollrenderer.php");
    $poll = new ForumPoll($polldata["poll_id"]);
		$renderer = new ForumPollRenderer($poll);
		echo $renderer->renderResults();
		echo "</td></tr></table>";
  	
  	}
//END of Hack by xtremdj

if ($viewmode == "flat") {
	$count = 0;
	foreach ($postsArray as $obj) {
		if ( !($count % 2) ) {
			$row_color = 1;
			} else {
				$row_color = 2;
			}
		$forumpost->setType($obj->type);
		$obj->showPost($viewmode, $order, $permissions, $forumdata['topic_status'], $forumdata['allow_sig'], $adminview, $row_color);
		$count++;
	}
}

//if the mode is thread, show thread
if ($viewmode == "thread") {
	foreach ($postsArray as $obj) {
		$forumpost->setType($obj->type);
		$obj->showPost($viewmode, $order, $permissions, $forumdata['topic_status'], $forumdata['allow_sig'], $adminview);

		//if not in the top page, show links
		if ( $forumpost->parent() ) {
			echo "<div style='text-align:left'>";
			echo "&nbsp;<a href='./viewtopic.php?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 />";
	}
}

echo "<p>";
include("include/navbar.php");
echo "</p>";

if ($newbbConfig['wol_enabled'])
{
	$whosonline->show_online_list();
}

if ( !isset($refresh) && $viewmode != "thread" ) {
	$sql = "UPDATE ".$bbTable['topics']." SET topic_views=topic_views+1 WHERE topic_id=$topic_id";
	$db->query($sql);
}

//if ( !$forumpost->parent() )
{
echo "<table width='$bbWidth'><tr>";
echo "<td align='left'>";
if ($forumdata['topic_status']!=1 && $permissions['can_reply'] )
{
	echo "<img src='".$bbImage['quick_reply']."' onClick=\"toggle('quick_reply');\"><br>";
}
echo "</td>";
echo "<td align='right'>";

echo "<a href='print.php?forum=$forum&amp;topic_id=$topic_id' target='_blank'><img src='".$bbImage['topic_print']."' alt='"._MD_PRINT_TOPIC."' /></a>&nbsp;";

if ( $permissions['can_post'] == 1 )
{
	echo "<a href='newtopic.php?forum=$forum'><img src='".$bbImage['newthread']."'></a>&nbsp;";
}

if ($forumdata['topic_status']!=1 && $permissions['can_reply'] )
{
	echo "<a href='reply.php?forum=$forum&amp;post_id=".$postsArray[count($postsArray)-1]->postid()."&amp;topic_id=$topic_id&amp;viewmode=".$viewmode."&amp;order=".$order."'><img src='".$bbImage['reply']."' alt='"._MD_REPLY."' /></a>&nbsp;";
}
else if($forumdata['topic_status']==1)
{
	echo "<img src='".$bbImage['reply_locked']."'>";
}

echo "</td>";
echo "</tr></table>";
}

echo "<table width='$bbWidth'><tr>
<td>";
if ($xoopsUser) {
	if ( $xoopsUser->isAdmin($xoopsModule->mid()) || is_moderator($forum, $xoopsUser->uid()) ) {
		if ( $forumdata['topic_status'] != 1 ) {
			echo "<a href='".$bbPath['url']."topicmanager.php?mode=lock&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['locktopic']."' alt='"._MD_LOCKTOPIC."' /></a> ";
			} else {
				echo "<a href='".$bbPath['url']."topicmanager.php?mode=unlock&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['unlocktopic']."' alt='"._MD_UNLOCKTOPIC."' /></a> ";
			}
		echo "<a href='".$bbPath['url']."topicmanager.php?mode=move&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['movetopic']."' alt='"._MD_MOVETOPIC."' /></a> ";
		echo "<a href='".$bbPath['url']."topicmanager.php?mode=del&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['deltopic']."' alt='"._MD_DELETETOPIC."' /></a>";
		if ( $forumdata['topic_sticky'] == 0 ) {
			echo "&nbsp;<a href='".$bbPath['url']."topicmanager.php?mode=sticky&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['stickytopic']."' alt='"._MD_STICKYTOPIC."' /></a>";
			} else {
				echo "&nbsp;<a href='".$bbPath['url']."topicmanager.php?mode=unsticky&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['unstickytopic']."' alt='"._MD_UNSTICKYTOPIC."' /></a>";
			}
	}
}
echo "</td>
<td align='right' valign='top'>";
make_jumpbox();
echo "</td></tr></table>";
echo "<table width='$bbWidth'><tr align=right>";
echo "<td width='75%'><div id=\"quick_reply\" style='display:none'>";
$num_posts = count($postsArray);
echo "<form action='post.php' method='post'>";
echo "<input type='hidden' name='forum' value='".intval($forum)."' />";
echo "<input type='hidden' name='topic_id' value='".intval($topic_id)."' />";
echo "<input type='hidden' name='pid' value='".$postsArray[$num_posts-1]->post_id."' />";
echo "<input type='hidden' name='is_reply' value='1' />";
echo "<input type='hidden' name='allow_smileys' value='1' />";
echo "<input type='hidden' name='allow_bbcode' value='1' />";
echo "<input type='hidden' name='viewmode' value='$viewmode' />";

$new_subject = $postsArray[$num_posts-1]->subject;
if(substr($new_subject,0,3)!='Re:') $new_subject = 'Re: '.$new_subject;
echo "<input type='hidden' name='subject' value='$new_subject' />";
echo '<table border="0" cellpadding="0" cellspacing="0" align="center" valign="top" width="100%"><tr><td class="bg2">';
echo '<table border="0" cellpadding="4" cellspacing="1" width="100%">';
echo '<tr class="bg3" align="center"><td colspan=7><b>'._MD_QUICK_REPLY.'</b></td></tr>';
echo '<tr class="bg1"><td align=center>';
$desc = new XoopsFormDhtmlTextArea('', 'message', $message, 10, 50);
echo $desc->render();
echo '</td></tr>';
echo '<tr class="bg2"><td align=center><input class="button" type="submit" name="contents_submit" value="Reply"></input></td></tr>';
echo '</table>';
echo '</table>';
echo '</form>';
echo "</div></td>";
echo "<td align=right valign='top'>";
echo "<table width='100%' cellpadding='2'><tr><td align=left valign='top'>";
echo $permissions['can_post'] ? _MD_CAN_POST : _MD_CANNOT_POST;
echo $permissions['can_reply'] ? _MD_CAN_REPLY : _MD_CANNOT_REPLY;
echo $permissions['can_edit'] ? _MD_CAN_EDIT : _MD_CANNOT_EDIT;
echo $permissions['can_delete'] ? _MD_CAN_DELETE : _MD_CANNOT_DELETE;
echo $permissions['can_addpoll'] ? _MD_CAN_ADDPOLL : _MD_CANNOT_ADDPOLL;
echo $permissions['can_vote'] ? _MD_CAN_VOTE : _MD_CANNOT_VOTE;
echo $permissions['can_attach'] ? _MD_CAN_ATTACH : _MD_CANNOT_ATTACH;
echo "</td></tr></table>";
echo "</td></tr></table>";
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>";
}
CloseTable();
include_once(XOOPS_ROOT_PATH."/footer.php");
?>
