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