]> git.mxchange.org Git - friendica.git/blob - src/Core/Session.php
542307a5ca4a2d27b6acb60f2f2d727f8e47737b
[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\App;
9 use Friendica\BaseObject;
10 use Friendica\Core\Session\CacheSessionHandler;
11 use Friendica\Core\Session\DatabaseSessionHandler;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\User;
15 use Friendica\Util\Strings;
16
17 /**
18  * High-level Session service class
19  *
20  * @author Hypolite Petovan <hypolite@mrpetovan.com>
21  */
22 class Session
23 {
24         public static $exists = false;
25         public static $expire = 180000;
26
27         public static function init()
28         {
29                 ini_set('session.gc_probability', 50);
30                 ini_set('session.use_only_cookies', 1);
31                 ini_set('session.cookie_httponly', 1);
32
33                 if (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL) {
34                         ini_set('session.cookie_secure', 1);
35                 }
36
37                 $session_handler = Config::get('system', 'session_handler', 'database');
38                 if ($session_handler != 'native') {
39                         if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') {
40                                 $SessionHandler = new CacheSessionHandler();
41                         } else {
42                                 $SessionHandler = new DatabaseSessionHandler();
43                         }
44
45                         session_set_save_handler($SessionHandler);
46                 }
47         }
48
49         public static function exists($name)
50         {
51                 return isset($_SESSION[$name]);
52         }
53
54         /**
55          * Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
56          *
57          * Handle the case where session_start() hasn't been called and the super global isn't available.
58          *
59          * @param string $name
60          * @param mixed $defaults
61          * @return mixed
62          */
63         public static function get($name, $defaults = null)
64         {
65                 return $_SESSION[$name] ?? $defaults;
66         }
67
68         /**
69          * Sets a single session variable.
70          * Overrides value of existing key.
71          *
72          * @param string $name
73          * @param mixed $value
74          */
75         public static function set($name, $value)
76         {
77                 $_SESSION[$name] = $value;
78         }
79
80         /**
81          * Sets multiple session variables.
82          * Overrides values for existing keys.
83          *
84          * @param array $values
85          */
86         public static function setMultiple(array $values)
87         {
88                 $_SESSION = $values + $_SESSION;
89         }
90
91         /**
92          * Removes a session variable.
93          * Ignores missing keys.
94          *
95          * @param $name
96          */
97         public static function remove($name)
98         {
99                 unset($_SESSION[$name]);
100         }
101
102         /**
103          * Clears the current session array
104          */
105         public static function clear()
106         {
107                 session_unset();
108                 session_start();
109                 $_SESSION = [];
110         }
111
112         /**
113          * Returns contact ID for given user ID
114          *
115          * @param integer $uid User ID
116          * @return integer Contact ID of visitor for given user ID
117          */
118         public static function getRemoteContactID($uid)
119         {
120                 if (empty($_SESSION['remote'][$uid])) {
121                         return false;
122                 }
123
124                 return $_SESSION['remote'][$uid];
125         }
126
127         /**
128          * Returns User ID for given contact ID of the visitor
129          *
130          * @param integer $cid Contact ID
131          * @return integer User ID for given contact ID of the visitor
132          */
133         public static function getUserIDForVisitorContactID($cid)
134         {
135                 if (empty($_SESSION['remote'])) {
136                         return false;
137                 }
138
139                 return array_search($cid, $_SESSION['remote']);
140         }
141
142         /**
143          * Set the session variable that contains the contact IDs for the visitor's contact URL
144          *
145          * @param string $url Contact URL
146          */
147         public static function setVisitorsContacts()
148         {
149                 $_SESSION['remote'] = [];
150
151                 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
152                 while ($contact = DBA::fetch($remote_contacts)) {
153                         if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
154                                 continue;
155                         }
156
157                         $_SESSION['remote'][$contact['uid']] = $contact['id'];
158                 }
159                 DBA::close($remote_contacts);
160         }
161
162         /**
163          * Returns if the current visitor is authenticated
164          *
165          * @return boolean "true" when visitor is either a local or remote user
166          */
167         public static function isAuthenticated()
168         {
169                 if (empty($_SESSION['authenticated'])) {
170                         return false;
171                 }
172
173                 return $_SESSION['authenticated'];
174         }
175
176         /**
177          * @brief Kills the "Friendica" cookie and all session data
178          */
179         public static function delete()
180         {
181                 /** @var User\Cookie $cookie */
182                 $cookie = BaseObject::getClass(User\Cookie::class);
183                 $cookie->clear();
184                 $_SESSION = [];
185                 session_unset();
186                 session_destroy();
187         }
188 }