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;
29 * This class handles user sessions, which is directly extended from regular session
31 class UserSession implements IHandleUserSessions
33 /** @var IHandleSessions */
35 /** @var int|bool saves the public Contact ID for later usage */
36 protected $publicContactId = false;
38 public function __construct(IHandleSessions $session)
40 $this->session = $session;
44 public function getLocalUserId()
46 if (!empty($this->session->get('authenticated')) && !empty($this->session->get('uid'))) {
47 return intval($this->session->get('uid'));
54 public function getPublicContactId()
56 if (empty($this->publicContactId) && !empty($this->session->get('authenticated'))) {
57 if (!empty($this->session->get('my_address'))) {
59 $this->publicContactId = Contact::getIdForURL($this->session->get('my_address'), 0, false);
60 } elseif (!empty($this->session->get('visitor_home'))) {
62 $this->publicContactId = Contact::getIdForURL($this->session->get('visitor_home'), 0, false);
64 } elseif (empty($this->session->get('authenticated'))) {
65 $this->publicContactId = false;
68 return $this->publicContactId;
72 public function getRemoteUserId()
74 if (empty($this->session->get('authenticated'))) {
78 if (!empty($this->session->get('visitor_id'))) {
79 return (int)$this->session->get('visitor_id');
86 public function getRemoteContactID(int $uid): int
88 if (!empty($this->session->get('remote')[$uid])) {
89 $remote = $this->session->get('remote')[$uid];
94 $local_user = !empty($this->session->get('authenticated')) ? $this->session->get('uid') : 0;
96 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $this->session->get('my_address'))) {
97 $remote = Contact::getIdForURL($my_address, $uid, false);
104 public function getUserIDForVisitorContactID(int $cid): int
106 if (empty($this->session->get('remote'))) {
110 return array_search($cid, $this->session->get('remote'));
114 public function getMyUrl(): string
116 return $this->session->get('my_url', '');
120 public function isAuthenticated(): bool
122 return $this->session->get('authenticated', false);
126 public function setVisitorsContacts()
128 $this->session->set('remote', Contact::getVisitorByUrl($this->session->get('my_url')));
132 public function getSubManagedUserId()
134 return $this->session->get('submanage') ?? false;
138 public function setSubManagedUserId(int $managed_uid): void
140 $this->session->set('submanage', $managed_uid);
144 public function start(): IHandleSessions
150 public function exists(string $name): bool
152 return $this->session->exists($name);
156 public function get(string $name, $defaults = null)
158 return $this->session->get($name, $defaults);
162 public function pop(string $name, $defaults = null)
164 return $this->session->pop($name, $defaults);
168 public function set(string $name, $value)
170 $this->session->set($name, $value);
174 public function setMultiple(array $values)
176 $this->session->setMultiple($values);
180 public function remove(string $name)
182 $this->session->remove($name);
186 public function clear()
188 $this->session->clear();