]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/DatabaseSessionHandler.php
Merge pull request #7355 from JeroenED/task/relationship-status2/no-dead-nodes
[friendica.git] / src / Core / Session / DatabaseSessionHandler.php
1 <?php
2
3 namespace Friendica\Core\Session;
4
5 use Friendica\BaseObject;
6 use Friendica\Core\Logger;
7 use Friendica\Core\Session;
8 use Friendica\Database\DBA;
9 use SessionHandlerInterface;
10
11 /**
12  * SessionHandler using database
13  *
14  * @author Hypolite Petovan <hypolite@mrpetovan.com>
15  */
16 class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface
17 {
18         public function open($save_path, $session_name)
19         {
20                 return true;
21         }
22
23         public function read($session_id)
24         {
25                 if (empty($session_id)) {
26                         return '';
27                 }
28
29                 $session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]);
30                 if (DBA::isResult($session)) {
31                         Session::$exists = true;
32                         return $session['data'];
33                 }
34
35                 Logger::notice('no data for session', ['session_id' => $session_id, 'uri' => $_SERVER['REQUEST_URI']]);
36
37                 return '';
38         }
39
40         /**
41          * @brief Standard PHP session write callback
42          *
43          * This callback updates the DB-stored session data and/or the expiration depending
44          * on the case. Uses the Session::expire global for existing session, 5 minutes
45          * for newly created session.
46          *
47          * @param  string $session_id   Session ID with format: [a-z0-9]{26}
48          * @param  string $session_data Serialized session data
49          * @return boolean Returns false if parameters are missing, true otherwise
50          * @throws \Exception
51          */
52         public function write($session_id, $session_data)
53         {
54                 if (!$session_id) {
55                         return false;
56                 }
57
58                 if (!$session_data) {
59                         return true;
60                 }
61
62                 $expire = time() + Session::$expire;
63                 $default_expire = time() + 300;
64
65                 if (Session::$exists) {
66                         $fields = ['data' => $session_data, 'expire' => $expire];
67                         $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
68                         DBA::update('session', $fields, $condition);
69                 } else {
70                         $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
71                         DBA::insert('session', $fields);
72                 }
73
74                 return true;
75         }
76
77         public function close()
78         {
79                 return true;
80         }
81
82         public function destroy($id)
83         {
84                 DBA::delete('session', ['sid' => $id]);
85                 return true;
86         }
87
88         public function gc($maxlifetime)
89         {
90                 DBA::delete('session', ["`expire` < ?", time()]);
91                 return true;
92         }
93 }