<?php

/*
	[Discuz!] (C)2001-2006 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: thread.inc.php,v $
	$Revision: 1.16 $
	$Date: 2007/02/07 13:53:11 $
*/


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

$query = $db->query("SELECT * FROM {$tablepre}threads t
	LEFT JOIN {$tablepre}forums f ON f.fid=t.fid
	LEFT JOIN {$tablepre}forumfields ff ON ff.fid=f.fid
	WHERE t.tid='$tid' AND t.readperm='0' AND t.price<='0' AND t.displayorder>='0'
	AND f.status>0 AND ff.password=''");

$thread = $db->fetch_array($query);
$page = max(1, intval($page));

$navtitle = $thread['subject'].	'('.$lang['page'].' '.$page.') '.
	($thread['type'] == 'sub' ? ' - '.strip_tags($_DCACHE['forums'][$thread['fup']]['name']) : '').' - '.strip_tags($thread['name']).' - ';

if(!$thread || !(!$thread['viewperm'] || ($thread['viewperm'] && forumperm($thread['viewperm'])))) {
	require_once './include/header.inc.php';
?>
<a href="archiver/"><?=$_DCACHE['settings']['bbname']?></a></div><br>
<div class="simpletable smalltxt" style="width: <?=TABLEWIDTH?>"><div class="subtable altbg2"><br><?=$lang['thread_nonexistence']?><br><br></div></div>
<?

} else {

	$navsub = $thread['type'] == 'sub' ? " <a href=\"archiver/{$qm}fid-$thread[fup].html\">{$_DCACHE[forums][$thread[fup]][name]}</a> <b>&raquo;</b> ": ' ';
	$fullversion = array('title' => $thread['subject'], 'link' => "viewthread.php?tid=$tid");

	$ppp = $_DCACHE['settings']['postperpage'] * 2;
	$start = ($page - 1) * $ppp;

	$query = $db->query("SELECT p.author, p.dateline, p.subject, p.message, p.anonymous, m.groupid
		FROM {$tablepre}posts p
		LEFT JOIN {$tablepre}members m ON p.authorid=m.uid
		WHERE p.tid='$tid' AND p.invisible='0'
		ORDER BY dateline LIMIT $start, $ppp");

	if($firstpost = $db->fetch_array($query)) {
		if(in_array($firstpost['groupid'], array(4, 5, 6))) {
			include_once language('misc');
			$firstpost['message'] = $language['post_banned'];
		}
		$meta_contentadd = cutstr(strip_tags(str_replace(array("\r", "\n", "\t"), array('', '', ''), $firstpost['message'])), 200);
	}

	require_once './include/header.inc.php';
?>
<div class="subtable bold altbg1">
<a href="archiver/"><?=$_DCACHE['settings']['bbname']?></a> <b>&raquo;</b><?=$navsub?><a href="archiver/<?=$qm?>fid-<?=$thread['fid']?>.html"><?=$thread['name']?></a> <b>&raquo;</b> <?=$thread[subject]?></div><?=$headerbanner?>
<?
	while(($post = $firstpost) || ($post = $db->fetch_array($query))) {
		if(in_array($post['groupid'], array(4, 5, 6))) {
			include_once language('misc');
			$post['message'] = $language['post_banned'];
		}
		if(!empty($firstpost)) $firstpost = array();
		$post['dateline'] = gmdate($_DCACHE['settings']['dateformat'].' '.$_DCACHE['settings']['timeformat'], $post['dateline'] + $_DCACHE['settings']['timeoffset'] * 3600);
		$post['message'] = ($post['subject'] ? '<b>'.$post['subject'].'</b><br><br>' : '').nl2br(preg_replace(array('/&amp;(#\d{3,5};)/', "/\[hide=?\d*\](.+?)\[\/hide\]/is"),
			array('&\\1', '<b>**** Hidden Message *****</b>'),
			str_replace(array('&', '"', '<', '>', "\t", '   ', '  '),
			array('&amp;', '&quot;', '&lt;', '&gt;', '&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;'),
			$post['message'])));
		if($thread['jammer']) {
			$post['message'] =  preg_replace("/\<br \/\>/e", "jammer()", $post['message']);
		}
		$post['author'] = !$post['anonymous'] ? $post['author'] : $lang['anonymous'];

?>
<br><div class="simpletable smalltxt" style="width: <?=TABLEWIDTH?>"><div class="subtable altbg1">
<span class="right"><?=$post['dateline']?></span>
<b><?=$post['author']?></b></div>
<div class="subtable altbg2 t_msg" style="width: auto;height: auto">
<?=$post['message']?></div></div>
<?

	}

	echo "<br>".multi($thread['replies'] + 1, $page, $ppp, "{$qm}tid-$tid");

}

function jammer() {
	$randomstr = '';
	for($i = 0; $i < mt_rand(5, 15); $i++) {
		$randomstr .= chr(mt_rand(0, 59)).chr(mt_rand(63, 126));
	}
	return mt_rand(0, 1) ? '<font style="font-size:0px;color:'.ALTBG2.'">'.$randomstr.'</font><br />' :
		'<br /><span style="display:none">'.$randomstr.'</span>';
}

function cutstr($string, $length) {
	$strcut = '';
	if(strlen($string) > $length) {
		for($i = 0; $i < $length - 3; $i++) {
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
		return $strcut.' ...';
	} else {
		return $string;
	}
}

function language($file, $templateid = 0, $tpldir = '') {
	$tpldir = $tpldir ? $tpldir : TPLDIR;
	$templateid = $templateid ? $templateid : TEMPLATEID;

	$languagepack = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.lang.php';
	if(file_exists($languagepack)) {
		return $languagepack;
	} elseif($templateid != 1 && $tpldir != './templates/default') {
		return language($file, 1, './templates/default');
	} else {
		return FALSE;
	}
}
?>