lost_password.lib.php 6.9 KB

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