4 * @file src/Core/Session.php
6 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Core\Session\CacheSessionHandler;
11 use Friendica\Core\Session\DatabaseSessionHandler;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\User;
15 use Friendica\Util\Strings;
18 * High-level Session service class
20 * @author Hypolite Petovan <hypolite@mrpetovan.com>
24 public static $exists = false;
25 public static $expire = 180000;
27 public static function init()
29 ini_set('session.gc_probability', 50);
30 ini_set('session.use_only_cookies', 1);
31 ini_set('session.cookie_httponly', 1);
33 if (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL) {
34 ini_set('session.cookie_secure', 1);
37 $session_handler = Config::get('system', 'session_handler', 'database');
38 if ($session_handler != 'native') {
39 if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') {
40 $SessionHandler = new CacheSessionHandler();
42 $SessionHandler = new DatabaseSessionHandler();
45 session_set_save_handler($SessionHandler);
49 public static function exists($name)
51 return isset($_SESSION[$name]);
55 * Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
57 * Handle the case where session_start() hasn't been called and the super global isn't available.
60 * @param mixed $defaults
63 public static function get($name, $defaults = null)
65 return $_SESSION[$name] ?? $defaults;
69 * Sets a single session variable.
70 * Overrides value of existing key.
75 public static function set($name, $value)
77 $_SESSION[$name] = $value;
81 * Sets multiple session variables.
82 * Overrides values for existing keys.
84 * @param array $values
86 public static function setMultiple(array $values)
88 $_SESSION = $values + $_SESSION;
92 * Removes a session variable.
93 * Ignores missing keys.
97 public static function remove($name)
99 unset($_SESSION[$name]);
103 * Clears the current session array
105 public static function clear()
113 * Returns contact ID for given user ID
115 * @param integer $uid User ID
116 * @return integer Contact ID of visitor for given user ID
118 public static function getRemoteContactID($uid)
120 if (empty($_SESSION['remote'][$uid])) {
124 return $_SESSION['remote'][$uid];
128 * Returns User ID for given contact ID of the visitor
130 * @param integer $cid Contact ID
131 * @return integer User ID for given contact ID of the visitor
133 public static function getUserIDForVisitorContactID($cid)
135 if (empty($_SESSION['remote'])) {
139 return array_search($cid, $_SESSION['remote']);
143 * Set the session variable that contains the contact IDs for the visitor's contact URL
145 * @param string $url Contact URL
147 public static function setVisitorsContacts()
149 $_SESSION['remote'] = [];
151 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
152 while ($contact = DBA::fetch($remote_contacts)) {
153 if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
157 $_SESSION['remote'][$contact['uid']] = $contact['id'];
159 DBA::close($remote_contacts);
163 * Returns if the current visitor is authenticated
165 * @return boolean "true" when visitor is either a local or remote user
167 public static function isAuthenticated()
169 if (empty($_SESSION['authenticated'])) {
173 return $_SESSION['authenticated'];
177 * @brief Kills the "Friendica" cookie and all session data
179 public static function delete()
181 /** @var User\Cookie $cookie */
182 $cookie = BaseObject::getClass(User\Cookie::class);