3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
24 use Friendica\Database\DBA;
26 use Friendica\Model\Contact;
27 use Friendica\Util\Strings;
30 * High-level Session service class
34 public static $exists = false;
35 public static $expire = 180000;
37 public static function exists($name)
39 return DI::session()->exists($name);
42 public static function get($name, $defaults = null)
44 return DI::session()->get($name, $defaults);
47 public static function set($name, $value)
49 DI::session()->set($name, $value);
52 public static function setMultiple(array $values)
54 DI::session()->setMultiple($values);
57 public static function remove($name)
59 DI::session()->remove($name);
62 public static function clear()
64 DI::session()->clear();
68 * Returns contact ID for given user ID
70 * @param integer $uid User ID
71 * @return integer Contact ID of visitor for given user ID
73 public static function getRemoteContactID($uid)
75 $session = DI::session();
77 if (empty($session->get('remote')[$uid])) {
81 return $session->get('remote')[$uid];
85 * Returns User ID for given contact ID of the visitor
87 * @param integer $cid Contact ID
88 * @return integer User ID for given contact ID of the visitor
90 public static function getUserIDForVisitorContactID($cid)
92 $session = DI::session();
94 if (empty($session->get('remote'))) {
98 return array_search($cid, $session->get('remote'));
102 * Set the session variable that contains the contact IDs for the visitor's contact URL
104 * @param string $url Contact URL
106 public static function setVisitorsContacts()
108 $session = DI::session();
110 $session->set('remote', []);
112 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('my_url')), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
113 while ($contact = DBA::fetch($remote_contacts)) {
114 if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
118 $session->set('remote', [$contact['uid'] => $contact['id']]);
120 DBA::close($remote_contacts);
124 * Returns if the current visitor is authenticated
126 * @return boolean "true" when visitor is either a local or remote user
128 public static function isAuthenticated()
130 $session = DI::session();
132 return $session->get('authenticated', false);