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