]> git.mxchange.org Git - friendica.git/blob - src/Core/Session.php
The user related functions moved to the session class
[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         public static function exists($name)
38         {
39                 return DI::session()->exists($name);
40         }
41
42         public static function get($name, $defaults = null)
43         {
44                 return DI::session()->get($name, $defaults);
45         }
46
47         public static function pop($name, $defaults = null)
48         {
49                 return DI::session()->pop($name, $defaults);
50         }
51
52         public static function set($name, $value)
53         {
54                 DI::session()->set($name, $value);
55         }
56
57         public static function setMultiple(array $values)
58         {
59                 DI::session()->setMultiple($values);
60         }
61
62         public static function remove($name)
63         {
64                 DI::session()->remove($name);
65         }
66
67         public static function clear()
68         {
69                 DI::session()->clear();
70         }
71
72         /**
73          * Returns the user id of locally logged in user or false.
74          *
75          * @return int|bool user id or false
76          */
77         public static function getLocalUser()
78         {
79                 $session = DI::session();
80
81                 if (!empty($session->get('authenticated')) && !empty($session->get('uid'))) {
82                         return intval($session->get('uid'));
83                 }
84
85                 return false;
86         }
87
88         /**
89          * Returns the public contact id of logged in user or false.
90          *
91          * @return int|bool public contact id or false
92          */
93         public static function getPublicContact()
94         {
95                 static $public_contact_id = false;
96
97                 $session = DI::session();
98
99                 if (!$public_contact_id && !empty($session->get('authenticated'))) {
100                         if (!empty($session->get('my_address'))) {
101                                 // Local user
102                                 $public_contact_id = intval(Contact::getIdForURL($session->get('my_address'), 0, false));
103                         } elseif (!empty($session->get('visitor_home'))) {
104                                 // Remote user
105                                 $public_contact_id = intval(Contact::getIdForURL($session->get('visitor_home'), 0, false));
106                         }
107                 } elseif (empty($session->get('authenticated'))) {
108                         $public_contact_id = false;
109                 }
110
111                 return $public_contact_id;
112         }
113
114         /**
115          * Returns public contact id of authenticated site visitor or false
116          *
117          * @return int|bool visitor_id or false
118          */
119         public static function getRemoteUser()
120         {
121                 $session = DI::session();
122
123                 if (empty($session->get('authenticated'))) {
124                         return false;
125                 }
126
127                 if (!empty($session->get('visitor_id'))) {
128                         return intval($session->get('visitor_id'));
129                 }
130
131                 return false;
132         }
133
134         /**
135          * Return the user contact ID of a visitor for the given user ID they are visiting
136          *
137          * @param integer $uid User ID
138          * @return integer
139          */
140         public static function getRemoteContactID($uid)
141         {
142                 $session = DI::session();
143
144                 if (!empty($session->get('remote')[$uid])) {
145                         $remote = $session->get('remote')[$uid];
146                 } else {
147                         $remote = 0;
148                 }
149
150                 $local_user = !empty($session->get('authenticated')) ? $session->get('uid') : 0;
151
152                 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $session->get('my_address'))) {
153                         $remote = Contact::getIdForURL($my_address, $uid, false);
154                 }
155
156                 return $remote;
157         }
158
159         /**
160          * Returns User ID for given contact ID of the visitor
161          *
162          * @param integer $cid Contact ID
163          * @return integer User ID for given contact ID of the visitor
164          */
165         public static function getUserIDForVisitorContactID($cid)
166         {
167                 $session = DI::session();
168
169                 if (empty($session->get('remote'))) {
170                         return false;
171                 }
172
173                 return array_search($cid, $session->get('remote'));
174         }
175
176         /**
177          * Set the session variable that contains the contact IDs for the visitor's contact URL
178          *
179          * @param string $url Contact URL
180          */
181         public static function setVisitorsContacts()
182         {
183                 $session = DI::session();
184
185                 $session->set('remote', []);
186                 $remote = [];
187
188                 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('my_url')), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
189                 while ($contact = DBA::fetch($remote_contacts)) {
190                         if (($contact['uid'] == 0) || Contact\User::isBlocked($contact['id'], $contact['uid'])) {
191                                 continue;
192                         }
193                         $remote[$contact['uid']] = $contact['id'];
194                 }
195                 DBA::close($remote_contacts);
196                 $session->set('remote', $remote);
197         }
198
199         /**
200          * Returns if the current visitor is authenticated
201          *
202          * @return boolean "true" when visitor is either a local or remote user
203          */
204         public static function isAuthenticated()
205         {
206                 $session = DI::session();
207
208                 return $session->get('authenticated', false);
209         }
210 }