ListDatabase.php 4.2 KB

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