From 9a79d201c132055a06177febcacdd0a3c0a6bc79 Mon Sep 17 00:00:00 2001 From: Akkariin Meiko Date: Tue, 21 Jan 2020 11:22:55 +0800 Subject: [PATCH] Feature: Invitation registration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加凭邀请码注册功能,核心部分 --- core/UserManager.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/core/UserManager.php b/core/UserManager.php index 799ab61..4e5fc37 100755 --- a/core/UserManager.php +++ b/core/UserManager.php @@ -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; } } -} \ No newline at end of file +}