<?php
!function_exists('readover') && exit('Forbidden');

Class HOME {

	function update($type=''){//更新缓存数据
		global $db,$num,$timestamp;
		if($type == 'all'){
			$query = $db->query("SELECT name,title,config FROM pw_home WHERE ifopen='1'");
		} elseif($type){
			$query = $db->query("SELECT name,title,config FROM pw_home WHERE name=".pwEscape($type));
		} else{
			$query = $db->query("SELECT name,title,config FROM pw_home WHERE ifopen='1' AND lastupdate+upstep<".pwEscape($timestamp)."ORDER BY lastupdate ASC LIMIT $num");
		}
		while($rt = $db->fetch_array($query)){
			$config = unserialize($rt['config']);
			if(substr($rt['name'],0,7)=='forums_'){
				$this->forums($rt['name'],$rt['title'],$config);
			} elseif(method_exists($this,$rt['name'])){
				$this->$rt['name']($rt['name'],$rt['title'],$config);
			}
		}
	}

	function eye($name,$title,$config){// 社区幻灯眼模块
		global $db,$timestamp,$imgpath;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 5;
		if(!in_array($config['order'],array('hits','replies','dig'))){
			$config['order'] = 'hits';
		}
		$flashpics   = '';
		$flashlinks  = '';
		$flashtitles = '';
		$extra       = '';

		if($config['tids']){
			$tids = explode(',',$config['tids']);
			foreach($tids as $tid){
				$pw_tmsgs = GetTtable($tid);
				$rt = $db->get_one("SELECT t.tid,t.subject,tm.aid FROM pw_threads t LEFT JOIN $pw_tmsgs tm USING(tid) WHERE t.tid=".pwEscape($tid)."AND tm.aid!=''");
				if(!$rt) continue;
				$aid = unserialize($rt['aid']);
				foreach($aid as $key=>$value){
					if($value['type']=='img'){
						$value['attachurl'] = geturl($value['attachurl'],'lf');
						$flashpics   .= $extra.$value['attachurl'][0];
						$flashlinks  .= $extra."read.php?tid=$tid";
						$flashtitles .= $extra.$rt['subject'];
						$extra = '|';
						$config['nums']--;break;
					}
				}
			}
		}
		$html = "<div id=\"eye\"><h5 class=\"h\"><span class=\"fr gray f10\"></span>$title </h5>";
		$tmpCount = 0;
		if($config['nums']>0){
			if($GLOBALS['db_tlist']){
				@extract($db->get_one("SELECT MAX(tid) as mtid FROM pw_threads"));
				$pw_tmsgs = GetTtable($mtid);
			} else{
				$pw_tmsgs = 'pw_tmsgs';
			}
			$sql = $config['time']>0 ? " AND t.postdate>".pwEscape($timestamp-86400*$config['time']) : '';

			$query = $db->query("SELECT t.tid,t.subject,tm.aid FROM pw_threads t LEFT JOIN $pw_tmsgs tm ON t.tid=tm.tid LEFT JOIN pw_forums f ON t.fid=f.fid WHERE 1 $sql AND t.ifupload='1' AND tm.aid!='' AND f.password='' AND f.allowvisit='' AND f.f_type<>'hidden' ORDER BY $config[order] LIMIT $config[nums]");
			while($rt = $db->fetch_array($query)){
				$aid = unserialize($rt['aid']);
				foreach($aid as $key=>$value){
					if($value['type']=='img'){
						$value['attachurl'] = geturl($value['attachurl'],'lf');
						$subject = substrs($rt['subject'],40,1);
						$htmls .= "<div id=\"Switch_$tmpCount\" style=\"display:none\"><a href=\"read.php?tid=$rt[tid]\"><img src=\"{$value[attachurl][0]}\" title=\"{$rt[subject]}\" class=\"tac\" width=\"100%\" /><h3 class=\"fn f12\">{$subject}</h3></a></div>";
						$tmpCount++;
					}
				}
			}
		}

		if ($tmpCount) {
			$html .= "<div id=\"pwSlidePlayer\" class=\"flashNews\" onmouseover=\"pwSlidePlayer('pause');\" onmouseout=\"pwSlidePlayer('goon');\">$htmls
			<ul id=\"SwitchNav\"></ul>
			<div class=\"bg\"></div></div><div class=\"c\"></div>
			<script type=\"text/javascript\" src=\"js/picplayer.js\"></script>
			<script type=\"text/javascript\">
			var MaxScreen = $tmpCount;
			var CurScreen = 0;
			pwSlidePlayer();
			</script>";
		}

		$html .= "</div>";
		$html = addslashes($html);
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html))."WHERE name=".pwEscape($name));
	}

	function tags($name,$title,$config){//热门tag模块
		global $db,$timestamp;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 25;

		$tagdb = $config['tags'] ? explode(',',$config['tags']) : array();
		$query = $db->query("SELECT tagname FROM pw_tags ORDER BY num DESC LIMIT $config[nums]");
		while($rt = $db->fetch_array($query)){
			if(!in_array($rt['tagname'],$tagdb)){
				$tagdb[] = $rt['tagname'];
			}
		}
		$html = "<div id=\"tags\" class=\"tags\"><h5 class=\"h\">$title</h5><ul>";
		foreach($tagdb as $tag){
			$html .= "<li><a href=\"job.php?action=tag&tagname=".rawurlencode($tag)."\" target=\"_blank\">$tag</a></li>";
		}
		$html .= "</ul></div><div class=\"c\"></div>";
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name=".pwEscape($name));
	}

	function recommend($name,$title,$config){// 推荐帖子
		global $db,$timestamp,$imgpath;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 15;
		!$config['pic'] && $config['pic'] = "hack/home/image/none.bmp";
		if(!in_array($config['order'],array('hits','replies','dig'))){
			$config['order'] = 'dig';
		}

		$read = array();
		$tids = $this->checktids($config['tids']);
		if($tids){
			$query = $db->query("SELECT tid,subject FROM pw_threads WHERE tid IN($tids)");
			while($rt = $db->fetch_array($query)){
				$read[] = $rt;
			}
		}
		$config['nums'] -= count($read);
		if($config['nums']>0){
			$sql   = $config['time']>0 ? " AND t.postdate>".pwEscape($timestamp-86400*$config['time']) : '';
			$query = $db->query("SELECT t.tid,t.subject FROM pw_threads t LEFT JOIN pw_forums f ON t.fid=f.fid WHERE 1 $sql AND f.password='' AND f.allowvisit='' AND f.f_type<>'hidden' ORDER BY t.$config[order] LIMIT $config[nums]");
			while($rt = $db->fetch_array($query)){
				$read[] = $rt;
			}
		}
		$readone = $read[0];
		unset($read[0]);
		$pw_tmsgs = GetTtable($readone['tid']);
		$rs = $db->get_one("SELECT content FROM $pw_tmsgs WHERE tid=".pwEscape($readone['tid'],false));
		require_once(R_P.'require/bbscode.php');
		$readone['content'] = substrs(strip_tags(convert($rs['content'])),60);

		$html = "<h5 class=\"h\">$title </h5><div><dl class=\"dlA\"><dd class=\"pic\"><img src=\"$config[pic]\" /></dd><dt class=\"h2A\"><a href=\"read.php?tid=$readone[tid]\" class=\"cl1\">$readone[subject]</a></dt><dd class=\"txt\">$readone[content]<div class=\"tar\"><a href=\"read.php?tid=$readone[tid]\">(".getLangInfo('other','home_detail').")</a></div></dd><dd class=\"c\"></dd></dl></div><div class=\"c\"></div><div><ul class=\"listA\">";

		foreach($read as $value){
			$html .= "<li><a href=\"read.php?tid=$value[tid]\">$value[subject]</a></li>";
		}
		$html .= "</ul></div><div class=\"c\"></div>";

		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name='recommend'");
	}

	function hottopic($name,$title,$config){//热门帖子
		global $db,$timestamp;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 10;
		if(!in_array($config['order'],array('hits','replies','dig'))){
			$config['order'] = 'replies';
		}
		$sql  = $config['time']>0 ? " AND t.postdate>".pwEscape($timestamp-86400*$config['time']) : '';

		$html = "<div id=\"hottopic\" class=\"remen\"><h5 class=\"h\">$title</h5><ul class=\"listB\">";
		$query = $db->query("SELECT t.tid,t.subject FROM pw_threads t LEFT JOIN pw_forums f ON t.fid=f.fid WHERE 1 $sql AND f.password='' AND f.allowvisit='' AND f.f_type<>'hidden' ORDER BY t.$config[order] LIMIT $config[nums]");
		while($rt = $db->fetch_array($query)){
			$html .= "<li><a href=\"read.php?tid=$rt[tid]\">$rt[subject]</a></li>";
		}
		$html .= "</ul></div>";
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name='hottopic'");
	}

	function bbsinfo($name,$title,$config){//站点信息
		global $db,$imgpath,$timestamp,$db_online;

		if ($db_online) {
			$userinbbs = $guestinbbs = 0;
			$query = $db->query("SELECT uid!=0 as ifuser,COUNT(*) AS count FROM pw_online GROUP BY uid='0'");
			while($rt = $db->fetch_array($query)){
				if($rt['ifuser']) $userinbbs=$rt['count']; else	$guestinbbs=$rt['count'];
			}
		} else {
			@include_once(D_P.'data/bbscache/olcache.php');
		}
		$usertotal = $guestinbbs + $userinbbs;
		@extract($db->get_one("SELECT * FROM pw_bbsinfo WHERE id='1'"));
		@extract($db->get_one("SELECT SUM(tpost) AS tposts,SUM(topic) AS threads,SUM(article) AS posts FROM pw_forumdata"));
		$higholtime = get_date($higholtime);
		$html = "<div id=\"bbsinfo\"><h5 class=\"h\">$title </h5><div class=\"boxA\">".str_replace(array("\n",'{$totalmember}','{$newmember}','{$threads}','{$posts}','{$tposts}','{$yposts}','{$hposts}','{$usertotal}','{$userinbbs}','{$guestinbbs}','{$higholnum}','{$higholtime}'),array("<br />",$totalmember,$newmember,$threads,$posts,$tposts,$yposts,$hposts,$usertotal,$userinbbs, $guestinbbs,$higholnum,$higholtime),$config['info'])."</div></div><div class=\"c\"></div>";
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name='bbsinfo'");
	}

	function forumsort($name,$title,$config){//版块排行榜
		global $db,$imgpath,$timestamp;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 10;

		$i    = 1;
		$html = "<div id=\"forumsort\"><h5 class=\"h\">$title </h5><div class=\"boxA\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"paihang\"><tr class=\"tr2\"><td class=\"gray\">".getLangInfo('other','home_forums')."</td><td class=\"gray\">".getLangInfo('other','home_posts')."</td></tr>";

		$query  = $db->query("SELECT f.fid,f.name,fd.topic FROM pw_forumdata fd LEFT JOIN pw_forums f USING(fid) WHERE f.password='' AND f.allowvisit='' AND f.f_type<>'hidden' AND f.type<>'category' ORDER BY fd.topic DESC LIMIT $config[nums]");
		while($forums = $db->fetch_array($query)){
			$html .= "<tr><td>";
			$i<4 && $html .= '<span class="cl2">';
			$html .= ($i<10 ? '0' : '').$i.'.';
			$i<4 && $html .= '</span>';
			$forums['name']=strip_tags($forums['name']);
			$fname=substrs($forums['name'],10);
			$html .= "<a href=\"thread.php?fid=$forums[fid]\" title=\"$forums[name]\">$fname</a></td><td class=\"gray\">($forums[topic])</td></tr>";
			$i++;
		}
		$html .= "</table></div></div>";
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name='forumsort'");
	}

	function newreply($name,$title,$config){//最新回复
		global $db,$imgpath,$timestamp;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 15;

		$fids   = array('-99');
		$query  = $db->query("SELECT fid FROM pw_forums WHERE password='' AND allowvisit='' AND f_type<>'hidden'");
		while($rt = $db->fetch_array($query)){
			$fids[] = $rt['fid'];
		}
		$fids = pwImplode($fids);
		$sql  = $config['time']>0 ? " AND postdate>".pwEscape($timestamp-86400*$config['time']) : '';
		$html = "<div id=\"newreply\"><h5 class=\"h\">$title </h5><ul class=\"listC\">";

		$query  = $db->query("SELECT tid,subject,postdate FROM pw_threads WHERE fid IN($fids) AND topped<'1' $sql ORDER BY topped,lastpost DESC LIMIT $config[nums]");
		while($rt = $db->fetch_array($query)){
			$html .= "<li><em>".get_date($rt['postdate'],'m-d')."</em><a href=\"read.php?tid=$rt[tid]\" target=\"_blank\">$rt[subject]</a></li>";
		}
		$html .= "</ul></div>";
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name='newreply'");
	}

	function newtopic($name,$title,$config){//最新发表帖子
		global $db,$imgpath,$timestamp,$windid,$groupid,$db_anonymousname;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 15;

		$fids   = array('-99');
		$query  = $db->query("SELECT fid FROM pw_forums WHERE password='' AND allowvisit='' AND f_type<>'hidden'");
		while($rt = $db->fetch_array($query)){
			$fids[] = $rt['fid'];
		}
		$fids = pwImplode($fids);
		$html   = "<div><h5 class=\"h\">$title </h5><ul class=\"listD\">";

		$sql   = $config['time']>0 ? " AND postdate>".pwEscape($timestamp-86400*$config['time']) : '';
		$query = $db->query("SELECT tid,subject,author,authorid,postdate,anonymous FROM pw_threads WHERE fid IN($fids) $sql ORDER BY postdate DESC LIMIT $config[nums]");
		while($rt = $db->fetch_array($query)){
			if ($rt['anonymous']) {
				$rt['author'] = $db_anonymousname;
				$rt['authorid'] = 0;
			}
			$html .= "<li><em><cite><a class=\"gray\" href=\"u.php?action=show&uid=$rt[authorid]\">$rt[author]</a></cite>".get_date($rt['postdate'],'m-d')."</em><a href=\"read.php?tid=$rt[tid]\">$rt[subject]</a></li>";
		}
		$html .= "</ul></div>";
		$db->update("UPDATE pw_home SET lastupdate='$timestamp',cache='$html' WHERE name='newtopic'");
	}

	function usersort($name,$title,$config){//会员帖子排行榜
		global $db,$imgpath,$timestamp;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 10;
		if(!in_array($config['order'],array('postnum','rvrc','money'))){
			$config['order'] = 'postnum';
		}

		$i    = 1;
		$html = "<div id=\"usersort\"><h5 class=\"h\">$title </h5><div class=\"boxA\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"paihang\"><tr class=\"tr2\"><td class=\"gray\">".getLangInfo('other','home_user')."</td><td class=\"gray\">".getLangInfo('other','home_'.$config['order'])."</td></tr>";

		$query = $db->query("SELECT m.uid,m.username,md.$config[order] FROM pw_memberdata md LEFT JOIN pw_members m USING(uid) ORDER BY md.$config[order] DESC LIMIT $config[nums]");
		while($men = $db->fetch_array($query)){
			$config['order'] == 'rvrc' && $men[$config['order']] = floor($men[$config['order']]/10);
			$html .= "<tr><td>";
			$i<4 && $html .= '<span class="cl2">';
			$html .= ($i<10 ? '0' : '').$i.'.';
			$i<4 && $html .= '</span>';
			$html .= "<a href=\"u.php?action=show&uid=$men[uid]\">$men[username]</a></td><td class=\"gray\">({$men[$config[order]]})</td></tr>";
			$i++;
		}
		$html .= "</table></div></div>";
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name='usersort'");
	}

	function forums($name,$title,$config){//版块信息调用
		global $db,$imgpath,$timestamp;

		/**   默认配置   **/
		$config['nums']<1 && $config['nums'] = 10;
		!$config['pic'] && $config['pic'] = "hack/home/image/none.bmp";
		$fid = substr($name,7);

		$sql   = $config['time']>0 ? " AND postdate>".pwEscape($timestamp-86400*$config['time']) : '';
		$read  = array();
		$query = $db->query("SELECT tid,subject,postdate FROM pw_threads WHERE fid=".pwEscape($fid).$sql." ORDER BY postdate DESC LIMIT $config[nums]");
		while($rt = $db->fetch_array($query)){
			$read[] = $rt;
		}
		$readone = $read[0];
		unset($read[0]);
		$pw_tmsgs = GetTtable($readone['tid']);
		$rs = $db->get_one("SELECT content FROM $pw_tmsgs WHERE tid=".pwEscape($readone['tid'],false));
		require_once(R_P.'require/bbscode.php');
		$readone['content'] = substrs(strip_tags(convert($rs['content'])),60);
		$html = "<div id=\"forums_$fid\"><h5 class=\"h\"><a href=\"thread.php?fid=$fid\" class=\"fr fn cl4\"> ".getLangInfo('other','home_more')."</a>{$title}</h5><dl class=\"dlB\"><dd class=\"pic\"><img src=\"$config[pic]\" /></dd><dt class=\"b\"><a href=\"read.php?tid=$readone[tid]\" class=\"cl3\">$readone[subject]</a></dt><dd class=\"txt\">$readone[content]<div class=\"tar\"><a href=\"read.php?tid=$readone[tid]\">(".getLangInfo('other','home_detail').")</a></div></dd></dl><div class=\"c\"></div><ul class=\"listC listE\">";

		foreach($read as $key=>$value){
			$html .= "<li><em>".get_date($value['postdate'],'m-d')."</em><a href=\"read.php?tid=$value[tid]\" target=\"_blank\">$value[subject]</a></li>";
		}
		$html .= "</ul></div>";
		$db->update("UPDATE pw_home SET ".pwSqlSingle(array('lastupdate'=>$timestamp,'cache'=>$html),false)."WHERE name=".pwEscape($name));
	}

	function checktids($tids){
		$t = '';
		$s = explode(',',$tids);
		foreach($s as $v){
			is_numeric($v) && $t .= $t ? ','.$v : $v;
		}
		return $t;
	}
}
?>