<?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);
	$timglimit = $timestamp - 86400*3;
	$force = 'FORCE INDEX(postdate)';
	$where .= "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);
	$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);
		$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();
} else {
	if ($u!=$winduid) {
		if ($friend = getOneFriend($u)) {
			$where .= 'authorid='.pwEscape($u).' AND anonymous=0 AND ifhide=0';
			$faceimg = $friend['face'];
			$username = $friend['username'];
		} else {
			Showmsg('mode_o_not_friend');
		}
		$thisbase = $basename.'q=article&u='.$u;
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1);
		$where .= 'authorid='.pwEscape($winduid);
		$thisbase = $basename.'q=article&';
		$username = $windid;
	}
}
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);
		$rs = $db->query("SELECT tid,fid,author,authorid,subject,postdate,lastpost,replies,hits,titlefont FROM pw_threads $force WHERE $where ORDER BY postdate 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');
			$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;
		}
	}
}
?>