]> git.mxchange.org Git - friendica.git/blob - src/Core/Session.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / src / Core / Session.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;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Contact;
27 use Friendica\Util\Strings;
28
29 /**
30  * High-level Session service class
31  */
32 class Session
33 {
34         public static $exists = false;
35         public static $expire = 180000;
36
37         /**
38          * Returns the user id of locally logged in user or false.
39          *
40          * @return int|bool user id or false
41          */
42         public static function getLocalUser()
43         {
44                 $session = DI::session();
45
46                 if (!empty($session->get('authenticated')) && !empty($session->get('uid'))) {
47                         return intval($session->get('uid'));
48                 }
49
50                 return false;
51         }
52
53         /**
54          * Returns the public contact id of logged in user or false.
55          *
56          * @return int|bool public contact id or false
57          */
58         public static function getPublicContact()
59         {
60                 static $public_contact_id = false;
61
62                 $session = DI::session();
63
64                 if (!$public_contact_id && !empty($session->get('authenticated'))) {
65                         if (!empty($session->get('my_address'))) {
66                                 // Local user
67                                 $public_contact_id = intval(Contact::getIdForURL($session->get('my_address'), 0, false));
68                         } elseif (!empty($session->get('visitor_home'))) {
69                                 // Remote user
70                                 $public_contact_id = intval(Contact::getIdForURL($session->get('visitor_home'), 0, false));
71                         }
72                 } elseif (empty($session->get('authenticated'))) {
73                         $public_contact_id = false;
74                 }
75
76                 return $public_contact_id;
77         }
78
79         /**
80          * Returns public contact id of authenticated site visitor or false
81          *
82          * @return int|bool visitor_id or false
83          */
84         public static function getRemoteUser()
85         {
86                 $session = DI::session();
87
88                 if (empty($session->get('authenticated'))) {
89                         return false;
90                 }
91
92                 if (!empty($session->get('visitor_id'))) {
93                         return intval($session->get('visitor_id'));
94                 }
95
96                 return false;
97         }
98
99         /**
100          * Return the user contact ID of a visitor for the given user ID they are visiting
101          *
102          * @param integer $uid User ID
103          * @return integer
104          */
105         public static function getRemoteContactID($uid)
106         {
107                 $session = DI::session();
108
109                 if (!empty($session->get('remote')[$uid])) {
110                         $remote = $session->get('remote')[$uid];
111                 } else {
112                         $remote = 0;
113                 }
114
115                 $local_user = !empty($session->get('authenticated')) ? $session->get('uid') : 0;
116
117                 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $session->get('my_address'))) {
118                         $remote = Contact::getIdForURL($my_address, $uid, false);
119                 }
120
121                 return $remote;
122         }
123
124         /**
125          * Returns User ID for given contact ID of the visitor
126          *
127          * @param integer $cid Contact ID
128          * @return integer User ID for given contact ID of the visitor
129          */
130         public static function getUserIDForVisitorContactID($cid)
131         {
132                 $session = DI::session();
133
134                 if (empty($session->get('remote'))) {
135                         return false;
136                 }
137
138                 return array_search($cid, $session->get('remote'));
139         }
140
141         /**
142          * Set the session variable that contains the contact IDs for the visitor's contact URL
143          *
144          * @param string $url Contact URL
145          */
146         public static function setVisitorsContacts()
147         {
148                 $session = DI::session();
149
150                 $session->set('remote', []);
151                 $remote = [];
152
153                 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('my_url')), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
154                 while ($contact = DBA::fetch($remote_contacts)) {
155                         if (($contact['uid'] == 0) || Contact\User::isBlocked($contact['id'], $contact['uid'])) {
156                                 continue;
157                         }
158                         $remote[$contact['uid']] = $contact['id'];
159                 }
160                 DBA::close($remote_contacts);
161                 $session->set('remote', $remote);
162         }
163
164         /**
165          * Returns if the current visitor is authenticated
166          *
167          * @return boolean "true" when visitor is either a local or remote user
168          */
169         public static function isAuthenticated()
170         {
171                 $session = DI::session();
172
173                 return $session->get('authenticated', false);
174         }
175 }