]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/DatabaseSessionHandler.php
Merge pull request #5038 from tobiasd/20180519-3834
[friendica.git] / src / Core / Session / DatabaseSessionHandler.php
1 <?php
2
3 namespace Friendica\Core\Session;
4
5 use Friendica\BaseObject;
6 use Friendica\Core\Session;
7 use Friendica\Database\DBM;
8 use SessionHandlerInterface;
9 use dba;
10
11 require_once 'boot.php';
12 require_once 'include/dba.php';
13 require_once 'include/text.php';
14
15 /**
16  * SessionHandler using database
17  *
18  * @author Hypolite Petovan <mrpetovan@gmail.com>
19  */
20 class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface
21 {
22         public function open($save_path, $session_name)
23         {
24                 return true;
25         }
26
27         public function read($session_id)
28         {
29                 if (!x($session_id)) {
30                         return '';
31                 }
32
33                 $session = dba::selectFirst('session', ['data'], ['sid' => $session_id]);
34                 if (DBM::is_result($session)) {
35                         Session::$exists = true;
36                         return $session['data'];
37                 }
38                 logger("no data for session $session_id", LOGGER_TRACE);
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          * @param  string $session_id   Session ID with format: [a-z0-9]{26}
51          * @param  string $session_data Serialized session data
52          * @return boolean Returns false if parameters are missing, true otherwise
53          */
54         public function write($session_id, $session_data)
55         {
56                 if (!$session_id) {
57                         return false;
58                 }
59
60                 if (!$session_data) {
61                         return true;
62                 }
63
64                 $expire = time() + Session::$expire;
65                 $default_expire = time() + 300;
66
67                 if (Session::$exists) {
68                         $fields = ['data' => $session_data, 'expire' => $expire];
69                         $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
70                         dba::update('session', $fields, $condition);
71                 } else {
72                         $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
73                         dba::insert('session', $fields);
74                 }
75
76                 return true;
77         }
78
79         public function close()
80         {
81                 return true;
82         }
83
84         public function destroy($id)
85         {
86                 dba::delete('session', ['sid' => $id]);
87                 return true;
88         }
89
90         public function gc($maxlifetime)
91         {
92                 dba::delete('session', ["`expire` < ?", time()]);
93                 return true;
94         }
95 }