<?php
if(!defined('IN_MYMPS')) exit('Forbidden');
$ac = !in_array($ac,array('person','com','sjrz')) ? 'person' : trim($ac) ;

$appkey = $db->getOne("SELECT value FROM `{$db_mymps}config` WHERE description = 'key_sfz'");
if(submit_check('certify_submit')){
	require MYMPS_INC.'/upfile.fun.php';
	$name_file = 'certify_image';
	$name_file1 = 'certify_image1';
	$name_file2 = 'certify_image2';
	if($ac == 'person'){
		$typeid = 2;
		//************1.真实姓名和身份证号码判断是否一致************//
		if($appkey){	
			if($sfz && $cname && $mobile){
				$url = "http://v.juhe.cn/telecom/query";
				$params = array(
					  "idcard" => $sfz,
					  "realname" => $cname,
					  "key" => $appkey,
					  "mobile"=>$mobile,
					  "showid"=>1
				);
		
				$paramstring = http_build_query($params);
				$content = juhecurl($url,$paramstring);
				$result = json_decode($content,true);
		
				if($result){
					if($result['error_code']=='0'){
						if($result['result']['res']==1){
							//匹配
							$db->query("UPDATE `{$db_mymps}member` SET per_certify = '1',cname='$cname',mobile='$mobile',sfz='$sfz' {$where}");
							$db->query("INSERT INTO `{$db_mymps}certification` SET typeid='2',userid='$s_uid',img_path='',pubtime = '$timestamp'");
							write_msg('恭喜！身份证认证已提交通过！','?m=certify');
							exit;
						}else{
							write_msg($result['result']['resmsg']);
						}
					}else{
						write_msg($result['error_code'].":".$result['reason']);
					}
				}else{
					write_msg('请求失败');
				}
			}else{
				write_msg('姓名，身份证号，手机号为必填项，请返回重写');
			}
		}
		//************1.真实姓名和身份证号码判断是否一致************//
	}elseif($ac == 'com'){
		$typeid = 1;
	}elseif($ac == 'sjrz'){
		$typeid = 3;
		if(!$mobile) write_msg('请输入您的手机号码!');
		if(!is_mobile($mobile)) write_msg('手机号格式不正确!');
		$smscheckcode = $smscheckcode ? mhtmlspecialchars($smscheckcode) : '';
		if(!mymps_chk_smsrandcode($smscheckcode,$mobile)){
			write_msg('手机验证码输入不正确或与手机号不匹配！');
		}
		if(mymps_count("member","WHERE mobile = '$mobile' AND userid != '$s_uid'") > 0) {
			write_msg('该手机号已认证过，请更换一个手机号 ^_^');
		}
		$db->query("UPDATE `{$db_mymps}member` SET sj_certify = '1',mobile='$mobile' WHERE userid = '$s_uid'");
		write_msg('','?m=certify&ac=sjrz&success=8');
		exit;
	}
	
	if ($_FILES[$name_file]["name"]||$_FILES[$name_file1]["name"]||$_FILES[$name_file2]["name"]){
		if($_FILES[$name_file]["name"]) check_upimage($name_file);
		if($_FILES[$name_file1]["name"]) check_upimage($name_file1);
		if($_FILES[$name_file2]["name"]) check_upimage($name_file2);
		$destination = '/certification/'.date("Ym").'/';
		if($_FILES[$name_file]["name"]) $mymps_image = start_upload($name_file,$destination,0);
		if($_FILES[$name_file1]["name"]) $mymps_image1 = start_upload($name_file1,$destination,0);
		if($_FILES[$name_file2]["name"]) $mymps_image2 = start_upload($name_file2,$destination,0);
		if($mymps_image) $db->query("INSERT INTO `{$db_mymps}certification` SET typeid='$typeid',userid='$s_uid',img_path='$mymps_image',pubtime = '$timestamp'");
		if($mymps_image1) $db->query("INSERT INTO `{$db_mymps}certification` SET typeid='$typeid',userid='$s_uid',img_path='$mymps_image1',pubtime = '$timestamp'");
		if($mymps_image2) $db->query("INSERT INTO `{$db_mymps}certification` SET typeid='$typeid',userid='$s_uid',img_path='$mymps_image2',pubtime = '$timestamp'");
	} else {
		write_msg('','?m=certify&error=13');
	}
	write_msg('','?m=certify&success=9');
} else {
	if($pubtime = $db -> getOne("SELECT pubtime FROM `{$db_mymps}certification` $where ORDER BY pubtime DESC")) {
		$certifylang = '您已经于'.GetTime($pubtime).'提交过认证图片，确认要重新提交吗?';
	}
	$here = '实名认证';
	$location = location();
	include mayi_tpl('certify');
}
?>