]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Handler/Database.php
Reverted some changes that won't work with PHP7.3
[friendica.git] / src / Core / Session / Handler / Database.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Session\Handler;
23
24 use Friendica\Database\Database as DBA;
25 use Psr\Log\LoggerInterface;
26
27 /**
28  * SessionHandler using database
29  */
30 class Database extends AbstractSessionHandler
31 {
32         /** @var DBA */
33         private $dba;
34         /** @var LoggerInterface */
35         private $logger;
36         /** @var array The $_SERVER variable */
37         private $server;
38         /** @var bool global check, if the current Session exists */
39         private $sessionExists = false;
40
41         /**
42          * DatabaseSessionHandler constructor.
43          *
44          * @param DBA             $dba
45          * @param LoggerInterface $logger
46          * @param array           $server
47          */
48         public function __construct(DBA $dba, LoggerInterface $logger, array $server)
49         {
50                 $this->dba    = $dba;
51                 $this->logger = $logger;
52                 $this->server = $server;
53         }
54
55         public function open($path, $name): bool
56         {
57                 return true;
58         }
59
60         public function read($id)
61         {
62                 if (empty($id)) {
63                         return '';
64                 }
65
66                 try {
67                         $session = $this->dba->selectFirst('session', ['data'], ['sid' => $id]);
68                         if ($this->dba->isResult($session)) {
69                                 $this->sessionExists = true;
70                                 return $session['data'];
71                         }
72                 } catch (\Exception $exception) {
73                         $this->logger->warning('Cannot read session.', ['id' => $id, 'exception' => $exception]);
74                         return '';
75                 }
76
77                 $this->logger->notice('no data for session', ['session_id' => $id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
78
79                 return '';
80         }
81
82         /**
83          * Standard PHP session write callback
84          *
85          * This callback updates the DB-stored session data and/or the expiration depending
86          * on the case. Uses the Session::expire global for existing session, 5 minutes
87          * for newly created session.
88          *
89          * @param string $id   Session ID with format: [a-z0-9]{26}
90          * @param string $data Serialized session data
91          *
92          * @return bool Returns false if parameters are missing, true otherwise
93          */
94         public function write($id, $data): bool
95         {
96                 if (!$id) {
97                         return false;
98                 }
99
100                 if (!$data) {
101                         return $this->destroy($id);
102                 }
103
104                 $expire         = time() + static::EXPIRE;
105                 $default_expire = time() + 300;
106
107                 try {
108                         if ($this->sessionExists) {
109                                 $fields    = ['data' => $data, 'expire' => $expire];
110                                 $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire];
111                                 $this->dba->update('session', $fields, $condition);
112                         } else {
113                                 $fields = ['sid' => $id, 'expire' => $default_expire, 'data' => $data];
114                                 $this->dba->insert('session', $fields);
115                         }
116                 } catch (\Exception $exception) {
117                         $this->logger->warning('Cannot write session.', ['id' => $id, 'exception' => $exception]);
118                         return false;
119                 }
120
121                 return true;
122         }
123
124         public function close(): bool
125         {
126                 return true;
127         }
128
129         public function destroy($id): bool
130         {
131                 try {
132                         return $this->dba->delete('session', ['sid' => $id]);
133                 } catch (\Exception $exception) {
134                         $this->logger->warning('Cannot destroy session.', ['id' => $id, 'exception' => $exception]);
135                         return false;
136                 }
137         }
138
139         public function gc($max_lifetime): bool
140         {
141                 try {
142                         return $this->dba->delete('session', ["`expire` < ?", time()]);
143                 } catch (\Exception $exception) {
144                         $this->logger->warning('Cannot use garbage collector.', ['exception' => $exception]);
145                         return false;
146                 }
147         }
148 }