<?php
	header("Pragma:no-cache");
	!defined('DIRCMS_MEMBER') && exit('Access Denied!');
	$action=empty($action)?'login':trim($action);
	include(DIRCMS_ROOT.'include/member.class.php');
	$member=new member();
	switch($action)
	{
		case 'login':
			if($do_submit)
			{
				$url=$forwardurl?$forwardurl:($_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:$DIRCMS['site_url']);
				if($_userid) 
				{
					header('location:index.php'); 
					exit();
				}
				if(LOGIN_CHECKCODE_ENABLED && ($_SESSION['checkcode']!=$chkcode || !$_SESSION['checkcode']))showmsg('验证码不正确!');

				if(UC)
				{
					$action = 'login';
					require DIRCMS_ROOT.'api/passport_server_ucenter.php';
					$member->edit_password_username($username, $password);
				}

				$r=$member->login($username,$password);
				if(!$r)
				{
					showmsg($member->msg);
				}
				$_userid=$r['id'];
				unset($_SESSION['checkcode']);

				if(ENABLEPASSPORT) // 开启通行证
				{
					$action='login';
					$password=md5($password);
					require DIRCMS_ROOT.'api/passport_server_'.$DIRCMS['passport_file'].'.php';
				}
				showmsg('登陆成功!',$url);
			}
			if($_userid) 
			{
				header('location:index.php');
				exit();
			}
			if(LOGIN_CHECKCODE_ENABLED)$ischeckcode=true;
			include template('member_login');
			break;
		case 'register':
			if($do_submit)
			{
				$forwardurl=$DIRCMS['site_url'].'member/index.php';
				if($_userid)showmsg('您已经是注册会员!','index.php');
				if(!REGISTER_ENABLED)showmsg('网站关闭注册!',$DIRCMS['site_url']);
				if(REG_CHECKCODE_ENABLED && ($_SESSION['checkcode']!=$chkcode || !$_SESSION['checkcode']))showmsg('验证码不正确!');
				if(!$regagreement)showmsg('您必须阅读并同意《'.$DIRCMS['site_name'].'网络服务使用协议》!');
				if($pwd!=$pwdconfirm)showmsg('两次密码输入不一致!');
				$invitionaluser=INVITIONAL?intval($invitionaluser):0;
				if(UC)
				{
					$password=trim($pwd);
					@extract($registerinfo);
					require DIRCMS_ROOT.'api/passport_server_ucenter.php';
				}
				if(!$userid)
				{
					$userid=$member->register($registerinfo['modelid'],$registerinfo['username'],$pwd,$registerinfo['email'],$invitionaluser);
				}
				if(!$userid)
				{
					showmsg($member->msg);
				}
				else
				{
					unset($url);
					if(ENABLEPASSPORT) // 开启通行证
					{
						$action='register';
						$password=md5($pwd);
						require DIRCMS_ROOT.'api/passport_server_'.$DIRCMS['passport_file'].'.php';
					}
					
					if(AUDIT_TYPE=='1')
					{
						$url=$url?$url:$forwardurl;
						$member->login($registerinfo['username'],$pwd);
						showmsg('注册成功!',$url);
					}
					elseif(AUDIT_TYPE=='2')
					{
						$url=$url?$url:$forwardurl;
						showmsg('注册成功，请等待管理员审核!',$url,2000);
					}
					elseif(AUDIT_TYPE=='3')
					{
						$url=$url?$url:$forwardurl;
						showmsg('注册成功，请登录您的邮箱激活账号!',$url,2000);
					}
				}
			}
			
			if($_userid)showmsg('您已经是注册会员!','index.php');
			if(!REGISTER_ENABLED)showmsg('网站关闭注册!',$DIRCMS['site_url']);
			if(REG_CHECKCODE_ENABLED)$ischeckcode=true;
			$r=cache_read('cache_membermodel.cache.php',DIRCMS_ROOT.'data/cache_model/');
			$membermodel='<select name="registerinfo[modelid]">';			
			foreach($r as $v)
			{				
				$membermodel.='<option value='.$v['id'].'>'.$v['modelname'].'</option>';
			}
			$membermodel.='</select>';
			include template('member_register');
			unset($membermodel);
			break;
		case 'getpwd':
			if($do_submit)
			{
				if($_userid)showmsg('您已经是注册会员!','index.php');
				if($_SESSION['checkcode']!=$chkcode || !$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(!$member->is_username($username))showmsg('会员'.$username.'不存在!');
				if(!is_email($email))showmsg('请输入正确的邮箱!');
				$r=$db->fetch_one("SELECT * FROM `".DB_PRE."member` WHERE `".DB_PRE."member`.`username`='{$username}'");
				if(!$r)showmsg('会员'.$username.'不存在!');
				if($r['email']!=$email)
				{	
					showmsg('会员名和注册邮箱不一致!');
				}
				else
				{
					require_once(DIRCMS_ROOT.'include/email.class.php');
					$sendemail=new email();
					$sendemail->send($email, $DIRCMS['site_name'].'找回密码操作','请单击以下链接设置新密码：'.$DIRCMS['site_url'].'/member/index.php?file=login&action=resetpwd&verify='.md5(AUTH_KEY.$r['id'].$r['username'].$r['password'].$r['modelid']).'&id='.$r['id'],MAIL_USER);
					showmsg('操作成功，请登录邮箱修改新密码!','index.php?file=login&action=login');
				}
			}
			if($_userid)showmsg('您已经是注册会员!','index.php');
			include template('member_getpwd');
			break;
		case 'resetpwd':
			if($do_submit)
			{
				$r=$member->get(intval($id));
				if(!$r || $verify!=md5(AUTH_KEY.$r['id'].$r['username'].$r['password'].$r['modelid']))
				{
					showmsg('无效的操作!',$DIRCMS['site_url']);
				}
				else
				{
					if($pwd!=$pwdconfirm)showmsg('两次密码输入不一致!');
					if(UC)
					{
						$username = $_username;
						$email = $_email;
						$action = 'editpwd';
						require DIRCMS_ROOT.'api/passport_server_ucenter.php';
					}
					if($member->editpassword($r['id'],$pwd))
					{
						showmsg('用户密码修改成功，可以登录!','index.php?file=login&action=login');
					}
					else
					{
						showmsg('用户密码由3-30位字符组成!');
					}
				}
			}
			include template('member_resetpwd');
			break;
		case 'activate':
			$r=$member->get(intval($id));
			if(!$r || $verify!=md5(AUTH_KEY.$r['id'].$r['username'].$r['password'].$r['modelid']))
			{
				showmsg('会员账号激活失败!',$DIRCMS['site_url']);
			}
			if($r['groupid']!=3)
			{
				showmsg('该会员账号已经激活，请直接登录!','index.php?file=login&action=login');
			}
			if(ACTIVELINKTIME && ($r['regtime']+ACTIVELINKTIME*3600)<TIME)
			{
				showmsg('激活链接已失效!',$DIRCMS['site_url']);
			}
			else
			{
				$get_cookietime=get_cookie('cookietime');
				$_cookietime=$get_cookietime?$get_cookietime:0;
				$cookietime=$_cookietime?TIME + $_cookietime:0;
				$dircms_auth_key=md5(AUTH_KEY.$_SERVER['HTTP_USER_AGENT']);
				$dircms_auth=dircms_auth($r['id']."\t".$r['password'],'ENCODE',$dircms_auth_key);
				set_cookie('auth',$dircms_auth,$cookietime);
				set_cookie('cookietime',$_cookietime, $cookietime);
				$logintimes=$r['logintimes']+1;
				$db->update(DB_PRE.'member',array('groupid'=>4,'ispassed'=>1,'logintime'=>TIME,'loginip'=>IP,'logintimes'=>$logintimes),'id='.intval($r['id']));
				$db->update(DB_PRE.'member_cache',array('groupid'=>4,'ispassed'=>1,'logintime'=>TIME,'loginip'=>IP,'logintimes'=>$logintimes),'id='.intval($r['id']));
				showmsg('会员账号激活成功!','index.php');
			}
			break;
		case 'logout':
			$member->logout();
			unset($url);
			if(UC)
			{
				$action = 'logout';
				require DIRCMS_ROOT.'api/passport_server_ucenter.php';
			}
			if(ENABLEPASSPORT) // 开启通行证
			{
				$action='logout';
				require DIRCMS_ROOT.'api/passport_server_'.$DIRCMS['passport_file'].'.php';
			}
			$forwardurl=$_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:$DIRCMS['site_url'].'member/index.php';
			$url=$url?$url:$forwardurl;
			showmsg('退出成功!',$url);
			break;
		default:
			showmsg('参数有误,返回网站首页!',$DIRCMS['site_url']);
			break;
	}
?>