]> git.mxchange.org Git - friendica.git/blob - src/Core/Session.php
ef26cd929edc1dfd69f73b1f7f7004c8d6ee470e
[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                 if (empty($_SESSION['remote'][$uid])) {
63                         return false;
64                 }
65
66                 return $_SESSION['remote'][$uid];
67         }
68
69         /**
70          * Returns User ID for given contact ID of the visitor
71          *
72          * @param integer $cid Contact ID
73          * @return integer User ID for given contact ID of the visitor
74          */
75         public static function getUserIDForVisitorContactID($cid)
76         {
77                 if (empty($_SESSION['remote'])) {
78                         return false;
79                 }
80
81                 return array_search($cid, $_SESSION['remote']);
82         }
83
84         /**
85          * Set the session variable that contains the contact IDs for the visitor's contact URL
86          *
87          * @param string $url Contact URL
88          */
89         public static function setVisitorsContacts()
90         {
91                 $_SESSION['remote'] = [];
92
93                 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
94                 while ($contact = DBA::fetch($remote_contacts)) {
95                         if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
96                                 continue;
97                         }
98
99                         $_SESSION['remote'][$contact['uid']] = $contact['id'];
100                 }
101                 DBA::close($remote_contacts);
102         }
103
104         /**
105          * Returns if the current visitor is authenticated
106          *
107          * @return boolean "true" when visitor is either a local or remote user
108          */
109         public static function isAuthenticated()
110         {
111                 if (empty($_SESSION['authenticated'])) {
112                         return false;
113                 }
114
115                 return $_SESSION['authenticated'];
116         }
117
118         public static function delete()
119         {
120                 self::getClass(ISession::class)->delete();
121         }
122 }