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