<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
require_once(D_P."data/bbscache/forum_cache.php");
require_once(R_P.'require/showimg.php');

InitGP(array('see'),null,1);
$page = (int)GetGP('page');
$perpage = 20;
$u = (int)GetGP('u');
!$u && $u = $winduid;

include_once(D_P.'data/bbscache/forum_cache.php');
$fids = getSpecialFid();
$myshortcut = true;
$shortcutforum = array();
if ($winddb['shortcut']) {
	isset($forum) || require(D_P.'data/bbscache/forum_cache.php');
	foreach (explode(',',$winddb['shortcut']) as $key => $value) {
		if ($value && isset($forum[$value])) {
			$shortcutforum[$value] = $forum[$value]['name'];
		}
	}
}
if (empty($shortcutforum)) {
	if (!$db_shortcutforum && $winduid) {
		require_once(R_P.'require/updateforum.php');
		$db_shortcutforum = updateshortcut();
	}
	$shortcutforum = $db_shortcutforum;
	$myshortcut = false;
}


$force = $where = '';
$article = array();
if ($see == 'all') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	$timglimit = $timestamp - 86400*3;
	$force = 'FORCE INDEX(postdate)';
	$where .= ($fids ? "fid NOT IN($fids) AND " : '')."ifcheck='1' AND postdate>".pwEscape($timglimit);
	$thisbase = $basename.'q=article&see=all&';
	$username = $windid;
} elseif ($see == 'friend') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	$thisbase = $basename.'q=article&see=friend&';
	if ($friends = getFriends($winduid)) {
		$uids = array_keys($friends);
		$timglimit = $timestamp - 86400*5;
		$where .= "fid NOT IN($fids) AND authorid IN(".pwImplode($uids).") AND ifcheck=1 AND ifhide=0 AND anonymous=0";
		$where .= " AND postdate>".pwEscape($timglimit);
	} else {
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_article');
		footer();
	}
	$username = $windid;
} elseif ($see == 'favor') {
	InitGP('type');
	!$type && $type = 'all';
	if ($u!=$winduid) {
		if ($friend = getOneFriend($u)) {
			$faceimg = $friend['face'];
			$username = $friend['username'];
			$where = "AND fid NOT IN($fids)";
		} else {
			Showmsg('mode_o_not_friend');
		}
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$username = $windid;
		$where = '';
	}
	$favor  = $db->get_one("SELECT tids,type FROM pw_favors WHERE uid=".pwEscape($winduid));
	Add_S($favor);
	if ($favor['type']) {
		$typeid = explode(',',$favor['type']);
	}
	$tids = $ptids = array();
	if ($tiddb = getfavor($favor['tids'])) {
		if ($type == 'all') {
			foreach ($tiddb as $key => $val) {
				if ($val) {
					$tids += $val;
				}
			}
		} elseif ($tiddb[$type]) {
			$tids = $tiddb[$type];
		}
		$page<1 && $page = 1;
		$start_limit = intval(($page-1) * $perpage);
		$count = count($tids);
		$numofpage = ceil($count/$perpage);
		$numofpage < 1 && $numofpage = 1;
		$page > $numofpage && $page = $numofpage;
		$pages = numofpage($count,$page,$numofpage,$basename."q=article&see=$see&u=$u&");
	}
	if ($tids) {
		$tids = pwImplode($tids);
		$query = $db->query("SELECT fid,tid,subject,postdate,author,authorid,anonymous,replies,hits,titlefont FROM pw_threads WHERE tid IN($tids) $where ORDER BY postdate DESC");
		while ($rt = $db->fetch_array($query)) {
			$rt['subject']	= substrs($rt['subject'],45);
			$rt['postdate']	= get_date($rt['postdate']);
			$keyvalue		= get_key($rt['tid'],$tiddb);
			if ($rt['anonymous'] && !in_array($groupid,array('3','4')) && $rt['authorid'] != $winduid) {
				$rt['author']	= $db_anonymousname;
				$rt['authorid'] = 0;
			}
			$type == 'all' && $tidarray[$keyvalue][$rt['tid']] = $rt['tid'];
			$rt['forum']	= $forum[$rt['fid']]['name'];
			$rt['sel']		= $typeid[$keyvalue-1];
			$article[]		= $rt;
		}
		$article = array_slice($article,$start_limit,$perpage);
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_article');
	footer();
} elseif ($see == 'post') {
	InitGP(array('ptable'));
	if ($u!=$winduid) {
		$friend = $db->get_one("SELECT uid,username,icon FROM pw_members WHERE uid=".pwEscape($u));
		list($faceimg) = showfacedesign($friend['icon'],1,'s');
		$username = $friend['username'];
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$username = $windid;
	}
	!$page && $page = 1;
	$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
	!isset($ptable) && $ptable = $db_ptable;
	$pw_posts = GetPtable($ptable);

	$fidoff = $isU ? array(0) : getFidoff($groupid);
	$sqloff = ' AND p.fid NOT IN('.pwImplode($fidoff).')';

	$count = $db->get_value("SELECT COUNT(*) AS count FROM $pw_posts p WHERE authorid=".pwEscape($u)." $sqloff");
	$nurl = $basename.'q=article&see=post&u='.$u.'&';
	$p_list = $db_plist ? explode(',',$db_plist) : array();
	if ($p_list) {
		$p_table = "<tr><td id=\"up_post0\" class=\"fav\"><a href=\"{$nurl}ptable=0\">post</a></td></tr>";
		foreach ($p_list as $key => $val) {
			$p_table .= "<tr><td id=\"up_post$val\" class=\"fav\"><a href=\"{$nurl}ptable=$val\">post$val</a></td></tr>";
		}
		$nurl .= "ptable=$ptable&";
	}
	$pages = numofpage($count,$page,ceil($count/$db_perpage),$nurl);
	$isGM = CkInArray($windid,$manager);
	$query = $db->query("SELECT p.pid,p.postdate,t.tid,t.fid,t.subject,t.authorid,t.author,t.replies,t.hits,t.titlefont,t.anonymous FROM $pw_posts p LEFT JOIN pw_threads t USING(tid) WHERE p.authorid=".pwEscape($u)." $sqloff ORDER BY p.postdate DESC $limit");
	while ($rt = $db->fetch_array($query)){
		$rt['subject']	= substrs($rt['subject'],45);
		if ($rt['anonymous'] && $rt['authorid'] != $winduid && !$isGM) {
			$rt['author'] = $db_anonymousname;
			$rt['authorid'] = 0;
		}
		$rt['forum']	= strip_tags($forum[$rt['fid']]['name']);
		$rt['postdate']	= get_date($rt['postdate'],'Y-m-d');
		$article[]		= $rt;
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_article');
	footer();
} else {
	InitGP(array('ordertype'));
	empty($ordertype) && $ordertype = 'postdate';
	if ($u!=$winduid) {
		$where .= 'authorid='.pwEscape($u).' AND anonymous=0 AND ifhide=0';
		$friend = $db->get_one("SELECT uid,username,icon FROM pw_members WHERE uid=".pwEscape($u));
		list($faceimg) = showfacedesign($friend['icon'],1,'s');
		$username = $friend['username'];
		$thisbase = $basename.'q=article&u='.$u.'&ordertype='.$ordertype.'&';
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$where .= 'authorid='.pwEscape($winduid);
		$thisbase = $basename.'q=article&ordertype='.$ordertype.'&';
		$username = $windid;
		$u = $winduid;
	}
	
}
if ($where) {
	$count = $db->get_value("SELECT count(*) as count FROM pw_threads WHERE $where");
	if ($count) {
		!$page && $page = 1;
		$start = ($page-1)*$perpage;
		$numofpage = ceil($count/$perpage);
		$isGM = CkInArray($windid,$manager);
		$rs = $db->query("SELECT tid,fid,author,authorid,subject,postdate,lastpost,replies,hits,titlefont,anonymous FROM pw_threads $force WHERE $where ORDER BY $ordertype DESC ".pwLimit($start,$perpage));
		while ($rt = $db->fetch_array($rs)) {
			$rt['subject'] = substrs($rt['subject'],45);
			$rt['forum'] = strip_tags($forum[$rt['fid']]['name']);
			$rt['postdate'] = get_date($rt['postdate'],'Y-m-d');
			if (!$isGM && $rt['anonymous']) {
				$rt['author'] = $rt['authorid'] = '';
			}
			$article[] = $rt;
		}
		$pages = numofpage($count,$page,$numofpage,$thisbase);
	}
}
require_once(R_P.'require/header.php');
require_once PrintEot('m_article');footer();

function getfavor($tids) {
	$tids  = explode('|',$tids);
	$tiddb = array();
	foreach ($tids as $key => $t) {
		if ($t) {
			$v = explode(',',$t);
			foreach ($v as $k => $v1) {
				$tiddb[$key][$v1] = $v1;
			}
		}
	}
	return $tiddb;
}
function get_key($tid,$tiddb) {
	foreach ($tiddb as $key => $value) {
		if (in_array($tid,$value)) {
			return $key;
		}
	}
}
function getFidoff($gid) {
	global $db;
	$fidoff = array(0);
	$query = $db->query("SELECT fid FROM pw_forums WHERE type<>'category' AND (password!='' OR forumsell!='' OR allowvisit!='' AND allowvisit NOT LIKE '%,$gid,%')");
	while ($rt = $db->fetch_array($query)) {
		$fidoff[] = $rt['fid'];
	}
	return $fidoff;
}
?>