ListDatabase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use function array_merge;
  5. use function is_array;
  6. use function is_string;
  7. use function preg_match;
  8. use function sort;
  9. use function strlen;
  10. use function usort;
  11. /**
  12. * handles database lists
  13. *
  14. * <code>
  15. * $ListDatabase = new ListDatabase();
  16. * </code>
  17. *
  18. * @todo this object should be attached to the PMA_Server object
  19. */
  20. class ListDatabase extends ListAbstract
  21. {
  22. public function __construct()
  23. {
  24. global $dbi;
  25. parent::__construct();
  26. $checkUserPrivileges = new CheckUserPrivileges($dbi);
  27. $checkUserPrivileges->getPrivileges();
  28. $this->build();
  29. }
  30. /**
  31. * checks if the configuration wants to hide some databases
  32. *
  33. * @return void
  34. */
  35. protected function checkHideDatabase()
  36. {
  37. if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
  38. return;
  39. }
  40. foreach ($this->getArrayCopy() as $key => $db) {
  41. if (! preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
  42. continue;
  43. }
  44. $this->offsetUnset($key);
  45. }
  46. }
  47. /**
  48. * retrieves database list from server
  49. *
  50. * @param string $like_db_name usually a db_name containing wildcards
  51. *
  52. * @return array
  53. */
  54. protected function retrieve($like_db_name = null)
  55. {
  56. global $dbi;
  57. $database_list = [];
  58. $command = '';
  59. if (! $GLOBALS['cfg']['Server']['DisableIS']) {
  60. $command .= 'SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`';
  61. if ($like_db_name !== null) {
  62. $command .= " WHERE `SCHEMA_NAME` LIKE '" . $like_db_name . "'";
  63. }
  64. } else {
  65. if ($GLOBALS['dbs_to_test'] === false || $like_db_name !== null) {
  66. $command .= 'SHOW DATABASES';
  67. if ($like_db_name !== null) {
  68. $command .= " LIKE '" . $like_db_name . "'";
  69. }
  70. } else {
  71. foreach ($GLOBALS['dbs_to_test'] as $db) {
  72. $database_list = array_merge(
  73. $database_list,
  74. $this->retrieve($db)
  75. );
  76. }
  77. }
  78. }
  79. if ($command) {
  80. $database_list = $dbi->fetchResult(
  81. $command,
  82. null,
  83. null
  84. );
  85. }
  86. if ($GLOBALS['cfg']['NaturalOrder']) {
  87. usort($database_list, 'strnatcasecmp');
  88. } else {
  89. // need to sort anyway, otherwise information_schema
  90. // goes at the top
  91. sort($database_list);
  92. }
  93. return $database_list;
  94. }
  95. /**
  96. * builds up the list
  97. *
  98. * @return void
  99. */
  100. public function build()
  101. {
  102. if (! $this->checkOnlyDatabase()) {
  103. $items = $this->retrieve();
  104. $this->exchangeArray($items);
  105. }
  106. $this->checkHideDatabase();
  107. }
  108. /**
  109. * checks the only_db configuration
  110. *
  111. * @return bool false if there is no only_db, otherwise true
  112. */
  113. protected function checkOnlyDatabase()
  114. {
  115. if (is_string($GLOBALS['cfg']['Server']['only_db'])
  116. && strlen($GLOBALS['cfg']['Server']['only_db']) > 0
  117. ) {
  118. $GLOBALS['cfg']['Server']['only_db'] = [
  119. $GLOBALS['cfg']['Server']['only_db'],
  120. ];
  121. }
  122. if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
  123. return false;
  124. }
  125. $items = [];
  126. foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
  127. // check if the db name contains wildcard,
  128. // thus containing not escaped _ or %
  129. if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
  130. // ... not contains wildcard
  131. $items[] = Util::unescapeMysqlWildcards($each_only_db);
  132. continue;
  133. }
  134. $items = array_merge($items, $this->retrieve($each_only_db));
  135. }
  136. $this->exchangeArray($items);
  137. return true;
  138. }
  139. /**
  140. * returns default item
  141. *
  142. * @return string default item
  143. */
  144. public function getDefault()
  145. {
  146. if (strlen($GLOBALS['db']) > 0) {
  147. return $GLOBALS['db'];
  148. }
  149. return $this->getEmpty();
  150. }
  151. }