login.lib.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Code library for login process
  5. *
  6. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  7. * @author Julio Montoya <gugli100@gmail.com>
  8. * @package chamilo.login
  9. */
  10. /**
  11. * Class
  12. * @package chamilo.login
  13. */
  14. class Login
  15. {
  16. /**
  17. * Get user account list
  18. *
  19. * @param unknown_type $user
  20. * @param boolean $reset
  21. * @param boolean $by_username
  22. * @return unknown
  23. */
  24. public static function get_user_account_list($user, $reset = false, $by_username = false) {
  25. global $_configuration;
  26. $portal_url = $_configuration['root_web'];
  27. if ($_configuration['multiple_access_urls']) {
  28. $access_url_id = api_get_current_access_url_id();
  29. if ($access_url_id != -1 ) {
  30. $url = api_get_access_url($access_url_id);
  31. $portal_url = $url['url'];
  32. }
  33. }
  34. if ($reset) {
  35. if ($by_username) {
  36. $secret_word = self::get_secret_word($user['email']);
  37. if ($reset) {
  38. $reset_link = $portal_url."main/auth/lostPassword.php?reset=".$secret_word."&id=".$user['uid'];
  39. } else {
  40. $reset_link = get_lang('Pass')." : $user[password]";
  41. }
  42. $user_account_list = get_lang('YourRegistrationData')." : \n".get_lang('UserName').' : '.$user['loginName']."\n".get_lang('ResetLink').' : '.$reset_link.'';
  43. if ($user_account_list) {
  44. $user_account_list = "\n-----------------------------------------------\n" . $user_account_list;
  45. }
  46. } else {
  47. foreach ($user as $this_user) {
  48. $secret_word = self::get_secret_word($this_user['email']);
  49. if ($reset) {
  50. $reset_link = $portal_url."main/auth/lostPassword.php?reset=".$secret_word."&id=".$this_user['uid'];
  51. } else {
  52. $reset_link = get_lang('Pass')." : $this_user[password]";
  53. }
  54. $user_account_list[] = get_lang('YourRegistrationData')." : \n".get_lang('UserName').' : '.$this_user['loginName']."\n".get_lang('ResetLink').' : '.$reset_link.'';
  55. }
  56. if ($user_account_list) {
  57. $user_account_list = implode("\n-----------------------------------------------\n", $user_account_list);
  58. }
  59. }
  60. } else {
  61. if (!$by_username) {
  62. $user = $user[0];
  63. }
  64. $reset_link = get_lang('Pass')." : $user[password]";
  65. $user_account_list = get_lang('YourRegistrationData')." : \n".get_lang('UserName').' : '.$user['loginName']."\n".$reset_link.'';
  66. }
  67. return $user_account_list;
  68. }
  69. /**
  70. * This function sends the actual password to the user
  71. *
  72. * @param unknown_type $user
  73. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  74. */
  75. public static function send_password_to_user($user, $by_username = false) {
  76. global $_configuration;
  77. /*
  78. $emailHeaders = get_email_headers(); // Email Headers
  79. */
  80. $email_subject = "[".api_get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  81. if ($by_username) { // Show only for lost password
  82. $user_account_list = self::get_user_account_list($user, false, $by_username); // BODY
  83. $email_to = $user['email'];
  84. } else {
  85. $user_account_list = self::get_user_account_list($user); // BODY
  86. $email_to = $user[0]['email'];
  87. }
  88. $portal_url = $_configuration['root_web'];
  89. if ($_configuration['multiple_access_urls']) {
  90. $access_url_id = api_get_current_access_url_id();
  91. if ($access_url_id != -1 ) {
  92. $url = api_get_access_url($access_url_id);
  93. $portal_url = $url['url'];
  94. }
  95. }
  96. $email_body = get_lang('YourAccountParam')." ".$portal_url."\n\n$user_account_list";
  97. // SEND MESSAGE
  98. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  99. $email_admin = api_get_setting('emailAdministrator');
  100. if (@api_mail('', $email_to, $email_subject, $email_body, $sender_name, $email_admin) == 1) {
  101. if (api_get_setting('use_custom_pages') == 'true')
  102. return get_lang('your_password_has_been_reset');
  103. else
  104. Display::display_confirmation_message(get_lang('your_password_has_been_reset'));
  105. } else {
  106. $message = get_lang('SystemUnableToSendEmailContact').' '.Display :: encrypted_mailto_link(api_get_setting('emailAdministrator'), get_lang('PlatformAdmin')).".</p>";
  107. }
  108. }
  109. /**
  110. * Handle encrypted password, send an email to a user with his password
  111. *
  112. * @param int user id
  113. * @param bool $by_username
  114. *
  115. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  116. */
  117. public static function handle_encrypted_password($user, $by_username = false) {
  118. global $_configuration;
  119. $email_subject = "[".api_get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  120. if ($by_username) { // Show only for lost password
  121. $user_account_list = self::get_user_account_list($user, true, $by_username); // BODY
  122. $email_to = $user['email'];
  123. } else {
  124. $user_account_list = self::get_user_account_list($user, true); // BODY
  125. $email_to = $user[0]['email'];
  126. }
  127. $secret_word = self::get_secret_word($email_to);
  128. $email_body = get_lang('DearUser')." :\n".get_lang('password_request')."\n";
  129. $email_body .= $user_account_list."\n-----------------------------------------------\n\n";
  130. $email_body .= get_lang('PasswordEncryptedForSecurity');
  131. //$email_body .= "\n\n".get_lang('Formula').",\n".get_lang('PlataformAdmin');
  132. $email_body .= "\n\n".get_lang('Formula').",\n".api_get_setting('administratorName')." ".api_get_setting('administratorSurname')."\n".get_lang('PlataformAdmin')." - ".api_get_setting('siteName');
  133. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  134. $email_admin = api_get_setting('emailAdministrator');
  135. if (@api_mail('', $email_to, $email_subject, $email_body, $sender_name, $email_admin) == 1) {
  136. if (api_get_setting('use_custom_pages') == 'true')
  137. return get_lang('YourPasswordHasBeenEmailed');
  138. else
  139. Display::display_confirmation_message(get_lang('YourPasswordHasBeenEmailed'));
  140. } else {
  141. $message = get_lang('SystemUnableToSendEmailContact').' '.Display :: encrypted_mailto_link(api_get_setting('emailAdministrator'), get_lang('PlatformAdmin')).".</p>";
  142. if (api_get_setting('use_custom_pages') == 'true')
  143. return $message;
  144. else
  145. Display::display_error_message($message, false);
  146. }
  147. }
  148. /**
  149. * Gets the secret word
  150. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  151. */
  152. public static function get_secret_word($add) {
  153. global $_configuration;
  154. return $secret_word = md5($_configuration['security_key'].$add);
  155. }
  156. /**
  157. * Resets a password
  158. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  159. */
  160. public static function reset_password($secret, $id, $by_username = false) {
  161. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  162. $id = intval($id);
  163. $sql = "SELECT user_id AS uid, lastname AS lastName, firstname AS firstName, username AS loginName, password, email FROM ".$tbl_user." WHERE user_id=$id";
  164. $result = Database::query($sql);
  165. $num_rows = Database::num_rows($result);
  166. if ($result && $num_rows > 0) {
  167. $user = Database::fetch_array($result);
  168. } else {
  169. return get_lang('CouldNotResetPassword');
  170. }
  171. if (self::get_secret_word($user['email']) == $secret) { // OK, secret word is good. Now change password and mail it.
  172. $user['password'] = api_generate_password();
  173. $crypted = $user['password'];
  174. $crypted = api_get_encrypted_password($crypted);
  175. $sql = "UPDATE ".$tbl_user." SET password='$crypted' WHERE user_id=$id";
  176. $result = Database::query($sql);
  177. return self::send_password_to_user($user, $by_username);
  178. } else {
  179. return get_lang('NotAllowed');
  180. }
  181. }
  182. }