Elgg  Version 6.1
SessionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
7 
13 class SessionHandler implements \SessionHandlerInterface {
14 
15  use TimeUsing;
16 
20  public const TABLE_NAME = 'users_sessions';
21 
27  public function __construct(protected Database $db) {
28  }
29 
33  #[\ReturnTypeWillChange]
34  public function open($path, $name) {
35  return true;
36  }
37 
41  #[\ReturnTypeWillChange]
42  public function read($id) {
43  $select = Select::fromTable(self::TABLE_NAME);
44  $select->select('*')
45  ->where($select->compare('session', '=', $id, ELGG_VALUE_STRING));
46 
47  $result = $this->db->getDataRow($select);
48 
49  return $result ? (string) $result->data : '';
50  }
51 
55  #[\ReturnTypeWillChange]
56  public function write($id, $data) {
57  if (elgg_get_config('_disable_session_save')) {
58  return true;
59  }
60 
61  if ($this->read($id)) {
62  $update = Update::table(self::TABLE_NAME);
63  $update->set('data', $update->param($data, ELGG_VALUE_STRING))
64  ->set('ts', $update->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
65  ->where($update->compare('session', '=', $id, ELGG_VALUE_STRING));
66 
67  return $this->db->updateData($update);
68  }
69 
70  $insert = Insert::intoTable(self::TABLE_NAME);
71  $insert->values([
72  'session' => $insert->param($id, ELGG_VALUE_STRING),
73  'data' => $insert->param($data, ELGG_VALUE_STRING),
74  'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
75  ]);
76 
77  // not returning the result of the database call as the session table doesn't support an autoincrement column
78  // so the result of this call will always be 0
79  $this->db->insertData($insert);
80  return true;
81  }
82 
86  #[\ReturnTypeWillChange]
87  public function close() {
88  return true;
89  }
90 
94  #[\ReturnTypeWillChange]
95  public function destroy($id) {
96  $delete = Delete::fromTable(self::TABLE_NAME);
97  $delete->where($delete->compare('session', '=', $id, ELGG_VALUE_STRING));
98 
99  $this->db->deleteData($delete);
100 
101  return true;
102  }
103 
107  #[\ReturnTypeWillChange]
108  public function gc($max_lifetime) {
109  $delete = Delete::fromTable(self::TABLE_NAME);
110  $delete->where($delete->compare('ts', '<', $this->getCurrentTime("-{$max_lifetime} seconds")->getTimestamp(), ELGG_VALUE_TIMESTAMP));
111 
112  return (bool) $this->db->deleteData($delete);
113  }
114 }
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
The Elgg database.
Definition: Database.php:26
$delete
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
Definition: Insert.php:17
$path
Definition: details.php:70
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
__construct(protected Database $db)
Constructor.
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
const ELGG_VALUE_STRING
Definition: constants.php:112
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
Database session handler.
$id
Generic annotation delete action.
Definition: delete.php:6