Elgg  Version 4.3
DatabaseSessionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Http;
4 
10 
16 class DatabaseSessionHandler implements \SessionHandlerInterface {
17 
18  use TimeUsing;
19 
23  const TABLE_NAME = 'users_sessions';
24 
28  protected $db;
29 
35  public function __construct(\Elgg\Database $db) {
36  $this->db = $db;
37  }
38 
42  #[\ReturnTypeWillChange]
43  public function open($save_path, $name) {
44  return true;
45  }
46 
50  #[\ReturnTypeWillChange]
51  public function read($session_id) {
52  $select = Select::fromTable(self::TABLE_NAME);
53  $select->select('*')
54  ->where($select->compare('session', '=', $session_id, ELGG_VALUE_STRING));
55 
56  $result = $this->db->getDataRow($select);
57  if (!empty($result)) {
58  return (string) $result->data;
59  }
60 
61  return '';
62  }
63 
67  #[\ReturnTypeWillChange]
68  public function write($session_id, $session_data) {
69 
70  if (elgg_get_config('_disable_session_save')) {
71  return true;
72  }
73 
74  if ($this->read($session_id)) {
75  $update = Update::table(self::TABLE_NAME);
76  $update->set('data', $update->param($session_data, ELGG_VALUE_STRING))
77  ->set('ts', $update->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
78  ->where($update->compare('session', '=', $session_id, ELGG_VALUE_STRING));
79 
80  return $this->db->updateData($update);
81  }
82 
83  $insert = Insert::intoTable(self::TABLE_NAME);
84  $insert->values([
85  'session' => $insert->param($session_id, ELGG_VALUE_STRING),
86  'data' => $insert->param($session_data, ELGG_VALUE_STRING),
87  'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
88  ]);
89 
90  return $this->db->insertData($insert) !== false;
91  }
92 
96  #[\ReturnTypeWillChange]
97  public function close() {
98  return true;
99  }
100 
104  #[\ReturnTypeWillChange]
105  public function destroy($session_id) {
106  $delete = Delete::fromTable(self::TABLE_NAME);
107  $delete->where($delete->compare('session', '=', $session_id, ELGG_VALUE_STRING));
108 
109  $this->db->deleteData($delete);
110 
111  return true;
112  }
113 
117  #[\ReturnTypeWillChange]
118  public function gc($max_lifetime) {
119  $delete = Delete::fromTable(self::TABLE_NAME);
120  $delete->where($delete->compare('ts', '<', $this->getCurrentTime("-{$max_lifetime} seconds")->getTimestamp(), ELGG_VALUE_TIMESTAMP));
121 
122  return (bool) $this->db->deleteData($delete);
123  }
124 }
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
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
static intoTable($table)
{}
Definition: Insert.php:13
__construct(\Elgg\Database $db)
Constructor.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:130
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
write($session_id, $session_data)
const ELGG_VALUE_STRING
Definition: constants.php:127
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
elgg_get_config($name, $default=null)
Get an Elgg configuration value.