2 // Session management functions. These provide database storage of PHP
5 use Friendica\Core\Config;
7 require_once('include/cache.php');
10 $session_expire = 180000;
12 function ref_session_open($s, $n) {
16 function ref_session_read($id) {
17 global $session_exists;
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 $session_exists = true;
30 logger("no data for session $id", LOGGER_TRACE);
34 $r = dba::select('session', array('data'), array('sid' => $id), array('limit' => 1));
35 if (dbm::is_result($r)) {
36 $session_exists = true;
39 logger("no data for session $id", LOGGER_TRACE);
46 * @brief Standard PHP session write callback
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.
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
58 function ref_session_write($id, $data) {
59 global $session_exists, $session_expire;
65 $expire = time() + $session_expire;
66 $default_expire = time() + 300;
68 $memcache = cache::memcache();
70 if (is_object($memcache) && is_object($a)) {
71 $memcache->set($a->get_hostname().":session:".$id, $data, MEMCACHE_COMPRESSED, $expire);
75 if ($session_exists) {
76 $fields = array('data' => $data, 'expire' => $expire);
77 $condition = array("`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire);
78 dba::update('session', $fields, $condition);
80 $fields = array('sid' => $id, 'expire' => $default_expire, 'data' => $data);
81 dba::insert('session', $fields);
87 function ref_session_close() {
91 function ref_session_destroy($id) {
92 $memcache = cache::memcache();
94 if (is_object($memcache)) {
95 $memcache->delete(get_app()->get_hostname().":session:".$id);
99 dba::delete('session', array('sid' => $id));
103 function ref_session_gc($expire) {
104 dba::delete('session', array("`expire` < ?", time()));
108 $gc_probability = 50;
110 ini_set('session.gc_probability', $gc_probability);
111 ini_set('session.use_only_cookies', 1);
112 ini_set('session.cookie_httponly', 1);
114 if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL) {
115 ini_set('session.cookie_secure', 1);
118 if (!Config::get('system', 'disable_database_session')) {
119 session_set_save_handler('ref_session_open', 'ref_session_close',
120 'ref_session_read', 'ref_session_write',
121 'ref_session_destroy', 'ref_session_gc');