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