<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
include_once("../../lib/cs_label.php");
include "../loginstate.php";
include "version.php";

$op=CS_Request("op");
$ac=CS_Request("ac");
$Filename=@file_get_contents('http://vip.chshcms.com/uputf-8.txt');
$version=@file_get_contents('http://vip.chshcms.com/versionutf-8.txt');
$nameall=explode("|",$Filename);
if($version==$CS_Version) exit('<font color=red>您当前已经是最新版本!</font>');
if($ac=="up"){
          $name=$nameall[$op];
          $FileName="http://vip.chshcms.com/".$name;
          //开始升级文件
           if(!check_remote_file_exists($FileName)){
                echo '<font color=red>远程文件'.$FileName.',不存在,请联系作者。</font>';
           }else{
               //修改后台目录
                $FileName2=CS_ReplaceStr($name,".xml",".php");
                $FileName2=CS_ReplaceStr($FileName2,"admin/",$CS_Admin);
                $FileContent=@file_get_contents('../../'.$FileName2);
                $NewsFileName='../../lib/backup/'.$FileName2;
                   //复制原始文件备份
                if(file_exists("../../".$FileName2)){
                  if(write_file($NewsFileName, $FileContent)){
                           echo '<font color=#ff6600>/'.$FileName2.',备份成功。</font>';
                  }else{
                           exit('<font color=red>/lib/目录没有写入权限，请给予写入权限!</font>');
                  }
                }
                $NewsFileContent=getupdata($name);
                if(write_file("../../".$FileName2, $NewsFileContent)){
                       echo '<font color=#0000ff>'.$FileName2.',升级更新成功。</font>';
                }else{
                       exit('<font color=red>'.$FileName2.'文件没有修改权限，请手动下载升级包升级!</font>');
                }
           }
exit();
}
/*
 函数：check_remote_file_exists
 功能：判断远程文件是否存在
 参数： $url -远程文件URL
 返回：存在返回true，不存在或者其他原因返回false
*/
function check_remote_file_exists($url) {
	$curl = curl_init($url); // 不取回数据
	curl_setopt($curl, CURLOPT_NOBODY, true);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // 发送请求
	$result = curl_exec($curl);
	$found = false; // 如果请求没有发送失败
	if ($result !== false) {

		/** 再检查http响应码是否为200 */
		$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
		if ($statusCode == 200) {
			$found = true;
		}
	}
	curl_close($curl);

	return $found;
}
?>
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>chshdjcms management</title>
<link href="../res/css/admin_style.css" rel="stylesheet" />
<script type="text/javascript" src="../res/js/ajax.js"></script>
<script language="javascript" src="../res/js/jquery.js"></script>
<script language="javascript"> 
function updatago(num,i){
        $("#gengx_"+i).html('&nbsp;&nbsp;<img src="../../public/images/load.gif">正在升级文件,请稍等...');
 	$.get("updata.php?ac=up&op="+i,function(R){
                if(R){
		      $("#gengx_"+i).html(R);
                      if(num==i+1){
		            $("#gengx_"+i).html(R+"<br><br><font color=#0000ff><b>恭喜您，所有文件升级成功，原来的文件备份在/lib/backup/目录里面,如果升级后出现问题，请还原该目录下的文件！</b></font>");
                      } else {
		            $("#gengx_"+i).html(R);
		            $("#dnum").html(i+2);
                            updatago(num,i+1);	
                      }
                }											  
	});
}
</script>
</head>
<body>
<div class="wrap">
<?php
$num=count($nameall);
        echo "<div class=\"h_a\">&nbsp;&nbsp;正在升级更新到<font color=red>".getXmlVersion()."</font>版本,共<font color=red>".$num."</font>个文件需要更新,已经更新<font id=\"dnum\" color=red>0</font>个文件</div><div class=\"table_full\">
	<table width=\"100%\" class=\"J_check_wrap\">
		<col width=\"100%\" />";

	for($j=0;$j<count($nameall);$j++){
 		echo "<tr><td id=\"gengx_".$j."\"></td></tr>";
	}
        echo "<script>updatago(".count($nameall).",0);</script></table>";
?>
