]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Database.php
Overwrite constructor of Memory session handling so no session ini-setting in backend...
[friendica.git] / src / Core / Session / Database.php
1 <?php
2
3 namespace Friendica\Core\Session;
4
5 use Friendica\Core\Config\Configuration;
6 use Friendica\Core\Session;
7 use Friendica\Database\Database as DBA;
8 use Friendica\Model\User\Cookie;
9 use Psr\Log\LoggerInterface;
10 use SessionHandlerInterface;
11
12 /**
13  * SessionHandler using database
14  *
15  * @author Hypolite Petovan <hypolite@mrpetovan.com>
16  */
17 final class Database extends Native implements SessionHandlerInterface
18 {
19         /** @var DBA */
20         private $dba;
21         /** @var LoggerInterface */
22         private $logger;
23         /** @var array The $_SERVER variable */
24         private $server;
25
26         /**
27          * DatabaseSessionHandler constructor.
28          *
29          * @param Database        $dba
30          * @param LoggerInterface $logger
31          * @param array           $server
32          */
33         public function __construct(Configuration $config, Cookie $cookie, DBA $dba, LoggerInterface $logger, array $server)
34         {
35                 parent::__construct($config, $cookie);
36
37                 $this->dba    = $dba;
38                 $this->logger = $logger;
39                 $this->server = $server;
40
41                 session_set_save_handler($this);
42         }
43
44         public function open($save_path, $session_name)
45         {
46                 return true;
47         }
48
49         public function read($session_id)
50         {
51                 if (empty($session_id)) {
52                         return '';
53                 }
54
55                 $session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
56                 if ($this->dba->isResult($session)) {
57                         Session::$exists = true;
58                         return $session['data'];
59                 }
60
61                 $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
62
63                 return '';
64         }
65
66         /**
67          * @brief Standard PHP session write callback
68          *
69          * This callback updates the DB-stored session data and/or the expiration depending
70          * on the case. Uses the Session::expire global for existing session, 5 minutes
71          * for newly created session.
72          *
73          * @param string $session_id   Session ID with format: [a-z0-9]{26}
74          * @param string $session_data Serialized session data
75          *
76          * @return boolean Returns false if parameters are missing, true otherwise
77          * @throws \Exception
78          */
79         public function write($session_id, $session_data)
80         {
81                 if (!$session_id) {
82                         return false;
83                 }
84
85                 if (!$session_data) {
86                         return true;
87                 }
88
89                 $expire         = time() + Session::$expire;
90                 $default_expire = time() + 300;
91
92                 if (Session::$exists) {
93                         $fields    = ['data' => $session_data, 'expire' => $expire];
94                         $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
95                         $this->dba->update('session', $fields, $condition);
96                 } else {
97                         $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
98                         $this->dba->insert('session', $fields);
99                 }
100
101                 return true;
102         }
103
104         public function close()
105         {
106                 return true;
107         }
108
109         public function destroy($id)
110         {
111                 return $this->dba->delete('session', ['sid' => $id]);
112         }
113
114         public function gc($maxlifetime)
115         {
116                 return $this->dba->delete('session', ["`expire` < ?", time()]);
117         }
118 }