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