]> git.mxchange.org Git - friendica.git/blob - include/session.php
Improved type detection
[friendica.git] / include / session.php
1 <?php
2 // Session management functions. These provide database storage of PHP
3 // session info.
4
5 use Friendica\Core\Config;
6
7 require_once('include/cache.php');
8
9 $session_exists = 0;
10 $session_expire = 180000;
11
12 function ref_session_open($s, $n) {
13         return true;
14 }
15
16 function ref_session_read($id) {
17         global $session_exists;
18
19         if (!x($id)) {
20                 return '';
21         }
22
23         $memcache = cache::memcache();
24         if (is_object($memcache)) {
25                 $data = $memcache->get(get_app()->get_hostname().":session:".$id);
26                 if (!is_bool($data)) {
27                         return $data;
28                 }
29                 logger("no data for session $id", LOGGER_TRACE);
30                 return '';
31         }
32
33         $r = q("SELECT `data` FROM `session` WHERE `sid`= '%s'", dbesc($id));
34
35         if (dbm::is_result($r)) {
36                 $session_exists = true;
37                 return $r[0]['data'];
38         } else {
39                 logger("no data for session $id", LOGGER_TRACE);
40         }
41
42         return '';
43 }
44
45 /**
46  * @brief Standard PHP session write callback
47  *
48  * This callback updates the DB-stored session data and/or the expiration depending
49  * on the case. Uses the $session_expire global for existing session, 5 minutes
50  * for newly created session.
51  *
52  * @global bool $session_exists Whether a session with the given id already exists
53  * @global int $session_expire Session expiration delay in seconds
54  * @param string $id Session ID with format: [a-z0-9]{26}
55  * @param string $data Serialized session data
56  * @return boolean Returns false if parameters are missing, true otherwise
57  */
58 function ref_session_write($id, $data) {
59         global $session_exists, $session_expire;
60
61         if (!$id || !$data) {
62                 return false;
63         }
64
65         $expire = time() + $session_expire;
66         $default_expire = time() + 300;
67
68         $memcache = cache::memcache();
69         $a = get_app();
70         if (is_object($memcache) && is_object($a)) {
71                 $memcache->set($a->get_hostname().":session:".$id, $data, MEMCACHE_COMPRESSED, $expire);
72                 return true;
73         }
74
75         if ($session_exists) {
76                 $r = q("UPDATE `session`
77                                 SET `data` = '%s', `expire` = '%s'
78                                 WHERE `sid` = '%s'
79                                 AND (`data` != '%s' OR `expire` != '%s')",
80                                 dbesc($data), dbesc($expire), dbesc($id), dbesc($data), dbesc($expire));
81         } else {
82                 $r = q("INSERT INTO `session`
83                                 SET `sid` = '%s', `expire` = '%s', `data` = '%s'",
84                                 dbesc($id), dbesc($default_expire), dbesc($data));
85         }
86
87         return true;
88 }
89
90 function ref_session_close() {
91         return true;
92 }
93
94 function ref_session_destroy($id) {
95         $memcache = cache::memcache();
96
97         if (is_object($memcache)) {
98                 $memcache->delete(get_app()->get_hostname().":session:".$id);
99                 return true;
100         }
101
102         q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
103
104         return true;
105 }
106
107 function ref_session_gc($expire) {
108         q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time()));
109
110         return true;
111 }
112
113 $gc_probability = 50;
114
115 ini_set('session.gc_probability', $gc_probability);
116 ini_set('session.use_only_cookies', 1);
117 ini_set('session.cookie_httponly', 1);
118
119 if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL) {
120         ini_set('session.cookie_secure', 1);
121 }
122
123 if (!get_config('system', 'disable_database_session')) {
124         session_set_save_handler('ref_session_open', 'ref_session_close',
125                                 'ref_session_read', 'ref_session_write',
126                                 'ref_session_destroy', 'ref_session_gc');
127 }