]> git.mxchange.org Git - friendica.git/blob - src/Core/Session.php
Fix worker priorities
[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 set($name, $value)
48         {
49                 DI::session()->set($name, $value);
50         }
51
52         public static function setMultiple(array $values)
53         {
54                 DI::session()->setMultiple($values);
55         }
56
57         public static function remove($name)
58         {
59                 DI::session()->remove($name);
60         }
61
62         public static function clear()
63         {
64                 DI::session()->clear();
65         }
66
67         /**
68          * Return the user contact ID of a visitor for the given user ID they are visiting
69          *
70          * @param integer $uid User ID
71          * @return integer
72          */
73         public static function getRemoteContactID($uid)
74         {
75                 $session = DI::session();
76
77                 if (!empty($session->get('remote')[$uid])) {
78                         $remote = $session->get('remote')[$uid];
79                 } else {
80                         $remote = 0;
81                 }
82
83                 $local_user = !empty($session->get('authenticated')) ? $session->get('uid') : 0;
84
85                 if (empty($remote) && ($local_user != $uid) && !empty($my_address = $session->get('my_address'))) {
86                         $remote = Contact::getIdForURL($my_address, $uid, false);
87                 }
88
89                 return $remote;
90         }
91
92         /**
93          * Returns User ID for given contact ID of the visitor
94          *
95          * @param integer $cid Contact ID
96          * @return integer User ID for given contact ID of the visitor
97          */
98         public static function getUserIDForVisitorContactID($cid)
99         {
100                 $session = DI::session();
101
102                 if (empty($session->get('remote'))) {
103                         return false;
104                 }
105
106                 return array_search($cid, $session->get('remote'));
107         }
108
109         /**
110          * Set the session variable that contains the contact IDs for the visitor's contact URL
111          *
112          * @param string $url Contact URL
113          */
114         public static function setVisitorsContacts()
115         {
116                 $session = DI::session();
117
118                 $session->set('remote', []);
119                 $remote = [];
120
121                 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('my_url')), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
122                 while ($contact = DBA::fetch($remote_contacts)) {
123                         if (($contact['uid'] == 0) || Contact\User::isBlocked($contact['id'], $contact['uid'])) {
124                                 continue;
125                         }
126                         $remote[$contact['uid']] = $contact['id'];
127                 }
128                 DBA::close($remote_contacts);
129                 $session->set('remote', $remote);
130         }
131
132         /**
133          * Returns if the current visitor is authenticated
134          *
135          * @return boolean "true" when visitor is either a local or remote user
136          */
137         public static function isAuthenticated()
138         {
139                 $session = DI::session();
140
141                 return $session->get('authenticated', false);
142         }
143 }