]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Handler/Cache.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[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\ICache;
25 use Friendica\Core\Session;
26 use Psr\Log\LoggerInterface;
27 use SessionHandlerInterface;
28
29 /**
30  * SessionHandler using Friendica Cache
31  */
32 class Cache implements SessionHandlerInterface
33 {
34         /** @var ICache */
35         private $cache;
36         /** @var LoggerInterface */
37         private $logger;
38         /** @var array The $_SERVER array */
39         private $server;
40
41         public function __construct(ICache $cache, LoggerInterface $logger, array $server)
42         {
43                 $this->cache  = $cache;
44                 $this->logger = $logger;
45                 $this->server = $server;
46         }
47
48         public function open($save_path, $session_name)
49         {
50                 return true;
51         }
52
53         public function read($session_id)
54         {
55                 if (empty($session_id)) {
56                         return '';
57                 }
58
59                 $data = $this->cache->get('session:' . $session_id);
60                 if (!empty($data)) {
61                         Session::$exists = true;
62                         return $data;
63                 }
64
65                 $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
66
67                 return '';
68         }
69
70         /**
71          * Standard PHP session write callback
72          *
73          * This callback updates the stored session data and/or the expiration depending
74          * on the case. Uses the Session::expire for existing session, 5 minutes
75          * for newly created session.
76          *
77          * @param string $session_id   Session ID with format: [a-z0-9]{26}
78          * @param string $session_data Serialized session data
79          *
80          * @return boolean Returns false if parameters are missing, true otherwise
81          * @throws \Exception
82          */
83         public function write($session_id, $session_data)
84         {
85                 if (!$session_id) {
86                         return false;
87                 }
88
89                 if (!$session_data) {
90                         return $this->destroy($session_id);
91                 }
92
93                 return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
94         }
95
96         public function close()
97         {
98                 return true;
99         }
100
101         public function destroy($id)
102         {
103                 return $this->cache->delete('session:' . $id);
104         }
105
106         public function gc($maxlifetime)
107         {
108                 return true;
109         }
110 }