<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace user\controller;
use \temmoku\db;
use temmoku\user;
use \temmoku\lib\cookie;
use admin\public_class\renzheng as rz;
class renzheng extends user{
	
	public function index(){
		$array=[
			[
				'dir'=>'mob',
				'name'=>'手机验证',
			],
			[
				'dir'=>'email',
				'name'=>'Email验证'
			],
			[
				'dir'=>'idcard',
				'name'=>'身份主体验证'
			]
		];
		hook_listen('user_renzheng_begin',$array);
		if($_GET['type']=='idcard' && $_POST['step']=='post'){
			$renzheng=unserialize(C('renzheng'));
			if(C('MYDB.id_rz_state')==1){
				$this->err('已经通过审核，无法编辑');
			}
			if($renzheng['main']['is_yzm']){
				$yzm=\temmoku\lib\yzm::get_yzm_code($_POST['yzm']);
				if($yzm['code']==='9'){
					$this->err('验证码错误');
				}
			}
			$type=intval($_POST['_type']);
			if($type==1){
				$this->img_move($_POST['idcard_zheng'],'idcard_zheng');
				$this->img_move($_POST['idcard_fan'],'idcard_fan');
				$this->img_move($_POST['idcard_shouchi'],'idcard_shouchi');
				$array=[
					'real_name'=>$_POST['truename'],
					'id_number'=>$_POST['id_number'],
					'id_type'=>$type,
					'id_rz_state'=>'-1'
				];
			}else{
				$this->img_move($_POST['company_business'],'company_business');
				$array=[
					'real_name'=>$_POST['company_truename'],
					'id_number'=>$_POST['company_code'],
					'id_type'=>$type,
					'id_rz_state'=>'-1'
				];
			}
			db::update(jab.'user')->cols($array)->where('uid='.C('MYDB.uid'))->query();
			db::insert(jab.'renzheng_log')->cols(['action_uid'=>C('MYDB.uid'),'uid'=>C('MYDB.uid'),'type'=>'-1','msg'=>'提交主体认证审核','posttime'=>NOWTIME])->query();
			$this->success('提交成功,等待客服进行审核');
		}
		
		if($_GET['type']=='idcard'){
			$img=[];
			$path=Temmoku_PATH.C('UPFILES_CATALOG').'/';
			$uid=C('mydb.uid');
			$ceil = ceil(c('mydb.uid')/1000);
			$id_array=['idcard_zheng','idcard_fan','idcard_shouchi','company_business'];
			foreach($id_array as $value){
				$file='renzheng/'.$ceil."/".$uid."/".md5(C('MD5')."_".$uid."_".$value).".png";
				if(is_file($path.$file)){
					$img[$value]=$file;
				}
			}
			$this->assign('img',$img);
			if(C('MYDB.id_rz_state')=='-2'){
				$log=db::select('*')->from(jab.'renzheng_log')->where('uid='.$uid)->orderByDESC(['id'])->row();
				$this->assign('log',$log);
			}
		}
		
		hook_listen('user_renzheng_end',$array);
		$this->assign('list',$array);
		if(DEVICE != 'pc'){
			$this->assign('webname','身份认证');
		}
		$type=$_GET['type'] ? $_GET['type'] : $array['0']['dir'];
		$this->display(tpl('renzheng/'.$type));
	}
	
