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