reset.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once('database.php');
  16. require_once('templating.php');
  17. require_once('utils/EmailAddressValidator.php');
  18. global $adodb;
  19. $errors = '';
  20. function sendEmail($text, $email) {
  21. $subject = $site_name . ' Password Reset';
  22. return mail($email, $subject, $text);
  23. }
  24. if (isset($_GET['code'])) {
  25. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  26. $sql = 'SELECT * FROM Recovery_Request WHERE code=' . $adodb->qstr($_GET['code'])
  27. . ' AND expires > ' . $adodb->qstr(time());
  28. $row = $adodb->GetRow($sql);
  29. if (!$row) {
  30. displayError("Error", "Invalid reset token.");
  31. }
  32. $password = '';
  33. $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  34. for ($i = 0; $i < 8; $i++) {
  35. $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  36. }
  37. $email = $row['email'];
  38. $sql = 'UPDATE Users SET password=' . $adodb->qstr(md5($password)) . ' WHERE email='
  39. . $adodb->qstr($email);
  40. $adodb->Execute($sql);
  41. $content = "Hi!\n\nYour password has been set to " . $password . "\n\n - The " . $site_name . " Team";
  42. sendEmail($content, $email);
  43. $sql = 'DELETE FROM Recovery_Request WHERE code=' . $adodb->qstr($email);
  44. $adodb->Execute($sql);
  45. $smarty->assign('changed', true);
  46. } else if (isset($_POST['user']) || isset($_POST['email'])) {
  47. if (isset($_POST['email']) && !empty($_POST['email'])) {
  48. $field = 'email';
  49. $value = $_POST['email'];
  50. } else {
  51. $field = 'username';
  52. $value = $_POST['user'];
  53. }
  54. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  55. $err = 0;
  56. try {
  57. $row = $adodb->GetRow('SELECT * FROM Users WHERE lower(' . $field . ') = lower(' . $adodb->qstr($value) .')');
  58. } catch (Exception $e) {
  59. $err = 1;
  60. }
  61. if ($err || !$row) {
  62. displayError("Error", "User not found.");
  63. }
  64. $username = $row['username'];
  65. $code = md5($username . $row['email'] . time());
  66. // If a recovery_request already exists, delete it from the database
  67. $sql = 'SELECT COUNT(*) as c FROM Recovery_Request WHERE username =' .
  68. $adodb->qstr($username);
  69. try {
  70. $res = $adodb->GetRow($sql);
  71. if ($res['c'] != 0) {
  72. $sql = 'DELETE FROM Recovery_Request WHERE username =' .
  73. $adodb->qstr($username);
  74. $adodb->Execute($sql);
  75. }
  76. } catch (Exception $e) {
  77. displayError("Error", "Error on: {$sql}");
  78. }
  79. $sql = 'INSERT INTO Recovery_Request (username, email, code, expires) VALUES('
  80. . $adodb->qstr($username) . ', '
  81. . $adodb->qstr($row['email']) . ', '
  82. . $adodb->qstr($code) . ', '
  83. . $adodb->qstr(time() + 86400) . ')';
  84. try {
  85. $res = $adodb->Execute($sql);
  86. } catch (Exception $e) {
  87. displayError("Error", "Error on: {$sql}");
  88. }
  89. $url = $base_url . '/reset.php?code=' . $code;
  90. // TODO: Read names from variable
  91. $content = "Hi!\n\nSomeone requested a password reset on your account.\n\n"
  92. . "Username: {$username}\n\n"
  93. . "To reset your password, please visit\n\n"
  94. . $url . "\n\nIf you do not wish to reset your password, simply "
  95. . "disregard this email.\n\n- The " . $site_name . " Team";
  96. $status = sendEmail($content, $row['email']);
  97. if (!$status) {
  98. displayError("Error",
  99. "Error while trying to send email to: {$row['email']}. Please try again later, or contact the site administrators.");
  100. }
  101. $smarty->assign('sent', true);
  102. }
  103. $smarty->display('reset.tpl');