]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Handler/Database.php
Merge pull request #12562 from MrPetovan/bug/notices
[friendica.git] / src / Core / Session / Handler / Database.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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         #[\ReturnTypeWillChange]
61         public function read($id)
62         {
63                 if (empty($id)) {
64                         return '';
65                 }
66
67                 try {
68                         $session = $this->dba->selectFirst('session', ['data'], ['sid' => $id]);
69                         if ($this->dba->isResult($session)) {
70                                 $this->sessionExists = true;
71                                 return $session['data'];
72                         }
73                 } catch (\Exception $exception) {
74                         $this->logger->warning('Cannot read session.', ['id' => $id, 'exception' => $exception]);
75                         return '';
76                 }
77
78                 $this->logger->notice('no data for session', ['session_id' => $id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
79
80                 return '';
81         }
82
83         /**
84          * Standard PHP session write callback
85          *
86          * This callback updates the DB-stored session data and/or the expiration depending
87          * on the case. Uses the Session::expire global for existing session, 5 minutes
88          * for newly created session.
89          *
90          * @param string $id   Session ID with format: [a-z0-9]{26}
91          * @param string $data Serialized session data
92          *
93          * @return bool Returns false if parameters are missing, true otherwise
94          */
95         public function write($id, $data): bool
96         {
97                 if (!$id) {
98                         return false;
99                 }
100
101                 if (!$data) {
102                         return $this->destroy($id);
103                 }
104
105                 $expire         = time() + static::EXPIRE;
106                 $default_expire = time() + 300;
107
108                 try {
109                         if ($this->sessionExists) {
110                                 $fields    = ['data' => $data, 'expire' => $expire];
111                                 $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire];
112                                 $this->dba->update('session', $fields, $condition);
113                         } else {
114                                 $fields = ['sid' => $id, 'expire' => $default_expire, 'data' => $data];
115                                 $this->dba->insert('session', $fields);
116                         }
117                 } catch (\Exception $exception) {
118                         $this->logger->warning('Cannot write session.', ['id' => $id, 'exception' => $exception]);
119                         return false;
120                 }
121
122                 return true;
123         }
124
125         public function close(): bool
126         {
127                 return true;
128         }
129
130         public function destroy($id): bool
131         {
132                 try {
133                         return $this->dba->delete('session', ['sid' => $id]);
134                 } catch (\Exception $exception) {
135                         $this->logger->warning('Cannot destroy session.', ['id' => $id, 'exception' => $exception]);
136                         return false;
137                 }
138         }
139
140         #[\ReturnTypeWillChange]
141         public function gc($max_lifetime): bool
142         {
143                 try {
144                         return $this->dba->delete('session', ["`expire` < ?", time()]);
145                 } catch (\Exception $exception) {
146                         $this->logger->warning('Cannot use garbage collector.', ['exception' => $exception]);
147                         return false;
148                 }
149         }
150 }