]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/DatabaseSessionHandler.php
Fix PHPDoc comments project-wide
[friendica.git] / src / Core / Session / DatabaseSessionHandler.php
1 <?php
2
3 namespace Friendica\Core\Session;
4
5 use Friendica\BaseObject;
6 use Friendica\Core\Logger;
7 use Friendica\Core\Session;
8 use Friendica\Database\DBA;
9 use SessionHandlerInterface;
10
11 /**
12  * SessionHandler using database
13  *
14  * @author Hypolite Petovan <hypolite@mrpetovan.com>
15  */
16 class DatabaseSessionHandler 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                 $session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]);
30                 if (DBA::isResult($session)) {
31                         Session::$exists = true;
32                         return $session['data'];
33                 }
34                 Logger::log("no data for session $session_id", Logger::TRACE);
35
36                 return '';
37         }
38
39         /**
40          * @brief Standard PHP session write callback
41          *
42          * This callback updates the DB-stored session data and/or the expiration depending
43          * on the case. Uses the Session::expire global for existing session, 5 minutes
44          * for newly created session.
45          *
46          * @param  string $session_id   Session ID with format: [a-z0-9]{26}
47          * @param  string $session_data Serialized session data
48          * @return boolean Returns false if parameters are missing, true otherwise
49          * @throws \Exception
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                 $expire = time() + Session::$expire;
62                 $default_expire = time() + 300;
63
64                 if (Session::$exists) {
65                         $fields = ['data' => $session_data, 'expire' => $expire];
66                         $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
67                         DBA::update('session', $fields, $condition);
68                 } else {
69                         $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
70                         DBA::insert('session', $fields);
71                 }
72
73                 return true;
74         }
75
76         public function close()
77         {
78                 return true;
79         }
80
81         public function destroy($id)
82         {
83                 DBA::delete('session', ['sid' => $id]);
84                 return true;
85         }
86
87         public function gc($maxlifetime)
88         {
89                 DBA::delete('session', ["`expire` < ?", time()]);
90                 return true;
91         }
92 }