/
DatabaseSessionHandler.php
116 lines (94 loc) · 2.42 KB
/
DatabaseSessionHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Elgg\Http;
use Elgg\Database\Delete;
use Elgg\Database\Insert;
use Elgg\Database\Select;
use Elgg\Database\Update;
use Elgg\Traits\TimeUsing;
/**
* Database session handler
*
* @internal
*/
class DatabaseSessionHandler implements \SessionHandlerInterface {
use TimeUsing;
/**
* @var string name of the users sessions database table
*/
const TABLE_NAME = 'users_sessions';
/**
* @var \Elgg\Database $db
*/
protected $db;
/**
* Constructor
*
* @param \Elgg\Database $db The database
*/
public function __construct(\Elgg\Database $db) {
$this->db = $db;
}
/**
* {@inheritDoc}
*/
public function open($save_path, $name) {
return true;
}
/**
* {@inheritDoc}
*/
public function read($session_id) {
$select = Select::fromTable(self::TABLE_NAME);
$select->select('*')
->where($select->compare('session', '=', $session_id, ELGG_VALUE_STRING));
$result = $this->db->getDataRow($select);
if (!empty($result)) {
return (string) $result->data;
}
return '';
}
/**
* {@inheritDoc}
*/
public function write($session_id, $session_data) {
if (elgg_get_config('_disable_session_save')) {
return true;
}
if ($this->read($session_id)) {
$update = Update::table(self::TABLE_NAME);
$update->set('data', $update->param($session_data, ELGG_VALUE_STRING))
->set('ts', $update->param(time(), ELGG_VALUE_TIMESTAMP))
->where($update->compare('session', '=', $session_id, ELGG_VALUE_STRING));
return $this->db->updateData($update);
}
$insert = Insert::intoTable(self::TABLE_NAME);
$insert->values([
'session' => $insert->param($session_id, ELGG_VALUE_STRING),
'data' => $insert->param($session_data, ELGG_VALUE_STRING),
'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
]);
return $this->db->insertData($insert) !== false;
}
/**
* {@inheritDoc}
*/
public function close() {
return true;
}
/**
* {@inheritDoc}
*/
public function destroy($session_id) {
$delete = Delete::fromTable(self::TABLE_NAME);
$delete->where($delete->compare('session', '=', $session_id, ELGG_VALUE_STRING));
return (bool) $this->db->deleteData($delete);
}
/**
* {@inheritDoc}
*/
public function gc($max_lifetime) {
$delete = Delete::fromTable(self::TABLE_NAME);
$delete->where($delete->compare('ts', '<', $max_lifetime, ELGG_VALUE_TIMESTAMP));
return (bool) $this->db->deleteData($delete);
}
}