2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * holds the PMA_List_Database class
13 require_once './libraries/List.class.php';
16 * handles database lists
19 * $PMA_List_Database = new PMA_List_Database($userlink, $controllink);
22 * @todo this object should be attached to the PMA_Server object
23 * @todo ? make use of INFORMATION_SCHEMA
24 * @todo ? support --skip-showdatabases and user has only global rights
26 * @since phpMyAdmin 2.9.10
29 /*public*/ class PMA_List_Database
extends PMA_List
32 * @var mixed database link resource|object to be used
34 protected $_db_link = null;
37 * @var mixed user database link resource|object
39 protected $_db_link_user = null;
42 * @var mixed controluser database link resource|object
44 protected $_db_link_control = null;
47 * @var boolean whether SHOW DATABASES is disabled or not
50 protected $_show_databases_disabled = false;
53 * @var string command to retrieve databases from server
55 protected $_command = null;
60 * @uses PMA_List_Database::$_db_link
61 * @uses PMA_List_Database::$_db_link_user
62 * @uses PMA_List_Database::$_db_link_control
63 * @uses PMA_List_Database::build()
64 * @param mixed $db_link_user user database link resource|object
65 * @param mixed $db_link_control control database link resource|object
67 public function __construct($db_link_user = null, $db_link_control = null)
69 $this->_db_link
= $db_link_user;
70 $this->_db_link_user
= $db_link_user;
71 $this->_db_link_control
= $db_link_control;
73 parent
::__construct();
78 * checks if the configuration wants to hide some databases
80 * @todo temporaly use this docblock to test how to doc $GLOBALS
81 * @uses PMA_List_Database::$items
83 * @uses $cfg['Server']['hide_db']
85 protected function _checkHideDatabase()
87 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
91 foreach ($this->getArrayCopy() as $key => $db) {
92 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
93 $this->offsetUnset($key);
99 * retrieves database list from server
101 * @todo we could also search mysql tables if all fail?
102 * @uses PMA_List_Database::$_show_databases_disabled for not retrying if SHOW DATABASES is disabled
103 * @uses PMA_List_Database::$_db_link
104 * @uses PMA_List_Database::$_db_link_control in case of SHOW DATABASES is disabled for userlink
105 * @uses PMA_DBI_fetch_result()
106 * @uses PMA_DBI_getError()
107 * @uses $GLOBALS['error_showdatabases']
108 * @uses $GLOBALS['errno']
109 * @param string $like_db_name usally a db_name containing wildcards
111 protected function _retrieve($like_db_name = null)
113 if ($this->_show_databases_disabled
) {
117 if (null !== $like_db_name) {
118 $command = "SHOW DATABASES LIKE '" . $like_db_name . "'";
119 } elseif (null === $this->_command
) {
120 $command = str_replace('#user#', $GLOBALS['cfg']['Server']['user'],
121 $GLOBALS['cfg']['Server']['ShowDatabasesCommand']);
122 $this->_command
= $command;
124 $command = $this->_command
;
127 $database_list = PMA_DBI_fetch_result($command, null, null, $this->_db_link
);
130 if ($GLOBALS['errno'] !== 0) {
131 // failed to get database list, try the control user
132 // (hopefully there is one and he has SHOW DATABASES right)
133 $this->_db_link
= $this->_db_link_control
;
134 $database_list = PMA_DBI_fetch_result($command, null, null, $this->_db_link
);
138 if ($GLOBALS['errno'] !== 0) {
139 // failed! we will display a warning that phpMyAdmin could not safely
140 // retrieve database list, the admin has to setup a control user or
141 // allow SHOW DATABASES
142 $GLOBALS['error_showdatabases'] = true;
143 $this->_show_databases_disabled
= true;
147 return $database_list;
153 * @uses PMA_List_Database::$items to initialize it
154 * @uses PMA_List_Database::_checkOnlyDatabase()
155 * @uses PMA_List_Database::_retrieve()
156 * @uses PMA_List_Database::_checkHideDatabase()
157 * @uses array_values()
159 * @uses $cfg['NaturalOrder']
161 public function build()
163 if (! $this->_checkOnlyDatabase()) {
164 $items = $this->_retrieve();
165 if ($GLOBALS['cfg']['NaturalOrder']) {
168 $this->exchangeArray($items);
171 $this->_checkHideDatabase();
175 * checks the only_db configuration
177 * @uses PMA_List_Database::$_show_databases_disabled
178 * @uses PMA_List_Database::$items
179 * @uses PMA_List_Database::_retrieve()
180 * @uses PMA_unescape_mysql_wildcards()
183 * @uses array_merge()
187 * @uses $cfg['Server']['only_db']
188 * @return boolean false if there is no only_db, otherwise true
190 protected function _checkOnlyDatabase()
192 if (is_string($GLOBALS['cfg']['Server']['only_db'])
193 && strlen($GLOBALS['cfg']['Server']['only_db'])) {
194 $GLOBALS['cfg']['Server']['only_db'] = array(
195 $GLOBALS['cfg']['Server']['only_db']
199 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
205 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
206 if ($each_only_db === '*' && ! $this->_show_databases_disabled
) {
207 // append all not already listed dbs to the list
208 $items = array_merge($items,
209 array_diff($this->_retrieve(), $items));
210 // there can only be one '*', and this can only be last
214 // check if the db name contains wildcard,
215 // thus containing not escaped _ or %
216 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
217 // ... not contains wildcard
218 $items[] = PMA_unescape_mysql_wildcards($each_only_db);
222 if (! $this->_show_databases_disabled
) {
223 $items = array_merge($items, $this->_retrieve($each_only_db));
227 // @todo induce error, about not using wildcards with SHOW DATABASE disabled?
230 $this->exchangeArray($items);
236 * returns default item
238 * @uses PMA_List::getEmpty()
239 * @uses $GLOBALS['db']
241 * @return string default item
243 public function getDefault()
245 if (strlen($GLOBALS['db'])) {
246 return $GLOBALS['db'];
249 return $this->getEmpty();
253 * returns array with dbs grouped with extended infos
255 * @uses $GLOBALS['PMA_List_Database']
256 * @uses $GLOBALS['cfgRelation']['commwork']
257 * @uses $cfg['ShowTooltip']
258 * @uses $cfg['LeftFrameDBTree']
259 * @uses $cfg['LeftFrameDBSeparator']
260 * @uses $cfg['ShowTooltipAliasDB']
261 * @uses PMA_getTableCount()
262 * @uses PMA_getDbComment()
267 * @param integer $offset
268 * @param integer $count
269 * @return array db list
271 public function getGroupedDetails($offset, $count)
276 if ($GLOBALS['cfg']['ShowTooltip']
277 && $GLOBALS['cfgRelation']['commwork']) {
278 $db_tooltips = PMA_getDbComments();
281 if (!$GLOBALS['cfg']['LeftFrameDBTree']) {
282 $separators = array();
283 } elseif (is_array($GLOBALS['cfg']['LeftFrameDBSeparator'])) {
284 $separators = $GLOBALS['cfg']['LeftFrameDBSeparator'];
285 } elseif (!empty($GLOBALS['cfg']['LeftFrameDBSeparator'])) {
286 $separators = array($GLOBALS['cfg']['LeftFrameDBSeparator']);
288 $separators = array();
291 foreach ($this->getLimitedItems($offset, $count) as $key => $db) {
292 // garvin: Get comments from PMA comments table
295 if (isset($db_tooltips[$db])) {
296 $db_tooltip = $db_tooltips[$db];
301 foreach($separators as $separator) {
302 // use strpos instead of strrpos; it seems more common to
303 // have the db name, the separator, then the rest which
304 // might contain a separator
305 // like dbname_the_rest
306 $pos = strpos($db, $separator);
308 if ($pos !== false) {
313 if ($pos !== false) {
314 $group = substr($db, 0, $pos);
315 $disp_name_cut = substr($db, $pos);
318 $disp_name_cut = $db;
322 if ($db_tooltip && $GLOBALS['cfg']['ShowTooltipAliasDB']) {
323 $disp_name = $db_tooltip;
324 $disp_name_cut = $db_tooltip;
328 $dbgroups[$group][$db] = array(
330 'disp_name_cut' => $disp_name_cut,
331 'disp_name' => $disp_name,
332 'comment' => $db_tooltip,
335 if ($GLOBALS['cfg']['Server']['CountTables']) {
336 $dbgroups[$group][$db]['num_tables'] = PMA_getTableCount($db);
338 } // end foreach ($GLOBALS['PMA_List_Database']->items as $db)
343 * returns a part of the items
345 * @uses array_slice()
346 * @param integer $offset
347 * @param integer $count
348 * @return array some items
350 public function getLimitedItems($offset, $count)
352 return array_slice($this->getArrayCopy(), $offset, $count);
356 * returns html code for list with dbs
358 * @return string html code list
360 public function getHtmlListGrouped($selected = '', $offset, $count)
362 if (true === $selected) {
363 $selected = $this->getDefault();
366 $return = '<ul id="databaseList" xml:lang="en" dir="ltr">' . "\n";
367 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
368 if (count($dbs) > 1) {
369 $return .= '<li>' . htmlspecialchars($group) . '<ul>' . "\n";
370 // whether display db_name cut by the group part
376 foreach ($dbs as $db) {
378 if ($db['name'] == $selected) {
379 $return .= ' class="selected"';
382 if (! empty($db['comment'])) {
383 $return .= ' title="' . htmlspecialchars($db['comment']) . '"';
385 $return .= ' href="index.php?' . PMA_generate_common_url($db['name'])
386 . '" target="_parent">';
388 $return .= htmlspecialchars($db['disp_name_cut']);
390 $return .= htmlspecialchars($db['disp_name']);
393 if (! empty($db['num_tables'])) {
394 $return .= ' (' . $db['num_tables'] . ')';
396 $return .= '</a></li>' . "\n";
398 if (count($dbs) > 1) {
399 $return .= '</ul></li>' . "\n";
408 * returns html code for select form element with dbs
410 * @todo IE can not handle different text directions in select boxes so,
411 * as mostly names will be in english, we set the whole selectbox to LTR
414 * @return string html code select
416 public function getHtmlSelectGrouped($selected = '', $offset, $count)
418 if (true === $selected) {
419 $selected = $this->getDefault();
422 $return = '<select name="db" id="lightm_db" xml:lang="en" dir="ltr"'
423 . ' onchange="if (this.value != \'\') window.parent.openDb(this.value);">' . "\n"
424 . '<option value="" dir="' . $GLOBALS['text_dir'] . '">'
425 . '(' . $GLOBALS['strDatabases'] . ') ...</option>' . "\n";
426 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
427 if (count($dbs) > 1) {
428 $return .= '<optgroup label="' . htmlspecialchars($group)
430 // whether display db_name cuted by the group part
436 foreach ($dbs as $db) {
437 $return .= '<option value="' . htmlspecialchars($db['name']) . '"'
438 .' title="' . htmlspecialchars($db['comment']) . '"';
439 if ($db['name'] == $selected) {
440 $return .= ' selected="selected"';
442 $return .= '>' . htmlspecialchars($cut ?
$db['disp_name_cut'] : $db['disp_name']);
443 if (! empty($db['num_tables'])) {
444 $return .= ' (' . $db['num_tables'] . ')';
446 $return .= '</option>' . "\n";
448 if (count($dbs) > 1) {
449 $return .= '</optgroup>' . "\n";
452 $return .= '</select>';
458 * this is just a backup, if all is fine this can be deleted later
462 protected function _checkAgainstPrivTables()
464 // 1. get allowed dbs from the "mysql.db" table
465 // lem9: User can be blank (anonymous user)
467 SELECT DISTINCT `Db` FROM `mysql`.`db`
468 WHERE `Select_priv` = 'Y'
470 IN ('" . PMA_sqlAddslashes($GLOBALS['cfg']['Server']['user']) . "', '')";
471 $tmp_mydbs = PMA_DBI_fetch_result($local_query, null, null,
472 $GLOBALS['controllink']);
474 // Will use as associative array of the following 2 code
476 // the 1st is the only line intact from before
478 // the 2nd replaces $dblist[] = $row['Db'];
480 // Code following those 2 lines in correction continues
481 // populating $dblist[], as previous code did. But it is
482 // now populated with actual database names instead of
483 // with regular expressions.
484 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $GLOBALS['controllink']);
485 // loic1: all databases cases - part 2
486 if (isset($tmp_mydbs['%'])) {
487 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
488 $dblist[] = $tmp_row[0];
491 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
492 $tmp_db = $tmp_row[0];
493 if (isset($tmp_mydbs[$tmp_db]) && $tmp_mydbs[$tmp_db] == 1) {
495 $tmp_mydbs[$tmp_db] = 0;
496 } elseif (!isset($dblist[$tmp_db])) {
497 foreach ($tmp_mydbs as $tmp_matchpattern => $tmp_value) {
498 // loic1: fixed bad regexp
499 // TODO: db names may contain characters
500 // that are regexp instructions
501 $re = '(^|(\\\\\\\\)+|[^\])';
502 $tmp_regex = preg_replace('/' . addcslashes($re,'/') . '%/', '\\1.*', preg_replace('/' . addcslashes($re,'/') . '_/', '\\1.{1}', $tmp_matchpattern));
503 // Fixed db name matching
504 // 2000-08-28 -- Benjamin Gandon
505 if (preg_match('/^' . addcslashes($tmp_regex,'/') . '$/', $tmp_db)) {
510 } // end if ... elseif ...
513 PMA_DBI_free_result($tmp_alldbs);
517 // 2. get allowed dbs from the "mysql.tables_priv" table
518 $local_query = 'SELECT DISTINCT Db FROM mysql.tables_priv WHERE Table_priv LIKE \'%Select%\' AND User = \'' . PMA_sqlAddslashes($GLOBALS['cfg']['Server']['user']) . '\'';
519 $rs = PMA_DBI_try_query($local_query, $GLOBALS['controllink']);
520 if ($rs && @PMA_DBI_num_rows
($rs)) {
521 while ($row = PMA_DBI_fetch_assoc($rs)) {
522 if (!in_array($row['Db'], $dblist)) {
523 $dblist[] = $row['Db'];
526 PMA_DBI_free_result($rs);