Feature: Invitation registration

增加凭邀请码注册功能,核心部分
This commit is contained in:
Akkariin Meiko 2020-01-21 11:22:55 +08:00 committed by GitHub
parent e14938b79b
commit 9a79d201c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,6 +47,16 @@ class UserManager {
return Array("status" => false, "message" => "抱歉,本站暂不开放注册");
}
if($_config['register']['invite']) {
if(!isset($data['invitecode']) || empty($data['invitecode']) || !preg_match("/^[A-Za-z0-9]{32}$/", $data['invitecode'])) {
return Array("status" => false, "message" => "您需要填写正确的邀请码才能注册账号");
} else {
$inviteCode = $data['invitecode'];
}
} else {
$inviteCode = false;
}
if(!isset($data['username']) || !isset($data['email']) || !isset($data['password']) ||
empty($data['username']) || empty($data['email']) || empty($data['password'])) {
return Array("status" => false, "message" => "请将信息填写完整");
@ -85,6 +95,14 @@ class UserManager {
return Array("status" => false, "message" => "该邮箱已被注册");
}
if($inviteCode) {
if(!$this->checkInviteCode($data['invitecode'])) {
return Array("status" => false, "message" => "邀请码无效或已被使用");
} else {
Database::update("invitecode", Array("user" => $data['username']), Array("code" => $data['invitecode']));
}
}
// 执行注册
$this->addUser($data['username'], $data['email'], $data['password']);
@ -315,6 +333,12 @@ class UserManager {
return Database::querySingleLine("users", Array("email" => $email)) ? true : false;
}
public function checkInviteCode($code)
{
$rs = Database::querySingleLine("invitecode", Array("code" => $code));
return ($rs && empty($rs['user']));
}
public function checkEmail($email)
{
return preg_match("/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,48}$/", $email) ? true : false;
@ -367,4 +391,4 @@ class UserManager {
return 0;
}
}
}
}