]> git.mxchange.org Git - friendica.git/blob - include/session.php
Merge pull request #2978 from annando/issue-2518
[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         if (is_object($memcache)) {
68                 $memcache->set(get_app()->get_hostname().":session:".$id, $data, MEMCACHE_COMPRESSED, $expire);
69                 return true;
70         }
71
72         if ($session_exists) {
73                 $r = q("UPDATE `session`
74                                 SET `data` = '%s', `expire` = '%s'
75                                 WHERE `sid` = '%s'
76                                 AND (`data` != '%s' OR `expire` != '%s')",
77                                 dbesc($data), dbesc($expire), dbesc($id), dbesc($data), dbesc($expire));
78         } else {
79                 $r = q("INSERT INTO `session`
80                                 SET `sid` = '%s', `expire` = '%s', `data` = '%s'",
81                                 dbesc($id), dbesc($default_expire), dbesc($data));
82         }
83
84         return true;
85 }
86
87 function ref_session_close() {
88         return true;
89 }
90
91 function ref_session_destroy($id) {
92         $memcache = cache::memcache();
93
94         if (is_object($memcache)) {
95                 $memcache->delete(get_app()->get_hostname().":session:".$id);
96                 return true;
97         }
98
99         q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
100
101         return true;
102 }
103
104 function ref_session_gc($expire) {
105         q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time()));
106
107         return true;
108 }
109
110 $gc_probability = 50;
111
112 ini_set('session.gc_probability', $gc_probability);
113 ini_set('session.use_only_cookies', 1);
114 ini_set('session.cookie_httponly', 1);
115
116 if (!get_config('system', 'disable_database_session')) {
117         session_set_save_handler('ref_session_open', 'ref_session_close',
118                                 'ref_session_read', 'ref_session_write',
119                                 'ref_session_destroy', 'ref_session_gc');
120 }