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

	$Id: thread_list hack 2010-09-20 22:00:00Z ANE $
 */

if(!defined('IN_DISCUZ')) {
       exit('Access Denied'); 
}
class plugin_thread_list_forum {
	function index_top() {
		global $_G,$thread_list;

		$thread_list = $_G['cache']['plugin']['thread_list'];
		$attachurl = $_G['setting']['attachurl'];
		$thread_list['cache_time'] = $thread_list['cache_time'] < 10 ? 10 : $thread_list['cache_time'];
		$pluginTime = DB::result_first("SELECT dateline FROM ".DB::table('common_syscache')." WHERE cname='plugin'");
		
		if($thread_list['is_guest'] || $_G['uid']) {
			$cacheFile = DISCUZ_ROOT.'./data/cache/cache_thread_list.php';
			$kk_xthread_listupdate = ($_G['timestamp']-@filemtime($cacheFile)) > $thread_list['cache_time'] ? 1 : 0;//
			$notshow = $thread_list['not_show'] ? 'AND f.fid not IN('.$thread_list['not_show'].')' : '';//不显示的版块  all
			$onlyshow = $thread_list['only_show'] ? 'AND f.fid IN('.$thread_list['only_show'].')' : '';//仅显示的版块  all
			$x_title = explode('|', str_replace(array("\r\n", "\n"), '|', $thread_list['set_title']));// all
			$propre = explode('|', str_replace(array("\r\n", "\n"), '|', $thread_list['propre']));//前缀 all

			$linkwindow = $thread_list['link_window'] == 1 ? ' target="_blank"' : ''; //all
			$pic_color = $thread_list['pic_color'] ? str_replace('#', '0x', $thread_list['pic_color']) : '0x0099ff'; //all
			$authnum_color = $thread_list['authnum_color'] ? $thread_list['authnum_color'] : str_replace('0x', '#', $pic_color); //all
			$author_color = $thread_list['author_color'] ? $thread_list['author_color'] : $authnum_color; //all
			$user_num = $thread_list['user_num'] ? $thread_list['user_num'] : '15';
			$pic_on = $thread_list['pic_on'];
			$userdefinecss = $thread_list['userdefinecss'];
			$thread_num = $thread_list['thread_num'] ? $thread_list['thread_num'] : 10;
			$thread_height = $thread_num*20;
			$tab_style = $thread_list['tab_style'];
			$pic_height = max($user_num,$thread_num)*20 -2;

			$row_num = 5;
			$td_star_row = '13%';
			!$pic_on && $row_num--;
			$thread_list['five_list'] && $row_num--;
			if($row_num == 5) {
				$td_row = '21%';
			} elseif($row_num == 4 && $pic_on) {
				$td_row = '25%';
			} elseif($row_num == 4 && !$pic_on) {
				$td_row = '29%';
			} elseif($row_num == 3) {
				$td_row = '33%';
			}
			if($thread_list['five_list']) {
				$tab_num = 3;
			} else {
				$tab_num = 4;
			}

				if ($thread_list['pic_switch'] && $pic_on) {//
				$picpics = str_replace(array("\r\n", "\n"), '|', $thread_list['pic_urls']);
				$piclinks = str_replace(array("\r\n", "\n"), '|', $thread_list['pic_links']);
				$pictexts = str_replace(array("\r\n", "\n"), '|', $thread_list['pic_titles']);
			}
			if ((($_G['timestamp'] - @filemtime($cacheFile)) > $thread_list['cache_time']) || $thread_list_xthread_listupdate) {//
				$cacheArray = '';
				require_once libfile('function/cache');
				require_once DISCUZ_ROOT.'./source/plugin/thread_list/thread_list.func.php';

				if($thread_list['pic_on']) {
					if (!@$picpics || !$thread_list['pic_switch']) {//
						$datapic = array();
						$fids = $thread_list['only_pic'] ? 'AND t.fid IN('.$thread_list['only_pic'].')' : '';
						$PicNums = $thread_list['pic_schnum'];
				
				if ($kk_xshow['pic_transfer'] == 1) { 
					$orderby = 'tid'; 
				} else if ($kk_xshow['pic_transfer'] == 2) { 
					$orderby = 'tid'; 
				} else { 
					$orderby = 'rand'; 
				}
					$orderby = $orderby != 'rand' ? 'attach.'.$orderby : 'rand()';
					$query = DB::query("SELECT attach.attachment,t.tid, t.fid, t.subject FROM ".DB::table('forum_threadimage')." attach INNER JOIN ".DB::table('forum_thread')." t ON t.tid=attach.tid WHERE t.isgroup=0 AND t.displayorder>=0 $fids GROUP BY attach.tid ORDER BY $orderby DESC LIMIT 0, ".$PicNums);
				    while($pic = DB::fetch($query)) {
					    $pics['picpics'] = $_G['setting']['attachurl'].'forum/'.$pic['attachment'];
					    $pics['piclinks'] = 'forum.php?mod=viewthread%26tid='.$pic['tid'];
					    $pics['pictexts'] = str_replace('\'', ' ',$pic['subject']);
					    $pics['attaid'] = $pic['aid'];
					    $datapic[] = $pics;
					}
					$cacheArray .= "\$pic=".arrayeval($datapic).";\n";
					unset($query, $datapic, $pics);
					}
				}
		        $sql = "SELECT t.*, f.name FROM ".DB::table('forum_thread')." t, ".DB::table('forum_forum')." f WHERE f.status<>'3' AND f.fid=t.fid $notshow $onlyshow AND t.displayorder not IN(-1,-2) ORDER BY t.dateline DESC LIMIT 0, $thread_num";
				$objarray = thread_list_query($sql);
				$cacheArray .= "\$newPost=".arrayeval($objarray).";\n\n";
				unset($sql, $objarray);
				$sql = "SELECT t.*, f.name FROM ".DB::table('forum_thread')." t, ".DB::table('forum_forum')." f WHERE f.status<>'3' AND f.fid=t.fid $notshow $onlyshow AND t.displayorder not IN(-1,-2) AND t.closed NOT LIKE 'moved|%' AND t.replies!=0 ORDER BY t.lastpost DESC LIMIT 0, $thread_num";
				$objarray = thread_list_query($sql);
				$cacheArray .= "\$newReply=".arrayeval($objarray).";\n\n";
				unset($sql, $objarray);
				$sortway = array('', 'replies', 'views', 'dateline', 'lastpost');
				$hotdig = $thread_list['hot_dig'] == 2 ? $sortway[$thread_list['dig_list']] : $sortway[$thread_list['hot_list']];
				$ctime = $thread_list['tid_day'] ? $_G['timestamp'] - (3600 * 24 * $thread_list['tid_day']) : 0;
				$DigShow = $thread_list['hot_dig'] == 2 && $thread_list['dig_rank'] ? 'AND t.digest IN('.$thread_list['dig_rank'].')' : '';
				$sql = "SELECT t.*, f.name FROM ".DB::table('forum_thread')." t, ".DB::table('forum_forum')." f WHERE f.status<>'3' AND f.fid=t.fid $notshow $onlyshow $DigShow AND t.displayorder not IN(-1,-2) AND t.replies!=0 AND t.dateline>$ctime ORDER BY t.$hotdig DESC LIMIT 0, $thread_num";
				$objarray = thread_list_query($sql);
				$cacheArray .= "\$ObjArray=".arrayeval($objarray).";\n\n";
				unset($sortway, $hotdig, $sql, $ctime, $DigShow, $objarray);
				$poststar = array(); $tomonth = date('n'); $todate = date('j'); $toyear = date('Y');
				if ($thread_list['author_date'] == 1) { $time = mktime(0, 0, 0, $tomonth, $todate, $toyear); }
				else if ($thread_list['author_date'] == 2) { $time = mktime(0, 0, 0, $tomonth, $todate, $toyear) - 604800; }
				else if ($thread_list['author_date'] == 3) { $time = mktime(0, 0, 0, $tomonth, 1, $toyear); }
				else if ($thread_list['author_date'] == 4) { $time = mktime(0, 0, 0, 1, 1, $toyear); }
				else { $time = 0; }
				if($thread_list['author_date'] != 6) {
			         $query = DB::query("select count(pid) as num, authorid, author from ".DB::table('forum_post')." where dateline>=$time group by authorid order by num desc limit 0, $user_num");
		        } else {
			         $query = DB::query("SELECT uid as authorid,username as author,regdate FROM ".DB::table('common_member')." ORDER BY regdate DESC limit 0, $user_num");
		        }
				while($result = DB::fetch($query)) {
					$result['avatar'] = discuz_uc_avatar($result['authorid'], small);
					$result['autitl'] = $result['author'];
					$result['author'] = cutstr($result['author'], $thread_list['ahthor_nums']);
					if($thread_list['author_date'] == 6) {
						$result['num'] = gmdate('Y-m-d', $result['regdate'] + $_G['setting']['timeoffset'] * 3600);
					}
					$poststar[] = $result;
				}
				$cacheArray .= "\$postStar=".arrayeval($poststar).";\n";
				unset($query, $poststar, $result);
				writetocache('thread_list',$cacheArray);
				unset($cacheArray);
			}
			require_once $cacheFile;
			
			if (@$pic && !$kk_xshow['pic_switch']) {//
				PHP_VERSION > 4.3 ? shuffle($pic) : '';
				$i = 0; $comma = '';
				foreach ($pic as $key=>$val) {
					if (is_readable($val['picpics']) || substr($val['picpics'], 0, 4) == 'http') {
						$picpics .= $comma.$val['picpics'];
						$piclinks .= $comma.$val['piclinks'];
						$pictexts .= $comma.$val['pictexts'];
						$i++;
					} else {
						if($_G['setting']['ftp']['on']) {
							$picpics .= $comma.$_G['setting']['ftp']['attachurl'].str_replace($attachurl, '', $val['picpics']);
							$piclinks .= $comma.$val['piclinks'];
							$pictexts .= $comma.$val['pictexts'];
							$i++;
						} else {
							$picpics .= $comma.$val['picpics'];
							$piclinks .= $comma.$val['piclinks'];
							$pictexts .= $comma.$val['pictexts'];
							$i++;
						}
					}
					$comma = '|';
					if ($i == $kk_xshow['pic_num']) break;
				}
				unset($pic);
			}

			
		}

		include_once DISCUZ_ROOT.'./data/cache/cache_thread_list.php';
		if($thread_list['tab_style']) {
			include template('thread_list:thread_list_tab');
		} else {
			include template('thread_list:thread_list');
		}
		return $return;
	}
}
?>
