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