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