<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends adminbase {
    private $mod = NULL;
    
	public function action_run() {
               
	       TPL::display($this->cptpl.'frame.tpl');
	}
    
    
    public function action_top(){
        $model1 = parent::model('language', 'am');
        $lang=parent::$lang;
        if($lang==''){
            $language='简体中文';
        }else{
            $lang_info=$model1->getData1($lang);
            $language=$lang_info['name'];
        }
        $var_array = array(
            'adminname'=>parent::$wrap_admin['adminname'],
            'language'=>$language,
        );
       
        TPL::assign($var_array);
        TPL::display($this->cptpl.'frm_top.tpl');
    }
    
    
    public function action_drag(){
        TPL::display($this->cptpl.'frm_drag.tpl');
    }
    
    
    public function action_left(){
        $this->mod = XRequest::getArgs('mod');
        
        $model = parent::model('category', 'am');
        $data = $model->getForAdmMenu();
        
        unset($model);
        $var_array = array(
            'mod'=>$this->mod,
            'category'=>$data,
        );

        TPL::assign($var_array);
        TPL::display($this->cptpl.'frm_menu.tpl');
    }
    
    
    public function action_main(){
        $model = parent::model('count', 'am');
        $count = array(
            'article'=>$model->getCount('article'),
            'product'=>$model->getCount('product'),
            'photo'=>$model->getCount('photo'),
            'download'=>$model->getCount('download'),
            'hr'=>$model->getCount('hr'),
            'about'=>$model->getCount('about'),
        );
        unset($model);
        
        $model = parent::model('index', 'am');
        $data = $model->doGetSysData();
        unset($model);
        
        $var_array = array(
            'count'=>$count,
            'sysdata'=>$data,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'frm_main.tpl');
    }
    
    
    public function action_footer(){
        $model = parent::model('index', 'am');
        $data = $model->doGetSysData();
        unset($model);
        $var_array = array(
            'sysdata'=>$data,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'frm_footer.tpl');
    }

    
    public function action_mynav() {
        
        $model = parent::model('mynav', 'am');
        $data = $model->getAllList();
        unset($model);
        
        $var_array = array(
            'mynav'=>$data,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'frm_mynav.tpl');
    }
         public function action_ajax_get_action()
     {     
     	$advContrl = get_class_methods('control');
   
        
     	$html = '';
     	foreach ($advContrl as $val){
            if(strpos($val,'action') !== false){
                   
               $html .= "<option value='".$val."'>".$val."</option>";
                    
            } else {
               
            }
     		
     	}
     	exit($html);
     }

     public function action_edition(){
         $model = parent::model('index', 'am');
         $data = $model->doGetSysData();
         unset($model);

         $var_array = array(
             'sysdata'=>$data,
         );
         TPL::assign($var_array);
         TPL::display($this->cptpl.'frm_edition.tpl');
     }
     public function action_check_edition(){
         $model  = parent::model('member', 'im');
         $system_edition = $model->getsetup('system_edition');
         $value = $system_edition['setvalue'];
         $url     = "https://www.8cms.cn/api.php?a=getedition&banben=$value";
         $res      = $this->curl_get($url);
         echo $res;
         die;
        // $data1     = json_decode($res, TRUE);
       //  print_r($data1);
     }
    private function curl_get($url)
    {
        //初始化
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        return $tmpInfo;    //返回json对象
    }

    public function action_update_edition(){
        $download_url=$_REQUEST['download_url'];
        $version=$_REQUEST['version'];
        $download_url='http://'.$download_url;
        $path='data/attachment/edition/';
        $lujing=$this->downFile($download_url,$path);
        if($lujing){
            $save_path = BASE_ROOT . 'source/control/';
            parent::loadUtil('file');
            if (true === XFile::delDir($save_path)) {
                $res=$this->unzip($lujing,'./source/', true, false);
                if($res){
                    $model = parent::model('setting', 'am');
                    $model->doUpdatesetup('system_edition',$version);
                    $data['slay']='1';
                    $data['msg']='更新成功';
                    $data['path']=$lujing;
                    echo json_encode($data);die;
                }
            }
        }

    }
    function downFile($url,$path){
        error_reporting(E_ALL ^ E_WARNING);
        $arr=parse_url($url);
        $fileName=basename($arr['path']);
        $url = "http://www.8cms.cn/data/attachment/edition/$fileName"; // fixed by li9hu
        $file=file_get_contents($url);
        if($file == false){
                    $data['slay']='0';
                    $data['msg']='操作失败，请联系客服处理';
                    echo json_encode($data);die;
        }
        $res=file_put_contents($path.$fileName,$file);
        if($res == false){
                    $data['slay']='0';
                    $data['msg']='操作失败，请联系客服处理';
                    echo json_encode($data);die;
        }  else {
              return $path.$fileName;
        }
	}
    public function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true){
        header("content-type:text/html;charset=utf8");
        if ($zip = zip_open($src_file)){
            if ($zip){
                $splitter = ($create_zip_name_dir === true) ? "." : "/";
                if($dest_dir === false){
                    $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
                }
                // 如果不存在 创建目标解压目录
                $this->create_dirs($dest_dir);
                // 对每个文件进行解压
                while ($zip_entry = zip_read($zip)){
                    // 文件不在根目录
                    $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
                    if ($pos_last_slash !== false){
                        // 创建目录 在末尾带 /
                        $this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
                    }
                    // 打开包
                    if (zip_entry_open($zip,$zip_entry,"r")){
                        // 文件名保存在磁盘上
                        $file_name = $dest_dir.zip_entry_name($zip_entry);
                        // 检查文件是否需要重写
                        if ($overwrite === true || $overwrite === false && !is_file($file_name)){
                            // 读取压缩文件的内容
                            $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                            @file_put_contents($file_name, $fstream);
                            // 设置权限
                            chmod($file_name, 0777);
                        }
                        // 关闭入口
                        zip_entry_close($zip_entry);
                    }
                }
                // 关闭压缩包
                zip_close($zip);
            }
        }else{
            return false;
        }
        return true;
    }
    /**
     * 创建目录
     */
    public function create_dirs($path){
        header("content-type:text/html;charset=utf8");
        if (!is_dir($path)){
            $directory_path = "";
            $directories = explode("/",$path);
            array_pop($directories);
            foreach($directories as $directory){
                $directory_path .= $directory."/";
                if (!is_dir($directory_path)){
                    mkdir($directory_path);
                    chmod($directory_path, 0777);
                }
            }
        }
    }


    
}
?>
