]> git.mxchange.org Git - friendica.git/blob - src/Core/Session.php
Normalize the return value type of Session::getRemoteContactID
[friendica.git] / src / Core / Session.php
1 <?php
2
3 /**
4  * @file src/Core/Session.php
5  */
6 namespace Friendica\Core;
7
8 use Friendica\Database\DBA;
9 use Friendica\DI;
10 use Friendica\Model\Contact;
11 use Friendica\Util\Strings;
12
13 /**
14  * High-level Session service class
15  *
16  * @author Hypolite Petovan <hypolite@mrpetovan.com>
17  */
18 class Session
19 {
20         public static $exists = false;
21         public static $expire = 180000;
22
23         public static function exists($name)
24         {
25                 return DI::session()->exists($name);
26         }
27
28         public static function get($name, $defaults = null)
29         {
30                 return DI::session()->get($name, $defaults);
31         }
32
33         public static function set($name, $value)
34         {
35                 DI::session()->set($name, $value);
36         }
37
38         public static function setMultiple(array $values)
39         {
40                 DI::session()->setMultiple($values);
41         }
42
43         public static function remove($name)
44         {
45                 DI::session()->remove($name);
46         }
47
48         public static function clear()
49         {
50                 DI::session()->clear();
51         }
52
53         /**
54          * Returns contact ID for given user ID
55          *
56          * @param integer $uid User ID
57          * @return integer Contact ID of visitor for given user ID
58          */
59         public static function getRemoteContactID($uid)
60         {
61                 $session = DI::session();
62
63                 if (empty($session->get('remote')[$uid])) {
64                         return 0;
65                 }
66
67                 return $session->get('remote')[$uid];
68         }
69
70         /**
71          * Returns User ID for given contact ID of the visitor
72          *
73          * @param integer $cid Contact ID
74          * @return integer User ID for given contact ID of the visitor
75          */
76         public static function getUserIDForVisitorContactID($cid)
77         {
78                 $session = DI::session();
79
80                 if (empty($session->get('remote'))) {
81                         return false;
82                 }
83
84                 return array_search($cid, $session->get('remote'));
85         }
86
87         /**
88          * Set the session variable that contains the contact IDs for the visitor's contact URL
89          *
90          * @param string $url Contact URL
91          */
92         public static function setVisitorsContacts()
93         {
94                 $session = DI::session();
95
96                 $session->set('remote', []);
97
98                 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('my_url')), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
99                 while ($contact = DBA::fetch($remote_contacts)) {
100                         if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
101                                 continue;
102                         }
103
104                         $session->set('remote', [$contact['uid'] => $contact['id']]);
105                 }
106                 DBA::close($remote_contacts);
107         }
108
109         /**
110          * Returns if the current visitor is authenticated
111          *
112          * @return boolean "true" when visitor is either a local or remote user
113          */
114         public static function isAuthenticated()
115         {
116                 $session = DI::session();
117
118                 return $session->get('authenticated', false);
119         }
120 }