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\Session\Model;
24 use Friendica\Core\Session\Capability\IHandleSessions;
25 use Friendica\Core\Session\Capability\IHandleUserSessions;
26 use Friendica\Model\Contact;
28 class UserSession implements IHandleUserSessions
30 /** @var IHandleSessions */
32 /** @var int|bool saves the public Contact ID for later usage */
33 protected $publicContactId = false;
35 public function __construct(IHandleSessions $session)
37 $this->session = $session;
41 public function getLocalUserId()
43 if (!empty($this->session->get('authenticated')) && !empty($this->session->get('uid'))) {
44 return intval($this->session->get('uid'));
51 public function getPublicContactId()
53 if (empty($this->publicContactId) && !empty($this->session->get('authenticated'))) {
54 if (!empty($this->session->get('my_address'))) {
56 $this->publicContactId = Contact::getIdForURL($this->session->get('my_address'), 0, false);
57 } elseif (!empty($this->session->get('visitor_home'))) {
59 $this->publicContactId = Contact::getIdForURL($this->session->get('visitor_home'), 0, false);
61 } elseif (empty($this->session->get('authenticated'))) {
62 $this->publicContactId = false;
65 return $this->publicContactId;
69 public function getRemoteUserId()
71 if (empty($this->session->get('authenticated'))) {
75 if (!empty($this->session->get('visitor_id'))) {
76 return (int)$this->session->get('visitor_id');
83 public function getRemoteContactID(int $uid): int
85 if (!empty($this->session->get('remote')[$uid])) {
86 $remote = $this->session->get('remote')[$uid];
91 $local_user = !empty($this->session->get('authenticated')) ? $this->session->get('uid') : 0;
93 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $this->session->get('my_address'))) {
94 $remote = Contact::getIdForURL($my_address, $uid, false);
101 public function getUserIDForVisitorContactID(int $cid): int
103 if (empty($this->session->get('remote'))) {
107 return array_search($cid, $this->session->get('remote'));
111 public function isAuthenticated(): bool
113 return $this->session->get('authenticated', false);
117 public function setVisitorsContacts()
119 $this->session->set('remote', Contact::getVisitorByUrl($this->session->get('my_url')));