-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionHandler.php
More file actions
175 lines (159 loc) Β· 4.66 KB
/
Copy pathSessionHandler.php
File metadata and controls
175 lines (159 loc) Β· 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php declare(strict_types=1);
/**
* contains \DavidLienhard\SessionHandler\SessionHandler class
*
* @package tourBase
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
*/
namespace DavidLienhard\SessionHandler;
use DavidLienhard\Database\DatabaseInterface;
use DavidLienhard\Database\Parameter as DBParam;
use DavidLienhard\Database\ResultInterface;
/**
* sessionhandler using database
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
*/
class SessionHandler implements \SessionHandlerInterface
{
/**
* sets the given dependencies
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param \DavidLienhard\Database\DatabaseInterface $db database connection
* @return void
* @uses self::$db
*/
public function __construct(private DatabaseInterface $db)
{
}
/**
* placeholder
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param string $savePath path to save sessions to
* @param string $sessionID unique session id
*/
public function open($savePath, $sessionID) : bool
{
return true;
}
/**
* cleanup
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
*/
public function close() : bool
{
return true;
}
/**
* read session data
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param string $sessionID unique session id
* @return string the session data
* @uses self::$db
*/
public function read($sessionID) : string
{
try {
$result = $this->db->query(
"SELECT
`sessionData`
FROM
`sessions`
WHERE
`sessionID` = ?",
new DBParam("s", $sessionID)
);
$sessionData = ($result instanceof ResultInterface) && $result->num_rows() === 1
? $result->resultAsString(0, "sessionData")
: "";
} catch (\Exception $e) {
throw $e;
}
return $sessionData;
}
/**
* write session data
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param string $sessionID unique session id
* @param string $sessionData data to be written to the session
* @uses self::$db
*/
public function write($sessionID, $sessionData) : bool
{
try {
$this->db->query(
"REPLACE INTO
`sessions`
SET
`sessionID` = ?,
`sessionLastSave` = UNIX_TIMESTAMP(),
`sessionData` = ?",
new DBParam("s", $sessionID),
new DBParam("s", $sessionData)
);
} catch (\Exception $e) {
throw $e;
}
return true;
}
/**
* destroys a session
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param string $sessionID unique session id
* @uses self::$db
*/
public function destroy($sessionID) : bool
{
try {
$this->db->query(
"DELETE FROM
`sessions`
WHERE
`sessionID` = ?",
new DBParam("s", $sessionID)
);
} catch (\Exception $e) {
throw $e;
}
return true;
}
/**
* garbage collection
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param int $maxLifetime max lifetime of a session in seconds
* @uses self::$db
*/
public function gc($maxLifetime) : int|false
{
try {
$checkStamp = time() - $maxLifetime;
$this->db->query(
"DELETE FROM
`sessions`
WHERE
`sessionLastSave` < ? OR
`sessionLastSave` IS NULL",
new DBParam("i", $checkStamp)
);
return $this->db->affected_rows();
} catch (\Exception $e) {
throw $e;
}
}
}