1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Service\DbAdmin; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Config\AuthInterface; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface; |
7
|
|
|
use Lagdo\DbAdmin\Driver\DriverInterface; |
8
|
|
|
use Lagdo\DbAdmin\Service\Options; |
9
|
|
|
use Lagdo\Facades\Logger; |
10
|
|
|
|
11
|
|
|
use function json_encode; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* SQL queries logging and storage. |
15
|
|
|
*/ |
16
|
|
|
class QueryLogger |
17
|
|
|
{ |
18
|
|
|
use ConnectionTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private bool $enduserEnabled; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private bool $historyEnabled; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private array $userDatabase; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private int $category; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ConnectionInterface |
42
|
|
|
*/ |
43
|
|
|
private ConnectionInterface $connection; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The constructor |
47
|
|
|
* |
48
|
|
|
* @param AuthInterface $auth |
49
|
|
|
* @param DriverInterface $driver |
50
|
|
|
* @param array $database |
51
|
|
|
* @param array $options |
52
|
|
|
*/ |
53
|
|
|
public function __construct(private AuthInterface $auth, |
54
|
|
|
private DriverInterface $driver, array $database, array $options) |
55
|
|
|
{ |
56
|
|
|
$this->enduserEnabled = (bool)($options['enduser']['enabled'] ?? false); |
57
|
|
|
$this->historyEnabled = (bool)($options['history']['enabled'] ?? false); |
58
|
|
|
$this->category = Options::CAT_BUILDER; |
59
|
|
|
$this->userDatabase = $options['database']; |
60
|
|
|
if (!$this->enduserEnabled && !$this->historyEnabled) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Connect to the logging database. |
65
|
|
|
$this->connect($driver, $database); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected function user(): string |
72
|
|
|
{ |
73
|
|
|
return $this->auth->user(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function setCategoryToHistory(): void |
80
|
|
|
{ |
81
|
|
|
$this->category = Options::CAT_EDITOR; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
private function enduserDisabled(): bool |
88
|
|
|
{ |
89
|
|
|
return (!$this->enduserEnabled && !$this->historyEnabled) || |
90
|
|
|
!$this->auth->user() || !$this->getOwnerId(true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param int $category |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
private function categoryDisabled(int $category): bool |
99
|
|
|
{ |
100
|
|
|
return (!$this->enduserEnabled && $category === Options::CAT_BUILDER) || |
101
|
|
|
($category < Options::CAT_BUILDER || $category > Options::CAT_EDITOR); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $query |
106
|
|
|
* @param int $category |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
private function saveRunnedCommand(string $query, int $category): bool |
111
|
|
|
{ |
112
|
|
|
if ($this->categoryDisabled($category)) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (isset($this->userDatabase['password'])) { |
117
|
|
|
// Hide the password. |
118
|
|
|
$this->userDatabase['password'] = ''; |
119
|
|
|
} |
120
|
|
|
$values = [ |
121
|
|
|
'query' => $query, |
122
|
|
|
'driver' => $this->userDatabase['driver'], |
123
|
|
|
'options' => json_encode($this->userDatabase) ?? '{}', |
124
|
|
|
'category' => $category, |
125
|
|
|
'last_update' => $this->currentTime(), |
126
|
|
|
'owner_id' => $this->getOwnerId(), |
127
|
|
|
]; |
128
|
|
|
// Duplicates on query are checked on client side, not here. |
129
|
|
|
$query = "insert into dbadmin_runned_commands" . |
130
|
|
|
"(query,driver,options,category,last_update,owner_id) values" . |
131
|
|
|
"(:query,:driver,:options,:category,:last_update,:owner_id)"; |
132
|
|
|
$statement = $this->executeQuery($query, $values); |
133
|
|
|
if ($statement !== false) { |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
Logger::warning('Unable to save command in the query logging database.', [ |
138
|
|
|
'error' => $this->connection->error(), |
139
|
|
|
]); |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $query |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function saveCommand(string $query): bool |
149
|
|
|
{ |
150
|
|
|
$category = $this->category; |
151
|
|
|
// Reset to the default category. |
152
|
|
|
$this->category = Options::CAT_BUILDER; |
153
|
|
|
return $this->enduserDisabled() ? false : |
154
|
|
|
$this->saveRunnedCommand($query, $category); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|