Explorar el Código

支持邀请码注册

visuddhinanda hace 4 años
padre
commit
dfac8b8ddf
Se han modificado 4 ficheros con 455 adiciones y 32 borrados
  1. 4 0
      app/api/user.php
  2. 107 32
      app/db/user.php
  3. 57 0
      app/ucenter/sign.js
  4. 287 0
      app/ucenter/sign_up.php

+ 4 - 0
app/api/user.php

@@ -34,6 +34,10 @@ switch ($_REQUEST["_method"]) {
 		# get
 		$model->reset_password_send_email();
 		break;	
+	case 'reset_pwd':
+		# get
+		$model->reset_password();
+		break;	
 	default:
 		# code...
 		break;

+ 107 - 32
app/db/user.php

@@ -74,40 +74,66 @@ class User extends Table
 
 
 	public function  create(){
-		if(!isset($_COOKIE["userid"])){
-			return;
-		}
 		$json = file_get_contents('php://input');
 		$data = json_decode($json,true);
-		$data["user_id"] = $_COOKIE["userid"];
-		$isExist = $this->medoo->has("likes",$data);
+		//验证邀请码
+		if(isset($data["invite"])){
+			if ($this->redis == false) {
+				$this->result["ok"]=false;
+				$this->result["message"]="no_redis_connect";
+				echo json_encode($this->result, JSON_UNESCAPED_UNICODE);	
+				return;	
+			}
+			$code = $this->redis->exists("invitecode://".$data["invite"]);
+			if(!$code){
+				$this->result["ok"]=false;
+				$this->result["message"]="invite_code_invalid";
+				echo json_encode($this->result, JSON_UNESCAPED_UNICODE);	
+				return;	
+			}
+		}else{
+			$this->result["ok"]=false;
+			$this->result["message"]="no_invite_code";
+			echo json_encode($this->result, JSON_UNESCAPED_UNICODE);	
+			return;	
+		}
+		//验证用户名有效性
+		if(!$this->isValidUsername($data["username"])){
+			echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
+			return;
+		}
+		$isExist = $this->medoo->has($this->table,["username"=>$data["username"]]);
 		if(!$isExist){
-			echo json_encode($this->_create($data,["like_type","resource_type","resource_id","user_id"]), JSON_UNESCAPED_UNICODE);
+			if(!$this->isValidEmail($data["email"])){
+				echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
+				return;
+			}
+			$isExist = $this->medoo->has($this->table,["email"=>$data["email"]]);
+			if(!$isExist){
+				if(!$this->isValidPassword($data["password"])){
+					echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
+					return;
+				}
+				$data["userid"] = UUID::v4();
+				$data["password"] = md5($data["password"]);
+				$data["create_time"] = mTime();
+				$data["modify_time"] = mTime();
+				$data["setting"] = "{}";
+				echo json_encode($this->_create($data,["userid","username","email","password","nickname","setting","create_time","modify_time"]), JSON_UNESCAPED_UNICODE);
+			}else{
+				$this->result["ok"]=false;
+				$this->result["message"]="email_is_exist";
+				echo json_encode($this->result, JSON_UNESCAPED_UNICODE);				
+			}
 		}
 		else{
 			$this->result["ok"]=false;
-			$this->result["message"]="is exist";
+			$this->result["message"]="account_is_exist";
 			echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
 		}
 	}
 	
-	public function  delete(){
-		if(!isset($_COOKIE["userid"])){
-			return;
-		}
-		$where["like_type"] = $_GET["like_type"];
-		$where["resource_type"] = $_GET["resource_type"];
-		$where["resource_id"] = $_GET["resource_id"];
-		$where["user_id"] = $_COOKIE["userid"];
-		$row = $this->_delete($where);
-		if($row["data"]>0){
-			$this->result["data"] = $where;
-		}else{
-			$this->result["ok"]=false;
-			$this->result["message"]="no delete";			
-		}
-		echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
-	}
+
 
 	#发送密码重置邮件
 	public function reset_password_send_email(){
@@ -199,23 +225,72 @@ class User extends Table
 	}
 
 	#重置密码
-	public function reset_password($username,$password,$token){
-		$isExist = $this->medoo->has($this->table,["user_name"=>$username,"token"=>$token]);
+	public function reset_password(){
+		$json = file_get_contents('php://input');
+		$data = json_decode($json,true);
+		$isExist = $this->medoo->has($this->table,["username"=>$data["username"],"reset_password_token"=>$data["reset_password_token"]]);
 		if($isExist){
 			#reset password
-			$ok = $this->_update(["password"=>$password],"password",["user_name"=>$username]);
-			if($ok){
-				echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
-				
-			}else{
+			if(!isValidPassword($data["password"])){
 				echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
+				return;
 			}
+			$ok = $this->_update(["password"=>md5($data["password"])],["password"],["username"=>$data["username"]]);
+			if($ok){
+				#成功后删除reset_password_token
+				$ok = $this->_update(["reset_password_token"=>null,
+									  "reset_password_sent_at"=>null],
+									  null,
+									  ["username"=>$data["username"]]);	
+			}
+			echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
 		}else{
 			$this->result["ok"]=false;
-			$this->result["message"]="invalid token";
+			$this->result["message"]="invalid_token";
 			echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
 		}
 	}
+
+	private function isValidPassword($password){
+		if(mb_strlen($password,"UTF-8")<6){
+			$this->result["ok"]=false;
+			$this->result["message"]="password_too_short";
+			return false;
+		}
+		if(mb_strlen($password,"UTF-8")>32){
+			$this->result["ok"]=false;
+			$this->result["message"]="password_too_long";
+			return false;
+		}
+		if(strpos($password," ")!==false){
+			$this->result["ok"]=false;
+			$this->result["message"]="can_not_space";
+			return false;
+		}
+		return true;
+	}
+	private function isValidUsername($username){
+		if(mb_strlen($username,"UTF-8")>32){
+			$this->result["ok"]=false;
+			$this->result["message"]="username_too_long";
+			return false;
+		}
+		if(preg_match("/@|\s|\//",$username)!==0){
+			$this->result["ok"]=false;
+			$this->result["message"]="char_error";
+			return false;
+		}
+		return true;
+	}
+	private function isValidEmail($email){	
+		$isValid = filter_var($email, FILTER_VALIDATE_EMAIL);
+		if($isValid===false){
+			$this->result["ok"]=false;
+			$this->result["message"]="email_format_error";
+		}
+		return $isValid;
+	}
+
 }
 
 ?>

+ 57 - 0
app/ucenter/sign.js

@@ -0,0 +1,57 @@
+function submit(){
+	if($("#password").val()!==$("#repassword").val()){
+		$("#error_password").text("两次密码输入不一致");
+		return;
+	}
+	$.ajax({
+		type: 'POST',
+		url:"../api/user.php?_method=create",
+		contentType:"application/json; charset=utf-8",
+		data:JSON.stringify({
+			invite:$("#invite").val(),
+			username:$("#username").val(),
+			password:$("#password").val(),
+			email:$("#email").val(),
+			nickname:$("#nickname").val(),
+			lang:$("#lang").val()
+		}),
+		dataType:"json"
+		}).done(function (data) {
+			
+			if(data.ok){
+				$("#form_div").hide();
+				$("#message").removeClass("form_error");
+				$("#message").html("注册成功。<a href='index.php?op=login'>登录</a>");
+
+			}else{
+				$("#message").addClass("form_error");
+				$("#message").text(data.message);
+			}
+	}).fail(function(jqXHR, textStatus, errorThrown){
+		$("#message").removeClass("form_error");
+		$("#message").text(textStatus);				
+		switch (textStatus) {
+	
+			case "timeout":
+				break;
+			case "error":
+				switch (jqXHR.status) {
+					case 404:
+						break;
+					case 500:
+						break;				
+					default:
+						break;
+				}
+				break;
+			case "abort":
+				break;
+			case "parsererror":			
+				console.log("delete-parsererror",jqXHR.responseText);
+				break;
+			default:
+				break;
+		}
+		
+	});
+}

+ 287 - 0
app/ucenter/sign_up.php

@@ -0,0 +1,287 @@
+<?php
+#重置密码
+require_once '../path.php';
+require_once "../public/load_lang.php";
+require_once "../public/function.php";
+require_once "../redis/function.php";
+
+
+if (!isset($_GET["token"])) {
+    
+}
+
+
+?>
+
+<!DOCTYPE html>
+<html>
+	<head>
+		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+		<meta name="viewport" content="width=device-width, initial-scale=1.0">
+		<link type="text/css" rel="stylesheet" href="../studio/css/font.css"/>
+		<link type="text/css" rel="stylesheet" href="../studio/css/style.css"/>
+		<link type="text/css" rel="stylesheet" href="../studio/css/color_day.css" id="colorchange" />
+		<title>wikipali reset password</title>
+		<script src="../public/js/comm.js"></script>
+		<script src="../studio/js/jquery-3.3.1.min.js"></script>
+		<script src="../studio/js/fixedsticky.js"></script>
+		<script src="../ucenter/sign.js"></script>
+		<style>
+		#login_body{
+			display: flex;
+			padding: 2em;
+			margin: auto;
+		}
+		#login_left {
+			padding-right: 12em;
+			padding-top: 5em;
+		}
+		.title{
+			font-size: 150%;
+			margin-top: 1em;
+			margin-bottom: 0.5em;
+		}
+		#login_form{
+			padding: 2em 0 1em 0;
+		}
+		#tool_bar {
+			padding: 1em;
+			display: flex;
+			justify-content: space-between;
+		}
+		#login_shortcut {
+			display: flex;
+			flex-direction: column;
+			padding: 2em 0;
+		}
+		#login_shortcut button{
+			height:3em;
+		}
+		#button_area{
+			text-align: right;
+				padding: 1em 0;
+		}
+		.form_help{
+			font-weight: 400;
+			color: var(--bookx);
+		}
+		.login_form input{
+			margin-top:2em;
+			padding:0.5em 0.5em;
+		}
+		.login_form select{
+			margin-top:2em;
+			padding:0.5em 0.5em;
+		}
+		.login_form input[type="submit"]{
+			margin-top:2em;
+			padding:0.1em 0.5em;
+		}
+
+		.form_error{
+			color:var(--error-text);
+		}
+		#login_form_div{
+			width:30em;
+		}
+
+		#ucenter_body {
+			display: flex;
+			flex-direction: column;
+			margin: 0;
+			padding: 0;
+			background-color: var(--tool-bg-color3);
+			color: var(--btn-color);
+		}
+		.icon_big {
+			height: 2em;
+			width: 2em;
+			fill: var(--btn-color);
+			transition: all 0.2s ease;
+		}
+		.form_field_name{
+			position: absolute;
+			margin-left: 7px;
+			margin-top: 2em;
+			color: var(--btn-border-line-color);
+			-webkit-transition-duration: 0.4s;
+			-moz-transition-duration: 0.4s;
+			transition-duration: 0.4s;
+			transform: translateY(0.5em);
+		}
+		.viewswitch_on {
+			position: absolute;
+			margin-left: 7px;
+			margin-top: 1.5em;
+			color: var(--bookx);
+			-webkit-transition-duration: 0.4s;
+			-moz-transition-duration: 0.4s;
+			transition-duration: 0.4s;
+			transform: translateY(-15px);
+		}
+
+		</style>
+
+		<script>
+
+		function login_init(){
+			$("input").focus(function(){
+				let name = $(this).attr("name");
+				var objNave = document.getElementById("tip_"+name);
+				objNave.className = "viewswitch_on";
+			});
+			$(".form_field_name").click(function(){
+				let id = $(this).attr("id");
+				var objNave = document.getElementById(id);
+				objNave.className = "viewswitch_on";
+				let arrId=id.split("_");
+				document.getElementById('input_'+arrId[1]).focus();
+			});
+
+		}
+		</script>
+	<link type="text/css" rel="stylesheet" href="mobile.css" media="screen and (max-width:800px)">
+	</head>
+	<body id="ucenter_body" onload="login_init()">
+
+	<div id="tool_bar">
+	</div>
+<div id="login_body" >
+
+	<div id="login_left">
+		<div  >
+			<svg  style="height: 8em;width: 25em;">
+				<use xlink:href="../public/images/svg/wikipali_login_page.svg#logo_login"></use>
+			</svg>
+		</div>
+		<div style="    padding: 1em 0 0 3.5em;font-weight: 400;">
+		<?php echo $_local->gui->pali_literature_platform; ?>
+		<ul style="padding-left: 1.2em;">
+			<li><?php echo $_local->gui->online_dict_db; ?></li>
+			<li><?php echo $_local->gui->user_data_share; ?></li>
+			<li><?php echo $_local->gui->cooperate_edit; ?></li>
+		</ul>
+		</div>
+	</div>
+	<div id="login_right">
+		<div id = "login_form_div" class="fun_block" >
+
+			<div class="title">
+			注册wikipali账号
+			</div>
+			<div class="login_new">
+				<span class="form_help"><?php echo $_local->gui->have_account; ?> ?</span><a href="index.php?language=<?php echo $currLanguage; ?>">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $_local->gui->login; //登入账户 ?></a>
+			</div>
+			<div class="form_error">
+			<?php
+			if (!isset($_GET["invite"])) {
+				echo "目前只支持邀请码注册。";
+				exit;
+			}else{
+				$redis = redis_connect();
+				if ($redis == false) {
+					echo "服务器故障,请稍后重试。<br> 错误代码:no_redis_connect";
+					exit;
+				}
+				$code = $redis->exists("invitecode://".$_REQUEST["invite"]);
+				if(!$code){
+					echo "无效的邀请码,或邀请码已经过期。";
+					exit;
+				}
+				$invite_email = $redis->get("invitecode://".$_REQUEST["invite"]);				
+			?>
+			</div>
+		<div class="login_form" style="    padding: 3em 0 3em 0;">
+			<div class="form_help" id="message"> </div>	
+			<div id="form_div">
+				<form id="user_create_form" action="#" method="post">
+					<div>
+						<div>
+							<span id='tip_username' class='form_field_name'><?php echo $_local->gui->account; ?></span>
+							<input type="input" id="username" name="username"  value="" />
+						</div>
+						<div id="error_username" class="form_error"> </div>
+						<div class="form_help"> <?php echo $_local->gui->account_demond; ?> </div>
+					</div>
+
+					<div>
+						<span id='tip_email' class='form_field_name'><?php echo $_local->gui->email_address; ?></span>
+						<input type="input" id="email" name="email"  value="<?php echo $invite_email; ?>" />
+						<div id="error_email" class="form_error"> </div>
+					</div>
+
+					<div>
+						<div>
+							<span id='tip_password' class='form_field_name'><?php echo $_local->gui->password; ?></span>
+							<input type="password" id="password" name="password"  value="" />
+							<input type="password" id="repassword" name="repassword" placeholder="再次输入密码" value="" />
+						</div>
+						<div class="form_help">
+						<?php echo $_local->gui->password_demond; ?>
+						</div>
+						<div id="error_password" class="form_error"> </div>
+					</div>
+
+						<div>
+							<span id='tip_language' class='viewswitch_on'><?php echo "惯常使用的语言"; ?></span>
+							<select name="lang" name="language" style="width: 100%;">
+							<?php
+							$currLang = $_COOKIE["language"];
+							$langList = [
+											"en"=>$_local->language->en,
+											"zh-cn"=>$_local->language->zh_cn,
+											"zh-tw"=>$_local->language->zh_tw,
+											"my"=>$_local->language->my,
+											"si"=>$_local->language->si,
+							];
+							foreach ($langList as $key => $value) {
+								# code...
+								if($currLang==$key){
+									$selected = " selected";
+								}else{
+									$selected = "";
+								}
+								echo "<option value='{$key}' {$selected}>{$value}</option>";
+							}
+							?>
+							</select>
+						</div>
+						
+						<div>
+							<div>
+								<span id='tip_nickname' class='form_field_name'><?php echo $_local->gui->nick_name; ?></span>
+								<input type="input" id="nickname" name="nickname" value="" />
+							</div>
+							<div class="form_help">
+							<?php echo $_local->gui->name_for_show; ?>
+							</div>
+							<div id="error_password" class="form_error"> </div>
+						</div>
+
+						<input type="hidden" id="invite" name="invite" value="<?php echo $_REQUEST["invite"]; ?>" />
+				</form>
+				<div id="button_area">
+					<button  onclick="submit()" style="background-color: var(--link-hover-color);border-color: var(--link-hover-color);" >
+					<?php echo $_local->gui->continue; ?>
+					</button>
+				</div>	
+			</div>	
+		</div>
+			<?php
+			}
+			?>
+		</div>
+	</div>
+</div>
+
+<script>
+	login_init();
+
+
+	
+	
+	
+</script>
+
+	</body>
+</html>