lost_password.lib.php 6.4 KB

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