user.php 7.0 KB

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