Elgg  Version 5.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  const TABLE_NAME = 'users_sessions';
21 
22  protected Database $db;
23 
29  public function __construct(Database $db) {
30  $this->db = $db;
31  }
32 
36  #[\ReturnTypeWillChange]
37  public function open($path, $name) {
38  return true;
39  }
40 
44  #[\ReturnTypeWillChange]
45  public function read($id) {
46  $select = Select::fromTable(self::TABLE_NAME);
47  $select->select('*')
48  ->where($select->compare('session', '=', $id, ELGG_VALUE_STRING));
49 
50  $result = $this->db->getDataRow($select);
51 
52  return $result ? (string) $result->data : '';
53  }
54 
58  #[\ReturnTypeWillChange]
59  public function write($id, $data) {
60  if (elgg_get_config('_disable_session_save')) {
61  return true;
62  }
63 
64  if ($this->read($id)) {
65  $update = Update::table(self::TABLE_NAME);
66  $update->set('data', $update->param($data, ELGG_VALUE_STRING))
67  ->set('ts', $update->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
68  ->where($update->compare('session', '=', $id, ELGG_VALUE_STRING));
69 
70  return $this->db->updateData($update);
71  }
72 
73  $insert = Insert::intoTable(self::TABLE_NAME);
74  $insert->values([
75  'session' => $insert->param($id, ELGG_VALUE_STRING),
76  'data' => $insert->param($data, ELGG_VALUE_STRING),
77  'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
78  ]);
79 
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  return true;
84  }
85 
89  #[\ReturnTypeWillChange]
90  public function close() {
91  return true;
92  }
93 
97  #[\ReturnTypeWillChange]
98  public function destroy($id) {
99  $delete = Delete::fromTable(self::TABLE_NAME);
100  $delete->where($delete->compare('session', '=', $id, ELGG_VALUE_STRING));
101 
102  $this->db->deleteData($delete);
103 
104  return true;
105  }
106 
110  #[\ReturnTypeWillChange]
111  public function gc($max_lifetime) {
112  $delete = Delete::fromTable(self::TABLE_NAME);
113  $delete->where($delete->compare('ts', '<', $this->getCurrentTime("-{$max_lifetime} seconds")->getTimestamp(), ELGG_VALUE_TIMESTAMP));
114 
115  return (bool) $this->db->deleteData($delete);
116  }
117 }
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
$delete
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
$path
Definition: details.php:70
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
static intoTable($table)
{}
Definition: Insert.php:13
__construct(Database $db)
Constructor.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:112
Database session handler.
$id
Generic annotation delete action.
Definition: delete.php:6
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13