forgot.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace WY\app\controller;
  3. use WY\app\libs\Controller;
  4. if (!defined('WY_ROOT')) {
  5. exit;
  6. }
  7. class forgot extends Controller
  8. {
  9. function __construct()
  10. {
  11. parent::__construct();
  12. if ($this->req->session('login_userid')) {
  13. $this->res->redirect('/user');
  14. }
  15. }
  16. public function index()
  17. {
  18. $data = array('title' => '找回密码');
  19. $this->put('forgot.php', $data);
  20. }
  21. public function send()
  22. {
  23. $username = $this->req->post('username');
  24. $email = $this->req->post('email');
  25. $chkcode = $this->req->post('chkcode');
  26. if ($username == '' || $email == '' || $chkcode == '') {
  27. echo json_encode(array('status' => 1, 'msg' => '选项填写不完整'));
  28. exit;
  29. }
  30. if (strtolower($chkcode) != $this->session->get('chkcode')) {
  31. echo json_encode(array('status' => 1, 'msg' => '验证码错误'));
  32. exit;
  33. }
  34. $info = array('fields' => 'email=?', 'values' => array($email));
  35. $where = array('fields' => 'username=?', 'values' => array($username));
  36. if (!($userinfo = $this->model()->select('userid')->from('userinfo')->where($info)->fetchRow() || !($user = $this->model()->select()->from('users')->where($where)->fetchRow()))) {
  37. echo json_encode(array('status' => 1, 'msg' => '用户名和邮箱账号不匹配'));
  38. exit;
  39. }
  40. $data = array('token' => sha1($this->res->getRandomString(40)));
  41. if ($mailtpl = $this->model()->select()->from('mailtpl')->where(array('fields' => 'is_state=? and cname=?', 'values' => array(0, '找回密码')))->fetchRow()) {
  42. $orginData = array('sitename' => $this->config['sitename'], 'username' => $username, 'url' => 'http://' . $this->config['siteurl'] . '/forgot/retpwd/' . $data['token']);
  43. $newData = $this->res->replaceMailTpl($mailtpl, $orginData);
  44. $subject = array('email' => $email, 'title' => $newData['title'], 'content' => $newData['content']);
  45. $this->res->sendMail($subject, $this->config);
  46. if ($this->model()->from('users')->updateSet($data)->where($where)->update()) {
  47. echo json_encode(array('status' => 1, 'msg' => '重置确认邮件已发送,请登录邮箱查收', 'url' => '/login'));
  48. exit;
  49. }
  50. }
  51. echo json_encode(array('status' => 1, 'msg' => '重置失败,用户名和邮箱账号不匹配'));
  52. exit;
  53. }
  54. public function retpwd()
  55. {
  56. $token = isset($this->action[2]) ? $this->action[2] : '';
  57. if ($token == '' || !preg_match('/[0-9a-z]{40}/', $token)) {
  58. echo $this->put('woodyapp.php', array('msg' => '来源错误'));
  59. exit;
  60. }
  61. if (!($user = $this->model()->select()->from('users')->where(array('fields' => 'token=?', 'values' => array($token)))->fetchRow())) {
  62. echo $this->put('woodyapp.php', array('msg' => '无此用户记录'));
  63. exit;
  64. }
  65. $this->put('retpwd.php', $data = array('title' => '重置密码', 'token' => $token));
  66. }
  67. public function save()
  68. {
  69. $token = $this->req->post('token');
  70. $username = $this->req->post('username');
  71. $password = $this->req->post('password');
  72. $cirmpwd = $this->req->post('cirmpwd');
  73. $chkcode = $this->req->post('chkcode');
  74. if ($token == '' || $username == '' || $password == '' || $cirmpwd == '' || $chkcode == '') {
  75. echo json_encode(array('status' => 1, 'msg' => '选项填写不完整'));
  76. exit;
  77. }
  78. if (strtolower($chkcode) != $this->session->get('chkcode')) {
  79. echo json_encode(array('status' => 1, 'msg' => '验证错误'));
  80. exit;
  81. }
  82. if (strlen($password) < 6 || strlen($password) > 20) {
  83. echo json_encode(array('status' => 1, 'msg' => '密码长度在6-20位长度之间'));
  84. exit;
  85. }
  86. if ($password != $cirmpwd) {
  87. echo json_encode(array('status' => 1, 'msg' => '两次填写的密码不匹配'));
  88. exit;
  89. }
  90. $where = array('fields' => 'username=? and token=?', 'values' => array($username, $token));
  91. if ($user = $this->model()->select('id')->from('users')->where($where)->fetchRow()) {
  92. $data = array('token' => sha1($this->res->getRandomString(40)), 'userpass' => sha1($password));
  93. if ($this->model()->from('users')->updateSet($data)->where(array('fields' => 'id=?', 'values' => array($user['id'])))->update()) {
  94. echo json_encode(array('status' => 1, 'msg' => '密码重置成功', 'url' => '/login'));
  95. exit;
  96. }
  97. }
  98. echo json_encode(array('status' => 1, 'msg' => '密码重置失败'));
  99. exit;
  100. }
  101. }
  102. ?>