]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Model/UserSession.php
Merge remote-tracking branch 'origin/2022.12-rc' into fixes
[friendica.git] / src / Core / Session / Model / UserSession.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core\Session\Model;
23
24 use Friendica\Core\Session\Capability\IHandleSessions;
25 use Friendica\Core\Session\Capability\IHandleUserSessions;
26 use Friendica\Model\Contact;
27
28 /**
29  * This class handles user sessions, which is directly extended from regular session
30  */
31 class UserSession implements IHandleUserSessions
32 {
33         /** @var IHandleSessions */
34         private $session;
35         /** @var int|bool saves the public Contact ID for later usage */
36         protected $publicContactId = false;
37
38         public function __construct(IHandleSessions $session)
39         {
40                 $this->session = $session;
41         }
42
43         /** {@inheritDoc} */
44         public function getLocalUserId()
45         {
46                 if (!empty($this->session->get('authenticated')) && !empty($this->session->get('uid'))) {
47                         return intval($this->session->get('uid'));
48                 }
49
50                 return false;
51         }
52
53         /** {@inheritDoc} */
54         public function getPublicContactId()
55         {
56                 if (empty($this->publicContactId) && !empty($this->session->get('authenticated'))) {
57                         if (!empty($this->session->get('my_address'))) {
58                                 // Local user
59                                 $this->publicContactId = Contact::getIdForURL($this->session->get('my_address'), 0, false);
60                         } elseif (!empty($this->session->get('visitor_home'))) {
61                                 // Remote user
62                                 $this->publicContactId = Contact::getIdForURL($this->session->get('visitor_home'), 0, false);
63                         }
64                 } elseif (empty($this->session->get('authenticated'))) {
65                         $this->publicContactId = false;
66                 }
67
68                 return $this->publicContactId;
69         }
70
71         /** {@inheritDoc} */
72         public function getRemoteUserId()
73         {
74                 if (empty($this->session->get('authenticated'))) {
75                         return false;
76                 }
77
78                 if (!empty($this->session->get('visitor_id'))) {
79                         return (int)$this->session->get('visitor_id');
80                 }
81
82                 return false;
83         }
84
85         /** {@inheritDoc} */
86         public function getRemoteContactID(int $uid): int
87         {
88                 if (!empty($this->session->get('remote')[$uid])) {
89                         $remote = $this->session->get('remote')[$uid];
90                 } else {
91                         $remote = 0;
92                 }
93
94                 $local_user = !empty($this->session->get('authenticated')) ? $this->session->get('uid') : 0;
95
96                 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $this->session->get('my_address'))) {
97                         $remote = Contact::getIdForURL($my_address, $uid, false);
98                 }
99
100                 return $remote;
101         }
102
103         /** {@inheritDoc} */
104         public function getUserIDForVisitorContactID(int $cid): int
105         {
106                 if (empty($this->session->get('remote'))) {
107                         return false;
108                 }
109
110                 return array_search($cid, $this->session->get('remote'));
111         }
112
113         /** {@inheritDoc} */
114         public function getMyUrl(): string
115         {
116                 return $this->session->get('my_url', '');
117         }
118
119         /** {@inheritDoc} */
120         public function isAuthenticated(): bool
121         {
122                 return $this->session->get('authenticated', false);
123         }
124
125         /** {@inheritDoc} */
126         public function setVisitorsContacts()
127         {
128                 $this->session->set('remote', Contact::getVisitorByUrl($this->session->get('my_url')));
129         }
130
131         /** {@inheritDoc} */
132         public function getSubManagedUserId()
133         {
134                 return $this->session->get('submanage') ?? false;
135         }
136
137         /** {@inheritDoc} */
138         public function setSubManagedUserId(int $managed_uid): void
139         {
140                 $this->session->set('submanage', $managed_uid);
141         }
142
143         /** {@inheritDoc} */
144         public function start(): IHandleSessions
145         {
146                 return $this;
147         }
148
149         /** {@inheritDoc} */
150         public function exists(string $name): bool
151         {
152                 return $this->session->exists($name);
153         }
154
155         /** {@inheritDoc} */
156         public function get(string $name, $defaults = null)
157         {
158                 return $this->session->get($name, $defaults);
159         }
160
161         /** {@inheritDoc} */
162         public function pop(string $name, $defaults = null)
163         {
164                 return $this->session->pop($name, $defaults);
165         }
166
167         /** {@inheritDoc} */
168         public function set(string $name, $value)
169         {
170                 $this->session->set($name, $value);
171         }
172
173         /** {@inheritDoc} */
174         public function setMultiple(array $values)
175         {
176                 $this->session->setMultiple($values);
177         }
178
179         /** {@inheritDoc} */
180         public function remove(string $name)
181         {
182                 $this->session->remove($name);
183         }
184
185         /** {@inheritDoc} */
186         public function clear()
187         {
188                 $this->session->clear();
189         }
190 }