3 * @file /src/Core/Authentication.php
6 namespace Friendica\Core;
8 use Friendica\BaseObject;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Logger;
13 use Friendica\Core\PConfig;
14 use Friendica\Database\DBA;
15 use Friendica\Model\User;
16 use Friendica\Util\DateTimeFormat;
19 * Handle Authentification, Session and Cookies
21 class Authentication extends BaseObject
24 * @brief Calculate the hash that is needed for the "Friendica" cookie
26 * @param array $user Record from "user" table
28 * @return string Hashed data
30 public static function getCookieHashForUser($user)
32 return(hash("sha256", Config::get("system", "site_prvkey") .
38 * @brief Set the "Friendica" cookie
41 * @param array $user Record from "user" table
43 public static function setCookie($time, $user = [])
46 $time = $time + time();
50 $value = json_encode(["uid" => $user["uid"],
51 "hash" => self::getCookieHashForUser($user),
52 "ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]);
57 setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL), true);
61 * @brief Sets the provided user's authenticated session
63 * @todo Should be moved to Friendica\Core\Session once it's created
65 * @param type $user_record
66 * @param type $login_initial
67 * @param type $interactive
68 * @param type $login_refresh
70 public static function setAuthenticatedSessionForUser($user_record, $login_initial = false, $interactive = false, $login_refresh = false)
74 $_SESSION['uid'] = $user_record['uid'];
75 $_SESSION['theme'] = $user_record['theme'];
76 $_SESSION['mobile-theme'] = PConfig::get($user_record['uid'], 'system', 'mobile_theme');
77 $_SESSION['authenticated'] = 1;
78 $_SESSION['page_flags'] = $user_record['page-flags'];
79 $_SESSION['my_url'] = $a->getbaseUrl() . '/profile/' . $user_record['nickname'];
80 $_SESSION['my_address'] = $user_record['nickname'] . '@' . substr($a->getbaseUrl(), strpos($a->getbaseUrl(), '://') + 3);
81 $_SESSION['addr'] = defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0');
83 $a->user = $user_record;
86 if ($a->user['login_date'] <= DBA::NULL_DATETIME) {
87 $_SESSION['return_path'] = 'profile_photo/new';
88 $a->module = 'profile_photo';
89 info(L10n::t("Welcome ") . $a->user['username'] . EOL);
90 info(L10n::t('Please upload a profile photo.') . EOL);
92 info(L10n::t("Welcome back ") . $a->user['username'] . EOL);
96 $member_since = strtotime($a->user['register_date']);
97 if (time() < ($member_since + ( 60 * 60 * 24 * 14))) {
98 $_SESSION['new_member'] = true;
100 $_SESSION['new_member'] = false;
102 if (strlen($a->user['timezone'])) {
103 date_default_timezone_set($a->user['timezone']);
104 $a->timezone = $a->user['timezone'];
107 $masterUid = $user_record['uid'];
109 if ((x($_SESSION, 'submanage')) && intval($_SESSION['submanage'])) {
110 $user = DBA::selectFirst('user', ['uid'], ['uid' => $_SESSION['submanage']]);
111 if (DBA::isResult($user)) {
112 $masterUid = $user['uid'];
116 $a->identities = User::identities($masterUid);
118 if ($login_initial) {
119 Logger::log('auth_identities: ' . print_r($a->identities, true), Logger::DEBUG);
121 if ($login_refresh) {
122 Logger::log('auth_identities refresh: ' . print_r($a->identities, true), Logger::DEBUG);
125 $contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]);
126 if (DBA::isResult($contact)) {
127 $a->contact = $contact;
128 $a->cid = $contact['id'];
129 $_SESSION['cid'] = $a->cid;
132 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] . '"');
134 if ($login_initial || $login_refresh) {
135 DBA::update('user', ['login_date' => DateTimeFormat::utcNow()], ['uid' => $_SESSION['uid']]);
137 // Set the login date for all identities of the user
138 DBA::update('user', ['login_date' => DateTimeFormat::utcNow()],
139 ['parent-uid' => $masterUid, 'account_removed' => false]);
142 if ($login_initial) {
144 * If the user specified to remember the authentication, then set a cookie
145 * that expires after one week (the default is when the browser is closed).
146 * The cookie will be renewed automatically.
147 * The week ensures that sessions will expire after some inactivity.
149 if (!empty($_SESSION['remember'])) {
150 Logger::log('Injecting cookie for remembered user ' . $a->user['nickname']);
151 self::setCookie(604800, $user_record);
152 unset($_SESSION['remember']);
156 if ($login_initial) {
157 Addon::callHooks('logged_in', $a->user);
159 if (($a->module !== 'home') && isset($_SESSION['return_path'])) {
160 $a->internalRedirect($_SESSION['return_path']);
166 * @brief Kills the "Friendica" cookie and all session data
168 public static function deleteSession()
170 self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure