<?php
@ini_set('memory_limit', '-1');
@set_time_limit(0);
!defined("MONKEYMAO") && die("Accessed Defined!");
require_once MODEPATH.'./'.$adminpath.'/makehtml.func.php';
require_once MODEPATH.'./includes/theme.func.php';

$sorts=$fsorts=array();
$query = $db -> query("SELECT * FROM {$tablepre}picsort ORDER BY ordermode ");
while($sort = $db -> fetch_array($query)){
	$sorts[$sort['id']]=$sort;
	$fsorts[$sort['upid']][$sort['id']]=$sort;
}
if($menu == 'makeindex'){
	makeindex();
	if($makeall == '1') {
		cpmsg('makehtml_index_ok_gonext','index.php?action=makehtml&menu=makelist&make=makeall&makeall=1');
	}else{
		cpmsg('makehtml_index_ok');
	}
}elseif($menu == 'makelist'){
	if(!$sorts) {
		cpmsg('makehtml_sort_not_finded');
	}
	if($make) {
		if($make == 'makeall' && !$id) {
			$query=$db -> query("SELECT * FROM {$tablepre}picsort ORDER BY id LIMIT 1");
			$start = $db -> result($query,0);
			$id = $start[$id];
		}
		$sort=$sorts[$id];
		if(!$sort) {
			cpmsg('error');
		}
		$getpage = readfromfile(theme('list'));
	
		@preg_match("/\[pagesize:([0-9_]+)\]/ies",$getpage, $pageinfo);

		$pagesize = intval($pageinfo[1]) ? intval($pageinfo[1]) : $pagelistnum;
		
		$query=$db -> query("SELECT COUNT(*) FROM {$tablepre}pic WHERE id='$id'");
		$nowlines = $db -> result($query,0);

		$pages = ceil($nowlines/$pagesize);
		
		$htmls ='';
		if($pages > 1) {
			for($i=1;$i<=$pages;$i++) {
				$htmlname=makelist($id,$i);		
				$htmls .= '<a href="'.$htmlname.'" target="_blank">'.$htmlname.'</a><br>';
			}
		}else{
				$htmlname=makelist($id,1);
				$htmls .= '<a href="'.$htmlname.'" target="_blank">'.$htmlname.'</a><br>';
		}

		if($make == 'makeall') {
			$next=$db -> queryarray("SELECT * FROM {$tablepre}picsort WHERE id>'$id'");
			if($next) {
				cpmsg('makehtml_sort_ok','index.php?action=makehtml&menu=makelist&make=makeall&makeall='.$makeall.'&id='.$next['id']);
			}else{
				if($makeall=='1') {
					cpmsg('makehtml_allsort_ok','index.php?action=makehtml&menu=makepage&make=makeall&makeall=1');
				}
				cpmsg('makehtml_ok');
			}
		}else{
			cpmsg('makehtml_ok');
		}
	}else{
		$select=sortselect($fsorts);
		include theme('makehtml_list', 'themes/admin','admin');	
	}

}elseif($menu == 'makepage'){
	if($make){
		$urladd=$sql='';

		if($make == 'makeid') {
			if(!$id || !$sorts[$id]) {
				cpmsg('sort_must');
			}
			$sql= " WHERE id='$id' ";
			$urladd = '&id='.$id;
		}elseif($make == 'makedate'){
		
			if($startime != '') {
				$start = strtotime($startime);
				$sql= "WHERE postime>'$start'";
			}

			if($endtime!= '') {
				$end = strtotime($endtime);
				$sql.= ($sql != '' ? " AND " : " WHERE ")." postime<='$end'";
			}
			$urladd = '&startime='.$startime.'&endtime='.$endtime;
		}

		$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}pic $sql");
		$lines = $db -> result($query,0);
		$num = 40;
		$pages = ceil($lines/$num);
		$page = ispage($page) ? $page : 1;	
		$start_limit = ($page-1) * $num;
		$query = $db -> query("SELECT * FROM {$tablepre}pic $sql ORDER BY postime DESC LIMIT $start_limit,$num");
		$htmls = '';
		while($view = $db->fetch_array($query)) {		
			$htmlname=makepage($view['pid']);	
			$htmls .= '<a href="'.$htmlname.'" target="_blank">'.$htmlname.'</a><br>';
		}

		if($page >= $pages) {
		cpmsg('makehtml_ok');
		}else{
			$nextpage= $page+1;
			$last = $lines - $page * $num;		
			cpmsg('makehtml_page_ok_gonext','index.php?action=makehtml&menu=makepage&make='.$make.'&makeall='.$makeall.'&page='.$nextpage.$urladd);
		}

	}else{
		$startime = gmdate("Y-m-d", $time+ $timeoffset * 3600 -86400);
		$edntime = gmdate("Y-m-d", $time+ $timeoffset * 3600);
		$select=sortselect($fsorts);
		include theme('makehtml_page', 'themes/admin','admin');
	}
}elseif($menu == 'makepic'){
		cpmsg('图片页生成功能，为商业版功能。需要该功能的请联系QQ426420。');
}
else{
	$select=sortselect($fsorts);
	include theme('makehtml', 'themes/admin','admin');
}
?>