<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

include_once('Second_2fa_configs.php');
include_once('../system/lib/config.php');

$sys = new System();

/*
*  读取前台传过来的数据
*/
$catalog = $sys -> getColumnData($_POST, 'catalog');
$func_name = $sys -> getColumnData($_POST, 'func_name');
$lang = $sys -> getColumnData($_POST, 'lang');
$user_id = $sys -> getColumnData($_POST, 'user_id');
$title = $sys -> getColumnData($_POST, 'title');
$newCodes = $sys -> getColumnData($_POST, 'newCodes');
$fifteentime = '';
$con = True;
$takeName = 'tmp_2fa_public.php';
$useraccount = '';
$class_name =getlogic($catalog);
$status = NO_STRING;
$time = NO_STRING;

/*
*	1、判断是否有加二次认证功能，没有则加上该功能
*/

if($sys -> getUserData(PARAM_USER_ACCOUNT)!= NO_STRING){
	$useraccount = $sys -> getUserData(PARAM_USER_ACCOUNT);

}else{
	$accountd_list = $sys -> accessSelect('SELECT_2FA_USER_ACCOUNT',array($user_id));

	if(count($accountd_list) == NO_COUNT){
		echo '-1';
		return;
	}
	$useraccount = $sys -> getColumnData($accountd_list[0],'user_account');
}

$enable = $sys -> accessSelect('SELECT_2FA_ENABLE', array($useraccount));

$userMail = $sys -> getUserMail(array($useraccount));

$account_Type = $sys ->  accessSelect('SELECT_2FA_ACCOUNT_TYPE_VIA_USERID', array($useraccount));
$accountType = $sys -> getColumnData($account_Type[0],'account_type');

if($accountType == VAR_PERSON){
	if($catalog == 'login'){
		echo '-1';
		return;
	}
}


if (($user_id != '') && (count($enable) == NO_COUNT)) {

	if($accountType == VAR_CORP){
		$sys -> accessModify('INSERT_2FA_ENABLE_CORP', array($useraccount), false);
		$enable = $sys -> accessSelect('SELECT_2FA_ENABLE', array($useraccount));
	}else{
		$sys -> accessModify('INSERT_2FA_ENABLE', array($useraccount), false);
		$sys -> accessModify('INSERT_2FA_CODE', array($useraccount), false);
		$enable = $sys -> accessSelect('SELECT_2FA_ENABLE', array($useraccount));
	}

}

/*
* 	2、判断是否是登录功能
*/
include_once($_SERVER['DOCUMENT_ROOT'] . '/logic/'.$class_name.'/model.php');

if($catalog == 'login'){
	$class = new ReflectionClass(ucfirst($class_name).'ModelClass');
	$sys = $class -> newInstanceArgs();
	$takeName = 'tmp_2fa_model.php';
	$con = $sys -> init();
}else if (getlogic($catalog) == 'request') {
	$class = new ReflectionClass(ucfirst($class_name).'ModelClass');
	$sys = $class -> newInstanceArgs();
	$sys -> setType('save');
	$con = $sys -> init();
}

//格式化提示文言
$sys -> newMessage($lang);

/*
* 	3、开始进行二阶段code界面输出处理
*/

clearsession();
if (count($enable) == NO_COUNT){

		echo '-1';
}else if ($con) {

	if ($sys -> getType() == TYPE_VERI_ERROR) {
		echo '-1';
	} else {

		/*
		* 	4、用于判断是否超过3次错误
		*/
		$inspect = $sys ->  accessSelect('SELECT_2FA_24_INSPECT', array( $useraccount,$catalog.'_status',$catalog.'_update_time'));


		if(count($inspect) != NO_COUNT){

			/*
			* 	5、成功登陆判断是否需要充值 code 锁计数器
			*/
			$status = $sys -> getColumnData($inspect[0], $catalog.'_status');
			$time 	= $sys -> getColumnData($inspect[0], 'time');
			if ( $status == '3' || $time == VAL_INT_1) {
				$sys -> accessModify('UPDATE_2FA_ZERO_STATUS',array($catalog.'_status',$useraccount),false);
				$status = '0';
			}

			/*
			* 	6、判断是否需要进行二次认证
			*/
			if($sys -> getColumnData($enable[0], $catalog) == 1){

				/*
				* 	7、生成二次认证 code
				*/
				// 生成二次认证code
				$code = rand('100000','999999');
				$sql = NO_STRING;

				//拼接sql语句
				$sql = $catalog.'_code = '.$code;
				//超过24小时重置计数器
				if ( $status == '0') {
					$sql .= ','.$catalog.'_update_time = NOW()';
				}
				$sys -> accessModify('UPDATE_2FA_CODE', array($useraccount, $sql), false);
				$params = array();
				$params[] = $sys -> getColumnData($enable[0], COLUMN_USER_ACCOUNT);
				$params[] = $sys -> getColumnData($enable[0],'first_name').' '.$sys -> getColumnData($enable[0],'last_name');
				$params[] = $code;

				//发送二次认证Code
				$sys -> sendMailByTmpFromAPI('2fa_'.$catalog.'.xml', $params, $userMail, VAR_CS_MAIL_ADDRESS, $lang);
				clearsession();
				if ($newCodes == 'send_new_code') {
					$sys -> popUpSessionMessage(INFO, 'I_2FA_NEW_CODE', array());
				}

				//十五分钟验证
				$fifteentime = date('Y-m-d h:i:s',time());
				include_once('../'.$lang.'/template/'.$takeName);

			} else {
				echo '-1';
			}
		} else {
			//对锁定账户进行文言提示

			$sys -> popUpSessionMessage(ERROR, getTwentyTwe($catalog), array());
			include_once('../'.$lang.'/template/tmp_2fa_public2.php');
		}
	}
} else {
	echo '-1';
}
