<?php
/**
 * @version $Id: show.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

define('PB_CSCRIPT', 'show');
require_once('include/common.inc.php');

if (!$tid)
{
	showMsg($u_message['show_illegal_tid']);
}
if (!$allowread)
{
	showMsg($u_message['show_read_denied']);
}

if ($pb_visitcache && !$log_status)
{
	$TPL->caching = TRUE;
	$TPL->cache_lifetime = $pb_cachetime;
	$showdir = '';
	$tidlen = strlen($tid);
	$ctid = 0;
	while ($ctid < $tidlen)
	{
		$showdir .= '/'.$tid{$ctid};
		$ctid++;
	}
	$TPL->cache_dir = $cache_dir.$dir.$showdir;
	Pmkdir($showdir, $cache_dir.$dir, TRUE);
}

if ($log_status || !$TPL->is_cached('show.html', $tid.$page))
{
	if (!$customer['uid'])
	{
		if ($pb_ifdigg && ($pbdcookie = gCookie('pb_digg')))
		{
			list($tdigg, $cdigg) = explode('|', $pbdcookie);
		}
		if ($pb_ifbury && ($pbbcookie = gCookie('pb_bury')))
		{
			list($tbury, $cbury) = explode('|', $pbbcookie);
		}
	}
	$style = '';
	$rs = $article = $article_maction = $comment_maction = $attach = array();
	$SQL = 'SELECT t.tid, t.cid, t.author, t.uid, t.subject, t.content, t.contentlink, t.linkhost, t.topicimg, t.ifcheck, t.topped, t.postdate, t.postip, t.digg, t.bury, t.views, t.comments, t.keywords, 
					ti.skeywords, ti.sdesc, ti.pbrank, ti.commend, ti.titlecolor, ti.titlestyle, ti.ainfo, ti.iflock, ti.iffirst, ti.ishtml, ti.ifconvert, ti.anonsite, ti.anonymity,  
					mx.showsign, mx.signature, mx.avatar, mx.site, mx.gender, mx.ctsig 
					FROM '.$db_prefix.'threads t 
					LEFT JOIN '.$db_prefix.'tinfo ti 
					ON t.tid = ti.tid 
					LEFT JOIN '.$db_prefix.'memberexp mx 
					ON t.uid = mx.uid 
					WHERE t.tid = '.$tid;
	$rs = $DB->db_fetch_one_array($SQL);
	if (!$rs)
	{
		showMsg($u_message['show_illegal_tid']);
	}
	if (!$rs['ifcheck'] && !$allowlockatc)
	{
		showMsg($u_message['show_article_unchecked']);
	}
	if (!$categories[$rs['cid']]['status'] && !$allowsort)
	{
		showMsg($u_message['show_visit_cate_denied']);
	}
	$cid = $rs['cid'];
	$rs['cate'] = $categories[$cid]['name'];
	$rssurl = 'rss.php?action=cate&amp;cid='.$rs['cid'];
	$rs['postdate'] = formatPostTime($rs['postdate']);

	!$rs['author'] && $rs['author'] = $pb_sitename.$u_message['net_friend'];
	$seo['pagetitle'] = $rs['subject'].'_'.$rs['cate'];
	$seo['keywords'] = $rs['skeywords'] ? $rs['skeywords'] : $rs['subject'];
	$seo['description'] = $rs['sdesc'] ? preg_replace('/(?:\r\n|\n\r|\r|\n)/i', '', $rs['sdesc']) : $rs['subject'];
	if ($pb_showsign && $rs['showsign'] && $rs['signature'])
	{
		$rs['showsign'] = 1;
	}
	else
	{
		$rs['showsign'] = 0;
	}
	switch ($rs['gender'])
	{
		case '0':
			$rs['gender_name'] = $u_message['male'];
			break;
		case '1':
			$rs['gender_name'] = $u_message['female'];
			break;
		default:
			$rs['gender_name'] = $u_message['secrecy'];
			break;
	}
	if (!$rs['avatar'])
	{
		$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
	}
	elseif (substr($rs['avatar'], 0, 7) != 'http://')
	{
		$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$rs['avatar'];
	}
	if ($rs['topicimg'])
	{
		list($topicimg, $topicimgh, $topicimgw) = explode('|', $rs['topicimg']);
		list($pb_topicimgh, $pb_topicimgw) = explode("\t", $pb_topicimgsize);
		$addstyle = '';
		if (substr($topicimg, 0, 7) != 'http://')
		{
			$topicimg = $_PBENV['PB_URL'].'attachments/'.$topicimg;
		}
		if ($topicimgh > $pb_topicimgh) $addstyle .= 'height:'.$pb_topicimgh.'px;';
		if ($topicimgw > $pb_topicimgw) $addstyle .= 'width:'.$pb_topicimgw.'px;';
		$rs['topicimg'] = '<img src="'.$topicimg.'" alt="'.$rs['subject'].'" style="'.$addstyle.'" class="topicimg" />';
	}

	//ubb
	require_once(PBDIGG_ROOT.'include/ubb.func.php');
	
	if ($rs['titlecolor'])
	{
		$style = 'color:#'.$rs['titlecolor'].';';
	}
	if ($rs['titlestyle'])
	{
		/*b/em/u*/
		($rs['titlestyle']{0}) && $style .= 'font-weight:bold;';
		($rs['titlestyle']{1}) && $style .= 'font-style:italic;';
		($rs['titlestyle']{2}) && $style .= 'text-decoration:underline;';
	}
	$style && $rs['subject'] = '<span style="'.$style.'">'.$rs['subject'].'</span>';
	$rs['ifconvert'] && $rs['content'] = UBB2HTML($rs['content'], 'content');
	/*
	if (!$rs['ishtml'])
	{
		$rs['ifconvert'] && $rs['content'] = UBB2HTML($rs['content'], 'content');
		$rs['content'] = preg_replace('/(?:\r\n|\n\r|\r|\n)/i', '<br />', $rs['content']);
	}
	*/
	if ($rs['signature'])
	{
		$rs['ctsig'] && $rs['signature'] = UBB2HTML($rs['signature'], 'signature');
		$rs['signature'] = preg_replace('/(?:\r\n|\n\r|\r|\n)/i', '<br />', $rs['signature']);
	}
	if ($pb_ifdigg)
	{
		$rs['digged'] = (strpos($tdigg, $rs['tid']) === FALSE) ? '0' : '1';
	}
	if ($pb_ifbury)
	{
		$rs['buryed'] = (strpos($tbury, $rs['tid']) === FALSE) ? '0' : '1';
	}
	$writeruid = $rs['uid'];
