user.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. $redisKey = "invitecode://".$data["invite"];
  80. $code = $this->redis->exists($redisKey);
  81. if(!$code){
  82. $this->result["ok"]=false;
  83. $this->result["message"]="invite_code_invalid";
  84. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  85. return;
  86. }
  87. $data["email"] = $this->redis->get($redisKey);
  88. }else{
  89. $this->result["ok"]=false;
  90. $this->result["message"]="no_invite_code";
  91. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  92. return;
  93. }
  94. //验证用户名有效性
  95. if(!$this->isValidUsername($data["username"])){
  96. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  97. return;
  98. }
  99. $isExist = $this->medoo->has($this->table,["username"=>$data["username"]]);
  100. if(!$isExist){
  101. if(!$this->isValidEmail($data["email"])){
  102. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  103. return;
  104. }
  105. $isExist = $this->medoo->has($this->table,["email"=>$data["email"]]);
  106. if(!$isExist){
  107. if(!$this->isValidPassword($data["password"])){
  108. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  109. return;
  110. }
  111. $data["userid"] = UUID::v4();
  112. $data["password"] = md5($data["password"]);
  113. $data["create_time"] = mTime();
  114. $data["modify_time"] = mTime();
  115. $data["setting"] = "{}";
  116. $result = $this->_create($data,["userid","username","email","password","nickname","setting","create_time","modify_time"]);
  117. if($result["ok"]){
  118. $channel = new Channel($this->redis);
  119. $newChannel1 = $channel->create(["owner"=>$data["userid"],
  120. "lang"=>$data["lang"],
  121. "name"=>$data["username"],
  122. "lang"=>$data["lang"],
  123. "status"=>30,
  124. "summary"=>""
  125. ]);
  126. $newChannel2 = $channel->create(["owner"=>$data["userid"],
  127. "lang"=>$data["lang"],
  128. "name"=>"draft",
  129. "lang"=>$data["lang"],
  130. "status"=>10,
  131. "summary"=>""
  132. ]);
  133. echo json_encode($newChannel1, JSON_UNESCAPED_UNICODE);
  134. //删除
  135. $this->redis->del($redisKey);
  136. }else{
  137. echo json_encode($result, JSON_UNESCAPED_UNICODE);
  138. }
  139. }else{
  140. $this->result["ok"]=false;
  141. $this->result["message"]="email_is_exist";
  142. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  143. }
  144. }
  145. else{
  146. $this->result["ok"]=false;
  147. $this->result["message"]="account_is_exist";
  148. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  149. }
  150. }
  151. #发送密码重置邮件
  152. public function reset_password_send_email(){
  153. $email = $_GET["email"];
  154. $isExist = $this->medoo->has($this->table,["email"=>$email]);
  155. if($isExist){
  156. $resetToken = UUID::v4();
  157. $ok = $this->_update(["reset_password_token"=>$resetToken],["reset_password_token"],["email"=>$email]);
  158. if($ok){
  159. #send email
  160. $resetLink="https://www.wikipali.org/app/ucenter/reset.php?token=".$resetToken;
  161. $resetString="https://www.wikipali.org/app/ucenter/reset.php";
  162. // 打开文件并读取数据
  163. $irow=0;
  164. $strSubject = "";
  165. $strBody = "";
  166. if(($fp=fopen("../ucenter/reset_pwd_letter.html", "r"))!==FALSE){
  167. while(($data=fgets($fp))!==FALSE){
  168. $irow++;
  169. if($irow==1){
  170. $strSubject = $data;
  171. }else{
  172. $strBody .= $data;
  173. }
  174. }
  175. fclose($fp);
  176. }
  177. else{
  178. $this->result["ok"] = false;
  179. $this->result["message"] = "can not load email file.";
  180. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  181. return;
  182. }
  183. $strBody = str_replace("%ResetLink%",$resetLink,$strBody);
  184. $strBody = str_replace("%ResetString%",$resetString,$strBody);
  185. //TODO sendmail
  186. //Create an instance; passing `true` enables exceptions
  187. $mail = new PHPMailer(true);
  188. try {
  189. //Server settings
  190. $mail->SMTPDebug = SMTP::DEBUG_OFF; //Enable verbose debug output
  191. $mail->isSMTP(); //Send using SMTP
  192. $mail->Host = Email["Host"]; //Set the SMTP server to send through
  193. $mail->SMTPAuth = Email["SMTPAuth"]; //Enable SMTP authentication
  194. $mail->Username = Email["Username"]; //SMTP username
  195. $mail->Password = Email["Password"]; //SMTP password
  196. $mail->SMTPSecure = Email["SMTPSecure"]; //Enable implicit TLS encryption
  197. $mail->Port = Email["Port"]; //TCP port to connect to 465; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
  198. //Recipients
  199. $mail->setFrom(Email["From"], Email["Sender"]);
  200. $mail->addAddress($email); //Add a recipient Name is optional
  201. //Content
  202. $mail->isHTML(true); //Set email format to HTML
  203. $mail->Subject = $strSubject;
  204. $mail->Body = $strBody;
  205. $mail->AltBody = $strBody;
  206. $mail->send();
  207. #邮件发送成功,修改数据库
  208. $this->_update(["reset_password_sent_at"=>Medoo::raw('datetime(<now>)')],["reset_password_sent_at"],["email"=>$email]);
  209. //邮件地址脱敏
  210. $show_email = mb_substr($email,0,2,"UTF-8") . "****" . strstr($email,"@");
  211. $this->result["message"] = 'Message has been sent to your email : ' . $show_email;
  212. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  213. return;
  214. } catch (Exception $e) {
  215. $this->result["ok"] = false;
  216. $this->result["message"] = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  217. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  218. return;
  219. }
  220. }else{
  221. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  222. return;
  223. }
  224. }else{
  225. $this->result["ok"]=false;
  226. $this->result["message"]="::invalid_email";
  227. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  228. }
  229. }
  230. #重置密码
  231. public function reset_password(){
  232. $json = file_get_contents('php://input');
  233. $data = json_decode($json,true);
  234. $isExist = $this->medoo->has($this->table,["username"=>$data["username"],"reset_password_token"=>$data["reset_password_token"]]);
  235. if($isExist){
  236. #reset password
  237. if(!$this->isValidPassword($data["password"])){
  238. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  239. return;
  240. }
  241. $ok = $this->_update(["password"=>md5($data["password"])],["password"],["username"=>$data["username"]]);
  242. if($ok){
  243. #成功后删除reset_password_token
  244. $ok = $this->_update(["reset_password_token"=>null,
  245. "reset_password_sent_at"=>null],
  246. null,
  247. ["username"=>$data["username"]]);
  248. }
  249. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  250. }else{
  251. $this->result["ok"]=false;
  252. $this->result["message"]="::invalid_token";
  253. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  254. }
  255. }
  256. private function isValidPassword($password){
  257. if(mb_strlen($password,"UTF-8")<6){
  258. $this->result["ok"]=false;
  259. $this->result["message"]="::password_too_short";
  260. return false;
  261. }
  262. if(mb_strlen($password,"UTF-8")>32){
  263. $this->result["ok"]=false;
  264. $this->result["message"]="::password_too_long";
  265. return false;
  266. }
  267. if(strpos($password," ")!==false){
  268. $this->result["ok"]=false;
  269. $this->result["message"]="::password_invaild_symbol";
  270. return false;
  271. }
  272. return true;
  273. }
  274. private function isValidUsername($username){
  275. if(mb_strlen($username,"UTF-8")>32){
  276. $this->result["ok"]=false;
  277. $this->result["message"]="::username_too_long";
  278. return false;
  279. }
  280. if(mb_strlen($username,"UTF-8")<4){
  281. $this->result["ok"]=false;
  282. $this->result["message"]="::username_too_short";
  283. return false;
  284. }
  285. if(preg_match("/@|\s|\//",$username)!==0){
  286. $this->result["ok"]=false;
  287. $this->result["message"]="::username_invaild_symbol";
  288. return false;
  289. }
  290. return true;
  291. }
  292. private function isValidEmail($email){
  293. $isValid = filter_var($email, FILTER_VALIDATE_EMAIL);
  294. if($isValid===false){
  295. $this->result["ok"]=false;
  296. $this->result["message"]="::invaild_email";
  297. }
  298. return $isValid;
  299. }
  300. }
  301. ?>