user.php 11 KB

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