Elgg  Version 6.2
SessionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
6 use Elgg\Database;
9 
15 class SessionHandler implements \SessionHandlerInterface {
16 
17  use TimeUsing;
18 
22  public const TABLE_NAME = 'users_sessions';
23 
29  public function __construct(protected Database $db) {
30  }
31 
35  #[\ReturnTypeWillChange]
36  public function open($path, $name) {
37  return true;
38  }
39 
43  #[\ReturnTypeWillChange]
44  public function read($id) {
45  $select = Select::fromTable(self::TABLE_NAME);
46  $select->select('*')
47  ->where($select->compare('session', '=', $id, ELGG_VALUE_STRING));
48 
49  $result = $this->db->getDataRow($select);
50 
51  return $result ? (string) $result->data : '';
52  }
53 
57  #[\ReturnTypeWillChange]
58  public function write($id, $data) {
59  if (elgg_get_config('_disable_session_save')) {
60  return true;
61  }
62 
63  if ($this->read($id)) {
64  $update = Update::table(self::TABLE_NAME);
65  $update->set('data', $update->param($data, ELGG_VALUE_STRING))
66  ->set('ts', $update->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
67  ->where($update->compare('session', '=', $id, ELGG_VALUE_STRING));
68 
69  return $this->db->updateData($update);
70  }
71 
72  $insert = Insert::intoTable(self::TABLE_NAME);
73  $insert->values([
74  'session' => $insert->param($id, ELGG_VALUE_STRING),
75  'data' => $insert->param($data, ELGG_VALUE_STRING),
76  'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
77  ]);
78 
79  try {
80  // not returning the result of the database call as the session table doesn't support an autoincrement column
81  // so the result of this call will always be 0
82  $this->db->insertData($insert);
83  } catch (DatabaseException $e) {
84  $prev = $e->getPrevious();
85  if ($prev instanceof UniqueConstraintViolationException) {
86  // ignoring issues with duplicate key insertions
87  return true;
88  }
89 
90  throw $e;
91  }
92 
93  return true;
94  }
95 
99  #[\ReturnTypeWillChange]
100  public function close() {
101  return true;
102  }
103 
107  #[\ReturnTypeWillChange]
108  public function destroy($id) {
109  $delete = Delete::fromTable(self::TABLE_NAME);
110  $delete->where($delete->compare('session', '=', $id, ELGG_VALUE_STRING));
111 
112  $this->db->deleteData($delete);
113 
114  return true;
115  }
116 
120  #[\ReturnTypeWillChange]
121  public function gc($max_lifetime) {
122  $delete = Delete::fromTable(self::TABLE_NAME);
123  $delete->where($delete->compare('ts', '<', $this->getCurrentTime("-{$max_lifetime} seconds")->getTimestamp(), ELGG_VALUE_TIMESTAMP));
124 
125  return (bool) $this->db->deleteData($delete);
126  }
127 }
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
A generic parent class for database exceptions.
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
Database session handler.
static fromTable(string $table,?string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
$id
Generic annotation delete action.
Definition: delete.php:6