3 * @copyright Copyright (C) 2010-2022, the Friendica project
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
35 * Returns the user id of locally logged in user or false.
37 * @return int|bool user id or false
39 public static function getLocalUser()
41 $session = DI::session();
43 if (!empty($session->get('authenticated')) && !empty($session->get('uid'))) {
44 return intval($session->get('uid'));
51 * Returns the public contact id of logged in user or false.
53 * @return int|bool public contact id or false
55 public static function getPublicContact()
57 static $public_contact_id = false;
59 $session = DI::session();
61 if (!$public_contact_id && !empty($session->get('authenticated'))) {
62 if (!empty($session->get('my_address'))) {
64 $public_contact_id = intval(Contact::getIdForURL($session->get('my_address'), 0, false));
65 } elseif (!empty($session->get('visitor_home'))) {
67 $public_contact_id = intval(Contact::getIdForURL($session->get('visitor_home'), 0, false));
69 } elseif (empty($session->get('authenticated'))) {
70 $public_contact_id = false;
73 return $public_contact_id;
77 * Returns public contact id of authenticated site visitor or false
79 * @return int|bool visitor_id or false
81 public static function getRemoteUser()
83 $session = DI::session();
85 if (empty($session->get('authenticated'))) {
89 if (!empty($session->get('visitor_id'))) {
90 return intval($session->get('visitor_id'));
97 * Return the user contact ID of a visitor for the given user ID they are visiting
99 * @param integer $uid User ID
102 public static function getRemoteContactID($uid)
104 $session = DI::session();
106 if (!empty($session->get('remote')[$uid])) {
107 $remote = $session->get('remote')[$uid];
112 $local_user = !empty($session->get('authenticated')) ? $session->get('uid') : 0;
114 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $session->get('my_address'))) {
115 $remote = Contact::getIdForURL($my_address, $uid, false);
122 * Returns User ID for given contact ID of the visitor
124 * @param integer $cid Contact ID
125 * @return integer User ID for given contact ID of the visitor
127 public static function getUserIDForVisitorContactID($cid)
129 $session = DI::session();
131 if (empty($session->get('remote'))) {
135 return array_search($cid, $session->get('remote'));
139 * Set the session variable that contains the contact IDs for the visitor's contact URL
141 * @param string $url Contact URL
143 public static function setVisitorsContacts()
145 $session = DI::session();
147 $session->set('remote', []);
150 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('my_url')), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
151 while ($contact = DBA::fetch($remote_contacts)) {
152 if (($contact['uid'] == 0) || Contact\User::isBlocked($contact['id'], $contact['uid'])) {
155 $remote[$contact['uid']] = $contact['id'];
157 DBA::close($remote_contacts);
158 $session->set('remote', $remote);
162 * Returns if the current visitor is authenticated
164 * @return boolean "true" when visitor is either a local or remote user
166 public static function isAuthenticated()
168 $session = DI::session();
170 return $session->get('authenticated', false);