]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Handler/Cache.php
Merge branch 'master' 2019.12 into develop
[friendica.git] / src / Core / Session / Handler / Cache.php
1 <?php
2
3 namespace Friendica\Core\Session\Handler;
4
5 use Friendica\Core\Cache\ICache;
6 use Friendica\Core\Session;
7 use Psr\Log\LoggerInterface;
8 use SessionHandlerInterface;
9
10 /**
11  * SessionHandler using Friendica Cache
12  *
13  * @author Hypolite Petovan <hypolite@mrpetovan.com>
14  */
15 final class Cache implements SessionHandlerInterface
16 {
17         /** @var ICache */
18         private $cache;
19         /** @var LoggerInterface */
20         private $logger;
21         /** @var array The $_SERVER array */
22         private $server;
23
24         public function __construct(ICache $cache, LoggerInterface $logger, array $server)
25         {
26                 $this->cache  = $cache;
27                 $this->logger = $logger;
28                 $this->server = $server;
29         }
30
31         public function open($save_path, $session_name)
32         {
33                 return true;
34         }
35
36         public function read($session_id)
37         {
38                 if (empty($session_id)) {
39                         return '';
40                 }
41
42                 $data = $this->cache->get('session:' . $session_id);
43                 if (!empty($data)) {
44                         Session::$exists = true;
45                         return $data;
46                 }
47
48                 $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
49
50                 return '';
51         }
52
53         /**
54          * @brief Standard PHP session write callback
55          *
56          * This callback updates the stored session data and/or the expiration depending
57          * on the case. Uses the Session::expire for existing session, 5 minutes
58          * for newly created session.
59          *
60          * @param string $session_id   Session ID with format: [a-z0-9]{26}
61          * @param string $session_data Serialized session data
62          *
63          * @return boolean Returns false if parameters are missing, true otherwise
64          * @throws \Exception
65          */
66         public function write($session_id, $session_data)
67         {
68                 if (!$session_id) {
69                         return false;
70                 }
71
72                 if (!$session_data) {
73                         return true;
74                 }
75
76                 return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
77         }
78
79         public function close()
80         {
81                 return true;
82         }
83
84         public function destroy($id)
85         {
86                 return $this->cache->delete('session:' . $id);
87         }
88
89         public function gc($maxlifetime)
90         {
91                 return true;
92         }
93 }