<?php
if($action == 'register')
{
	$uid = uc_call("uc_user_register", array($username, $password, $email));

	if($uid <= 0)
	{
		if($uid == -1)
		{
			showmsg('用户名不合法');
		}
		elseif($uid == -2)
		{
			showmsg('包含要允许注册的词语');
		}
		elseif($uid == -3)
		{
			showmsg('用户名已经存在');
		}
		elseif($uid == -4)
		{
			showmsg('Email 格式有误');
		}
		elseif($uid == -5)
		{
			showmsg('Email 不允许注册');
		}
		elseif($uid == -6)
		{
			showmsg('该 Email 已经被注册');
		}
		else
		{
			showmsg('请正确设置 UC或者关闭 UC');
		}

	}
	$userid = '';
	$r = $member->get_by_touserid($uid);
	if(!$r)
	{
		$r=cache_read('cache_membermodel'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
		$registerinfo['touserid'] = $uid;
		$registerinfo['password']=$member->password($password);
		$registerinfo['vipstart']=$registerinfo['vipto']=$registerinfo['regtime']=TIME; 
		$registerinfo['logintime']=TIME; 
		$registerinfo['movetime']=TIME; 
		$registerinfo['loginip']=IP;
		$registerinfo['groupid']=AUDIT_TYPE=='1'?4:3; //设置用户组
		$registerinfo['ispassed']=AUDIT_TYPE=='1'?1:(AUDIT_TYPE=='2'?0:99); // MAIL_TYPE=1：$ispassed=1 审核通过  MAIL_TYPE=2：$ispassed=0 后台审核 MAIL_TYPE=3：$ispassed=99 邮箱激活
		if($r['register_enabled']==2) ///////////会员组特别设置
		{
			$registerinfo['groupid']=3;
			$registerinfo['ispassed']=0;
		}
		$registerinfo['gradeid']=10;
		$registerinfo['template']='company';
		$userid = $member->add($registerinfo);
		$spaceurl=SITE_URL.'space/index.php?id='.$userid;
		$companyurl=SITE_URL.'company/company_'.$userid.'.html';
		$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`spaceurl`='{$spaceurl}',`".DB_PRE."member`.`companyurl`='{$companyurl}' WHERE `".DB_PRE."member`.`id`={$userid}",true);
		$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`spaceurl`='{$spaceurl}',`".DB_PRE."member_cache`.`companyurl`='{$companyurl}' WHERE `".DB_PRE."member_cache`.`id`={$userid}",true);
	}
}
elseif ($action == 'login')
{
    $username = trim($username);
    $password = trim($password);
	$dircmsUsername=$username;
	$dircmsPassword=$password;
	list($uid, $username, $uc_password, $email) =  uc_call("uc_user_login", array($username, $password));

	if($uid==-1){
		$_userid = $member->get_userid($dircmsUsername);
		$_userInfo=$member->get($_userid, '`id`,`username`,`email`,`password`');
		if($_userInfo['id']>0){
			$md5_password = $member->password($dircmsPassword);
			if($_userInfo['password'] != $md5_password)
			{
				if($r['password'] == substr($md5_password, 8, 16))
				{
					$uid = uc_call("uc_user_register", array($_userInfo['username'], $dircmsPassword, $_userInfo['email']));
					if($uid<=0){
						showmsg('用户不存在,或者被删除');
					}
				}
				else
				{
					showmsg('用户不存在,或者被删除');
					return FALSE;
				}
			}else{
				$uid = uc_call("uc_user_register", array($_userInfo['username'], $dircmsPassword, $_userInfo['email']));
				if($uid<=0){
					showmsg('用户不存在,或者被删除');
				}
			}
		}
		list($uid, $username, $uc_password, $email) =  uc_call("uc_user_login", array($dircmsUsername, $dircmsPassword));
	}
	if( $uid == -1 )
	{
		showmsg('用户不存在,或者被删除');
	}
	if($uid == '-2')
	{
		showmsg('密码错误',$DIRCMS['site_url'].'member/index.php?file=login&action=login');
	}
    $code = uc_call('uc_user_synlogin', array($uid));
	$dircms_userid = $member->get_userid($username);
	if($dircms_userid<=0 && $uid > 0)
	{
		$r=cache_read('cache_membermodel1.cache.php',DIRCMS_ROOT.'data/cache_model/');
		$registerinfo['username'] = $username;
		$registerinfo['password'] = $password;
		$registerinfo['email'] = $email;
		$registerinfo['modelid'] = 1;
		$registerinfo['touserid'] = $uid;
		$registerinfo['password']=$member->password($password);
		$registerinfo['vipstart']=$registerinfo['vipto']=$registerinfo['regtime']=TIME; 
		$registerinfo['logintime']=TIME; 
		$registerinfo['movetime']=TIME; 
		$registerinfo['loginip']=IP;
		$registerinfo['groupid']=AUDIT_TYPE=='1'?4:3; //设置用户组
		$registerinfo['ispassed']=AUDIT_TYPE=='1'?1:(AUDIT_TYPE=='2'?0:99); // MAIL_TYPE=1：$ispassed=1 审核通过  MAIL_TYPE=2：$ispassed=0 后台审核 MAIL_TYPE=3：$ispassed=99 邮箱激活
		if($r['register_enabled']==2) ///////////会员组特别设置
		{
			$registerinfo['groupid']=3;
			$registerinfo['ispassed']=0;
		}
		$registerinfo['gradeid']=10;
		$registerinfo['template']='company';
		$userid = $member->add($registerinfo);
		$spaceurl=SITE_URL.'space/index.php?id='.$userid;
		$companyurl=SITE_URL.'company/company_'.$userid.'.html';
		$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`spaceurl`='{$spaceurl}',`".DB_PRE."member`.`companyurl`='{$companyurl}' WHERE `".DB_PRE."member`.`id`={$userid}",true);
		$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`spaceurl`='{$spaceurl}',`".DB_PRE."member_cache`.`companyurl`='{$companyurl}' WHERE `".DB_PRE."member_cache`.`id`={$userid}",true);

	}else if($dircms_userid>0 && $uid > 0){
		$arr_member=array();
		$arr_member['touserid'] = $uid;
		$member->set($dircms_userid,$arr_member);
	}
}
elseif ($action == 'logout')
{
    $ucsynlogout = uc_call('uc_user_synlogout', array());
}
elseif ($action == 'editpwd')
{
	uc_call("uc_user_edit", array($username, $old_password, $new_password, $email));
	if($ucresult == -1)
	{
		showmsg('旧密码不正确');
	}
	elseif($ucresult == -4)
	{
		showmsg('Email 格式有误');
	}
	elseif($ucresult == -5)
	{
		showmsg('Email 不允许注册');
	}
	elseif($ucresult == -6)
	{
		showmsg('该 Email 已经被注册');
	}
}
elseif ($action == 'edit')
{
	uc_call("uc_user_edit", array($username, '', $password, $email, 1));
}
?>