-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathListDatabase.php
More file actions
181 lines (153 loc) · 5.02 KB
/
ListDatabase.php
File metadata and controls
181 lines (153 loc) · 5.02 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use ArrayObject;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Query\Utilities;
use function array_merge;
use function in_array;
use function is_array;
use function is_string;
use function preg_match;
use function sort;
use function strnatcasecmp;
use function strtr;
use function usort;
/**
* Handles database lists
*
* @extends ArrayObject<int, string>
*/
class ListDatabase extends ArrayObject
{
public function __construct(
private readonly DatabaseInterface $dbi,
private readonly Config $config,
private readonly UserPrivilegesFactory $userPrivilegesFactory,
) {
parent::__construct();
$userPrivileges = $this->userPrivilegesFactory->getPrivileges();
$this->build($userPrivileges);
}
/** @return array<int, array<string, bool|string>> */
public function getList(): array
{
$list = [];
foreach ($this as $eachItem) {
if (Utilities::isSystemSchema($eachItem)) {
continue;
}
$list[] = ['name' => $eachItem, 'is_selected' => $eachItem === Current::$database];
}
return $list;
}
/**
* checks if the configuration wants to hide some databases
*/
protected function checkHideDatabase(): void
{
if (empty($this->config->selectedServer['hide_db'])) {
return;
}
foreach ($this->getArrayCopy() as $key => $db) {
if (preg_match('/' . $this->config->selectedServer['hide_db'] . '/', $db) !== 1) {
continue;
}
$this->offsetUnset($key);
}
}
/**
* retrieves database list from server
*
* @param string|null $likeDbName usually a db_name containing wildcards
*
* @return mixed[]
*/
protected function retrieve(UserPrivileges $userPrivileges, string|null $likeDbName = null): array
{
$databaseList = [];
$command = '';
if (! $this->config->selectedServer['DisableIS']) {
$command .= 'SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`';
if ($likeDbName !== null) {
$command .= " WHERE `SCHEMA_NAME` LIKE '" . $likeDbName . "'";
}
} elseif ($userPrivileges->databasesToTest === false || $likeDbName !== null) {
$command .= 'SHOW DATABASES';
if ($likeDbName !== null) {
$command .= " LIKE '" . $likeDbName . "'";
}
} else {
foreach ($userPrivileges->databasesToTest as $db) {
$databaseList = array_merge(
$databaseList,
$this->retrieve($userPrivileges, $db),
);
}
}
if ($command !== '') {
/** @var string[] $databaseList */
$databaseList = $this->dbi->fetchSingleColumn($command);
}
if ($this->config->settings['NaturalOrder']) {
usort($databaseList, strnatcasecmp(...));
} else {
// need to sort anyway, otherwise information_schema
// goes at the top
sort($databaseList);
}
return $databaseList;
}
/**
* builds up the list
*/
public function build(UserPrivileges $userPrivileges): void
{
if (! $this->checkOnlyDatabase($userPrivileges)) {
$items = $this->retrieve($userPrivileges);
$this->exchangeArray($items);
}
$this->checkHideDatabase();
}
/**
* checks the only_db configuration
*/
protected function checkOnlyDatabase(UserPrivileges $userPrivileges): bool
{
if (
is_string($this->config->selectedServer['only_db']) && $this->config->selectedServer['only_db'] !== ''
) {
$this->config->selectedServer['only_db'] = [$this->config->selectedServer['only_db']];
}
if (! is_array($this->config->selectedServer['only_db'])) {
return false;
}
$items = [];
foreach ($this->config->selectedServer['only_db'] as $eachOnlyDb) {
// check if the db name contains wildcard,
// thus containing not escaped _ or %
if (preg_match('/(^|[^\\\\])(_|%)/', $eachOnlyDb) !== 1) {
// ... not contains wildcard
$items[] = strtr($eachOnlyDb, ['\\\\' => '\\', '\\_' => '_', '\\%' => '%']);
continue;
}
$items = array_merge($items, $this->retrieve($userPrivileges, $eachOnlyDb));
}
$this->exchangeArray($items);
return true;
}
/**
* Checks if the given strings exists in the current list, if there is
* missing at least one item it returns false otherwise true
*/
public function exists(string ...$params): bool
{
$elements = $this->getArrayCopy();
foreach ($params as $param) {
if (! in_array($param, $elements, true)) {
return false;
}
}
return true;
}
}