<?php
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
$requester = 'viewtopic';
include($phpbb_root_path . 'common.'.$phpEx);

include($config->url('includes/bbcode'));
include($config->url('includes/class_forums'));
include($config->url('includes/class_topics'));
include($config->url('includes/class_posts'));
include($config->url('includes/class_message'));

//
// Start initial var setup
//
if ( isset($HTTP_GET_VARS['font']) )
{
	$font_size=$HTTP_GET_VARS['font'];
}else{
	$font_size=10;
}
if ( isset($HTTP_GET_VARS[POST_TOPIC_URL]) )
{
	$topic_id = intval($HTTP_GET_VARS[POST_TOPIC_URL]);
}
else if ( isset($HTTP_GET_VARS['topic']) )
{
	$topic_id = intval($HTTP_GET_VARS['topic']);
}
if ( !isset($topic_id))
{	
	message_die(GENERAL_MESSAGE, 'Topic_post_not_exist');
}

//
// This rather complex gaggle of code handles querying for topics but
// also allows for direct linking to a post (and the calculation of which
// page the post is on and the correct display of viewtopic)
//
$sql = "SELECT f.forum_id
	FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f 
	WHERE   t.topic_id=" . $topic_id . "
		AND f.forum_id = t.forum_id";

if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, "Could not obtain topic information", '', __LINE__, __FILE__, $sql);
}

if ( !($forum_topic_data = $db->sql_fetchrow($result)) )
{
	message_die(GENERAL_MESSAGE, 'Topic_post_not_exist');
}

$forum_id = $forum_topic_data['forum_id'];

//
// Start session management
//
$userdata = session_pagestart($user_ip, $forum_id);
init_userprefs($userdata);
//
// End session management
//

//
// Start auth check
//
$is_auth = array();
$is_auth = auth(AUTH_ALL, $forum_id, $userdata);

if( !$is_auth['auth_view'] || !$is_auth['auth_read'] )
{
	if ( !$userdata['session_logged_in'] )
	{
		$redirect = ( isset($post_id) ) ? "txttopic.php?t=$post_id" : "txttopic.php?t=$topic_id"; 
		$redirect .= ( isset($start) ) ? "&start=$start" : '';
		$header_location = ( @preg_match("/Microsoft|WebSTAR|Xitami/", getenv("SERVER_SOFTWARE")) ) ? "Refresh: 0; URL=" : "Location: ";
		header($header_location . append_sid("login.$phpEx?redirect=$redirect", true));
		exit;
	}

	$message = ( !$is_auth['auth_view'] ) ? $lang['Topic_post_not_exist'] : sprintf($lang['Sorry_auth_read'], $is_auth['auth_read_type']);

	message_die(GENERAL_MESSAGE, $message);
}
//
// End auth check
//

	$sql = "SELECT p.post_time, p.enable_bbcode, p.enable_smilies, p.post_edit_time, p.post_edit_count, t.bbcode_uid, t.post_text, t.post_subject, u.username
		FROM " . POSTS_TABLE . " p, " . POSTS_TEXT_TABLE . " t, " . USERS_TABLE . " u 
		WHERE p.topic_id=" . $topic_id . " AND p.post_id=t.post_id AND p.poster_id=u.user_id
		ORDER BY p.post_time ASC";

			if( !($result = $db->sql_query($sql)) )
					{
						message_die(GENERAL_ERROR, 'Could not Process', '', __LINE__, __FILE__, $sql);
					}

					$IsFirst=1;
					while( $row = $db->sql_fetchrow($result) )
					{
// To be printed first go-through only.
						if ($IsFirst)
						{
							echo "<HTML><HEAD><TITLE>" . $row['post_subject'] . "</TITLE>\n";
							echo "<STYLE>\n TD {font-size: " . $font_size . "pt; } \n</STYLE></HEAD><BODY>\n";
							echo "<B><CENTER>" . $row['post_subject'] . "</CENTER></B><TABLE ALIGN=CENTER CELLSPACING=0 border=1 WIDTH=95%>\n";
							$IsFirst=0;
						}
						$bbcode_uid = $row['bbcode_uid'];
						if ( $bbcode_uid != '' )
						{
							$row['post_text'] = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($row['post_text'], $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $row['post_text']);
							$row['post_text'] = preg_replace('/\:[0-9a-z\:]+\]/si', ']', $row['post_text']);
						}
						$date=date("M d, Y -  g:i a", $row['post_time']);
						$eddate=date("M d, Y -  g:i a", $row['post_edit_time']);
						$row['post_text'] = smilies_pass($row['post_text']);
						$row['post_text'] = str_replace ("\n","<BR>", $row['post_text']);


						echo "<TR><TD ALIGN=LEFT><B>{$row['post_subject']}</B></TD><TD ALIGN=right>{$date}</TD></TR>\n";
						echo "<TR><TD COLSPAN=2 ALIGN=LEFT>{$row['username']}</TD></TR>\n";
						echo "<TR><TD COLSPAN=2 CLASS=NewsBody>{$row['post_text']}<BR><BR></TD></TR>\n";
					}
						echo "</TABLE>\n";

				
?>
</BODY></HTML>
