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