<?php 
class cogClassAction extends Action
{
	public function initAction()
	{
		$this->keyss = $this->option->getval('systemnum');
		if(isempt($this->keyss)){
			$rnd  	= md5(str_shuffle('abcedfghijk').rand(1000,9999));
			$this->option->setval('systemnum', $rnd);
			$this->keyss = $rnd;
		}
	}
	
	public function defaultAction()
	{
		$type = $this->get('type');
		$xinhukey = getconfig('xinhukey');
		if($xinhukey)$xinhukey = substr($xinhukey,0,5).'*****'.substr($xinhukey,-5);
		
		$this->assign('type', $type);
		$this->assign('xinhukey', $xinhukey);
	}
	
	public function savecheckAction()
	{
		if(getconfig('systype')=='demo')return '演示不要改';
		$type = $this->post('type');
		$msg  = 'ok';
		if(isempt($type))$msg = $this->saveconfig('title,xinhukey,officebj_fan');
		if($type=='server')$msg = $this->saveconfig('server_host,server_push,server_recid');
		if($type=='qywx')$msg = $this->saveconfig('qywx_corpid,qywx_secret,qywx_deptid');
		
		return $msg;
	}
	
	private function saveconfig($cont)
	{
		$path = ''.P.'/'.P.'Config.php';
		
		$neir = file_get_contents($path);
		$zdar = explode(',', $cont);
		$neira= explode("\n", $neir);
		
		$strs = '';
		$szida= array();
		foreach($neira as $line){
			if($line==');')break;
			$bo = false;
			foreach($zdar as $fid){
				if(contain($line,"'".$fid."'")){
					$val = $this->post($fid);
					if(contain($val,'*****')){
						$strs.="".$line."\n";
					}else{
						$strs.="	'".$fid."'	=> '".$val."',\n";
					}
					$bo = true;
					$szida[]=$fid;
				}
			}
			if(!$bo)$strs.="".$line."\n";
		}
		
		foreach($zdar as $fid){
			if(!in_array($fid, $szida)){
				$val = $this->post($fid);
				$strs.="	'".$fid."'	=> '".$val."',\n";
			}
		}
		
		$strs.=');';
		$bo = @file_put_contents($path,$strs);
		if(!$bo)return '无权限写入：'.$path.'';
		return 'ok';
	}
	
	
	
	public function upgdataAction()
	{
		$xinhu	= c('xinhu');
		$db 	= m('chargems');
		$wet	= $xinhu->getwebsite();
		$barr	= $xinhu->getdata('mode',array('sysnum'=>$this->keyss));
		if($barr['code']!=200)return $barr['msg'];
		
		$rows  	= array();
		$wodekey= $this->jm->getRandkey();
		foreach($barr['data'] as $k=>$rs){
			$id = $rs['id'];
			$state  = 0;
			$key	= '';
			$ors 	= $db->getone("`type`=0 and `mid`='$id'");
			if($ors){
				$state = 1;
				if($rs['updatedt']>$ors['updatedt'])$state=2;
				$key= $ors['key'];
			}
			$view = '<a href="'.$wet.'view_'.$rs['num'].'.html" target="_blank" class="a">介绍</a>';
			if($state==0 && $rs['price']>0)$view.=',<a href="'.$wet.'view_'.$rs['num'].'.html" target="_blank" style="color:red">去购买</a>';
			$rows[] = array(
				'id' 	=> $id,
				'name' 	=> $rs['name'],
				'price' => $rs['price'],
				'isaz'  => $rs['isaz'],
				'explain' 	=> $rs['explain'],
				'updatedt' 	=> $rs['updatedt'],
				'key'		=> $key,
				'view' 	=> $view,
				'opt'	=> $state
			);
			if($k==0)$wodekey = $rs['wodekey'];
		}
		$arr['rows'] 	= $rows;
		$arr['wodekey'] = $wodekey;
		
		return array(
			'rows' => $rows,
			'success'=>true,
		);
	}
	
	
	
	public function upgAction()
	{
		$sid = (int)$this->jm->base64decode($this->get('sid'));
		if($sid=='0')return '无效页面';
		
		$this->assign('sid', $sid);
	}
	//获取更新文件
	public function upgallfileAction()
	{
		$id = (int)$this->get('sid','0');
		$rows= array();
		$key 	= $this->getaxkey($id);
		$barr	= c('xinhu')->getdata('getinstallfile',array('id'=>$id,'sysnum'=>$this->keyss,'key'=>$key));
		if($barr['code']!=200)return $barr['msg'];
		
		$huira  = $this->gethuiarr($id);
		
		$data 	= $barr['data'];
		$mrs 	= $data['rs'];
		$rows 	= $data['rows'];
		
		$uparr  = array();
		foreach($rows as $k=>$rs){
			$file 	= $rs['filepath'];
			$md5file= $rs['md5file'];
			
			if(file_exists($file)){
				//$_size = filesize($file);
				//if($_size == $rs['filesize'])continue;
				$mymd5 = md5_file($file);
				if($mymd5==$md5file)continue;
			}
			
			$ishui = 0;
			if(isset($huira[$rs['id']])){
				$ishui = 1;
			}
			$rs['ishui'] = $ishui;
			
			if($ishui==0){
				$ulx = 0;
				if(file_exists($file))$ulx = 1;
				$rs['ulx'] = $ulx;
			}else{
				
			}
			$uparr[] = $rs;
		}
		
		return array(
			'rows' => $uparr,
			'success'=>true,
		);
	}
	