	//进行认证操作
	public function approve(){
		hook_listen('user_renzheng_approve_begin');
		//加载一下登录插件配置
		$login_conf_path=APP_PATH."plugin/login/conf";
		if($_POST['type']=='email'){
			if(!$_POST['code']){
				$this->err('Email认证码不可为空');
			}
			$code=Cookie::get('REG_CODE');
			if(!$code){
				$this->err('不存在Email认证码原始值');
			}
			$code=decode($code);
			list($email,$code,$onlineip,$codetime)=explode(' ',$code);
			if($codetime < time()-3600){
				$this->err('认证码已经超时');
			}
			if($email != $_POST['email'] || $code!=$_POST['code']){
				$this->err('Email认证码错误，请重新输入');
			}
			Cookie::del('REG_CODE');
			$rz=(new rz)->give(C('MYDB.uid'),'email');
			if($rz['code']){
				$this->err($rz['text']);
			}
			db::update(jab.'user')->cols(['email'=>$_POST['email'],'email_rz_state'=>1])->where('uid='.C('MYDB.uid'))->query();
			$this->success('Email认证成功');
		}elseif($_POST['type']=='mob'){
			if(!$_POST['code']){
				$this->err('手机认证码不可为空');
			}
			$code=Cookie::get('mobile_code');
			$decode=decode($code);
			list($mobile,$code,$time)=explode(" ",$decode);
			
			if($mobile != $_POST['mobile'] || $code != $_POST['code']){
				$this->err('手机认证失败');
			}
			
			$mobile_useful_time=C('login|login.mobile_useful_time') ? C('login|login.mobile_useful_time') : 5;
			$mobile_useful_time=$mobile_useful_time*60;
			$time=$mobile_useful_time+$time;
			if($time < NOWTIME){
				$this->err('手机认证码已经超时');
			}
			Cookie::del('mobile_code');
			$rz=(new rz)->give(C('MYDB.uid'),'mob');
			if($rz['code']){
				$this->err($rz['text']);
			}
			$array=['mobile'=>$_POST['mobile'],'mobile_rz_state'=>1];
			db::update(jab.'user')->cols($array)->where('uid='.C('MYDB.uid'))->query();
			$this->success('手机号码认证通过');
		}
		hook_listen('user_renzheng_approve_end');
	}
	
	
	//发信接口
	public function send(){
		hook_listen('user_renzheng_send_begin');
		//加载一下登录插件配置
		$login_conf_path=APP_PATH."plugin/login/conf";
		load_conf($login_conf_path);
		$renzheng=unserialize(C('renzheng'));
		if($_POST['type']=='email' && $_POST['email']){
			if($renzheng['email']['is_yzm']){
				$yzm=\temmoku\lib\yzm::get_yzm_code($_POST['yzm']);
				if($yzm['code']==='9'){
					$array=['code'=>'9','text'=>'验证码错误'];
					echo json_encode($array);
					exit;
				}
			}
			if(C('login|login.reg_is_email_repeat')){
				//查询一下是否存在此email;
				$test=db::select('uid')->from(jab.'user')->where("email Like '$_POST[email]' ")->row();
				if($test && $test['uid'] == C('MYDB.uid')){
					echo json_encode(['code'=>'31','text'=>'系统已经存在此Email地址，请更换一个']);
					exit;
				}
			}
			if(is_email($_POST['email'])==false){
				echo json_encode(['code'=>'32','text'=>'Email地址不正确']);
				exit;
			}
			$title="您在".C('WEBNAME')."认证时所需的Email认证码";
			$rand=rand(100000,999999);
			$code=encode($_POST['email'].' '.$rand.' '.onlineip." ".NOWTIME);
			Cookie::set('REG_CODE',$code,'0.04');
			$content="Email认证码为:".$rand.".有效期为1小时";
			$SMS=(new \temmoku\lib\sms)->smtp($_POST['email'],$title,$content);
			if($SMS != true){
				$text=ob_get_clean();
				echo json_encode(['code'=>'33','text'=>'发信失败，请联系客服。错误信息：'.$text]);
				exit;
			}
			echo json_encode(['code'=>'0','text'=>'ok']);
		}elseif($_POST['type']=='mob' && $_POST['mobile']){
			if(!is_phone($_POST['mobile'])){
				$array=['code'=>'102','text'=>'手机号码不正确'];
				echo json_encode($array);
				exit;
			}
			if(!is_plugin('sms')){
				$this->err('没有安装短信插件。或者短信插件目录不是为SMS');
			}
			if($renzheng['mob']['is_yzm']){
				$yzm=\temmoku\lib\yzm::get_yzm_code($_POST['yzm']);
				if($yzm['code']==='9'){
					$array=['code'=>'9','text'=>'验证码错误'];
					echo json_encode($array);
					exit;
				}
			}
			//判断如果禁止重复
			if(C('login|login.reg_is_mobile_repeat')){
				$test=db::select('*')->from(jab.'user')->where("mobile Like '$_POST[mobile]'")->row();
				if($test){
					echo json_encode(['code'=>'1','text'=>'手机号码已经在系统中存在，请更换一个']);
					exit;
				}
			}
			
			$timer=db::select('*')->from(jab.'plugin_sms_put')->where("mobile='$_POST[mobile]' AND code='100'")->orderByDESC(['id'])->row();
			
			$_put_timer=C('login|LOGIN.mobile_put_time') ? C('login|LOGIN.mobile_put_time') : 60;
			$put_timer=intval($timer['posttime'])+intval($_put_timer);
			if($timer &&  $put_timer > NOWTIME){
				echo json_encode(['code'=>'103','text'=>'该手机号在'.$_put_timer.'秒内不允许重复发送']);
				exit;
			}
			
			$mobile_yzm_num=!C('login|login.mobile_yzm_num') || C('login|login.mobile_yzm_num') >'6' ? '4' :C('login|login.mobile_yzm_num');
			$yzm='';
			for($i=0;$i<$mobile_yzm_num;$i++){
				$yzm .=rand(0,9);
			}

			$short=\temmoku\lib\sms::short($_POST['mobile'],$yzm,'100');
			if($short['code']=='0'){
				/*
				 * 这个地方不适用cook
				 * sql及cookie双层保证，防止短信轰炸
				 */
				$array=['mobile'=>$_POST['mobile'],'text'=>$yzm,'posttime'=>NOWTIME,'code'=>'100'];
				db::insert(jab.'plugin_sms_put')->cols($array)->query();
				$encode=encode($_POST['mobile'].' '.$yzm.' '.NOWTIME);
				Cookie::set('mobile_code',$encode);
			}
			$data['code']=$short['code'];
			$data['text']=$short['text'];
			echo json_encode($data);
		}
		hook_listen('user_renzheng_send_end');
	}
	
	private function img_move($img,$type=''){
		if($img){
			$oldfile=realpath(Temmoku_PATH.C('UPFILES_CATALOG').'/'.$img);
			$is_upfiles=stripos($oldfile,Temmoku_PATH.C('UPFILES_CATALOG'));
			if($is_upfiles !== 0){
				return;
			}
			if(is_file($oldfile)){//只能是上传的本地文件,远程都不行
				if(strpos($img, "tmp") !== 0 && !stristr($oldfile, C('UPFILES_CATALOG')."/tmp") ){
					return $img;
				}
				$uid=C('mydb.uid');
				$ceil = ceil(c('mydb.uid')/1000);
				$url='renzheng/'.$ceil."/".$uid."/".md5(C('MD5')."_".c('mydb.uid')."_".$type).".png";
				$newfile=Temmoku_PATH.C('UPFILES_CATALOG')."/".$url;
				write_dir($newfile);
				if(@move_uploaded_file($oldfile,$newfile)){
					@chmod($newfile, 0777);
					unlink($oldfile);
				}else{
					if(@copy($oldfile,$newfile))
					{
						@chmod($newfile, 0777);
						unlink($oldfile);
					}
				}
				return $url;
			}
			return ;
		}
		return ;
	}
}
?>