lost_password.lib.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // $Id: lost_password.lib.php 14021 2007-12-18 19:48:14Z yannoo $
  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. /**
  21. * Enter description here...
  22. *
  23. * @return unknown
  24. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  25. */
  26. function get_email_headers()
  27. {
  28. global $charset;
  29. $emailHeaders = "From: \"".addslashes(get_setting('administratorSurname')." ".get_setting('administratorName'))."\" <".get_setting('emailAdministrator').">\n";
  30. $emailHeaders .= "Reply-To: ".get_setting('emailAdministrator')."\n";
  31. $emailHeaders .= "X-Sender: ".get_setting('emailAdministrator')."\n";
  32. $emailHeaders .= "X-Mailer: PHP / ".phpversion()."\n";
  33. $emailHeaders .= "Content-Type: text/plain;\n\tcharset=\"".$charset."\"\n";
  34. $emailHeaders .= "Mime-Version: 1.0";
  35. return $emailHeaders;
  36. }
  37. /**
  38. * Enter description here...
  39. *
  40. * @param unknown_type $user
  41. * @param unknown_type $reset
  42. * @return unknown
  43. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  44. */
  45. function get_user_account_list($user, $reset = false)
  46. {
  47. global $_configuration;
  48. foreach ($user as $thisUser)
  49. {
  50. $secretword = get_secret_word($thisUser["email"]);
  51. if ($reset)
  52. {
  53. $reset_link = $_configuration['root_web']."main/auth/lostPassword.php?reset=".$secretword."&id=".$thisUser['uid'];
  54. }
  55. else
  56. {
  57. $reset_link = get_lang('Pass')." : $thisUser[password]";
  58. }
  59. $userAccountList[] = get_lang('YourRegistrationData')." : \n".get_lang('UserName').' : '.$thisUser["loginName"]."\n".get_lang('ResetLink').' : '.$reset_link.'';
  60. }
  61. if ($userAccountList)
  62. {
  63. $userAccountList = implode("\n------------------------\n", $userAccountList);
  64. }
  65. return $userAccountList;
  66. }
  67. /**
  68. * This function sends the actual password to the user
  69. *
  70. * @param unknown_type $user
  71. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  72. */
  73. function send_password_to_user($user)
  74. {
  75. global $charset;
  76. global $_configuration;
  77. $emailHeaders = get_email_headers(); // Email Headers
  78. $emailSubject = "[".get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  79. $userAccountList = get_user_account_list($user); // BODY
  80. $emailBody = get_lang('YourAccountParam')." ".$_configuration['root_web']."\n\n$userAccountList";
  81. // SEND MESSAGE
  82. $emailTo = $user[0]["email"];
  83. if (@ api_send_mail($emailTo, $emailSubject, $emailBody, $emailHeaders))
  84. {
  85. Display::display_confirmation_message(get_lang('YourPasswordHasBeenEmailed'));
  86. }
  87. else
  88. {
  89. $message = get_lang('SystemUnableToSendEmailContact') . Display :: encrypted_mailto_link(get_setting('emailAdministrator'), get_lang('PlatformAdmin')).".</p>";
  90. Display::display_error_message($message, false);
  91. }
  92. }
  93. /**
  94. * Enter description here...
  95. *
  96. * @param unknown_type $user
  97. * @return unknown
  98. *
  99. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  100. */
  101. function handle_encrypted_password($user)
  102. {
  103. global $charset;
  104. global $_configuration;
  105. $emailHeaders = get_email_headers(); // Email Headers
  106. $emailSubject = "[".get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  107. $userAccountList = get_user_account_list($user, true); // BODY
  108. $emailTo = $user[0]["email"];
  109. $secretword = get_secret_word($emailTo);
  110. //$emailBody = get_lang("password_request")."\n\n\n".get_lang("YourAccountParam")." ".$_configuration['root_web']."\n\n".$userAccountList;
  111. $emailBody = get_lang('DearUser')." :\n".get_lang("password_request")."\n\n";
  112. $emailBody .= "-----------------------------------------------\n".$userAccountList."\n-----------------------------------------------\n\n";
  113. $emailBody .=get_lang('PasswordEncryptedForSecurity');
  114. $emailBody .="\n\n".get_lang('Formula').",\n".get_lang('PlataformAdmin');
  115. if (@ api_send_mail($emailTo, $emailSubject, $emailBody, $emailHeaders))
  116. {
  117. Display::display_confirmation_message(get_lang('YourPasswordHasBeenEmailed'));
  118. }
  119. else
  120. {
  121. $message = get_lang('SystemUnableToSendEmailContact') . Display :: encrypted_mailto_link(get_setting('emailAdministrator'), get_lang('PlatformAdmin')).".</p>";
  122. Display::display_error_message($message, false);
  123. }
  124. }
  125. /**
  126. * Enter description here...
  127. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  128. */
  129. function get_secret_word($add)
  130. {
  131. global $_configuration;
  132. return $secretword = md5($_configuration['security_key'].$add);
  133. }
  134. /**
  135. * Enter description here...
  136. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  137. */
  138. function reset_password($secret, $id)
  139. {
  140. global $your_password_has_been_reset,$userPasswordCrypted;
  141. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  142. $id = (int) $id;
  143. $sql = "SELECT user_id AS uid, lastname AS lastName, firstname AS firstName, username AS loginName, password, email FROM ".$tbl_user." WHERE user_id=$id";
  144. $result = api_sql_query($sql,__FILE__,__LINE__);
  145. if ($result && mysql_num_rows($result))
  146. {
  147. $user[] = mysql_fetch_array($result);
  148. }
  149. else
  150. {
  151. return "Could not reset password.";
  152. }
  153. if (get_secret_word($user[0]["email"]) == $secret) // OK, secret word is good. Now change password and mail it.
  154. {
  155. $user[0]["password"] = api_generate_password();
  156. $crypted = $user[0]["password"];
  157. if( $userPasswordCrypted)
  158. {
  159. $crypted = md5($crypted);
  160. }
  161. api_sql_query("UPDATE ".$tbl_user." SET password='$crypted' WHERE user_id=$id");
  162. return send_password_to_user($user, $your_password_has_been_reset);
  163. }
  164. else
  165. {
  166. return "Not allowed.";
  167. }
  168. }
  169. ?>