<?php
/*
Runtime Envirment file
位置: /passport
*/
require('../rte.php');
define('MODULE','passport');
define('WORK_DIR',ROOT.MODULE."/");
require(WORK_DIR.'config/baseconfig.php');
$Lang -> LoadLangFromFile('i18n_passport');

if ($base_config['ip_ban_login'] && !CheckIpBan()) {
	ShowMessage('your.ip.not.allow');
}

$return = $return ? $return : ($forward ? $forward : ($_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $base_config['passport_root']));

#整合UC后跳转到UC其他网站登陆
if ($rtc['uc_use']) {
	ShowMessage('',GetLoginurl(),0,0);
}

$username = trim($username);
if (isset($Submit)) {
	
	if ($rtc['login_gdcode']) {
		Iimport('CheckNum');
		$checknum = new CheckNum();
		if (!$checknum -> Check($gdcode)) {
			ShowMessage('gdcode.error',-1,1);
		}
	}
	
	Iimport('PassPort_User');
	$passport = new PassPort_User();
	if($passport -> CheckUserUniqID($username,$password)) {
		#禁止登陆
		if ($passport -> U['u_loginban']) {
			ShowMessage('forbidden.login');
		}
		
		#用户过期
		if ($base_config['expired_ban'] && $passport -> U['u_expired'] && $passport -> U['u_expired'] < $timestamp) {
			ShowMessage('user.expired.forbidden.login');
		}
		
		$passport -> PassCheckRebuild();
		$passport -> PutLoginedInfo();
		$passport -> UpdateLastLogin();
		//passport api
		if ($base_config['passport_method'] == 2 && $base_config['passport_api_address']) {
			
			#变量约定
			
			//动作标记
			$inter_action = 'login';
			//传送数据
			$inter_db = array(
				'username' => $username,
				'password'=> $passport -> U[$passport -> U_PasswordKey],
				'email' => $passport -> U['email'] ? $passport -> U['email'] : '@' ,
				'time' => $timestamp,
			);
			//跳转地址
			$inter_forward = $forward ? $forward : ($return ? $return : $base_config['passport_root']) ;
			
			//验证签名,在模块中计算.
			$Inter_verify = '';
			
			$interface_file = WORK_DIR.'api/passport_interface/'.$base_config['passport_api_program'].'_interface.php';
			define('passport_safe',1);
			require($interface_file);
			
			#变量约定
			if ($base_config['passport_server'] != 1) {		//不使用认证服务器功能
				header("Location: $inter_url");
				echo "<meta http-equiv='refresh' content='0;url=$inter_url'>";
				exit;
			}
		}
		//passport api
		
		#认证服务器
		if ($base_config['passport_server'] == 1) {	//认证服务器服务端
			define('ppfapi_safe',true);
			$ppf_api_userdata = array(
				'uid' => $passport -> U_ID,
				'username' => $passport -> U_Uname,
				'password' => $password,
				'email' => $passport -> U['email'],
			);
			$ppf_api_return = $inter_url ? $inter_url : $return;
			require(WORK_DIR.'ppfapi/interface_login.php');
		}
		#认证服务器
		
		if($return) {
			ShowMessage('passport.login.success',$return,0,0);
		}else {
			ShowMessage('passport.login.success','./',0,0);
		}
		exit;
	}else {
		ShowMessage('passport.login.fail');
	}
}else {
	Iimport('template');
	$tpl = new Template();
	$tpl -> LanguageAssign('title','passport.login');
	$tpl -> DisPlay('login');
}
?>