user.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if(!isset($_COOKIE["userid"])){
  69. return;
  70. }
  71. $json = file_get_contents('php://input');
  72. $data = json_decode($json,true);
  73. $data["user_id"] = $_COOKIE["userid"];
  74. $isExist = $this->medoo->has("likes",$data);
  75. if(!$isExist){
  76. echo json_encode($this->_create($data,["like_type","resource_type","resource_id","user_id"]), JSON_UNESCAPED_UNICODE);
  77. }
  78. else{
  79. $this->result["ok"]=false;
  80. $this->result["message"]="is exist";
  81. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  82. }
  83. }
  84. public function delete(){
  85. if(!isset($_COOKIE["userid"])){
  86. return;
  87. }
  88. $where["like_type"] = $_GET["like_type"];
  89. $where["resource_type"] = $_GET["resource_type"];
  90. $where["resource_id"] = $_GET["resource_id"];
  91. $where["user_id"] = $_COOKIE["userid"];
  92. $row = $this->_delete($where);
  93. if($row["data"]>0){
  94. $this->result["data"] = $where;
  95. }else{
  96. $this->result["ok"]=false;
  97. $this->result["message"]="no delete";
  98. }
  99. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  100. }
  101. #发送密码重置邮件
  102. public function reset_password_send_email(){
  103. $email = $_GET["email"];
  104. $isExist = $this->medoo->has($this->table,["email"=>$email]);
  105. if($isExist){
  106. $resetToken = UUID::v4();
  107. $ok = $this->_update(["reset_password_token"=>$resetToken],["reset_password_token"],["email"=>$email]);
  108. if($ok){
  109. #send email
  110. $resetLink="https://www.wikipali.org/ucenter/reset.php?token=".$resetToken;
  111. $resetString="https://www.wikipali.org/ucenter/reset.php?token=".$resetToken;
  112. // 打开文件并读取数据
  113. $irow=0;
  114. $strSubject = "";
  115. $strBody = "";
  116. if(($fp=fopen("../ucenter/reset_pwd_letter.html", "r"))!==FALSE){
  117. while(($data=fgets($fp))!==FALSE){
  118. $irow++;
  119. if($irow==1){
  120. $strSubject = $data;
  121. }else{
  122. $strBody .= $data;
  123. }
  124. }
  125. fclose($fp);
  126. }
  127. else{
  128. $this->result["ok"] = false;
  129. $this->result["message"] = "can not load email file.";
  130. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  131. return;
  132. }
  133. $strBody = str_replace("%resetLink%",$resetLink,$strBody);
  134. $strBody = str_replace("%resetString%",$resetString,$strBody);
  135. //TODO sendmail
  136. //Create an instance; passing `true` enables exceptions
  137. $mail = new PHPMailer(true);
  138. try {
  139. //Server settings
  140. $mail->SMTPDebug = SMTP::DEBUG_OFF; //Enable verbose debug output
  141. $mail->isSMTP(); //Send using SMTP
  142. $mail->Host = Email["Host"]; //Set the SMTP server to send through
  143. $mail->SMTPAuth = Email["SMTPAuth"]; //Enable SMTP authentication
  144. $mail->Username = Email["Username"]; //SMTP username
  145. $mail->Password = Email["Password"]; //SMTP password
  146. $mail->SMTPSecure = Email["SMTPSecure"]; //Enable implicit TLS encryption
  147. $mail->Port = Email["Port"]; //TCP port to connect to 465; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
  148. //Recipients
  149. $mail->setFrom(Email["From"], Email["Sender"]);
  150. $mail->addAddress($email); //Add a recipient Name is optional
  151. //Content
  152. $mail->isHTML(true); //Set email format to HTML
  153. $mail->Subject = $strSubject;
  154. $mail->Body = $strBody;
  155. $mail->AltBody = $strBody;
  156. $mail->send();
  157. #邮件发送成功,修改数据库
  158. $this->_update(["reset_password_sent_at"=>Medoo::raw('datetime(<now>)')],["reset_password_sent_at"],["email"=>$email]);
  159. //邮件地址脱敏
  160. $show_email = mb_substr($email,0,2,"UTF-8") . "****" . strstr($email,"@");
  161. $this->result["message"] = 'Message has been sent to your email : ' . $show_email;
  162. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  163. return;
  164. } catch (Exception $e) {
  165. $this->result["ok"] = false;
  166. $this->result["message"] = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  167. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  168. return;
  169. }
  170. }else{
  171. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  172. return;
  173. }
  174. }else{
  175. $this->result["ok"]=false;
  176. $this->result["message"]="invalid email";
  177. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  178. }
  179. }
  180. #重置密码
  181. public function reset_password($username,$password,$token){
  182. $isExist = $this->medoo->has($this->table,["user_name"=>$username,"token"=>$token]);
  183. if($isExist){
  184. #reset password
  185. $ok = $this->_update(["password"=>$password],"password",["user_name"=>$username]);
  186. if($ok){
  187. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  188. }else{
  189. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  190. }
  191. }else{
  192. $this->result["ok"]=false;
  193. $this->result["message"]="invalid token";
  194. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  195. }
  196. }
  197. }
  198. ?>