<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upgrade extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjAdmin');
		$this->load->model('CsdjSkins');
                $this->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
                $this->load->helper('file');
                $this->upurl='http://vip.chshcms.com/v35/';
	}

	public function index()
	{
            $upurl=$this->upurl.'up.php?v='.CS_Uptime;
 	    $arrs = $this->CsdjAdmin->HtmlUp($upurl);
 	    $arrs = json_decode($arrs);

            if($arrs->version==CS_Version){
 	       $arrs->cscms = "";
            }

            $data['show']=$arrs->cscms;
            $this->load->view('admin/upgrade',$data);
        }

	public function upall()
	{
            $this->CsdjAdmin->Admin_Qx('47');
            $upurl=$this->upurl.'up.php?v='.CS_Uptime;
 	    $arrs = $this->CsdjAdmin->HtmlUp($upurl);
 	    $arrs = json_decode($arrs);
            if($arrs->version==CS_Version){
 	        $this->CsdjAdmin->Admin_Url('<font color=red>您已经是最新版本！</font>','javascript:history.back();');
            }
            if(empty($arrs->cscms)){
 	        $this->CsdjAdmin->Admin_Url('<font color=red>当前没有文件需要更新！</font>','javascript:history.back();');
            }
            echo '<LINK href="'.base_url().'csdj/admin/images/admin.css" type="text/css" rel="stylesheet"><br>';
            foreach ($arrs->cscms as $row) {
                   $name =$row->name;
 	           $names = str_replace('.php','.xml',$name);
                   $upurl=$this->upurl.$names;
                   if($this->check_remote_file_exists($upurl)){
 	                 $strsss = $this->CsdjAdmin->HtmlUp($upurl);
                         if(!empty($strsss)){
                             if(write_file('./'.$name, $strsss)){
                                     echo '&nbsp;&nbsp;&nbsp;&nbsp;<font color=#0000ff>恭喜您，'.$name.'更新成功！</font><br>';
                             }else{
                                     echo '&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>出错了，'.$name.'没有写入权限，更新失败！</font><br>';
                             }
                         }
                   }else{
                         echo '&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>出错了，远程文件'.$name.'不存在,更新失败！</font><br>';
                   }
            }
               //更新版本
	       $configdb=read_file("./csdj/lib/Cs_Version.php");
	       $configdb=preg_replace('/"CS_Uptime","(.*?)"/','"CS_Uptime","'.$arrs->version.'"',$configdb);
               write_file('./csdj/lib/Cs_Version.php', $configdb);

               echo "&nbsp;&nbsp;&nbsp;&nbsp;<b>恭喜您，全部更新完成啦,版本已更新直:".$arrs->version."。。。。。</b><script>setTimeout('updatenext();',8000);function updatenext(){location.href='".site_url('admin/upgrade/')."';}</script>";
        }

        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)  {   
                 $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  
                        if ($statusCode == 200)  {  
                               $found = true;  
                        }  
            }  
            curl_close($curl);  
            return $found;  
        }  
}

