ListDatabase.php 4.3 KB

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