Skip to content

Commit

Permalink
Fixed implementation of SessionHandlerInterface in Mage_Core_Model_Re…
Browse files Browse the repository at this point in the history
…source_Session (#3499)

Co-authored-by: Mohamed ELIDRISSI <[email protected]>
Co-authored-by: Ng Kiat Siong <[email protected]>
  • Loading branch information
3 people authored Sep 8, 2023
1 parent 08c6e41 commit 6b7dbf1
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions app/code/core/Mage/Core/Model/Resource/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public function __construct()
$this->_write = $resource->getConnection('core_write');
}

/**
* Destrucor
*
*/
public function __destruct()
{
session_write_close();
Expand Down Expand Up @@ -162,8 +158,7 @@ public static function setStaticSaveHandler()
* @param string $sessName ignored
* @return bool
*/
#[\ReturnTypeWillChange]
public function open($savePath, $sessName)
public function open($savePath, $sessName): bool
{
return true;
}
Expand All @@ -173,8 +168,7 @@ public function open($savePath, $sessName)
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function close()
public function close(): bool
{
$this->gc($this->getLifeTime());

Expand All @@ -187,21 +181,18 @@ public function close()
* @param string $sessId
* @return string
*/
#[\ReturnTypeWillChange]
public function read($sessId)
public function read($sessId): string|false
{
$select = $this->_read->select()
->from($this->_sessionTable, ['session_data'])
->where('session_id = :session_id')
->where('session_expires > :session_expires');
->from($this->_sessionTable, ['session_data'])
->where('session_id = :session_id')
->where('session_expires > :session_expires');
$bind = [
'session_id' => $sessId,
'session_expires' => Varien_Date::toTimestamp(true)
];

$data = $this->_read->fetchOne($select, $bind);

return (string)$data;
return $this->_read->fetchOne($select, $bind);
}

/**
Expand All @@ -211,15 +202,14 @@ public function read($sessId)
* @param string $sessData
* @return bool
*/
#[\ReturnTypeWillChange]
public function write($sessId, $sessData)
public function write($sessId, $sessData): bool
{
$bindValues = [
'session_id' => $sessId
];
$select = $this->_write->select()
->from($this->_sessionTable)
->where('session_id = :session_id');
->from($this->_sessionTable)
->where('session_id = :session_id');
$exists = $this->_read->fetchOne($select, $bindValues);

$bind = [
Expand All @@ -245,8 +235,7 @@ public function write($sessId, $sessData)
* @param string $sessId
* @return bool
*/
#[\ReturnTypeWillChange]
public function destroy($sessId)
public function destroy($sessId): bool
{
$where = ['session_id = ?' => $sessId];
$this->_write->delete($this->_sessionTable, $where);
Expand All @@ -257,19 +246,16 @@ public function destroy($sessId)
* Garbage collection
*
* @param int $sessMaxLifeTime ignored
* @return bool
* @return int|false
*/
#[\ReturnTypeWillChange]
public function gc($sessMaxLifeTime)
public function gc($sessMaxLifeTime): int|false
{
if ($this->_automaticCleaningFactor > 0) {
if ($this->_automaticCleaningFactor == 1 ||
rand(1, $this->_automaticCleaningFactor) == 1
) {
if ($this->_automaticCleaningFactor == 1 || rand(1, $this->_automaticCleaningFactor) == 1) {
$where = ['session_expires < ?' => Varien_Date::toTimestamp(true)];
$this->_write->delete($this->_sessionTable, $where);
return $this->_write->delete($this->_sessionTable, $where);
}
}
return true;
return 0;
}
}

0 comments on commit 6b7dbf1

Please sign in to comment.