<?php
define('CURSCRIPT','login');
define('IN_SMT',true);
define('IN_MYMPS', true);
define('IN_MANAGE',true);

require_once dirname(__FILE__)."/include/global.php";
require_once dirname(__FILE__)."/data/config.php";

ifsiteopen();
if($mymps_global['cfg_if_member_log_in'] != 1) write_msg("操作失败！系统管理员关闭了会员功能！");
require_once MYMPS_DATA."/config.db.php";
require_once MYMPS_INC."/db.class.php";

@include MYMPS_DATA.'/caches/authcodesettings.php';
$authcodesettings = $data;
$data = NULL;
require_once MYMPS_MEMBER.'/include/common.func.php';
require_once MYMPS_SMARTY."/include.php";

if(!in_array($mod,array(CURSCRIPT,'register','forgetpass','out','openlogin'))){
	$mod = $_REQUEST['mod'] = CURSCRIPT;
}
require_once MYMPS_DATA."/config.inc.php";
require_once MYMPS_INC."/member.class.php";

$url   = isset($url) ? strip_tags($url) : '';
$action = isset($action) ? trim($action) : '';
$action = ($mymps_global['cfg_if_corp'] != 1 && $action == 'store') ? 'person' : $action;

if(!submit_check('log_submit')){

	if($mod != 'out' && $mod != 'openlogin'){
		$member_log -> chk_in() && write_msg('','member/index.php');
	}
	
	switch($mod){
		case 'out':
			if(PASSPORT_TYPE == 'ucenter'){
				//整合UC
				require MYMPS_ROOT.'/uc_client/client.php';
				$ucsynlogout = uc_user_synlogout();
				echo $ucsynlogout;
				echo mymps_goto($url ? $url : $mymps_global['SiteUrl'].'/'.$mymps_global['cfg_member_logfile']);
				
			} elseif(PASSPORT_TYPE == 'phpwind'){
				//整合PW
				$member_log -> out('noredirect');
				require MYMPS_ROOT.'/pw_client/uc_client.php';
				$ucsynlogout = uc_user_synlogout();
				echo $ucsynlogout;
				echo mymps_goto($url ? $url : $mymps_global['SiteUrl'].'/'.$mymps_global['cfg_member_logfile']);
			} else {
				$member_log -> out(trim($url));
			}
		break;
		
		case 'login':
			$data = NULL;
			@require MYMPS_INC.'/qqlogin/config.inc.php';
			$mymps_global['ifqqlogin'] = $data['open'];
			$data = NULL;
			
			$url = trim($url);

			$loc 		= get_location('login',0,'会员登录');
			$page_title = $loc['page_title'];
			$location 	= $loc['location'];
			tpl_assign();
			$smarty->assign('mymps_imgcode',$authcodesettings['login']);
			$smarty->assign('url',$url);
			unset($loc,$page_title,$location,$member_log);

			$smarty -> display(mymps_tpl(CURSCRIPT));
			
		break;
		
		case 'register':

			$mymps_global['cfg_if_member_register'] != 1 && write_msg("操作失败！系统管理员关闭了新会员注册！");
			
			$data = NULL;
			@require MYMPS_INC.'/qqlogin/config.inc.php';
			$mymps_global['ifqqlogin'] = $data['open'];
			$data = NULL;
			
			$loc 		= get_location('register',0,'会员注册');
			$page_title = $loc['page_title'];
			$location	= $loc['location'];
			tpl_assign();
			
			if(in_array($action,array('store','person'))){
				require MYMPS_DATA.'/safequestions.php';

				$smarty->assign('page_title',$page_title);
				$smarty->assign('safequestion',GetSafequestion(0,'safequestion'));
				$smarty->assign('mymps_imgcode',$authcodesettings['register']);
				$smarty->assign('submit','立即注册');
				$smarty->assign('get_area_options',cat_list('area'));
				$smarty->assign('get_member_cat',get_member_cat());
				$smarty->assign('mixcode',md5($cookiepre));
				
				$data = '';
				@include MYMPS_DATA.'/caches/checkanswer_settings.php';
				if(is_array($data)){
					$whenregister = $data['whenregister'];
					if($whenregister == '1' && $checkanswer = read_static_cache('checkanswer')){
						$checkquestion['id']		= $randid = array_rand($checkanswer,1);
						$checkquestion['question']  = $checkanswer[$randid]['question'];
						$checkquestion['answer']	= $checkanswer[$randid]['answer'];
						$smarty -> assign('checkquestion',$checkquestion);
					}
					unset($whenregister,$checkquestion,$data,$checkanswer,$loc,$page_title,$location,$member_log);
				}
				$smarty -> display(mymps_tpl($mod.'_'.$action));
			}else{
				$smarty -> display(mymps_tpl($mod));
			}
		break;
		
		case 'forgetpass':
			$uid = $uid ? intval($uid) : '';
			$code = $code ? mhtmlspecialchars($code) : '';
			if($code){
				$code = $code ? mhtmlspecialchars($code) : '';
				$arr = explode('.',base64_decode($code));
				$uid = $arr[0];
				$user_info = $db -> getRow("SELECT id,userid,userpwd,email FROM `{$db_mymps}member` WHERE id = '$uid'");
				
				tpl_assign();
				if(($timestamp - $arr[2] < 1800) && $arr[1] == md5($user_info['id'].'+'.$user_info['userpwd'])){
					$smarty -> assign('userid',$user_info['userid']);
					$smarty -> assign('uid',$user_info['id']);
					$smarty -> assign('email',$user_info['email']);
					$smarty -> display(mymps_tpl($mod.'_3'),$mod.'_3');
				} else {
					$status = 'error';
					$smarty -> assign('status','error');
					$smarty -> assign('msg','该密码重设链接已失效！');
					$smarty -> display(mymps_tpl($mod.'_4'),$mod.'_4');
				}
			} else {
				tpl_assign();
				$smarty->assign('mymps_imgcode',$authcodesettings['forgetpass']);
				$smarty -> display(mymps_tpl($mod),$mod);
			}
			
		break;
		
		case 'openlogin':
			!in_array($action,array('bind','reg')) && $action = 'reg';
			session_start();
			
			if(empty($_SESSION['nickname']) || empty($_SESSION['openid'])) write_msg('session会话失效，请重新登陆！',$mymps_global[SiteUrl].'/include/qqlogin/qq_login.php');
			$nickname = $_SESSION['nickname'];
			
			$loc 		= get_location('login',0,'会员登录');
			$page_title = $loc['page_title'];
			$location 	= $loc['location'];
			tpl_assign();
			$smarty -> assign('nickname',$nickname);
			$smarty -> assign('figureurl_qq_1',$_SESSION['figureurl_qq_1']);
			$smarty -> assign('mixcode',md5($cookiepre));
			$smarty -> display(mymps_tpl('openlogin'));
		break;
	}
}else{

	if($mod == 'openlogin'){
		define('QQLOGIN',1);//开启应用QQ登录接口
		!in_array($action,array('bind','reg')) && write_msg('您请求的来路不正确!','olmsg');
		session_start();
		$openid = mhtmlspecialchars($_SESSION['openid']);
		$cname  = mhtmlspecialchars($_SESSION['nickname']);
		
		if($action == 'bind'){
			if(empty($cname) || empty($openid)) write_msg('登录状态已超时，请重新登陆！',$mymps_global[SiteUrl].'/include/qqlogin/qq_login.php');
			$userid  = mhtmlspecialchars(trim($bind_userid));
			$userpwd = mhtmlspecialchars(trim($bind_userpwd));
			//绑定已有帐号
			if(!$mixcode || $mixcode != md5($cookiepre)){
				die('您请求的来路不正确');
				exit();
			}
			if(empty($bind_userid) || empty($bind_userpwd)){
				write_msg('原用户名和原密码输入不能为空!');
			}
		
			if(!$db -> getOne("SELECT id  FROM `{$db_mymps}member` WHERE userid = '$userid' AND userpwd = '".md5($userpwd)."'")){
				write_msg('原帐号或密码输入不正确，请返回重新输入!');
			}
			$db -> query("DELETE FROM `{$db_mymps}member` WHERE openid = '$openid'");//删除QQ登录自动注册的帐号
			$db -> query("UPDATE `{$db_mymps}member` SET openid = '$openid' WHERE userid = '$userid'");
			//开始登录
			$bind_userid = $bind_userpwd = $qqlogin = NULL;
			
			
			//开始登录
			if(PASSPORT_TYPE == 'phpwind'){
				require MYMPS_ROOT.'/pw_client/uc_client.php';
				$user_login = uc_user_login($userid,md5($userpwd),0);
				$member_log -> in($userid,md5($userpwd),'off','noredirect');
				echo $user_login['synlogin'];
			} elseif(PASSPORT_TYPE == 'ucenter'){
				require MYMPS_ROOT.'/uc_client/client.php';
				list($uid, $username, $password, $email) = uc_user_login($userid, $userpwd);
				echo uc_user_synlogin($uid);
			} else {
				$member_log -> in($userid,md5($userpwd),'off','noredirect');
			}
			/*
			@session_unregister('openid');
			@session_unregister('nickname');
			@session_unregister('access_token');
			@session_unregister('appid');
			*/
			session_unset();
			echo mymps_goto($mymps_global['SiteUrl'].'/member/index.php');
			
		} elseif($action == 'reg'){
			$userid  = mhtmlspecialchars(trim($userid));
			$userpwd = mhtmlspecialchars(trim($userpwd));
			$email 	 = mhtmlspecialchars(trim($email));
			
			if(!$userid){
				write_msg('请填写您的登录用户名!');
				exit;
			}
			
			if(!$userpwd){
				write_msg('请填写您的登录密码!');
				exit;
			}
			
			if(!$email){
				write_msg('请填写您的电子邮箱帐号!');
				exit;
			}
			
			//用QQ帐号注册新帐号
			$reg_corp = 0;
			
			if(PASSPORT_TYPE == 'phpwind'){
				//pw整合
				require MYMPS_ROOT.'/pw_client/uc_client.php';
				$checkuser = uc_check_username($userid);
				if($checkuser == -2){
					write_msg('用户名重复，请换一个用户名注册');
				} elseif($checkuser == -1){
					write_msg('用户名不符合规范，请换一个用户名注册');
				} elseif($checkuser == 1){
				
				} else {
					write_msg('未知错误，请换一个用户名注册');
				}
				
				$checkemail = uc_check_email($email);
				if($checkemail == -3){
					write_msg('EMAIL格式不正确，请填写正确的email');
				}
				
				uc_user_register($userid,md5($userpwd),$email);
				
			} elseif(PASSPORT_TYPE == 'ucenter'){
				//uc整合
				require MYMPS_ROOT.'/uc_client/client.php';
				//在UCenter注册用户信息
				if($activation && ($activeuser = uc_get_user($activation))) {
					list($uid, $userid) = $activeuser;
				} else {
					$user = $db -> getRow("SELECT id,userid FROM `{$db_mymps}member` WHERE userid = '$userid'");
					if(uc_get_user($userid) && !$user['userid']) {
						//判断需要注册的用户如果是需要激活的用户，则需跳转到登录页面
						write_msg("该用户无需注册，请重新登录",$mymps_global[SiteUrl]."/".$mymps_global['cfg_member_logfile']);
					}
					$uid = uc_user_register($userid,$userpwd, $email);
					if($uid <= 0) {
						if($uid == -1) {
							write_msg('用户名不合法');
						} elseif($uid == -2) {
							write_msg( '包含不允许注册的词语');
						} elseif($uid == -3) {
							write_msg( '用户名已经存在');
						} elseif($uid == -4) {
							write_msg( 'Email 格式有误');
						} elseif($uid == -5) {
							write_msg( 'Email 不允许注册');
						} elseif($uid == -6) {
							write_msg( '该 Email 已经被注册');
						} else {
							write_msg( '未定义');
						}
					} else {
						$userid  = trim($userid);
						$userpwd = trim($userpwd);
						$email 	 = trim($email);
					}
				}
				
			} else {
				//不整合
				$openlogin_uri = $mymps_global[SiteUrl]."/".$mymps_global[cfg_member_logfile]."?mod=openlogin";
				$rs	= CheckUserID($userid,'用户名');
				$rs != 'ok' && write_msg($rs);
				//strlen($userid) > 40 && write_msg("您的用户名多于40个字符，不允许注册！");
				strlen($userid) < 4 && write_msg("你的用户名或密码过短(不能少于4个字符)，不允许注册！",$openlogin_uri);
				!is_email($email) && write_msg("Email格式不正确！");
				$db->getOne("SELECT id FROM `{$db_mymps}member` WHERE userid = '$userid' AND openid = ''") && write_msg("你指定的用户名 {$userid} 已存在，请使用别的用户名！",$openlogin_uri);
			}
			
			/*积分变化*/
			$score_change = get_credit_score();
			$score_changer = $score_change['score']['rank']['register'] == '0' ? '+0' : $score_change['score']['rank']['register'];
			
			$userpwd = md5($userpwd);
			$money_own	=	$db -> getOne("SELECT money_own FROM `{$db_mymps}member_level` WHERE id = '1'");
			$money_own	=	$money_own ? $money_own : 0;
			
			$db -> query("UPDATE `{$db_mymps}member` SET jointime='$timestamp',logintime='$timestamp',userpwd = '$userpwd',email='$email',money_own = '$money_own',score = score".$score_changer."  WHERE userid = '$userid' ");
			//member_reg($userid,md5($userpwd),$email,$safequestion,$safeanswer,$openid,$cname);
			
			$score_change = $score_changer = NULL;
			
			/*
			 发送欢迎短消息
			 */
			if($mymps_global['cfg_member_reg_title'] && $mymps_global['cfg_member_reg_content']){
				$title 	 = str_replace('{username}',$userid,$mymps_global['cfg_member_reg_title']);
				$content = str_replace('{sitename}',$mymps_global['SiteName'],$mymps_global['cfg_member_reg_content']);
				$content = str_replace('{time}',GetTime($timestamp),$content);
				$content = str_replace('{username}',$userid,$content);
				sendpm('admin',$userid,$title,$content,1);
			}
			
			//如果是点击注册推广人的链接注册
			if(mgetcookie('fromuid') && $mymps_global['cfg_if_affiliate'] == 1){
				$fromuid = intval(mgetcookie('fromuid'));
				$fromip = trim(mgetcookie('fromip'));
				if($fromip != GetIP()){
					$score_changer = "+".$mymps_global['cfg_affiliate_score'];
					$db -> query("UPDATE `{$db_mymps}member` SET score = score".$score_changer." WHERE id = '$fromuid'");
				}
			}
			/*
			@session_unregister('openid');
			@session_unregister('nickname');
			@session_unregister('access_token');
			@session_unregister('appid');
			*/
			session_unset();
			if(PASSPORT_TYPE == 'phpwind'){
				$member_log -> in($userid,md5($userpwd),'off','noredirect');
				$user_login = uc_user_login($userid,md5($userpwd),0);
				$ucsynlogin = $user_login['synlogin'];
				echo $ucsynlogin;
				echo mymps_goto($url ? $url : $mymps_global['SiteUrl'].'/member/index.php');
			}elseif(PASSPORT_TYPE == 'ucenter'){
				list($uid, $username, $password, $email) = uc_user_login($userid, $userpwd);
				echo uc_user_synlogin($uid);
				echo mymps_goto($url ? $url : $mymps_global['SiteUrl'].'/member/index.php');
			} else {
				$member_log -> in($userid,$userpwd,$memory,'noredirect');
				write_msg("现在转入用户管理中心，请稍候...",$mymps_global['SiteUrl']."/member/index.php");
			}
			
			
		}
		
	} else{
		include MYMPS_MEMBER.'/include/'.$mod.'.inc.php';
	}
}

is_object($db) && $db->Close();
$mymps_global = $smarty = $timestamp = $mod = $smarty = $member_log = NULL;
?>