//	if($rs['keywords'])
//	{
//		$keywords = explode(',', $rs['keywords']);
//		foreach ($keywords as $key => $value)
//		{
//			if($value) $rs['content'] = preg_replace("/(?<=[\s\"\]>()]|[\x7f-\xff]|^)(".preg_quote($value,'/').")([.,:;-?!()\s\"<\[]|[\x7f-\xff]|$)/siU", "<a href=\"".$_PBENV['PB_URL']."index.php?tag=\\1\"><span style=\"text-decoration:underline;\">\\1</span></a>\\2", $rs['content']);
//		}
//	}
	!$rs['contentlink'] && $rs['contentlink'] = 'show.php?tid='.$rs['tid'];
	!$rs['linkhost'] && $rs['linkhost'] = $_SERVER['HTTP_HOST'];
	if ($rs['anonymity'])
	{
		$rs['anonsite'] = $rs['anonsite'] ? ((substr($rs['anonsite'], 0, 7) == 'http://') ? $rs['anonsite'] : 'http://'.$rs['anonsite']) : '#';
	}
	$TPL->assign('navlink', navlink($cid));
	$nav_menu = PMenu();
	$TPL->assign('nav_menu', $nav_menu[0]);
	$TPL->assign('sub_menu', $nav_menu[1]);
	$TPL->assign('showsub', ($nav_menu[1] ? '1' : '0'));
	$TPL->assign('cid', $cid);

	//attachment
	$attachother = $attachimg = $extension = $onload = '';
	$ainfo = $rs['ainfo'];
	if ($ainfo)
	{
		$ainfo = unserialize($ainfo);
		$attachother = '<div id="attachother">';
		$attachimg = '<div id="attachimg">';
		list ($pb_previewh, $pb_previeww) = explode("\t", $pb_previewsize);
		if ($pb_previewh || $pb_previeww)
		{
			$onload = ' onload="';
			if ($pb_previeww) $onload .= "if(this.width>'".$pb_previeww."')this.width='".$pb_previeww."';";
			if ($pb_previewh) $onload .= "if(this.height>'".$pb_previewh."')this.height='".$pb_previewh."';";
			$onload .= '"';
		}
		require_once(PBDIGG_ROOT.'include/attachment.inc.php');
		$rs['content'] = attachment($rs['content']);
		foreach ($ainfo as $v)
		{
			$extension = strtolower(Fext($v['filename']));
			$url = $_PBENV['PB_URL'].($v['filesize'] > 8388608 ? 'attachment/'.$v['filepath'] : 'attachments.php?aid='.$v['aid']);
			if ($v['isimg'] && in_array($extension, array('jpg', 'jpeg', 'gif', 'png', 'bmp')))
			{
				$attachimg .= '<a href="'.$url.'" title="'.htmlspecialchars($v['filename']).'" rel="gallery" class="thickbox attachimg"><img src="'.$url.'"'.$onload.' class="previewimg" /></a>';
			}
			else
			{
				$attachother .= '<span class="attachother">'.attachtype($extension).' <a href="'.$url.'" target="_blank" title="'.$v['filename'].'">'.$v['filename'].'</a></span>';
			}
		}
		$attachother .= '</div>';
		$attachimg .= '</div>';
		
	}

	$TPL->assign('attachimg', $attachimg);
	$TPL->assign('attachother', $attachother);
	$TPL->assign('article', $rs);

	$rs = $tagid = $articlelink = $articleattention = $taglink = $tids = '';
	$SQL = 'SELECT tc.tagid, tg.tagname 
			FROM '.$db_prefix.'tagcache tc, '.$db_prefix.'tags tg, '.$db_prefix.'threads t 
			WHERE tc.tagid = tg.tagid 
			AND tc.tid = t.tid 
			AND t.tid = '.$tid;
	$DB->db_query($SQL);
	if ($DB->db_num())
	{
		while ($rs = $DB->db_fetch_array())
		{
			$tagid .= $rs['tagid'].',';
			$taglink .= '<a href="'.$_PBENV['PB_URL'].'index.php?tag='.rawurlencode($rs['tagname']).'" title="'.$rs['tagname'].'" class="ctag">'.$rs['tagname'].'</a>';
		}
		$query = $DB->db_query("SELECT tid FROM {$db_prefix}tagcache WHERE tagid IN (".substr($tagid, 0, -1).")");
		while ($rs = $DB->db_fetch_array())
		{
			$tids .= $rs['tid'].',';
		}
	}
	else
	{
		$taglink = $u_message['null'];
	}
	if ($tids)
	{
		$SQL = "SELECT t.tid, t.subject, ti.titlecolor, ti.titlestyle 
				FROM {$db_prefix}threads t 
				LEFT JOIN {$db_prefix}tinfo ti 
				USING (tid) 
				WHERE t.tid IN (".substr($tids, 0, -1).") 
				ORDER BY t.tid DESC 
				LIMIT 0, 15";
		$DB->db_query($SQL);
		if ($DB->db_num())
		{
			while ($r = $DB->db_fetch_array())
			{		
				$r['altsubject'] = $r['subject'];
				$r['subject'] = PBSubstr($r['subject'], $pb_indextitle);
				$articlelink .= '<li><a href="show.php?tid='.$r['tid'].'" title="'.$r['altsubject'].'">'.$r['subject'].'</a></li>';
			}
		}
		else
		{
			$articlelink = $u_message['show_nolinkarticle'];
		}
	}
	else
	{
		$articlelink = $u_message['show_nolinkarticle'];
	}
	$TPL->assign('taglink', $taglink);

	//article attention
	$articleattention .= '<div class="articleattention"><h2>'.$ui_message['show_attention'].'</h2><ul>';
	$SQL = "SELECT m.uid, m.username, mx.avatar 
			FROM {$db_prefix}members m 
			INNER JOIN {$db_prefix}memberexp mx 
			ON m.uid = mx.uid 
			INNER JOIN {$db_prefix}tdata t 
			ON m.uid = t.uid 
			WHERE t.tid = $tid 
			AND t.type = 'digg' 
			LIMIT 0, 16";
	$DB->db_query($SQL);
	if ($DB->db_num())
	{
		while ($result = $DB->db_fetch_array())
		{
			if (!$result['avatar'])
			{
				$result['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
			}
			elseif (substr($result['avatar'], 0, 7) != 'http://')
			{
				$result['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$result['avatar'];
			}
			$articleattention .= '<li><a href="user.php?uid='.$result['uid'].'" target="_blank"><img src="'.$result['avatar'].'" alt="" /><br />'.$result['username'].'</a></li>';
		}
	}
	else
	{
		$articleattention .= $u_message['show_noannony'];
	}
	$articleattention .= '</ul><div class="clear"></div></div>';
	$prevarticle = $nextarticle = $u_message['show_noarticle'];
	$rs = $DB->db_fetch_one_array("SELECT tid, subject FROM {$db_prefix}threads WHERE cid = $cid AND tid < $tid ORDER BY tid DESC LIMIT 1");
	if ($rs)
	{
		$prevarticle = '<a href="show.php?tid='.$rs['tid'].'">'.$rs['subject'].'</a>';
	}
	$rs = $DB->db_fetch_one_array("SELECT tid, subject FROM {$db_prefix}threads WHERE cid = $cid AND tid > $tid ORDER BY tid ASC LIMIT 1");
	if ($rs)
	{
		$nextarticle = '<a href="show.php?tid='.$rs['tid'].'">'.$rs['subject'].'</a>';
	}

	$checkset = $totalRecords = $mult = '';
	//comment
	if (!$allowlockatc)
	{
		$checkset = ' AND ifcheck = 1';
	}
	$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}comments WHERE tid = ".$tid.$checkset);
	$totalRecords = (int)$rs['num'];
	$TPL->assign('comment_num', sprintf($ui_message['show_commentnum'], $totalRecords));
	$comment = $comments = $cmuurl = array();
	if ($totalRecords)
	{
		$pagesize = (int)$pb_cperpage;
		$page = !is_Page($page) ? 1 : $page;
		$start_limit = ($page -1) * $pagesize;
		$mult = multLink($page, $totalRecords, 'show.php?tid='.$tid.'&amp;', $pagesize);
		$SQL = 'SELECT c.rid, c.tid, c.uid, c.author, c.content, c.ifcheck, c.postdate, c.postip, c.digg, c.bury, m.avatar 
				FROM '.$db_prefix.'comments c 
				LEFT JOIN '.$db_prefix.'memberexp m 
				ON c.uid = m.uid 
				WHERE c.tid = '.$tid.$checkset.' 
				ORDER BY c.postdate ASC 
				LIMIT '.$start_limit.', '.$pagesize;
		$DB->db_query($SQL);
		$ci = 0;
		while ($comment = $DB->db_fetch_array())
		{
			$ci++;
			$comment['postdate'] = formatPostTime($comment['postdate']);
			if (!$comment['author']) $comment['author'] = $pb_sitename.$u_message['net_friend'];
			$comment['digged'] = (strpos($cdigg, $comment['rid']) === FALSE) ? '0' : '1';
			$comment['buryed'] = (strpos($cbury, $comment['rid']) === FALSE) ? '0' : '1';
			$comment['content'] = preg_replace('/(?:\r\n|\n\r|\r|\n)/i', '<br />', UBB2HTML($comment['content'], 'comment'));
			if (!$comment['avatar'])
			{
				$comment['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
			}
			elseif (substr($comment['avatar'], 0, 7) != 'http://')
			{
				$comment['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$comment['avatar'];
			}
			$cmuurl[$ci] = $comment['uid'];
			if (($comment['uid'] == $customer['uid'] && $customer['uid']) || $customer['adminid'])
			{
				$comment['m_del'] = $comment['m_edit'] = '1';
			}
			$comments[$ci] = $comment;
		}
	}

	//¹ÜÀíÈ¨ÏÞ
	$article_manager = '0';
	if ($customer['adminid'])
	{
		$article_manager = '1';
		$article_maction['del'] = $allowdelatc;
		$article_maction['edit'] = $alloweditatc;
		$article_maction['move'] = $allowmoveatc;
		$article_maction['copy'] = $allowcopyatc;
		$article_maction['lock'] = $allowlockatc;
		$article_maction['shield'] = $comment_maction['shield'] = $allowshield;
		$article_maction['top'] = $allowtopatc;
		$article_maction['commend'] = $allowcommend;
		$article_maction['ip'] = 1;
	}
	if ($writeruid == $customer['uid'] && $customer['uid'])
	{
		$article_manager = $article_maction['del'] = $article_maction['edit'] = '1';
	}
	$TPL->assign('article_manager', $article_manager);
	$TPL->assign('article_maction', $article_maction);
	$TPL->assign('comment_maction', $comment_maction);

	list($pb_commentlenmin, $pb_commentlenmax) = explode("\t",$pb_commentlen);
	$TPL->assign('comment_len_min', $pb_commentlenmin);
	$TPL->assign('comment_len_max', $pb_commentlenmax);
	$TPL->assign('comment_length_error', sprintf($u_message['thread_comment_limit'], $pb_commentlenmin, $pb_commentlenmax));



	$DB->db_query('UPDATE '.$db_prefix.'threads SET views = views + 1 WHERE tid = '.$tid);
	loadTranCache();
	//link article
	$TPL->assign('articlelink', $articlelink);
	$TPL->assign('articleattention', $articleattention);
	$TPL->assign('prevarticle', $prevarticle);
	$TPL->assign('nextarticle', $nextarticle);
	$TPL->assign('mult_page', $mult);
	$TPL->assign('comments', $comments);
	$TPL->assign('cmuurl', $cmuurl);
	$TPL->assign('customer', $customer);
	$TPL->assign('seo', $seo);
	$TPL->assign('rss_url', $_PBENV['PB_URL'].$rssurl);
	$TPL->assign('diggbury', $pb_ifdigg || $pb_ifbury);
	$TPL->assign('showdigg', $pb_ifdigg);
	$TPL->assign('showbury', $pb_ifbury);
	$TPL->assign('jsmore', '<script src="'.$_PBENV['PB_URL'].'templates/'.$_PBENV['PB_TEMPLATE'].'/js/show.js" type="text/javascript"></script><script src="'.$_PBENV['PB_URL'].'templates/'.$_PBENV['PB_TEMPLATE'].'/js/tip.js" type="text/javascript"></script>'."\r\n".'<script src="'.$_PBENV['PB_URL'].'templates/'.$_PBENV['PB_TEMPLATE'].'/js/thickbox.js" type="text/javascript"></script>');
	$TPL->assign('cssmore', '<link href="'.$_PBENV['PB_URL'].'templates/'.$_PBENV['PB_TEMPLATE'].'/css/thickbox.css" rel="stylesheet" type="text/css" />');
	unset($attachimg,$nav_menu,$attachother,$articlelink,$taglink,$comments,$comment,$mult,$customer,$seo,$rssurl,$article_maction,$comment_maction,$rs);
}
PBOutPut('show.html', $tid.$page);
?>
