<?php
class pluginAction  extends BackendAction {
	public function _initialize() {
		parent::_initialize();		
	}
	public function make_dir($path) {
		if (!file_exists($path)) {
			$mk = @mkdir($path, 0777, true);
			@chmod($path, 0777, true);
		}
		return true;
	}
	public function curl_post($url,$post){
		$post = $post + array(
	    'from' => C('ftx_site_url'),
		'svs'=>get_client_ip()
	    );		
		$post = http_build_query($post);
		$ch = curl_init();      
	    $timeout = 300;       
	    curl_setopt($ch, CURLOPT_URL, $url);         
		curl_setopt($ch, CURLOPT_REFERER, $_SERVER["HTTP_REFERER"]);  
	    curl_setopt($ch, CURLOPT_POST, true);      
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);      
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);      
	    $source = curl_exec($ch);      
	    curl_close($ch);	
		return $source;
	}
	
	public function index() {
		$sqldir = 'app/Lib/Action/index/upsqlAction.class.php';	
		if(file_exists($sqldir)){
	    unlink($sqldir);
		}
		$p = I('p',1, 'intval');
		$url = "https://cms.douhao.com/?m=down&a=getplugin";		
		$post = array(
	    'p' => $p,	    
	    'mod'=>'list'
	    );	
	    $source = $this->curl_post($url,$post);		
		$arr = json_decode($source,true);
		if(!$arr['msg']){
		$list = $arr['list'];
		$count = $arr['count'];
		foreach($list as $key=>$val){ 
		   $list[$key] = $val;
		   $list[$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);			   
		   $list[$key]['install'] = $this->check($val['plugin_key']);
		}			
		$this->assign('list',$list);		
		$pager = new Page($count, 50);
		$page = $pager->show();
        $this->assign("page", $page);			
		}			
		$this->display();
	}
	protected function check($plugin_key){	        
		if(D('plugin_log')->where(array('plugin_key'=>$plugin_key))->find()){
		return 1;	
		}else{
		return 0;		
		}
	}
	public function down() {
		$sqldir = 'app/Lib/Action/index/upsqlAction.class.php';	
		if(file_exists($sqldir)){			
		unlink($sqldir);
		}
		// 这里省略了版本判断逻辑，根据自己需要去写							
		// 更新包信息	
		$plugin_key = I('plugin_key');		
		$title = I('title');
		$plugin_version = I('plugin_version');
		$re = I('re');
		$version = I('version');//插件适用最低版本号
		if($version>DOUHAO_CMS_BANBEN){
		$this->ajaxReturn(0,'该插件版本不适用于当前程序版本号！');		
		}		
		if($this->check($plugin_key) && !$re){
		  $this->ajaxReturn(0,'该插件你已经安装过，请勿重复安装！');	
		}
		
		//更新包文件名称
		$filename = $plugin_key.'.zip';				
		
		//检查和创建文件夹
		$dir = C('ftx_attach_path') . 'plugin/';				
		$this->make_dir($dir);
		// 下载更新包到本地并赋值文件路径变量
		$path = file_exists($dir.$filename)?$dir.$filename:$this->download_file($plugin_key,$dir,$filename);
		
		// 如果下载没成功就返回报错
		if (!file_exists($dir.$filename)) {
			$this->ajaxReturn(0,'文件下载失败');
		}
		// PHP解压的扩展类
		if(!class_exists("\ZipArchive")) 				
			    $this->ajaxReturn(0, '请安装ZipArchive扩展');
		// 实例化ZipArchive
		$zip = new \ZipArchive();
		
		//打开压缩包
		if ($zip->open($path) === true) {			
			$tpath	= Root_PATH;		
			try {
				// 解压文件到dir路径下
				$zip->extractTo($tpath);
				// 必须销毁变量，否则会报错
				unset($zip);
				// 删除更新包
				unlink($path);
			}
			catch (\Exception $e) {				
				$this->ajaxReturn(0, '没有[' . $tpath . ']目录的写入权限');
			}
			if($re){
			$item['plugin_version'] = $plugin_version;
			$item['install_time'] = time();
			$where['plugin_key'] = $plugin_key;	
			D('plugin_log')->where($where)->save($item);	
			}else{
			$item['title'] = $title;		
			$item['version'] = $version;	
			$item['plugin_key'] = $plugin_key;	
			$item['plugin_version'] = $plugin_version;
			$item['install_time'] = time();
			D('plugin_log')->add($item);	
			}
							
			if(file_exists($sqldir)) {
				$sqlurl = C('ftx_site_url')."?m=upsql&a=index";	
				$this->ajaxReturn(1,'插件安装完成，点此升级数据库：<a href="'.$sqlurl.'" target="_blank" class="red">去升级SQL</a>');	
			}
			
			$this->ajaxReturn(1,'插件安装完成');	
			
		} else {
			//压缩包打开失败，删除文件并且返回报错
			unlink($path);			
			$this->ajaxReturn(0, '文件解压失败，请重试！');
		}
	}
	
	/*
    * 文件下载方法
    * $url 文件下载地址
    * $dir 存储的文件夹
    * $filename 文件名字
    */
	protected function download_file($plugin_key, $dir, $filename = '') {
		if (empty($plugin_key)) {
			return false;
		}
		$dir = realpath($dir);
		$filename = $dir . "/".$filename;
		//开始捕捉			    
		$url = 'https://cms.douhao.com/?m=down&a=download_plugin';
		$post = array(
	    'mod' => 'down',
        'plugin_key' => $plugin_key,
	    );
		$data = $this->curl_post($url,$post);		
		if($data==1){
		$this->ajaxReturn(0, '该插件需付费后方可下载！');		
		}
		if($data==2){
		$this->ajaxReturn(0, '插件不存在！');		
		}
		if($data==3){
		$this->ajaxReturn(0, '错误，请勿非法请求！');		
		}
		if($data==4){
		$this->ajaxReturn(0, '非正式环境禁止安装插件！');		
		}
		$size = strlen($data);
		$fp2 = fopen($filename, "a");		
		fwrite($fp2, $data);
		fclose($fp2);
		return $filename;
	}
	
	
	public function topay() {		
			$id = I('id');	
			$url = "https://cms.douhao.com/topay/index";		
			$post = array(
			    'mod'=>'alipay',
			    'id' => $id,		
	    	    'type' => 'plugin',
			);				
	        $source = $this->curl_post($url,$post);				
			echo $source;		
	}
	public function weixinpay() {		
			$id = I('id');	
			$url = "https://cms.douhao.com/?m=wxpay&a=plugin";		
			$post = array(
			    'mod'=>'weixinpay',
			    'id' => $id,			    	    
			);				
	        $source = $this->curl_post($url,$post);		
			$item = json_decode($source,true);
			if($item['status']){				
			$this->ajaxReturn(1,'ok',$item['data']);		
			}else{
			$this->ajaxReturn(0,$item['msg']);		
			}
				
	}
	public function cx() {		
			$id = I('id');	
			$paymod = I('mod');	
			$url = "https://cms.douhao.com/?m=wxpay&a=chaxun";		
			$post = array(
			    'mod'=>'cx',
			    'id' => $id,		
	    	    'type' => 'plugin',
				'paymod' => $paymod,
			);				
	        $source = $this->curl_post($url,$post);		
			$item = json_decode($source,true);
			if($item['status']){					
			$this->ajaxReturn(1);		
			}else{
			$this->ajaxReturn(0,$item['msg']);		
			}
				
	}
	public function read() {		
			$id = I('id');	
			$url = "https://cms.douhao.com/?m=down&a=read";		
			$post = array(
			    'mod'=>'read',
			    'id' => $id,			    	    
			);				
	        $source = $this->curl_post($url,$post);		
			$item = json_decode($source,true);
			if($item['status']){				
			$this->ajaxReturn(1,'ok',$item['data']);		
			}else{
			$this->ajaxReturn(0,'',$item['msg']);		
			}
				
	}
	public function getorder() {		
			$id = I('id');	
			$url = "https://cms.douhao.com/?m=down&a=payinfo";		
			$post = array(
			    'mod'=>'getorder',
			    'id' => $id,			    	    
			);				
	        $source = $this->curl_post($url,$post);		
			$item = json_decode($source,true);
			if($item['status']){				
			$this->ajaxReturn(1,'ok',$item['data']);		
			}else{
			$this->ajaxReturn(0,'',$item['msg']);		
			}
				
	}
}