]> git.mxchange.org Git - friendica.git/blob - src/Core/Session.php
Create own base URL class which holds the whole base url business logic
[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\Core\Session\CacheSessionHandler;
9 use Friendica\Core\Session\DatabaseSessionHandler;
10 use Friendica\Util\BaseURL;
11
12 /**
13  * High-level Session service class
14  *
15  * @author Hypolite Petovan <hypolite@mrpetovan.com>
16  */
17 class Session
18 {
19         public static $exists = false;
20         public static $expire = 180000;
21
22         public static function init()
23         {
24                 ini_set('session.gc_probability', 50);
25                 ini_set('session.use_only_cookies', 1);
26                 ini_set('session.cookie_httponly', 1);
27
28                 if (Config::get('system', 'ssl_policy') == BaseUrl::SSL_POLICY_FULL) {
29                         ini_set('session.cookie_secure', 1);
30                 }
31
32                 $session_handler = Config::get('system', 'session_handler', 'database');
33                 if ($session_handler != 'native') {
34                         if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') {
35                                 $SessionHandler = new CacheSessionHandler();
36                         } else {
37                                 $SessionHandler = new DatabaseSessionHandler();
38                         }
39
40                         session_set_save_handler($SessionHandler);
41                 }
42         }
43
44         public static function exists($name)
45         {
46                 return isset($_SESSION[$name]);
47         }
48
49         /**
50          * Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
51          * 
52          * Handle the case where session_start() hasn't been called and the super global isn't available.
53          *
54          * @param string $name
55          * @param mixed $defaults
56          * @return mixed
57          */
58         public static function get($name, $defaults = null)
59         {
60                 if (isset($_SESSION)) {
61                         $return = defaults($_SESSION, $name, $defaults);
62                 } else {
63                         $return = $defaults;
64                 }
65
66                 return $return;
67         }
68
69         public static function set($name, $value)
70         {
71                 $_SESSION[$name] = $value;
72         }
73 }