1515use CodeIgniter \Session \Exceptions \SessionException ;
1616use Config \App as AppConfig ;
1717use Config \Database ;
18- use Exception ;
18+ use ReturnTypeWillChange ;
1919
2020/**
2121 * Session handler using current Database for storage
@@ -58,27 +58,24 @@ class DatabaseHandler extends BaseHandler
5858 protected $ rowExists = false ;
5959
6060 /**
61- * Constructor
61+ * @throws SessionException
6262 */
6363 public function __construct (AppConfig $ config , string $ ipAddress )
6464 {
6565 parent ::__construct ($ config , $ ipAddress );
66-
67- // Determine Table
6866 $ this ->table = $ config ->sessionSavePath ;
6967
7068 if (empty ($ this ->table )) {
7169 throw SessionException::forMissingDatabaseTable ();
7270 }
7371
74- // Get DB Connection
7572 // @phpstan-ignore-next-line
7673 $ this ->DBGroup = $ config ->sessionDBGroup ?? config (Database::class)->defaultGroup ;
7774
7875 $ this ->db = Database::connect ($ this ->DBGroup );
7976
80- // Determine Database type
8177 $ driver = strtolower (get_class ($ this ->db ));
78+
8279 if (strpos ($ driver , 'mysql ' ) !== false ) {
8380 $ this ->platform = 'mysql ' ;
8481 } elseif (strpos ($ driver , 'postgre ' ) !== false ) {
@@ -87,16 +84,12 @@ public function __construct(AppConfig $config, string $ipAddress)
8784 }
8885
8986 /**
90- * Open
91- *
92- * Ensures we have an initialized database connection.
93- *
94- * @param string $savePath Path to session files' directory
95- * @param string $name Session cookie name
87+ * Re-initialize existing session, or creates a new one.
9688 *
97- * @throws Exception
89+ * @param string $path The path where to store/retrieve the session
90+ * @param string $name The session name
9891 */
99- public function open ($ savePath , $ name ): bool
92+ public function open ($ path , $ name ): bool
10093 {
10194 if (empty ($ this ->db ->connID )) {
10295 $ this ->db ->initialize ();
@@ -106,30 +99,29 @@ public function open($savePath, $name): bool
10699 }
107100
108101 /**
109- * Read
102+ * Reads the session data from the session storage, and returns the results.
110103 *
111- * Reads session data and acquires a lock
104+ * @param string $id The session ID
112105 *
113- * @param string $sessionID Session ID
114- *
115- * @return string Serialized session data
106+ * @return false|string Returns an encoded string of the read data.
107+ * If nothing was read, it must return false.
116108 */
117- public function read ($ sessionID ): string
109+ #[ReturnTypeWillChange]
110+ public function read ($ id )
118111 {
119- if ($ this ->lockSession ($ sessionID ) === false ) {
112+ if ($ this ->lockSession ($ id ) === false ) {
120113 $ this ->fingerprint = md5 ('' );
121114
122115 return '' ;
123116 }
124117
125- // Needed by write() to detect session_regenerate_id() calls
126118 if (! isset ($ this ->sessionID )) {
127- $ this ->sessionID = $ sessionID ;
119+ $ this ->sessionID = $ id ;
128120 }
129121
130122 $ builder = $ this ->db ->table ($ this ->table )
131123 ->select ($ this ->platform === 'postgre ' ? "encode(data, 'base64') AS data " : 'data ' )
132- ->where ('id ' , $ sessionID );
124+ ->where ('id ' , $ id );
133125
134126 if ($ this ->matchIP ) {
135127 $ builder = $ builder ->where ('ip_address ' , $ this ->ipAddress );
@@ -160,87 +152,78 @@ public function read($sessionID): string
160152 }
161153
162154 /**
163- * Write
164- *
165- * Writes (create / update) session data
155+ * Writes the session data to the session storage.
166156 *
167- * @param string $sessionID Session ID
168- * @param string $sessionData Serialized session data
157+ * @param string $id The session ID
158+ * @param string $data The encoded session data
169159 */
170- public function write ($ sessionID , $ sessionData ): bool
160+ public function write ($ id , $ data ): bool
171161 {
172162 if ($ this ->lock === false ) {
173163 return $ this ->fail ();
174164 }
175165
176- // Was the ID regenerated?
177- if ($ sessionID !== $ this ->sessionID ) {
166+ if ($ this ->sessionID !== $ id ) {
178167 $ this ->rowExists = false ;
179- $ this ->sessionID = $ sessionID ;
168+ $ this ->sessionID = $ id ;
180169 }
181170
182171 if ($ this ->rowExists === false ) {
183172 $ insertData = [
184- 'id ' => $ sessionID ,
173+ 'id ' => $ id ,
185174 'ip_address ' => $ this ->ipAddress ,
186175 'timestamp ' => 'now() ' ,
187- 'data ' => $ this ->platform === 'postgre ' ? '\x ' . bin2hex ($ sessionData ) : $ sessionData ,
176+ 'data ' => $ this ->platform === 'postgre ' ? '\x ' . bin2hex ($ data ) : $ data ,
188177 ];
189178
190179 if (! $ this ->db ->table ($ this ->table )->insert ($ insertData )) {
191180 return $ this ->fail ();
192181 }
193182
194- $ this ->fingerprint = md5 ($ sessionData );
183+ $ this ->fingerprint = md5 ($ data );
195184 $ this ->rowExists = true ;
196185
197186 return true ;
198187 }
199188
200- $ builder = $ this ->db ->table ($ this ->table )->where ('id ' , $ sessionID );
189+ $ builder = $ this ->db ->table ($ this ->table )->where ('id ' , $ id );
201190
202191 if ($ this ->matchIP ) {
203192 $ builder = $ builder ->where ('ip_address ' , $ this ->ipAddress );
204193 }
205194
206- $ updateData = [
207- 'timestamp ' => 'now() ' ,
208- ];
195+ $ updateData = ['timestamp ' => 'now() ' ];
209196
210- if ($ this ->fingerprint !== md5 ($ sessionData )) {
211- $ updateData ['data ' ] = ($ this ->platform === 'postgre ' ) ? '\x ' . bin2hex ($ sessionData ) : $ sessionData ;
197+ if ($ this ->fingerprint !== md5 ($ data )) {
198+ $ updateData ['data ' ] = ($ this ->platform === 'postgre ' ) ? '\x ' . bin2hex ($ data ) : $ data ;
212199 }
213200
214201 if (! $ builder ->update ($ updateData )) {
215202 return $ this ->fail ();
216203 }
217204
218- $ this ->fingerprint = md5 ($ sessionData );
205+ $ this ->fingerprint = md5 ($ data );
219206
220207 return true ;
221208 }
222209
223210 /**
224- * Close
225- *
226- * Releases locks and closes file descriptor.
211+ * Closes the current session.
227212 */
228213 public function close (): bool
229214 {
230215 return ($ this ->lock && ! $ this ->releaseLock ()) ? $ this ->fail () : true ;
231216 }
232217
233218 /**
234- * Destroy
235- *
236- * Destroys the current session.
219+ * Destroys a session
237220 *
238- * @param string $sessionID
221+ * @param string $id The session ID being destroyed
239222 */
240- public function destroy ($ sessionID ): bool
223+ public function destroy ($ id ): bool
241224 {
242225 if ($ this ->lock ) {
243- $ builder = $ this ->db ->table ($ this ->table )->where ('id ' , $ sessionID );
226+ $ builder = $ this ->db ->table ($ this ->table )->where ('id ' , $ id );
244227
245228 if ($ this ->matchIP ) {
246229 $ builder = $ builder ->where ('ip_address ' , $ this ->ipAddress );
@@ -261,18 +244,20 @@ public function destroy($sessionID): bool
261244 }
262245
263246 /**
264- * Garbage Collector
247+ * Cleans up expired sessions.
265248 *
266- * Deletes expired sessions
249+ * @param int $max_lifetime Sessions that have not updated
250+ * for the last max_lifetime seconds will be removed.
267251 *
268- * @param int $maxlifetime Maximum lifetime of sessions
252+ * @return false| int Returns the number of deleted sessions on success, or false on failure.
269253 */
270- public function gc ($ maxlifetime ): bool
254+ #[ReturnTypeWillChange]
255+ public function gc ($ max_lifetime )
271256 {
272257 $ separator = $ this ->platform === 'postgre ' ? '\'' : ' ' ;
273- $ interval = implode ($ separator , ['' , "{$ maxlifetime } second " , '' ]);
258+ $ interval = implode ($ separator , ['' , "{$ max_lifetime } second " , '' ]);
274259
275- return $ this ->db ->table ($ this ->table )->delete ("timestamp < now() - INTERVAL {$ interval }" ) ? true : $ this ->fail ();
260+ return $ this ->db ->table ($ this ->table )->delete ("timestamp < now() - INTERVAL {$ interval }" ) ? 1 : $ this ->fail ();
276261 }
277262
278263 /**
0 commit comments