login.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Elgg login action
  4. *
  5. * @package Elgg.Core
  6. * @subpackage User.Authentication
  7. */
  8. $session = elgg_get_session();
  9. // set forward url
  10. if ($session->has('last_forward_from')) {
  11. $forward_url = $session->get('last_forward_from');
  12. $forward_source = 'last_forward_from';
  13. } elseif (get_input('returntoreferer')) {
  14. $forward_url = REFERER;
  15. $forward_source = 'return_to_referer';
  16. } else {
  17. // forward to main index page
  18. $forward_url = '';
  19. $forward_source = null;
  20. }
  21. $username = get_input('username');
  22. $password = get_input('password', null, false);
  23. $persistent = (bool) get_input("persistent");
  24. $result = false;
  25. if (empty($username) || empty($password)) {
  26. register_error(elgg_echo('login:empty'));
  27. forward();
  28. }
  29. // check if logging in with email address
  30. if (strpos($username, '@') !== false && ($users = get_user_by_email($username))) {
  31. $username = $users[0]->username;
  32. }
  33. $result = elgg_authenticate($username, $password);
  34. if ($result !== true) {
  35. register_error($result);
  36. forward(REFERER);
  37. }
  38. $user = get_user_by_username($username);
  39. if (!$user) {
  40. register_error(elgg_echo('login:baduser'));
  41. forward(REFERER);
  42. }
  43. try {
  44. login($user, $persistent);
  45. // re-register at least the core language file for users with language other than site default
  46. register_translations(dirname(dirname(__FILE__)) . "/languages/");
  47. } catch (LoginException $e) {
  48. register_error($e->getMessage());
  49. forward(REFERER);
  50. }
  51. // elgg_echo() caches the language and does not provide a way to change the language.
  52. // @todo we need to use the config object to store this so that the current language
  53. // can be changed. Refs #4171
  54. if ($user->language) {
  55. $message = elgg_echo('loginok', array(), $user->language);
  56. } else {
  57. $message = elgg_echo('loginok');
  58. }
  59. // clear after login in case login fails
  60. $session->remove('last_forward_from');
  61. $params = array('user' => $user, 'source' => $forward_source);
  62. $forward_url = elgg_trigger_plugin_hook('login:forward', 'user', $params, $forward_url);
  63. system_message($message);
  64. forward($forward_url);