<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	論壇查看主題頁面

	$RCSfile: viewthread.php,v $
	$Revision: 1.33 $
	$Date: 2006/10/31 04:58:44 $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}

$tid = empty($_SGET['tid'])?0:intval($_SGET['tid']);
if($tid) {
	dbconnect();
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('threads', 1).' WHERE tid=\''.$tid.'\'');
	$thread = $_SGLOBAL['db']->fetch_array($query);
}
if(empty($thread)) {
	messagebox('error', 'not_found', S_URL);
}

$threadurl = B_URL.'/viewthread.php?tid='.$tid;

$jumptobbs = false;
if(!empty($thread['readperm'])) {
	$jumptobbs = true;
} elseif (!empty($thread['price'])) {
	$jumptobbs = true;
}
if(B_VER == '5') {
	if($thread['supe_pushstatus'] <= 0) {
		$jumptobbs = true;
	}
}
if($jumptobbs) {
	header('location: '.$threadurl);
	exit;
}

if(!empty($_SCONFIG['htmlviewnews'])) {
	$_SHTML['action'] = 'viewthread';
	$_SHTML['tid'] = $tid;
	$_SHTML['dateline'] = $thread['dateline'];
	$_SGLOBAL['htmlfile'] = gethtmlfile($_SHTML);
	ehtml('get', $_SCONFIG['htmlviewnewstime']);
	$_SCONFIG['debug'] = 0;
}

include_once(S_ROOT.'./include/common.inc.php');

$perpage = 21;

$fid = $thread['fid'];
$query = $_SGLOBAL['db']->query('SELECT f.*, ff.* FROM '.tname('forums', 1).' f LEFT JOIN '.tname('forumfields', 1).' ff ON ff.fid=f.fid WHERE f.fid=\''.$fid.'\'');
if(!$forum = $_SGLOBAL['db']->fetch_array($query)) {
	messagebox('error', 'not_found', S_URL);
}
if($forum['status'] != 1) {
	$jumptobbs = true;
} elseif(!empty($forum['password'])) {
	$jumptobbs = true;
} elseif(!empty($forum['viewperm'])) {
	$viewpermarr = explode("\t", $forum['viewperm']);
	if(!in_array('7', $viewpermarr)) {
		$jumptobbs = true;
	}
} elseif(!empty($forum['redirect'])) {
	$forumurl = $forum['redirect'];
	$jumptobbs = true;
}
if($jumptobbs) {
	header('location: '.$threadurl);
	exit;
}

@include_once(S_ROOT.'./data/system/bbsforums.cache.php');
if(!empty($_SGLOBAL['bbsforumarr']) && !empty($_SGLOBAL['bbsforumarr'][$forum['fid']]['name'])) {
	$forum['name'] = $_SGLOBAL['bbsforumarr'][$forum['fid']]['name'];
}

$iarr = array();
$listcount = $thread['replies']+1;
unset($_SGET['lastpost']);
$page = 1;
$listkey = 'posts';
$action = 'viewthread';
$item['listcount'] = $listcount;
$item['tid'] = $tid;
$space['jammer'] = 0;
include_once(S_ROOT.'./include/bbs_post.inc.php');
$iarr = $item[$listkey];

$thread['attachments'] = array();
$thread['message'] = $iarr[$item['pid']]['message'];
$description = shtmlspecialchars(str_replace(array("\r", "\n"), '', cutstr(trim(strip_tags($thread['message'])), 200)));
if(!empty($iarr[$item['pid']]['attachments'])) $thread['attachments'] = $iarr[$item['pid']]['attachments'];
unset($iarr[$item['pid']]);

$keywords = $forum['name'].','.$thread['subject'];

$guidearr = array();
$guidearr[] = array('url' => geturl('action/bbs'),'name' => $lang['bbs']);
$guidearr[] = array('url' => geturl('action/forumdisplay/fid/'.$forum['fid']),'name' => $forum['name']);
$guidearr[] = array('url' => 'javascript:;','name' => $lang['view_thread']);

$title = $thread['subject'].'_'.$forum['name'].'_'.$lang['bbs'].'_'.$_SCONFIG['sitename'];

$tplname = 'bbs_viewthread';

include template($tplname);

if(!empty($_SCONFIG['htmlviewnews'])) {
	ehtml('make');
} else {
	maketplblocknames($tplname);
	maketplblockvalue('cache');
}

?>