4 * @file src/Core/Session.php
6 namespace Friendica\Core;
9 use Friendica\Core\Session\CacheSessionHandler;
10 use Friendica\Core\Session\DatabaseSessionHandler;
11 use Friendica\Database\DBA;
12 use Friendica\Model\User;
13 use Friendica\Util\BaseURL;
14 use Friendica\Util\DateTimeFormat;
17 * High-level Session service class
19 * @author Hypolite Petovan <hypolite@mrpetovan.com>
23 public static $exists = false;
24 public static $expire = 180000;
26 public static function init()
28 ini_set('session.gc_probability', 50);
29 ini_set('session.use_only_cookies', 1);
30 ini_set('session.cookie_httponly', 1);
32 if (Config::get('system', 'ssl_policy') == BaseURL::SSL_POLICY_FULL) {
33 ini_set('session.cookie_secure', 1);
36 $session_handler = Config::get('system', 'session_handler', 'database');
37 if ($session_handler != 'native') {
38 if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') {
39 $SessionHandler = new CacheSessionHandler();
41 $SessionHandler = new DatabaseSessionHandler();
44 session_set_save_handler($SessionHandler);
48 public static function exists($name)
50 return isset($_SESSION[$name]);
54 * Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
56 * Handle the case where session_start() hasn't been called and the super global isn't available.
59 * @param mixed $defaults
62 public static function get($name, $defaults = null)
64 if (isset($_SESSION)) {
65 $return = defaults($_SESSION, $name, $defaults);
74 * Sets a single session variable.
75 * Overrides value of existing key.
80 public static function set($name, $value)
82 $_SESSION[$name] = $value;
86 * Sets multiple session variables.
87 * Overrides values for existing keys.
89 * @param array $values
91 public static function setMultiple(array $values)
93 $_SESSION = $values + $_SESSION;
97 * Removes a session variable.
98 * Ignores missing keys.
102 public static function remove($name)
104 unset($_SESSION[$name]);
108 * @brief Sets the provided user's authenticated session
111 * @param array $user_record
112 * @param bool $login_initial
113 * @param bool $interactive
114 * @param bool $login_refresh
115 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
117 public static function setAuthenticatedForUser(App $a, array $user_record, $login_initial = false, $interactive = false, $login_refresh = false)
120 'uid' => $user_record['uid'],
121 'theme' => $user_record['theme'],
122 'mobile-theme' => PConfig::get($user_record['uid'], 'system', 'mobile_theme'),
123 'authenticated' => 1,
124 'page_flags' => $user_record['page-flags'],
125 'my_url' => $a->getBaseURL() . '/profile/' . $user_record['nickname'],
126 'my_address' => $user_record['nickname'] . '@' . substr($a->getBaseURL(), strpos($a->getBaseURL(), '://') + 3),
127 'addr' => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0'),
130 $member_since = strtotime($user_record['register_date']);
131 self::set('new_member', time() < ($member_since + ( 60 * 60 * 24 * 14)));
133 if (strlen($user_record['timezone'])) {
134 date_default_timezone_set($user_record['timezone']);
135 $a->timezone = $user_record['timezone'];
138 $masterUid = $user_record['uid'];
140 if (!empty($_SESSION['submanage'])) {
141 $user = DBA::selectFirst('user', ['uid'], ['uid' => $_SESSION['submanage']]);
142 if (DBA::isResult($user)) {
143 $masterUid = $user['uid'];
147 $a->identities = User::identities($masterUid);
149 if ($login_initial) {
150 $a->getLogger()->info('auth_identities: ' . print_r($a->identities, true));
153 if ($login_refresh) {
154 $a->getLogger()->info('auth_identities refresh: ' . print_r($a->identities, true));
157 $contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]);
158 if (DBA::isResult($contact)) {
159 $a->contact = $contact;
160 $a->cid = $contact['id'];
161 self::set('cid', $a->cid);
164 header('X-Account-Management-Status: active; name="' . $user_record['username'] . '"; id="' . $user_record['nickname'] . '"');
166 if ($login_initial || $login_refresh) {
167 DBA::update('user', ['login_date' => DateTimeFormat::utcNow()], ['uid' => $_SESSION['uid']]);
169 // Set the login date for all identities of the user
170 DBA::update('user', ['login_date' => DateTimeFormat::utcNow()],
171 ['parent-uid' => $masterUid, 'account_removed' => false]);
174 if ($login_initial) {
176 * If the user specified to remember the authentication, then set a cookie
177 * that expires after one week (the default is when the browser is closed).
178 * The cookie will be renewed automatically.
179 * The week ensures that sessions will expire after some inactivity.
182 if (self::get('remember')) {
183 $a->getLogger()->info('Injecting cookie for remembered user ' . $user_record['nickname']);
184 Authentication::setCookie(604800, $user_record);
185 self::remove('remember');
189 Authentication::twoFactorCheck($user_record['uid'], $a);
192 if ($user_record['login_date'] <= DBA::NULL_DATETIME) {
193 info(L10n::t('Welcome %s', $user_record['username']));
194 info(L10n::t('Please upload a profile photo.'));
195 $a->internalRedirect('profile_photo/new');
197 info(L10n::t("Welcome back %s", $user_record['username']));
201 $a->user = $user_record;
203 if ($login_initial) {
204 Hook::callAll('logged_in', $a->user);
206 if ($a->module !== 'home' && self::exists('return_path')) {
207 $a->internalRedirect(self::get('return_path'));