3 * @copyright Copyright (C) 2010-2023, 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;
27 use Friendica\Model\User;
30 * This class handles user sessions, which is directly extended from regular session
32 class UserSession implements IHandleUserSessions
34 /** @var IHandleSessions */
36 /** @var int|bool saves the public Contact ID for later usage */
37 protected $publicContactId = false;
39 public function __construct(IHandleSessions $session)
41 $this->session = $session;
45 public function getLocalUserId()
47 if (!empty($this->session->get('authenticated')) && !empty($this->session->get('uid'))) {
48 return intval($this->session->get('uid'));
55 public function getLocalUserNickname()
57 if ($this->isAuthenticated()) {
58 return $this->session->get('nickname');
65 public function getPublicContactId()
67 if (empty($this->publicContactId) && !empty($this->session->get('authenticated'))) {
68 if (!empty($this->session->get('my_address'))) {
70 $this->publicContactId = Contact::getIdForURL($this->session->get('my_address'), 0, false);
71 } elseif (!empty($this->session->get('visitor_home'))) {
73 $this->publicContactId = Contact::getIdForURL($this->session->get('visitor_home'), 0, false);
75 } elseif (empty($this->session->get('authenticated'))) {
76 $this->publicContactId = false;
79 return $this->publicContactId;
83 public function getRemoteUserId()
85 if (empty($this->session->get('authenticated'))) {
89 if (!empty($this->session->get('visitor_id'))) {
90 return (int)$this->session->get('visitor_id');
97 public function getRemoteContactID(int $uid): int
99 if (!empty($this->session->get('remote')[$uid])) {
100 $remote = $this->session->get('remote')[$uid];
105 $local_user = !empty($this->session->get('authenticated')) ? $this->session->get('uid') : 0;
107 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $this->session->get('my_address'))) {
108 $remote = Contact::getIdForURL($my_address, $uid, false);
115 public function getUserIDForVisitorContactID(int $cid): int
117 if (empty($this->session->get('remote'))) {
121 return array_search($cid, $this->session->get('remote'));
125 public function getMyUrl(): string
127 return $this->session->get('my_url', '');
131 public function isAuthenticated(): bool
133 return $this->session->get('authenticated', false);
137 public function isSiteAdmin(): bool
139 return User::isSiteAdmin($this->getLocalUserId());
143 public function setVisitorsContacts(string $my_url)
145 $this->session->set('remote', Contact::getVisitorByUrl($my_url));
149 public function getSubManagedUserId()
151 return $this->session->get('submanage') ?? false;
155 public function setSubManagedUserId(int $managed_uid): void
157 $this->session->set('submanage', $managed_uid);
161 public function start(): IHandleSessions
167 public function exists(string $name): bool
169 return $this->session->exists($name);
173 public function get(string $name, $defaults = null)
175 return $this->session->get($name, $defaults);
179 public function pop(string $name, $defaults = null)
181 return $this->session->pop($name, $defaults);
185 public function set(string $name, $value)
187 $this->session->set($name, $value);
191 public function setMultiple(array $values)
193 $this->session->setMultiple($values);
197 public function remove(string $name)
199 $this->session->remove($name);
203 public function clear()
205 $this->session->clear();