]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Handler/Cache.php
Merge pull request #12021 from nupplaphil/feat/session_util
[friendica.git] / src / Core / Session / Handler / Cache.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\Core\Cache\Capability\ICanCache;
25 use Friendica\Core\Cache\Exception\CachePersistenceException;
26 use Psr\Log\LoggerInterface;
27
28 /**
29  * SessionHandler using Friendica Cache
30  */
31 class Cache extends AbstractSessionHandler
32 {
33         /** @var ICanCache */
34         private $cache;
35         /** @var LoggerInterface */
36         private $logger;
37
38         public function __construct(ICanCache $cache, LoggerInterface $logger)
39         {
40                 $this->cache  = $cache;
41                 $this->logger = $logger;
42         }
43
44         public function open($path, $name): bool
45         {
46                 return true;
47         }
48
49         public function read($id)
50         {
51                 if (empty($id)) {
52                         return '';
53                 }
54
55                 try {
56                         $data = $this->cache->get('session:' . $id);
57                         if (!empty($data)) {
58                                 return $data;
59                         }
60                 } catch (CachePersistenceException $exception) {
61                         $this->logger->warning('Cannot read session.', ['id' => $id, 'exception' => $exception]);
62                         return '';
63                 }
64
65                 return '';
66         }
67
68         /**
69          * Standard PHP session write callback
70          *
71          * This callback updates the stored session data and/or the expiration depending
72          * on the case. Uses the Session::expire for existing session, 5 minutes
73          * for newly created session.
74          *
75          * @param string $id   Session ID with format: [a-z0-9]{26}
76          * @param string $data Serialized session data
77          *
78          * @return bool Returns false if parameters are missing, true otherwise
79          */
80         public function write($id, $data): bool
81         {
82                 if (!$id) {
83                         return false;
84                 }
85
86                 if (!$data) {
87                         return $this->destroy($id);
88                 }
89
90                 try {
91                         return $this->cache->set('session:' . $id, $data, static::EXPIRE);
92                 } catch (CachePersistenceException $exception) {
93                         $this->logger->warning('Cannot write session', ['id' => $id, 'exception' => $exception]);
94                         return false;
95                 }
96         }
97
98         public function close(): bool
99         {
100                 return true;
101         }
102
103         public function destroy($id): bool
104         {
105                 try {
106                         return $this->cache->delete('session:' . $id);
107                 } catch (CachePersistenceException $exception) {
108                         $this->logger->warning('Cannot destroy session', ['id' => $id, 'exception' => $exception]);
109                         return false;
110                 }
111         }
112
113         public function gc($max_lifetime): bool
114         {
115                 return true;
116         }
117 }