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