507735c4fc41a11391c9b70deefb416ab337d653
[friendica.git/.git] / src / Core / Session / CacheSessionHandler.php
1 <?php
2
3 namespace Friendica\Core\Session;
4
5 use Friendica\BaseObject;
6 use Friendica\Core\Cache;
7 use Friendica\Core\Session;
8 use SessionHandlerInterface;
9
10 require_once 'boot.php';
11 require_once 'include/text.php';
12
13 /**
14  * SessionHandler using Friendica Cache
15  *
16  * @author Hypolite Petovan <mrpetovan@gmail.com>
17  */
18 class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
19 {
20         public function open($save_path, $session_name)
21         {
22                 return true;
23         }
24
25         public function read($session_id)
26         {
27                 if (empty($session_id)) {
28                         return '';
29                 }
30
31                 $data = Cache::get('session:' . $session_id);
32                 if (!empty($data)) {
33                         Session::$exists = true;
34                         return $data;
35                 }
36                 logger("no data for session $session_id", LOGGER_TRACE);
37                 return '';
38         }
39
40         /**
41          * @brief Standard PHP session write callback
42          *
43          * This callback updates the stored session data and/or the expiration depending
44          * on the case. Uses the Session::expire for existing session, 5 minutes
45          * for newly created session.
46          *
47          * @param  string $session_id   Session ID with format: [a-z0-9]{26}
48          * @param  string $session_data Serialized session data
49          * @return boolean Returns false if parameters are missing, true otherwise
50          */
51         public function write($session_id, $session_data)
52         {
53                 if (!$session_id) {
54                         return false;
55                 }
56
57                 if (!$session_data) {
58                         return true;
59                 }
60
61                 $return = Cache::set('session:' . $session_id, $session_data, Session::$expire);
62
63                 return $return;
64         }
65
66         public function close()
67         {
68                 return true;
69         }
70
71         public function destroy($id)
72         {
73                 $return = Cache::delete('session:' . $id);
74
75                 return $return;
76         }
77
78         public function gc($maxlifetime)
79         {
80                 return true;
81         }
82 }