List_Database.class.php 4.5 KB

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