]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Model/UserSession.php
Merge pull request #12021 from nupplaphil/feat/session_util
[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 class UserSession implements IHandleUserSessions
29 {
30         /** @var IHandleSessions */
31         private $session;
32         /** @var int|bool saves the public Contact ID for later usage */
33         protected $publicContactId = false;
34
35         public function __construct(IHandleSessions $session)
36         {
37                 $this->session = $session;
38         }
39
40         /** {@inheritDoc} */
41         public function getLocalUserId()
42         {
43                 if (!empty($this->session->get('authenticated')) && !empty($this->session->get('uid'))) {
44                         return intval($this->session->get('uid'));
45                 }
46
47                 return false;
48         }
49
50         /** {@inheritDoc} */
51         public function getPublicContactId()
52         {
53                 if (empty($this->publicContactId) && !empty($this->session->get('authenticated'))) {
54                         if (!empty($this->session->get('my_address'))) {
55                                 // Local user
56                                 $this->publicContactId = Contact::getIdForURL($this->session->get('my_address'), 0, false);
57                         } elseif (!empty($this->session->get('visitor_home'))) {
58                                 // Remote user
59                                 $this->publicContactId = Contact::getIdForURL($this->session->get('visitor_home'), 0, false);
60                         }
61                 } elseif (empty($this->session->get('authenticated'))) {
62                         $this->publicContactId = false;
63                 }
64
65                 return $this->publicContactId;
66         }
67
68         /** {@inheritDoc} */
69         public function getRemoteUserId()
70         {
71                 if (empty($this->session->get('authenticated'))) {
72                         return false;
73                 }
74
75                 if (!empty($this->session->get('visitor_id'))) {
76                         return (int)$this->session->get('visitor_id');
77                 }
78
79                 return false;
80         }
81
82         /** {@inheritDoc} */
83         public function getRemoteContactID(int $uid): int
84         {
85                 if (!empty($this->session->get('remote')[$uid])) {
86                         $remote = $this->session->get('remote')[$uid];
87                 } else {
88                         $remote = 0;
89                 }
90
91                 $local_user = !empty($this->session->get('authenticated')) ? $this->session->get('uid') : 0;
92
93                 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $this->session->get('my_address'))) {
94                         $remote = Contact::getIdForURL($my_address, $uid, false);
95                 }
96
97                 return $remote;
98         }
99
100         /** {@inheritDoc} */
101         public function getUserIDForVisitorContactID(int $cid): int
102         {
103                 if (empty($this->session->get('remote'))) {
104                         return false;
105                 }
106
107                 return array_search($cid, $this->session->get('remote'));
108         }
109
110         /** {@inheritDoc} */
111         public function isAuthenticated(): bool
112         {
113                 return $this->session->get('authenticated', false);
114         }
115
116         /** {@inheritDoc} */
117         public function setVisitorsContacts()
118         {
119                 $this->session->set('remote', Contact::getVisitorByUrl($this->session->get('my_url')));
120         }
121 }