PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mongo/reset.php

https://github.com/teffalump/ShareHere
PHP | 99 lines | 64 code | 17 blank | 18 comment | 8 complexity | 2e9f46184b8a5983e0065617acc755d3 MD5 | raw file
  1. <?php
  2. /* Resets a password through an email (sends it, etc)
  3. Required fields
  4. Send email:
  5. $_POST['email']
  6. Reset:
  7. $_GET['email']
  8. $_GET['dd']
  9. Return values
  10. 0 - mailed successfully
  11. 1 - unsuccessful or invalid
  12. 2 - required fields not set
  13. */
  14. if (isset($_POST['email']))
  15. {
  16. require_once "general.php";
  17. require_once "connection.php";
  18. require_once "variables.php";
  19. $email = $_POST['email'];
  20. $filter = array( "email" => $email );
  21. $fields = array( "name" => true);
  22. $info = $db->USERS->findOne($filter, $fields);
  23. $db->close();
  24. if (is_null($info))
  25. {
  26. echo 1;
  27. exit;
  28. }
  29. //Generate hash and send email
  30. $secret = $info["_id"] . date("Ymd");
  31. $reset_hash=generateHash($secret, EMAIL_SALT, EMAIL_LINK_HASH_LENGTH);
  32. $subject="Reset password for Sharehere.net";
  33. $url=vsprintf("http://localhost/~chris/reset.php?email=%s&dd=%s", array(urlencode($email), $reset_hash));
  34. $message=vsprintf("Hiya %s!\n\nYou wanted to reset your password. To reset your password follow this link...\n%s\n\nAll the best,\n\nSharehere.net", array($info["name"], $url));
  35. $headers = 'From: share@sharehere.net'."\r\n"
  36. .'Content-type: text/plain; charset=utf-8' . "\r\n";
  37. mail($to,$subject,$message,$headers);
  38. echo 0;
  39. exit;
  40. }
  41. elseif (isset($_GET['email']) && isset($_GET['dd']))
  42. {
  43. require_once "general.php";
  44. require_once "connection.php";
  45. require_once "variables.php";
  46. $email = $_GET['email'];
  47. $filter = array ("email" => $email );
  48. $fields = array ("_id" => 1 );
  49. $id = getvalue($db->findOne($filter, $fields), "_id");
  50. if (isset($id))
  51. {
  52. $email_link_hash=$_GET['dd'];
  53. $secret = $id . date("Ymd");
  54. $hash=generateHash($secret, EMAIL_SALT, EMAIL_LINK_HASH_LENGTH)
  55. if ( $hash === $email_link_hash )
  56. {
  57. $new_password=substr(md5(uniqid(mt_rand(), true)), 0, 15);
  58. $new_hash = generateHash($new_password, USER_SALT);
  59. $fields = array ('$set' => array( "password" => $new_hash ) );
  60. $db->USERS->update($filter, $fields);
  61. if ( getvalue($db->lastError(), "updateExisting") )
  62. {
  63. //$subject="New password for Sharehere";
  64. //$message=vsprintf("Hiya!\n\nThis is your new password for Sharehere: %s\n\nYou can change it at your homepage.\n\nAll the best,\n\nSharehere.net\n\nPS: Upon logging in, you'll be required to change the password.", array($new_password));
  65. //$headers = 'From: share@sharehere.net'."\r\n"
  66. // .'Content-type: text/plain; charset=utf-8' . "\r\n";
  67. //mail($email,$subject,$message,$headers);
  68. echo 0;
  69. unset($new_password);
  70. exit;
  71. }
  72. }
  73. }
  74. echo 1;
  75. exit;
  76. }
  77. else
  78. {
  79. echo 2;
  80. exit;
  81. }
  82. ?>