Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

208 rivejä
6.2 KiB

  1. <?php
  2. use Friendica\Core\Config;
  3. require_once('include/security.php');
  4. require_once('include/datetime.php');
  5. // When the "Friendica" cookie is set, take the value to authenticate and renew the cookie.
  6. if (isset($_COOKIE["Friendica"])) {
  7. $data = json_decode($_COOKIE["Friendica"]);
  8. if (isset($data->uid)) {
  9. $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
  10. FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
  11. intval($data->uid)
  12. );
  13. if ($r) {
  14. if ($data->hash != cookie_hash($r[0])) {
  15. logger("Hash for user ".$data->uid." doesn't fit.");
  16. nuke_session();
  17. goaway(z_root());
  18. }
  19. // Renew the cookie
  20. // Expires after 7 days by default,
  21. // can be set via system.auth_cookie_lifetime
  22. $authcookiedays = Config::get('system', 'auth_cookie_lifetime', 7);
  23. new_cookie($authcookiedays*24*60*60, $r[0]);
  24. // Do the authentification if not done by now
  25. if (!isset($_SESSION) OR !isset($_SESSION['authenticated'])) {
  26. authenticate_success($r[0]);
  27. if (get_config('system','paranoia'))
  28. $_SESSION['addr'] = $data->ip;
  29. }
  30. }
  31. }
  32. }
  33. // login/logout
  34. if (isset($_SESSION) && x($_SESSION,'authenticated') && (!x($_POST,'auth-params') || ($_POST['auth-params'] !== 'login'))) {
  35. if ((x($_POST,'auth-params') && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
  36. // process logout request
  37. call_hooks("logging_out");
  38. nuke_session();
  39. info(t('Logged out.').EOL);
  40. goaway(z_root());
  41. }
  42. if (x($_SESSION,'visitor_id') && !x($_SESSION,'uid')) {
  43. $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  44. intval($_SESSION['visitor_id'])
  45. );
  46. if (dbm::is_result($r)) {
  47. $a->contact = $r[0];
  48. }
  49. }
  50. if (x($_SESSION,'uid')) {
  51. // already logged in user returning
  52. $check = get_config('system','paranoia');
  53. // extra paranoia - if the IP changed, log them out
  54. if ($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
  55. logger('Session address changed. Paranoid setting in effect, blocking session. '.
  56. $_SESSION['addr'].' != '.$_SERVER['REMOTE_ADDR']);
  57. nuke_session();
  58. goaway(z_root());
  59. }
  60. $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
  61. FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
  62. intval($_SESSION['uid'])
  63. );
  64. if (!dbm::is_result($r)) {
  65. nuke_session();
  66. goaway(z_root());
  67. }
  68. // Make sure to refresh the last login time for the user if the user
  69. // stays logged in for a long time, e.g. with "Remember Me"
  70. $login_refresh = false;
  71. if (!x($_SESSION['last_login_date'])) {
  72. $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
  73. }
  74. if (strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0) {
  75. $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
  76. $login_refresh = true;
  77. }
  78. authenticate_success($r[0], false, false, $login_refresh);
  79. }
  80. } else {
  81. session_unset();
  82. if (x($_POST,'password') && strlen($_POST['password']))
  83. $encrypted = hash('whirlpool',trim($_POST['password']));
  84. else {
  85. if ((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
  86. (x($_POST,'username')) && strlen($_POST['username'])) {
  87. $noid = get_config('system','no_openid');
  88. $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']));
  89. // validate_url alters the calling parameter
  90. $temp_string = $openid_url;
  91. // if it's an email address or doesn't resolve to a URL, fail.
  92. if ($noid || strpos($temp_string,'@') || !validate_url($temp_string)) {
  93. $a = get_app();
  94. notice(t('Login failed.').EOL);
  95. goaway(z_root());
  96. // NOTREACHED
  97. }
  98. // Otherwise it's probably an openid.
  99. try {
  100. require_once('library/openid.php');
  101. $openid = new LightOpenID;
  102. $openid->identity = $openid_url;
  103. $_SESSION['openid'] = $openid_url;
  104. $_SESSION['remember'] = $_POST['remember'];
  105. $openid->returnUrl = App::get_baseurl(true).'/openid';
  106. goaway($openid->authUrl());
  107. } catch (Exception $e) {
  108. notice(t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.').'<br /><br >'.t('The error message was:').' '.$e->getMessage());
  109. }
  110. // NOTREACHED
  111. }
  112. }
  113. if (x($_POST,'auth-params') && $_POST['auth-params'] === 'login') {
  114. $record = null;
  115. $addon_auth = array(
  116. 'username' => trim($_POST['username']),
  117. 'password' => trim($_POST['password']),
  118. 'authenticated' => 0,
  119. 'user_record' => null
  120. );
  121. /**
  122. *
  123. * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
  124. * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
  125. * and later plugins should not interfere with an earlier one that succeeded.
  126. *
  127. */
  128. call_hooks('authenticate', $addon_auth);
  129. if ($addon_auth['authenticated'] && count($addon_auth['user_record']))
  130. $record = $addon_auth['user_record'];
  131. else {
  132. // process normal login request
  133. $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
  134. FROM `user` WHERE (`email` = '%s' OR `nickname` = '%s')
  135. AND `password` = '%s' AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
  136. dbesc(trim($_POST['username'])),
  137. dbesc(trim($_POST['username'])),
  138. dbesc($encrypted)
  139. );
  140. if (dbm::is_result($r))
  141. $record = $r[0];
  142. }
  143. if (!$record || !count($record)) {
  144. logger('authenticate: failed login attempt: '.notags(trim($_POST['username'])).' from IP '.$_SERVER['REMOTE_ADDR']);
  145. notice(t('Login failed.').EOL);
  146. goaway(z_root());
  147. }
  148. if (! $_POST['remember']) {
  149. new_cookie(0); // 0 means delete on browser exit
  150. }
  151. // if we haven't failed up this point, log them in.
  152. $_SESSION['remember'] = $_POST['remember'];
  153. $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
  154. authenticate_success($record, true, true);
  155. }
  156. }
  157. /**
  158. * @brief Kills the "Friendica" cookie and all session data
  159. */
  160. function nuke_session() {
  161. new_cookie(-3600); // make sure cookie is deleted on browser close, as a security measure
  162. session_unset();
  163. session_destroy();
  164. }