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