|
1
|
|
|
<?php |
|
2
|
|
|
namespace Elgg\Http; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Database session handler |
|
6
|
|
|
* |
|
7
|
|
|
* @access private |
|
8
|
|
|
* |
|
9
|
|
|
* @package Elgg.Core |
|
10
|
|
|
* @subpackage Http |
|
11
|
|
|
*/ |
|
12
|
|
|
class DatabaseSessionHandler implements \SessionHandlerInterface { |
|
13
|
|
|
|
|
14
|
|
|
/** @var \Elgg\Database $db */ |
|
15
|
|
|
protected $db; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor |
|
19
|
|
|
* |
|
20
|
|
|
* @param \Elgg\Database $db The database |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(\Elgg\Database $db) { |
|
23
|
|
|
$this->db = $db; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function open($save_path, $name) { |
|
30
|
|
|
return true; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritDoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function read($session_id) { |
|
37
|
|
|
|
|
38
|
|
|
$id = sanitize_string($session_id); |
|
39
|
|
|
$query = "SELECT * FROM {$this->db->getTablePrefix()}users_sessions WHERE session='$id'"; |
|
40
|
|
|
$result = $this->db->getDataRow($query); |
|
41
|
|
|
if ($result) { |
|
|
|
|
|
|
42
|
|
|
return (string) $result->data; |
|
43
|
|
|
} else { |
|
44
|
|
|
return false; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function write($session_id, $session_data) { |
|
52
|
|
|
$id = sanitize_string($session_id); |
|
53
|
|
|
$time = time(); |
|
54
|
|
|
$sess_data_sanitised = sanitize_string($session_data); |
|
55
|
|
|
|
|
56
|
|
|
// do not persist a session for health probes |
|
57
|
|
|
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'kube-probe') !== false && $_SERVER["REQUEST_URI"] == '/splash') |
|
58
|
|
|
return true; |
|
59
|
|
|
if ($_SERVER["REQUEST_URI"] == '/') |
|
60
|
|
|
return true; |
|
61
|
|
|
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'solr-crawler') !== false) |
|
62
|
|
|
return true; |
|
63
|
|
|
|
|
64
|
|
|
$query = "REPLACE INTO {$this->db->getTablePrefix()}users_sessions |
|
65
|
|
|
(session, ts, data) VALUES |
|
66
|
|
|
('$id', '$time', '$sess_data_sanitised')"; |
|
67
|
|
|
|
|
68
|
|
|
if ($this->db->insertData($query) !== false) { |
|
69
|
|
|
return true; |
|
70
|
|
|
} else { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function close() { |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function destroy($session_id) { |
|
86
|
|
|
|
|
87
|
|
|
$id = sanitize_string($session_id); |
|
88
|
|
|
$query = "DELETE FROM {$this->db->getTablePrefix()}users_sessions WHERE session='$id'"; |
|
89
|
|
|
return (bool) $this->db->deleteData($query); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function gc($max_lifetime) { |
|
96
|
|
|
|
|
97
|
|
|
$life = time() - $max_lifetime; |
|
98
|
|
|
$query = "DELETE FROM {$this->db->getTablePrefix()}users_sessions WHERE ts < '$life'"; |
|
99
|
|
|
return (bool) $this->db->deleteData($query); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.