<?php
require_once(dirname(__FILE__)."/admin_inc.php");
@set_time_limit(0);
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
checkPower();
viewHead("升级文件列表"."-" .$menuList[1][0]);
switch ($action) {
	case "single" : update_single();break;
	case "isNew" : isNewVersion();break;
	case "updateselect":updateselect();alertMsg("","admin_update.php");break;
	default : main();break;
}

function main(){
	global $verLockFile,$updateHost;
	$verLocal = trim(file_get_contents($verLockFile));
	$oktimear = array(0,0,0,0,0);
	$oktimear = explode('.',$verLocal);
	$oktime = $oktime = $oktimear[2].'-'.$oktimear[3].'-'.$oktimear[4];
?>
<div class="container" id="cpcontainer">
<form method="post"  name="editform">
<table class="tb">
<tr class="thead"><th colspan="7">升级文件列表</th></tr>	
<tr align="center">
<td align="left">选择文件</td>
<td align="left">文件路径</td>
<td align="left">文件发布时间</td>
<td align="left">功能说明</td>
<td align="left">是否更新</td>	
</tr>
<?php
$filelist = trim(getRemoteContent($updateHost.'/verinfo.txt'));
$filelist = trim(preg_replace( '/[\r\n]{1,}/', "\n", $filelist));
$fileArr = array();
$f = 0;
if (!empty($filelist)) {
	$verRemote = getrulevalue($filelist,"version");
	$filelist = getrulevalue($filelist,"updatefile");
	$filelists = explode("\n",$filelist);
	foreach ($filelists as $k => $filelist) {
		$filelist = trim($filelist);
		if (empty($filelist)) continue;
		$fs = explode(',',$filelist);
		$fileArr[$k]['uptime'] = trim($fs[0]);
		$fileArr[$k]['upfile'] = trim($fs[1]);
		$fileArr[$k]['updes'] = trim($fs[2]);
		$f++;
	}
}
if ($f==0) {
	echo "<tr align=\"center\"><td colspan='5'>目前没有升级文件</td></tr>";
} else {
	foreach ($fileArr as $v) {
		echo "<tr align=\"center\"><td align=\"left\"><input type=\"checkbox\" class=\"checkbox\" value=\"".$v['upfile']."----".$v['uptime']."\" name=\"m_id[]\" /></td><td align=\"left\">".$v['upfile']."</td><td align=\"left\">".$v['uptime']."</td><td align=\"left\">".$v['updes']."</td><td align=\"left\">".isUpdateFile($v['upfile'],$v['uptime'])."</td></tr>\n";
	}
?>
<tr ><td  colspan="5"><input type="hidden"  name="verRemote" value="<?php echo $verRemote;?>" />
全选<input type="checkbox" name="chkall" id="chkall" class="checkbox" onclick="checkAll(this.checked,'input','m_id[]')" />反选<input type="checkbox" name="chkothers" id="chkothers" class="checkbox" onclick="checkOthers('input','m_id[]')" /><input type="submit" value="批量升级文件" onclick="if(confirm('确定要升级选中的文件吗')){editform.action='?action=updateselect';}else{return false}" class="btn"  />
<br /><br />
<font color="red">注意：在升级文件发布时间以后安装程序的用户无需升级</font>
</td></tr>
<?php
}
?>
</table>
</form>  
<?php
}

function updateselect() {
	global $verLockFile,$m_id,$verRemote;
	if (!empty($m_id)) {
	    echo "<style type=\"text/css\">body{ font-size:12px;}</style>";
	    foreach ($m_id as $updatefiles) {
		    $updatefilesArray=explode('----',$updatefiles);
		    updateFile($updatefilesArray[0]);
		    writeUpdateLog($updatefiles);
	    }
	    fwrite(fopen($verLockFile,"w"),$verRemote);
	} else {
		alertMsg("请选择升级文件","");
	}
}

function StreamSave2File($content, $savefilename) {
	fwrite(fopen($savefilename,"w"),$content);
	return true;
}

function writeUpdateLog($srcAndUpdateTime) {
	if(!isupdate($srcAndUpdateTime)) {
		global $updatelogfile;
		$str="<updatelog>".$srcAndUpdateTime."</updatelog>";
		fwrite(fopen($updatelogfile,"a"),$str."\r\n");
	}
}

function isUpdateFile($src, $updateTime) {
	global $updatelogfile;
	$logFileStr=trim(file_get_contents($updatelogfile));
	if (isExistStr($logFileStr,$src."----".$updateTime)) return "已更新"; else return "<font color=red >未更新</font>";
}

function update_single() {	
	$fileurl = getForm("fileurl","both");
	updateFile($fileurl);
}

function isupdate($srcAndUpdateTime) {
	global $updatelogfile;
	$logFileStr=trim(file_get_contents($updatelogfile));
	if(strpos($logFileStr,$srcAndUpdateTime)>0) return true; else return false;
}

function isNewVersion() {
	global $verLockFile,$updateHost;
	AjaxHead();
	$verLocal=trim(file_get_contents($verLockFile));
	$verlist=trim(get($updateHost.'/verinfo.txt'));
	$verRemote=getrulevalue($verlist,"version");
	$updateInfo=getrulevalue($verlist,"info");
	$isNew=($verLocal<>$verRemote);
	if ($isNew) echo $updateInfo; else echo 'False';
}

function updateFile($relativeFileUrl) {
	global $updateHost;
	$fileurl = replaceStr($relativeFileUrl, "admin/", selfManageDir());
	$fileurl = replaceStr($fileurl, "channel/", channelDirName1."/");
	$fileurl = replaceStr($fileurl, "content/", contentDirName1."/");
	$fileurl = replaceStr($fileurl, "play/", playDirName1."/");
	$fileurl = replaceStr($fileurl, "news/", newsDirName."/");
	$fileurl = replaceStr($fileurl, "zt/", topicDirName4."/");
	$localFile ="../".replaceStr($fileurl, ".txt", ".php");
	$remoteFile =$updateHost.replaceStr($relativeFileUrl, ".php", ".txt");
	if (StreamSave2File(get($remoteFile), $localFile)) echo $localFile."更新成功<br>";
}

function getrulevalue($content, $str) {
	if (!empty($content) && !empty($str)) {
		$labelRule = buildregx("<".$str.">(.*?)"."</".$str.">","is");
		preg_match_all($labelRule,$content,$ar);
		return empty($ar[1][0]) ? "" : $ar[1][0];
	}
}
?>
