user.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. require_once "../path.php";
  3. require_once "../db/table.php";
  4. require_once "../public/function.php";
  5. // Require Composer's autoloader.
  6. require_once '../../vendor/autoload.php';
  7. require_once '../config.php';
  8. // Using Medoo namespace.
  9. use Medoo\Medoo;
  10. // Require Composer's autoloader.
  11. use PHPMailer\PHPMailer\PHPMailer;
  12. use PHPMailer\PHPMailer\SMTP;
  13. use PHPMailer\PHPMailer\Exception;
  14. /*
  15. CREATE TABLE users (
  16. id INTEGER PRIMARY KEY AUTOINCREMENT,
  17. like_type VARCHAR (16) NOT NULL,
  18. resource_type VARCHAR (32) NOT NULL,
  19. resource_id CHAR (36) NOT NULL,
  20. user_id INTEGER NOT NULL,
  21. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL //只做初始化,更新时不自动更新
  22. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL //自动更新
  23. );
  24. */
  25. class User extends Table
  26. {
  27. function __construct($redis=false) {
  28. parent::__construct(_FILE_DB_USERINFO_, "user", "", "",$redis);
  29. }
  30. public function index(){
  31. $where["like_type"] = "like";
  32. $where["resource_type"] = $_GET["type"];
  33. $where["resource_id"] = explode($_GET["id"],",");
  34. echo json_encode($this->_index(["resource_id","user_id"],$where), JSON_UNESCAPED_UNICODE);
  35. }
  36. public function list(){
  37. if(!isset($_COOKIE["userid"])){
  38. $userId = $_COOKIE["userid"];
  39. }
  40. $json = file_get_contents('php://input');
  41. $data = json_decode($json,true);
  42. foreach ($data as $key => $value) {
  43. # code...
  44. $data[$key]['like']=$this->medoo->count($this->table,[
  45. 'like_type'=>$value['like_type'],
  46. 'resource_type'=>$value['resource_type'],
  47. 'resource_id'=>$value['resource_id'],
  48. ]);
  49. }
  50. if(isset($_COOKIE["userid"])){
  51. $userId = $_COOKIE["userid"];
  52. foreach ($data as $key => $value) {
  53. # code...
  54. $data[$key]['me']=$this->medoo->count($this->table,[
  55. 'like_type'=>$value['like_type'],
  56. 'resource_type'=>$value['resource_type'],
  57. 'resource_id'=>$value['resource_id'],
  58. 'user_id'=>$userId,
  59. ]);
  60. }
  61. }
  62. $this->result["ok"]=true;
  63. $this->result["message"]="";
  64. $this->result["data"]=$data;
  65. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  66. }
  67. public function create(){
  68. $json = file_get_contents('php://input');
  69. $data = json_decode($json,true);
  70. //验证邀请码
  71. if(isset($data["invite"])){
  72. if ($this->redis == false) {
  73. $this->result["ok"]=false;
  74. $this->result["message"]="no_redis_connect";
  75. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  76. return;
  77. }
  78. $code = $this->redis->exists("invitecode://".$data["invite"]);
  79. if(!$code){
  80. $this->result["ok"]=false;
  81. $this->result["message"]="invite_code_invalid";
  82. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  83. return;
  84. }
  85. }else{
  86. $this->result["ok"]=false;
  87. $this->result["message"]="no_invite_code";
  88. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  89. return;
  90. }
  91. //验证用户名有效性
  92. if(!$this->isValidUsername($data["username"])){
  93. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  94. return;
  95. }
  96. $isExist = $this->medoo->has($this->table,["username"=>$data["username"]]);
  97. if(!$isExist){
  98. if(!$this->isValidEmail($data["email"])){
  99. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  100. return;
  101. }
  102. $isExist = $this->medoo->has($this->table,["email"=>$data["email"]]);
  103. if(!$isExist){
  104. if(!$this->isValidPassword($data["password"])){
  105. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  106. return;
  107. }
  108. $data["userid"] = UUID::v4();
  109. $data["password"] = md5($data["password"]);
  110. $data["create_time"] = mTime();
  111. $data["modify_time"] = mTime();
  112. $data["setting"] = "{}";
  113. echo json_encode($this->_create($data,["userid","username","email","password","nickname","setting","create_time","modify_time"]), JSON_UNESCAPED_UNICODE);
  114. }else{
  115. $this->result["ok"]=false;
  116. $this->result["message"]="email_is_exist";
  117. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  118. }
  119. }
  120. else{
  121. $this->result["ok"]=false;
  122. $this->result["message"]="account_is_exist";
  123. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  124. }
  125. }
  126. #发送密码重置邮件
  127. public function reset_password_send_email(){
  128. $email = $_GET["email"];
  129. $isExist = $this->medoo->has($this->table,["email"=>$email]);
  130. if($isExist){
  131. $resetToken = UUID::v4();
  132. $ok = $this->_update(["reset_password_token"=>$resetToken],["reset_password_token"],["email"=>$email]);
  133. if($ok){
  134. #send email
  135. $resetLink="https://www.wikipali.org/ucenter/reset.php?token=".$resetToken;
  136. $resetString="https://www.wikipali.org/ucenter/reset.php?token=".$resetToken;
  137. // 打开文件并读取数据
  138. $irow=0;
  139. $strSubject = "";
  140. $strBody = "";
  141. if(($fp=fopen("../ucenter/reset_pwd_letter.html", "r"))!==FALSE){
  142. while(($data=fgets($fp))!==FALSE){
  143. $irow++;
  144. if($irow==1){
  145. $strSubject = $data;
  146. }else{
  147. $strBody .= $data;
  148. }
  149. }
  150. fclose($fp);
  151. }
  152. else{
  153. $this->result["ok"] = false;
  154. $this->result["message"] = "can not load email file.";
  155. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  156. return;
  157. }
  158. $strBody = str_replace("%resetLink%",$resetLink,$strBody);
  159. $strBody = str_replace("%resetString%",$resetString,$strBody);
  160. //TODO sendmail
  161. //Create an instance; passing `true` enables exceptions
  162. $mail = new PHPMailer(true);
  163. try {
  164. //Server settings
  165. $mail->SMTPDebug = SMTP::DEBUG_OFF; //Enable verbose debug output
  166. $mail->isSMTP(); //Send using SMTP
  167. $mail->Host = Email["Host"]; //Set the SMTP server to send through
  168. $mail->SMTPAuth = Email["SMTPAuth"]; //Enable SMTP authentication
  169. $mail->Username = Email["Username"]; //SMTP username
  170. $mail->Password = Email["Password"]; //SMTP password
  171. $mail->SMTPSecure = Email["SMTPSecure"]; //Enable implicit TLS encryption
  172. $mail->Port = Email["Port"]; //TCP port to connect to 465; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
  173. //Recipients
  174. $mail->setFrom(Email["From"], Email["Sender"]);
  175. $mail->addAddress($email); //Add a recipient Name is optional
  176. //Content
  177. $mail->isHTML(true); //Set email format to HTML
  178. $mail->Subject = $strSubject;
  179. $mail->Body = $strBody;
  180. $mail->AltBody = $strBody;
  181. $mail->send();
  182. #邮件发送成功,修改数据库
  183. $this->_update(["reset_password_sent_at"=>Medoo::raw('datetime(<now>)')],["reset_password_sent_at"],["email"=>$email]);
  184. //邮件地址脱敏
  185. $show_email = mb_substr($email,0,2,"UTF-8") . "****" . strstr($email,"@");
  186. $this->result["message"] = 'Message has been sent to your email : ' . $show_email;
  187. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  188. return;
  189. } catch (Exception $e) {
  190. $this->result["ok"] = false;
  191. $this->result["message"] = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  192. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  193. return;
  194. }
  195. }else{
  196. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  197. return;
  198. }
  199. }else{
  200. $this->result["ok"]=false;
  201. $this->result["message"]="invalid email";
  202. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  203. }
  204. }
  205. #重置密码
  206. public function reset_password(){
  207. $json = file_get_contents('php://input');
  208. $data = json_decode($json,true);
  209. $isExist = $this->medoo->has($this->table,["username"=>$data["username"],"reset_password_token"=>$data["reset_password_token"]]);
  210. if($isExist){
  211. #reset password
  212. if(!isValidPassword($data["password"])){
  213. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  214. return;
  215. }
  216. $ok = $this->_update(["password"=>md5($data["password"])],["password"],["username"=>$data["username"]]);
  217. if($ok){
  218. #成功后删除reset_password_token
  219. $ok = $this->_update(["reset_password_token"=>null,
  220. "reset_password_sent_at"=>null],
  221. null,
  222. ["username"=>$data["username"]]);
  223. }
  224. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  225. }else{
  226. $this->result["ok"]=false;
  227. $this->result["message"]="invalid_token";
  228. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  229. }
  230. }
  231. private function isValidPassword($password){
  232. if(mb_strlen($password,"UTF-8")<6){
  233. $this->result["ok"]=false;
  234. $this->result["message"]="password_too_short";
  235. return false;
  236. }
  237. if(mb_strlen($password,"UTF-8")>32){
  238. $this->result["ok"]=false;
  239. $this->result["message"]="password_too_long";
  240. return false;
  241. }
  242. if(strpos($password," ")!==false){
  243. $this->result["ok"]=false;
  244. $this->result["message"]="can_not_space";
  245. return false;
  246. }
  247. return true;
  248. }
  249. private function isValidUsername($username){
  250. if(mb_strlen($username,"UTF-8")>32){
  251. $this->result["ok"]=false;
  252. $this->result["message"]="username_too_long";
  253. return false;
  254. }
  255. if(preg_match("/@|\s|\//",$username)!==0){
  256. $this->result["ok"]=false;
  257. $this->result["message"]="char_error";
  258. return false;
  259. }
  260. return true;
  261. }
  262. private function isValidEmail($email){
  263. $isValid = filter_var($email, FILTER_VALIDATE_EMAIL);
  264. if($isValid===false){
  265. $this->result["ok"]=false;
  266. $this->result["message"]="email_format_error";
  267. }
  268. return $isValid;
  269. }
  270. }
  271. ?>