<?php

/*
'**************************************************************************************************
' 程序名称: CCTVAI开源音乐管理系统
' 官方网站: http://www.cctvai.com
' 联系 Q Q: 1542367792
' QQ交流群：136687984
' 版本：(终结版)
' 备注：未经书面授权，不得向任何第三方提供本软件系统！
' 功能，模板，插件，扩展，定制请联系QQ：1542367792
'**************************************************************************************************
*/
error_reporting(0);
include '../../include/cctvai.conn.php';
require_once("../../include/cctvai.label.php");
include "../function_common.php";
admincheck(10);
/*if(Smallfivesolution()==$_SERVER['SERVER_NAME']) {
} else {
	echo Smallfivesq();
	exit();
}*/
echo "<style type='text/css'><!--body,td,th {background-color: #FFFFFF;font-size: 9pt;}a {font-size: 9pt;}a:link {text-decoration: none;color:#3F628C;}a:visited {text-decoration: none;color:#3F628C;}a:hover {text-decoration: none;color:#3F628C;}a:active {text-decoration: none;color:#3F628C;}--></style>";
@set_time_limit(0);
if(cd_webhtml==1 or cd_webhtml==3) {
	die('<font color=red>当前网站为动态模式，不能生成！</font>');
}
$ClassID=SafeRequest('ClassID', 'post');
if($ClassID=='') {
	$ClassID=SafeRequest('ClassID', 'get');
}
$type=SafeRequest('type', 'get');
if($ClassID=='0') {
	$type='all';
}
$classnow=SafeRequest('classnow', 'get');
if($type=='all' or $type=='spanall') {
	if($classnow=='') {
		$classnow=0;
	}
	$sql='select CD_ID from ' . tname('singerclass') . ' where CD_IsHide=0 Order by CD_ID asc';
	$classresult=$db->getAll($sql);
	$ClassID=$classresult[$classnow]['CD_ID'];
}
if(!IsNum($ClassID)) {
	$ClassID=1;
}
$page=SafeRequest('pages', 'get');
if(!IsNum($page)) {
	$page=1;
}
$sql='select * from ' . tname('singerclass') . ' where CD_ID=' . $ClassID . '';
$row=$db->getrow($sql);
if(!IsNul($row['CD_Template'])) {
	$row['CD_Template']='singer_list.html';
}
if(file_exists(_cctvai_root_ . cd_templateurl . $row['CD_Template'])) {
	$ListContent=@file_get_contents(_cctvai_root_ . cd_templateurl . $row['CD_Template']);
	preg_match_all('/{cctvai:singer(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cctvai:singer}/', $ListContent, $page_arr2);
	if(!empty($page_arr2) && !empty($page_arr2[2])) {
		$sqlstr2=Mark_Sql(tname('singer'), $page_arr2[1][0], $ClassID);
		$recount2=$db->num_rows($db->query($sqlstr2));
		$Arr2=SPage($sqlstr2, $page_arr2[2][0], 1, 1, 10);
	} else {
		$recount2=1;
		$Arr2[6]=1;
		$page_arr2[2][0]=1;
	}
	if($Arr2[6]<cd_createpre) {
		$spanpage=1;
	} else {
		$spanpage=ceil($Arr2[6]/cd_createpre);
	}
	echo ("<div style='width:100%; background: #F0F0F0; border-top:1px solid #ddd; border-bottom:1px solid #ddd;color:#668E1B; font-size:10pt; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;正在生成<font style='color:red; font-size:14px;'>" . $row['CD_Name'] . "</font>栏目列表第<font style='color:red; font-size:16px; font-style:italic'>" . $page . "</font>页，共<font style='color:red; font-size:16px; font-style:italic'>" . $spanpage . '</font>页</div><br/>');
	$pagenum=getpagenum($ListContent);
	preg_match_all('/{cctvai:singer(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cctvai:singer}/', $ListContent, $page_arr);
	if(!empty($page_arr) && !empty($page_arr[2])) {
		$sqlstr=Mark_Sql(tname('singer'), $page_arr[1][0], $ClassID);
	}
	for($l=($page-1)*cd_createpre+1; $l<(cd_createpre*$page)+1; $l++) {
		if($recount2=='0') {
			if((($l-1)*$page_arr2[2][0])>$recount2) {
				break;
			}
		} else {
			if((($l-1)*$page_arr2[2][0])>=$recount2) {
				break;
			}
		}
		$data_content='';
		if(!empty($page_arr) && !empty($page_arr[2])) {
			$Arr=SPage($sqlstr, $page_arr[2][0], $l, $ClassID, $pagenum);
			$result=$db->query($Arr[2]);
			$recount=$db->num_rows($result);
			if($recount==0) {
				$data_content='<div align=center><strong><font style=font-size:12px;>该栏目暂无数据！</font></strong></div>';
			} else {
				if($result) {
					$sorti=1;
					while($row2=$db->fetch_array($result)) {
						$datatmp=datasinger($page_arr[0][0], $page_arr[3][0], $row2, $sorti);
						$sorti=$sorti+1;
						$data_content.=$datatmp;
					}
				}
			}
			unset($data_content);
		}
		$spanurl=LinkClassUrl('singerclass', $ClassID, 1, $Arr[5]);
		$spanurl=substr($spanurl, strlen(cd_webpath));
		if(cd_websingerhtml=="1"){
			$spanpath="http://" . $_SERVER['HTTP_HOST'] . cd_webpath . cd_webmlartist . "?0," . $row['CD_AliasName'] . "," . $Arr[5] . ".html";
		}else{
			$spanpath="http://" . $_SERVER['HTTP_HOST'] . cd_webpath . cd_webmlartist . "?0," . $row['CD_ID'] . "," . $Arr[5] . ".html";
		}
		spandir($spanurl);
		fwrite(fopen('../../' . $spanurl, 'wb'), @file_get_contents($spanpath));
		echo '&nbsp;<font style=font-size:10pt;>生成栏目：<font color=red>' . $row['CD_Name'] . '</font> 第' . $Arr[5] . '页成功&nbsp;&nbsp;<a href=' . cd_webpath . $spanurl . ' target=_blank>' . cd_webpath . $spanurl . '</a>  </font><br/>';
		@mysql_free_result($result);
	}
	unset($page_arr);
	unset($Arr);
	$page=$page+1;
	if($page>$spanpage) {
		if($type=='all' or $type=='spanall') {
			if(($classnow+1)>(count($classresult)-1)) {
				if($type=='spanall') {
		echo "<script language=\"javascript\" type=\"text/javascript\"> \n";
		echo "var i = " . cd_stoptime . "; \n";
		echo "var intervalid; \n";
		echo "intervalid = setInterval(\"fun()\", 1000); \n";
		echo "function fun() { \n";
		echo "if (i == 0) { \n";
		echo "window.location.href = \"?ClassIDs=0\"; \n";
		echo "clearInterval(intervalid); \n";
		echo "} \n";
		echo "document.getElementById(\"mes\").innerHTML = i; \n";
		echo "i--; \n";
		echo "} \n";
		echo "</script> \n";
		echo '<br/>&nbsp;<font style=font-size:10pt;><b><font color=red>为了节约系统资源，将暂停 <span id="mes">' . cd_stoptime . '</span> 秒后继续生成，请不要关闭或刷新...</b></font>';
				} else {
					echo "<br/>&nbsp;<font style=font-size:10pt;><b>恭喜您，全部栏目列表生成完毕！</b></font><script language='javascript'>parent.form1.document.getElementById('StopTime').innerHTML='恭喜您，全部栏目列表生成完毕！';alert('恭喜您，全部栏目列表生成完毕！');</script>";
				}
			} else {
				echo "<script language=\"javascript\" type=\"text/javascript\"> \n";
				echo "var i = " . cd_stoptime . "; \n";
				echo "var intervalid; \n";
				echo "intervalid = setInterval(\"fun()\", 1000); \n";
				echo "function fun() { \n";
				echo "if (i == 0) { \n";
				echo "window.location.href = \"?ClassID=" . $ClassID . "&type=" . $type . "&classnow=" . ($classnow+1) . "\"; \n";
				echo "clearInterval(intervalid); \n";
				echo "} \n";
				echo "document.getElementById(\"mes\").innerHTML = i; \n";
				echo "i--; \n";
				echo "} \n";
				echo "</script> \n";
				echo '<br/>&nbsp;<font style=font-size:10pt;><b><font color=red>' . $row['CD_Name'] . '</font> 栏目列表生成完毕，暂停 <span id="mes">' . cd_stoptime . '</span> 秒后继续生成下个栏目...</b></font>';
			}
		} else {
			echo '<br/>&nbsp;<font style=font-size:10pt;><b>恭喜您，<font color=red>' . $row['CD_Name'] . "</font> 栏目列表生成完毕！</b></font><script language='javascript'>parent.form1.document.getElementById('StopTime').innerHTML='恭喜您，" . $row['CD_Name'] . " 栏目列表生成完毕！';alert('恭喜您，" . $row['CD_Name'] . " 栏目列表生成完毕！');</script>";
		}
	} else {
		echo "<script language=\"javascript\" type=\"text/javascript\"> \n";
		echo "var i = " . cd_stoptime . "; \n";
		echo "var intervalid; \n";
		echo "intervalid = setInterval(\"fun()\", 1000); \n";
		echo "function fun() { \n";
		echo "if (i == 0) { \n";
		echo "window.location.href = \"?ClassID=" . $ClassID . "&type=" . $type . "&classnow=" . $classnow . "&pages=" . ($page) . "\"; \n";
		echo "clearInterval(intervalid); \n";
		echo "} \n";
		echo "document.getElementById(\"mes\").innerHTML = i; \n";
		echo "i--; \n";
		echo "} \n";
		echo "</script> \n";
		echo '<br/>&nbsp;<font style=font-size:10pt;><b>为了节约系统资源，将暂停 ' . cd_stoptime . ' 秒后继续生成，请不要关闭或刷新...</b></font> 栏目列表生成完毕，暂停 <span id="mes">' . cd_stoptime . '</span> 秒后继续生成下个栏目...</b></font>';

	}
}
function SPage($mysql, $pagesize, $pages, $ClassID, $pagenum = 10) {
	global $db;
	$pagesok=$pagesize;
	if(!isset($pages) || $pages=='' || !is_numeric($pages) || $pages<=0) {
		$pages=1;
	}
	$sqlstr=$mysql;
	$res=$db->query($sqlstr);
	$nums=$db->num_rows($res);
	if($nums==0) {
		$nums=1;
	}
	$pagejs=ceil($nums/$pagesok);
	if($pages>$pagejs) {
		$pages=$pagejs;
	}
	$sql=$sqlstr . ' LIMIT ' . $pagesok*($pages-1) . ',' . $pagesok;
	$result=$db->query($sql);
	$str='';
	$first=LinkClassUrl('singerclass', $ClassID, 1, 1);
	if($pages==1) {
		$pageup=LinkClassUrl('singerclass', $ClassID, 1, 1);
	} else {
		$pageup=LinkClassUrl('singerclass', $ClassID, 1, ($pages-1));
	}
	if($pages==$pagejs) {
		$pagenext=LinkClassUrl('singerclass', $ClassID, 1, $pages);
	} else {
		$pagenext=LinkClassUrl('singerclass', $ClassID, 1, ($pages+1));
	}
	$last=LinkClassUrl('singerclass', $ClassID, 1, $pagejs);
	$pagelist="<select onchange=javascript:window.location=this.options[this.selectedIndex].value;>\r\n<option value='0'>跳转</option>\r\n";
	for($k=1; $k<=$pagejs; $k++) {
		if($k==$pages) {
			$pagelist.="<option value='" . LinkClassUrl('singerclass', $ClassID, 1, $k) . "' selected>第" . $k . "页</option>\r\n";
		} else {
			$pagelist.="<option value='" . LinkClassUrl('singerclass', $ClassID, 1, $k) . "'>第" . $k . "页</option>\r\n";
		}
	}
	$pagelist.='</select>';
	if($pagejs<=$pagenum) {
		for($i=1; $i<=$pagejs; $i++) {
			if($i==$pages) {
				$str.="<a href='" . LinkClassUrl('singerclass', $ClassID, 1, $i) . "' class='current'>" . $i . '</a>';
			} else {
				$str.="<a href='" . LinkClassUrl('singerclass', $ClassID, 1, $i) . "'>" . $i . '</a>';
			}
		}
	} else {
		if($pages>=$pagenum) {
			for($i=$pages-intval($pagenum/2); $i<=$pages+(intval($pagenum/2)); $i++) {
				if($i<=$pagejs) {
					if($i==$pages) {
						$str.="<a href='" . LinkClassUrl('singerclass', $ClassID, 1, $i) . "' class='current'>" . $i . '</a>';
					} else {
						$str.="<a href='" . LinkClassUrl('singerclass', $ClassID, 1, $i) . "'>" . $i . '</a>';
					}
				}
			}
		} else {
			for($i=1; $i<=$pagenum; $i++) {
				if($i==$pages) {
					$str.="<a href='" . LinkClassUrl('singerclass', $ClassID, 1, $i) . "' class='current'>" . $i . '</a>';
				} else {
					$str.="<a href='" . LinkClassUrl('singerclass', $ClassID, 1, $i) . "'>" . $i . '</a>';
				}
			}
		}
	}
	$arr=array(
		$str,
		$result,
		$sql,
		$nums,
		$pagelist,
		$pages,
		$pagejs,
		$first,
		$pageup,
		$pagenext,
		$last,
		$pagesok
	);
	@mysql_free_result($res);
	return $arr;
}
?>