<?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();
$action = getForm("action", "get");$svrdate=getForm("svrdate", "get");$upversion=getForm("upversion","both");
$logFile="imgs/updatelog.xml";
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 $versionXml;
	viewHead("升级文件列表"."-" .$menuList[1][0]);
	initialLogFile();
?>
<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
$xml=utf82gb(getRemoteContent($versionXml));
$version = StrSlice($xml,'<version>','</version>');
$nodes=array();
$nodes=getrulevaluearr($xml,'updatefile');
if (!empty($nodes)){
    foreach($nodes as $v){
		$src = StrSlice($v,'<src>','</src>');
		$time = StrSlice($v,'<time>','</time>');
		$des = StrSlice($v,'<des>','</des>');
		echo "<tr align=\"center\"><td align=\"left\"><input type=\"checkbox\" value=\"".$src."----".$time."\" name=\"m_id[]\" /></td><td align=\"left\">".$src."</td><td align=\"left\">".$time."</td><td align=\"left\">".$des."</td><td align=\"left\">".isUpdateFile($src,$updateTime)."</td></tr>";
	}
?>
<tr ><td  colspan="5"><input type="hidden"  name="verRemote" value="<?=$version?>" />
全选<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	
}else{
	echo "<tr align=\"center\"><td colspan='5'>目前没有升级文件</td></tr>";
}
?>
</table>
</form>  
<?php
}

function updateselect() {
	$updatefiles=getForm("m_id","both");$verRemote=getForm("verRemote","both");
	if (!empty($updatefiles)) {
		$updatefilesArray=explode(",",dimplode($updatefiles));$updatefilecount=count($updatefilesArray);
		echo "<style type=\"text/css\">body{ font-size:12px;}</style>";
		for ($i = 0;$i < $updatefilecount;$i++) {
			$Array=explode('----',$updatefilesArray[$i]);
			updateFile($Array[0]);
			writeUpdateLog($updatefilesArray[$i]);
		}
		$xmlstr="<?xml version=\"1.0\" encoding=\"gbk\" ?><djcms>%s</djcms>";$tmp="";
		$tmp="<version>$verRemote</version>";
		$xmlstr=replaceStr($xmlstr,"%s",$tmp);
		fwrite(fopen("imgs/version.xml","wb"),$xmlstr);
	}else{
		alertMsg("请选择升级文件","");
	}
}

function initialLogFile(){
	global $logFile;
	$initialStr="<?xml version=\"1.0\" encoding=\"gbk\" ?>\n<djcms>\n</djcms>";
	if (!isExistFile($logFile)){ fwrite(fopen($logFile,"wb"),$initialStr);}
}

function StreamSave2File($content, $savefilename) {
	CreateTextFile($content,$savefilename);
	return true;
}

function writeUpdateLog($srcAndUpdateTime) {
	global $logFile;
	$xmlNodeStr="<update log=\"".$srcAndUpdateTime."\" />";
	$logFileStr=loadFile($logFile);
	$logFileStrArray=explode("<djcms>",$logFileStr);
	$logFileStr1=$logFileStrArray[0];
	$logFileStr2=$logFileStrArray[1];
	$logFileStr=$logFileStr1."<djcms>\n".$xmlNodeStr.$logFileStr2;
	fwrite(fopen($logFile,"wb"),$logFileStr);
}

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

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

function isNewVersion() {
	global $versionXml;
	$xml = file_get_contents("imgs/version.xml");
    $verLocal = StrSlice($xml,'<version>','</version>');
	$xml=utf82gb(getRemoteContent($versionXml));
	$verRemote = StrSlice($xml,'<version>','</version>');
	$updateInfo = StrSlice($xml,'<info>','</info>');
	$isNew=($verLocal!=$verRemote);
	if ($isNew) echo $updateInfo; else echo 'False';
}

function updateFile($relativeFileUrl) {
	global $updateUrl;
	$fileurl = replaceStr($relativeFileUrl,"admin/",selfManageDir());
	$fileurl = replaceStr($fileurl,"list/",channelDirName1."/");
	$fileurl = replaceStr($fileurl,"detail/",contentDirName1."/");
	$fileurl = replaceStr($fileurl,"video/",playDirName1."/");
	$fileurl = replaceStr($fileurl,"news/",newsDirName1."/");
	$fileurl = replaceStr($fileurl,"articlelist/",partDirName1."/");
	$fileurl = replaceStr($fileurl,"article/",articleDirName1."/");
	$fileurl = replaceStr($fileurl,"topiclist/",topicPageDirName."/");
	$fileurl = replaceStr($fileurl,"topic/",topicDirName."/");
	$localFile = "../".replaceStr($fileurl, ".txt", ".php");
	$remoteFile = $updateUrl."/".replaceStr($relativeFileUrl, ".php", ".txt");
	if (StreamSave2File(getRemoteContent($remoteFile), $localFile)) echo $localFile."更新成功<br>";
}
?>
