4 * @file src/Core/Session.php
6 namespace Friendica\Core;
8 use Friendica\BaseObject;
9 use Friendica\Core\Session\ISession;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Util\Strings;
15 * High-level Session service class
17 * @author Hypolite Petovan <hypolite@mrpetovan.com>
19 class Session extends BaseObject
21 public static $exists = false;
22 public static $expire = 180000;
24 public static function exists($name)
26 return self::getClass(ISession::class)->exists($name);
29 public static function get($name, $defaults = null)
31 return self::getClass(ISession::class)->get($name, $defaults);
34 public static function set($name, $value)
36 self::getClass(ISession::class)->set($name, $value);
39 public static function setMultiple(array $values)
41 self::getClass(ISession::class)->setMultiple($values);
44 public static function remove($name)
46 self::getClass(ISession::class)->remove($name);
49 public static function clear()
51 self::getClass(ISession::class)->clear();
55 * Returns contact ID for given user ID
57 * @param integer $uid User ID
58 * @return integer Contact ID of visitor for given user ID
60 public static function getRemoteContactID($uid)
62 /** @var ISession $session */
63 $session = self::getClass(ISession::class);
65 if (empty($session->get('remote')[$uid])) {
69 return $session->get('remote')[$uid];
73 * Returns User ID for given contact ID of the visitor
75 * @param integer $cid Contact ID
76 * @return integer User ID for given contact ID of the visitor
78 public static function getUserIDForVisitorContactID($cid)
80 /** @var ISession $session */
81 $session = self::getClass(ISession::class);
83 if (empty($session->get('remote'))) {
87 return array_search($cid, $session->get('remote'));
91 * Set the session variable that contains the contact IDs for the visitor's contact URL
93 * @param string $url Contact URL
95 public static function setVisitorsContacts()
97 /** @var ISession $session */
98 $session = self::getClass(ISession::class);
100 $session->set('remote', []);
102 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('my_url')), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
103 while ($contact = DBA::fetch($remote_contacts)) {
104 if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
108 $session->set('remote', [$contact['uid'] => $contact['id']]);
110 DBA::close($remote_contacts);
114 * Returns if the current visitor is authenticated
116 * @return boolean "true" when visitor is either a local or remote user
118 public static function isAuthenticated()
120 /** @var ISession $session */
121 $session = self::getClass(ISession::class);
123 return $session->get('authenticated', false);