lost_password.lib.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // $Id: lost_password.lib.php 9246 2006-09-25 13:24:53Z bmol $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) various contributors
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /*** By Olivier Cauberghe, UGent ***/
  21. //-----------------------------------------------------------------------------
  22. function get_email_headers()
  23. {
  24. global $charset;
  25. $emailHeaders = "From: \"".addslashes(get_setting('administratorSurname')." ".get_setting('administratorName'))."\" <".get_setting('emailAdministrator').">\n";
  26. $emailHeaders .= "Reply-To: ".get_setting('emailAdministrator')."\n";
  27. $emailHeaders .= "X-Sender: ".get_setting('emailAdministrator')."\n";
  28. $emailHeaders .= "X-Mailer: PHP / ".phpversion()."\n";
  29. $emailHeaders .= "Content-Type: text/plain;\n\tcharset=\"".$charset."\"\n";
  30. $emailHeaders .= "Mime-Version: 1.0";
  31. return $emailHeaders;
  32. }
  33. /*** By Olivier Cauberghe, UGent ***/
  34. //-----------------------------------------------------------------------------
  35. function get_user_account_list($user, $reset = false)
  36. {
  37. global $rootWeb;
  38. foreach ($user as $thisUser)
  39. {
  40. $secretword = get_secret_word($thisUser["email"]);
  41. if ($reset)
  42. $reset_link = "\tReset link : $rootWeb"."main/auth/lostPassword.php?reset=$secretword&id=$thisUser[uid]";
  43. else
  44. $reset_link = "\t".get_lang('Pass')." : $thisUser[password]";
  45. $userAccountList[] = $thisUser["firstName"]." ".$thisUser["lastName"]."\n\n"."\t".get_lang('Username')." : ".$thisUser["loginName"]."\n"."$reset_link\n\n";
  46. }
  47. if ($userAccountList)
  48. $userAccountList = implode("------------------------\n", $userAccountList);
  49. return $userAccountList;
  50. }
  51. /*** By Olivier Cauberghe, UGent ***/
  52. //-----------------------------------------------------------------------------
  53. function send_password_to_user($user, $success_msg)
  54. {
  55. global $charset;
  56. global $rootWeb;
  57. $emailHeaders = get_email_headers(); // Email Headers
  58. $emailSubject = "[".get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  59. $userAccountList = get_user_account_list($user); // BODY
  60. $emailBody = get_lang('YourAccountParam')." $rootWeb\n\n$userAccountList";
  61. // SEND MESSAGE
  62. $emailTo = $user[0]["email"];
  63. if (@ api_send_mail($emailTo, $emailSubject, $emailBody, $emailHeaders))
  64. return $success_msg;
  65. else
  66. return "<p>The system is unable to send you an e-mail.<br/>Please contact the ".Display :: encrypted_mailto_link(get_setting('emailAdministrator'), "Platform administrator").".</p>";
  67. }
  68. //-----------------------------------------------------------------------------
  69. /*** By Olivier Cauberghe, UGent ***/
  70. function handle_encrypted_password($user)
  71. {
  72. global $security_key;
  73. global $charset;
  74. global $rootWeb;
  75. $emailHeaders = get_email_headers(); // Email Headers
  76. $emailSubject = "[".get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  77. $userAccountList = get_user_account_list($user, true); // BODY
  78. $emailTo = $user[0]["email"];
  79. $secretword = get_secret_word($emailTo);
  80. $emailBody = get_lang("password_request")."\n\n\n".get_lang("YourAccountParam")." $rootWeb\n\n".$userAccountList;
  81. if (@ api_send_mail($emailTo, $emailSubject, $emailBody, $emailHeaders))
  82. return get_lang("lang_your_password_has_been_emailed_to_you");
  83. else
  84. echo "<p>", "The system is unable to send you an e-mail.<br/>", "Please contact the ", Display::encrypted_mailto_link(get_setting('emailAdministrator'),"platform administrator"), ".<p>";
  85. return "";
  86. }
  87. //-----------------------------------------------------------------------------
  88. function get_secret_word($add)
  89. {
  90. global $security_key;
  91. return $secretword = md5($security_key.$add);
  92. }
  93. //-----------------------------------------------------------------------------
  94. function reset_password($secret, $id)
  95. {
  96. global $your_password_has_been_reset,$userPasswordCrypted;
  97. $tbl_user = Database::get_main_table(MAIN_USER_TABLE);
  98. $sql = "SELECT user_id AS uid, lastname AS lastName, firstname AS firstName, username AS loginName, password, email FROM ".$tbl_user." WHERE user_id=$id";
  99. $result = api_sql_query($sql,__FILE__,__LINE__);
  100. if ($result && mysql_num_rows($result))
  101. $user[] = mysql_fetch_array($result);
  102. else
  103. return "Could not reset password.";
  104. if (get_secret_word($user[0]["email"]) == $secret) // OK, secret word is good. Now change password and mail it.
  105. {
  106. $user[0]["password"] = api_generate_password();
  107. $crypted = $user[0]["password"];
  108. if( $userPasswordCrypted)
  109. {
  110. $crypted = md5($crypted);
  111. }
  112. api_sql_query("UPDATE ".$tbl_user." SET password='$crypted' WHERE user_id=$id");
  113. return send_password_to_user($user, $your_password_has_been_reset);
  114. }
  115. else
  116. return "Not allowed.";
  117. }
  118. ?>