	private function gethuiarr($id)
	{
		$hurs  = m('chargems')->getall("`modeid`='$id' and `type`=2",'mid');
		$huira = array();
		foreach($hurs as $k2=>$rs2)$huira[$rs2['mid']] = 1;
		return $huira;
	}
	public function upghuuesAction()
	{
		$id = (int)$this->get('sid','0');
		$ids  = $this->post('ids');

		$lx = (int)$this->post('lx');
		$db  = m('chargems');
		$db->delete("`modeid`='$id' and `mid` in($ids) and `type`=2");
		$sad = explode(',', $ids);
		if($lx==0)foreach($sad as $sids){
			$db->insert(array(
				'optdt' 	=> $this->now,
				'type' 		=> 2,
				'mid' 		=> $sids,
				'modeid' 	=> $id
			));
		}
		
		return 'ok';
	}
	
	//单更新文件
	public function upgfileAction()
	{
		$id = (int)$this->get('id','0');
		$fileid = (int)$this->get('fileid','0');
		$wc 	= (int)$this->get('wc','0');
		$key 	= $this->getaxkey($id);
		
		$barr	= c('xinhu')->getdata('getinstallfileid',array('fid'=>$fileid,'sysnum'=>$this->keyss,'key'=>$key));
		if($barr['code'] != 200)return $barr['msg'];
		
		$data 	= $barr['data'];
		$type 	= $data['type'];
		$filepath = $data['filepath'];
		$fcont 	= $this->jm->base64decode($data['content']);
				
		if($type==9){
			$this->rock->createdir($filepath);
			$this->beifenfile($filepath);
			@$bo = file_put_contents($filepath, $fcont);
			if(!$bo)return '无法写入：'.$filepath.'';
		}
		
		//数据库更新sql
		if($type==10){
			$this->rock->createdir($filepath);
			@file_put_contents($filepath, $fcont);
			$sqarr = explode('ROCKSPLIT', $fcont);
			foreach($sqarr as $_sql1)if($_sql1)$this->db->query($_sql1,false);
		}
		
		if($wc==1){
			$this->upsueecc($id);
		}
		return 'ok';
	}
	private function upsueecc($id)
	{
		$where  = "`type`=0 and `mid`='$id'";
		$db 	= m('chargems');
		if($db->rows($where)==0)$where='';
		$db->record(array(
			'type' 		=> 0,
			'mid'  		=> $id,
			'modeid'  	=> $id,
			'updatedt'  => $this->now,
			'optdt' 	=> $this->now,
		),$where);
	}
	//备份原来的文件
	private function beifenfile($path)
	{
		if(!file_exists($path))return;
		$wz 	= strripos($path, '/');
		$dir 	= '';
		if($wz===false){
			$file = $path;
		}else{
			$dir  = substr($path, 0, $wz);
			$file = substr($path, $wz+1);
		}
		$dir	= str_replace('/','-', $dir);
		$nfile 	= ''.$dir.''.date('YmdHis').'@'.$file.'';
		$topath = ''.UPDIR.'/logs/'.date('Y-m').'/'.$nfile.'';
		$this->rock->createdir($topath);
		@copy($path, $topath);
	}
	
	public function savekeyAction()
	{
		$id 	= (int)$this->get('id','0');
		$key 	= $this->get('key');
		
		$where  = "`type`=0 and `mid`='$id'";
		$db 	= m('chargems');
		if($db->rows($where)==0)$where='';
		
		$db->record(array(
			'type' 		=> 0,
			'mid'  		=> $id,
			'modeid'  	=> $id,
			'key'  	=> $this->jm->encrypt($key),
			'optdt' 	=> $this->now,
		),$where);
		
		return 'ok';
	}
	private function getaxkey($id)
	{
		$moukey = m('chargems')->getmou('`key`', "`type`=0 and `mid`='$id'");
		if(isempt($moukey)){
			$moukey='';
		}else{
			$moukey=$this->jm->uncrypt($moukey);
		}
		return $moukey;
	}
}