login.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // set forward url
  3. if (isset($_SESSION['last_forward_from']) && $_SESSION['last_forward_from']) {
  4. $forward_url = $_SESSION['last_forward_from'];
  5. unset($_SESSION['last_forward_from']);
  6. } elseif (get_input('returntoreferer')) {
  7. $forward_url = REFERER;
  8. } else {
  9. // forward to main index page
  10. $forward_url = '';
  11. }
  12. $username = get_input('username');
  13. $password = get_input('password', null, false);
  14. $persistent = get_input("persistent", false);
  15. $result = false;
  16. if (empty($username) || empty($password)) {
  17. register_error(elgg_echo('login:empty'));
  18. forward();
  19. }
  20. // check if logging in with email address
  21. if (strpos($username, '@') !== false && ($users = get_user_by_email($username))) {
  22. $username = $users[0]->username;
  23. }
  24. $result = elgg_authenticate($username, $password);
  25. // Open log
  26. openlog("elgg({$_SERVER['HTTP_HOST']})", LOG_PID, LOG_AUTH);
  27. $ip = $_SERVER['REMOTE_ADDR'];
  28. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  29. $proxies = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); // We are behind a proxy
  30. $ip = trim($proxies[0]);
  31. }
  32. if ($result !== true) {
  33. // Log authentication error, in a format almost identical to the SSH rule (for compatibility)
  34. syslog(LOG_NOTICE,"Authentication failure for $username from $ip");
  35. register_error($result);
  36. forward(REFERER);
  37. }
  38. // We got here, so login was successful
  39. syslog(LOG_INFO,"Accepted password for $username from $ip");
  40. closelog();
  41. $user = get_user_by_username($username);
  42. if (!$user) {
  43. register_error(elgg_echo('login:baduser'));
  44. forward(REFERER);
  45. }
  46. try {
  47. login($user, $persistent);
  48. // re-register at least the core language file for users with language other than site default
  49. register_translations(dirname(dirname(__FILE__)) . "/languages/");
  50. } catch (LoginException $e) {
  51. register_error($e->getMessage());
  52. forward(REFERER);
  53. }
  54. // elgg_echo() caches the language and does not provide a way to change the language.
  55. // @todo we need to use the config object to store this so that the current language
  56. // can be changed. Refs #4171
  57. if ($user->language) {
  58. $message = elgg_echo('loginok', array(), $user->language);
  59. } else {
  60. $message = elgg_echo('loginok');
  61. }
  62. system_message($message);
  63. forward($forward_url);