user.php 10 KB

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