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