user.php 11 KB

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