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
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use function array_merge;
use function is_array;
use function is_string;
use function preg_match;
use function sort;
use function strlen;
use function usort;
/**
* handles database lists
*
* <code>
* $ListDatabase = new ListDatabase();
* </code>
*
* @todo this object should be attached to the PMA_Server object
*/
class ListDatabase extends ListAbstract
{
public function __construct()
{
global $dbi;
parent::__construct();
$checkUserPrivileges = new CheckUserPrivileges($dbi);
$checkUserPrivileges->getPrivileges();
$this->build();
}
/**
* checks if the configuration wants to hide some databases
*/
protected function checkHideDatabase(): void
{
if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
return;
}
foreach ($this->getArrayCopy() as $key => $db) {
if (! preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
continue;
}
$this->offsetUnset($key);
}
}
/**
* retrieves database list from server
*
* @param string $like_db_name usually a db_name containing wildcards
*
* @return array
*/
protected function retrieve($like_db_name = null)
{
global $dbi;
$database_list = [];
$command = '';
if (! $GLOBALS['cfg']['Server']['DisableIS']) {
$command .= 'SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`';
if ($like_db_name !== null) {
$command .= " WHERE `SCHEMA_NAME` LIKE '" . $like_db_name . "'";
}
} else {
if ($GLOBALS['dbs_to_test'] === false || $like_db_name !== null) {
$command .= 'SHOW DATABASES';
if ($like_db_name !== null) {
$command .= " LIKE '" . $like_db_name . "'";
}
} else {
foreach ($GLOBALS['dbs_to_test'] as $db) {
$database_list = array_merge(
$database_list,
$this->retrieve($db)
);
}
}
}
if ($command) {
$database_list = $dbi->fetchResult($command, null, null);
}
if ($GLOBALS['cfg']['NaturalOrder']) {
usort($database_list, 'strnatcasecmp');
} else {
// need to sort anyway, otherwise information_schema
// goes at the top
sort($database_list);
}
return $database_list;
}
/**
* builds up the list
*/
public function build(): void
{
if (! $this->checkOnlyDatabase()) {
$items = $this->retrieve();
$this->exchangeArray($items);
}
$this->checkHideDatabase();
}
/**
* checks the only_db configuration
*/
protected function checkOnlyDatabase(): bool
{
if (is_string($GLOBALS['cfg']['Server']['only_db']) && strlen($GLOBALS['cfg']['Server']['only_db']) > 0) {
$GLOBALS['cfg']['Server']['only_db'] = [
$GLOBALS['cfg']['Server']['only_db'],
];
}
if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
return false;
}
$items = [];
foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
// check if the db name contains wildcard,
// thus containing not escaped _ or %
if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
// ... not contains wildcard
$items[] = Util::unescapeMysqlWildcards($each_only_db);
continue;
}
$items = array_merge($items, $this->retrieve($each_only_db));
}
$this->exchangeArray($items);
return true;
}
/**
* returns default item
*
* @return string default item
*/
public function getDefault()
{
if (strlen($GLOBALS['db']) > 0) {
return $GLOBALS['db'];
}
return $this->getEmpty();
}
}
|