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