1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class DBSessionHandler |
4
|
|
|
* |
5
|
|
|
* @filesource DBSessionHandler.php |
6
|
|
|
* @created 06.03.2017 |
7
|
|
|
* @package chillerlan\Session |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Session; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Database; |
16
|
|
|
use chillerlan\Traits\ContainerInterface; |
17
|
|
|
|
18
|
|
|
class DBSessionHandler extends SessionHandlerAbstract{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \chillerlan\Database\Database |
22
|
|
|
*/ |
23
|
|
|
protected $db; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* DBSessionHandler constructor. |
27
|
|
|
* |
28
|
|
|
* @param \chillerlan\Traits\ContainerInterface $options |
29
|
|
|
* @param \chillerlan\Database\Database $db |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ContainerInterface $options = null, Database $db){ |
32
|
|
|
parent::__construct($options); |
33
|
|
|
|
34
|
|
|
$this->db = $db->connect(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @inheritdoc */ |
38
|
|
|
public function close():bool{ |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @inheritdoc */ |
43
|
|
|
public function destroy($session_id):bool{ |
44
|
|
|
|
45
|
|
|
$this->db->delete |
46
|
|
|
->from($this->options->db_table) |
47
|
|
|
->where('id', $session_id) |
48
|
|
|
->query(); |
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @inheritdoc */ |
54
|
|
|
public function gc($maxlifetime):bool{ |
55
|
|
|
|
56
|
|
|
$this->db->delete |
57
|
|
|
->from($this->options->db_table) |
58
|
|
|
->where('time', time() - $maxlifetime, '<') |
59
|
|
|
->query(); |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @inheritdoc */ |
65
|
|
|
public function open($save_path, $name):bool{ |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @inheritdoc */ |
70
|
|
|
public function read($session_id):string{ |
71
|
|
|
|
72
|
|
|
$q = $this->db->select |
73
|
|
|
->cols(['data']) |
74
|
|
|
->from([$this->options->db_table]) |
75
|
|
|
->where('id', $session_id) |
76
|
|
|
->query(); |
77
|
|
|
|
78
|
|
|
try{ |
79
|
|
|
|
80
|
|
|
if(!$q || !isset($q[0])){ |
81
|
|
|
return ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->options->use_encryption ? $this->decrypt($q[0]->data) : $q[0]->data; |
85
|
|
|
} |
86
|
|
|
catch(\Exception $e){ |
87
|
|
|
throw new SessionHandlerException($e->getMessage()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** @inheritdoc */ |
93
|
|
|
public function write($session_id, $session_data):bool{ |
94
|
|
|
|
95
|
|
|
$q = $this->db->insert |
96
|
|
|
->into($this->options->db_table, 'REPLACE', 'id') |
97
|
|
|
->values([ |
98
|
|
|
'id' => $session_id, |
99
|
|
|
'time' => time(), |
100
|
|
|
'data' => $this->options->use_encryption ? $this->encrypt($session_data) : $session_data, |
101
|
|
|
]) |
102
|
|
|
->query(); |
103
|
|
|
|
104
|
|
|
return (bool)$q; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|