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.

175 lines
5.1 KiB

  1. <?php
  2. /**
  3. * @file /src/Core/Authentication.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\BaseObject;
  7. use Friendica\Core\Addon;
  8. use Friendica\Core\Config;
  9. use Friendica\Core\L10n;
  10. use Friendica\Core\Logger;
  11. use Friendica\Core\PConfig;
  12. use Friendica\Database\DBA;
  13. use Friendica\Model\User;
  14. use Friendica\Util\DateTimeFormat;
  15. /**
  16. * Handle Authentification, Session and Cookies
  17. */
  18. class Authentication extends BaseObject
  19. {
  20. /**
  21. * @brief Calculate the hash that is needed for the "Friendica" cookie
  22. *
  23. * @param array $user Record from "user" table
  24. *
  25. * @return string Hashed data
  26. */
  27. public static function getCookieHashForUser($user)
  28. {
  29. return(hash("sha256", Config::get("system", "site_prvkey") .
  30. $user["prvkey"] .
  31. $user["password"]));
  32. }
  33. /**
  34. * @brief Set the "Friendica" cookie
  35. *
  36. * @param int $time
  37. * @param array $user Record from "user" table
  38. */
  39. public static function setCookie($time, $user = [])
  40. {
  41. if ($time != 0) {
  42. $time = $time + time();
  43. }
  44. if ($user) {
  45. $value = json_encode(["uid" => $user["uid"],
  46. "hash" => self::getCookieHashForUser($user),
  47. "ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]);
  48. } else {
  49. $value = "";
  50. }
  51. setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL), true);
  52. }
  53. /**
  54. * @brief Sets the provided user's authenticated session
  55. *
  56. * @todo Should be moved to Friendica\Core\Session once it's created
  57. *
  58. * @param type $user_record
  59. * @param type $login_initial
  60. * @param type $interactive
  61. * @param type $login_refresh
  62. */
  63. public static function setAuthenticatedSessionForUser($user_record, $login_initial = false, $interactive = false, $login_refresh = false)
  64. {
  65. $a = self::getApp();
  66. $_SESSION['uid'] = $user_record['uid'];
  67. $_SESSION['theme'] = $user_record['theme'];
  68. $_SESSION['mobile-theme'] = PConfig::get($user_record['uid'], 'system', 'mobile_theme');
  69. $_SESSION['authenticated'] = 1;
  70. $_SESSION['page_flags'] = $user_record['page-flags'];
  71. $_SESSION['my_url'] = $a->getbaseUrl() . '/profile/' . $user_record['nickname'];
  72. $_SESSION['my_address'] = $user_record['nickname'] . '@' . substr($a->getbaseUrl(), strpos($a->getbaseUrl(), '://') + 3);
  73. $_SESSION['addr'] = defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0');
  74. $a->user = $user_record;
  75. if ($interactive) {
  76. if ($a->user['login_date'] <= DBA::NULL_DATETIME) {
  77. $_SESSION['return_path'] = 'profile_photo/new';
  78. $a->module = 'profile_photo';
  79. info(L10n::t("Welcome ") . $a->user['username'] . EOL);
  80. info(L10n::t('Please upload a profile photo.') . EOL);
  81. } else {
  82. info(L10n::t("Welcome back ") . $a->user['username'] . EOL);
  83. }
  84. }
  85. $member_since = strtotime($a->user['register_date']);
  86. if (time() < ($member_since + ( 60 * 60 * 24 * 14))) {
  87. $_SESSION['new_member'] = true;
  88. } else {
  89. $_SESSION['new_member'] = false;
  90. }
  91. if (strlen($a->user['timezone'])) {
  92. date_default_timezone_set($a->user['timezone']);
  93. $a->timezone = $a->user['timezone'];
  94. }
  95. $masterUid = $user_record['uid'];
  96. if (!empty($_SESSION['submanage'])) {
  97. $user = DBA::selectFirst('user', ['uid'], ['uid' => $_SESSION['submanage']]);
  98. if (DBA::isResult($user)) {
  99. $masterUid = $user['uid'];
  100. }
  101. }
  102. $a->identities = User::identities($masterUid);
  103. if ($login_initial) {
  104. Logger::log('auth_identities: ' . print_r($a->identities, true), Logger::DEBUG);
  105. }
  106. if ($login_refresh) {
  107. Logger::log('auth_identities refresh: ' . print_r($a->identities, true), Logger::DEBUG);
  108. }
  109. $contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]);
  110. if (DBA::isResult($contact)) {
  111. $a->contact = $contact;
  112. $a->cid = $contact['id'];
  113. $_SESSION['cid'] = $a->cid;
  114. }
  115. header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] . '"');
  116. if ($login_initial || $login_refresh) {
  117. DBA::update('user', ['login_date' => DateTimeFormat::utcNow()], ['uid' => $_SESSION['uid']]);
  118. // Set the login date for all identities of the user
  119. DBA::update('user', ['login_date' => DateTimeFormat::utcNow()],
  120. ['parent-uid' => $masterUid, 'account_removed' => false]);
  121. }
  122. if ($login_initial) {
  123. /*
  124. * If the user specified to remember the authentication, then set a cookie
  125. * that expires after one week (the default is when the browser is closed).
  126. * The cookie will be renewed automatically.
  127. * The week ensures that sessions will expire after some inactivity.
  128. */
  129. if (!empty($_SESSION['remember'])) {
  130. Logger::log('Injecting cookie for remembered user ' . $a->user['nickname']);
  131. self::setCookie(604800, $user_record);
  132. unset($_SESSION['remember']);
  133. }
  134. }
  135. if ($login_initial) {
  136. Addon::callHooks('logged_in', $a->user);
  137. if (($a->module !== 'home') && isset($_SESSION['return_path'])) {
  138. $a->internalRedirect($_SESSION['return_path']);
  139. }
  140. }
  141. }
  142. /**
  143. * @brief Kills the "Friendica" cookie and all session data
  144. */
  145. public static function deleteSession()
  146. {
  147. self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
  148. session_unset();
  149. session_destroy();
  150. }
  151. }