2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * holds the PMA_List_Database class
12 require_once './libraries/List.class.php';
15 * handles database lists
18 * $PMA_List_Database = new PMA_List_Database($userlink, $controllink);
21 * @todo this object should be attached to the PMA_Server object
22 * @todo ? make use of INFORMATION_SCHEMA
23 * @todo ? support --skip-showdatabases and user has only global rights
25 * @since phpMyAdmin 2.9.10
27 /*public*/ class PMA_List_Database
extends PMA_List
30 * @var mixed database link resource|object to be used
36 * @var mixed user database link resource|object
39 var $_db_link_user = null;
42 * @var mixed controluser database link resource|object
45 var $_db_link_control = null;
48 * @var boolean whether SHOW DATABASES is disabled or not
51 var $_show_databases_disabled = false;
56 * @uses PMA_List_Database::$_db_link
57 * @uses PMA_List_Database::$_db_link_user
58 * @uses PMA_List_Database::$_db_link_control
59 * @uses PMA_List_Database::build()
60 * @param mixed $db_link_user user database link resource|object
61 * @param mixed $db_link_control control database link resource|object
63 function __construct($db_link_user = null, $db_link_control = null) {
64 $this->_db_link
= $db_link_user;
65 $this->_db_link_user
= $db_link_user;
66 $this->_db_link_control
= $db_link_control;
72 * old PHP 4 style constructor
74 * @see PMA_List_Database::__construct()
76 function PMA_List_Database($db_link_user = null, $db_link_control = null) {
77 $this->__construct($db_link_user, $db_link_control);
81 * removes all databases not accessible by current user from list
84 * @uses PMA_List_Database::$items
85 * @uses PMA_List_Database::$_db_link_user
86 * @uses PMA_List_Database::$_need_to_reindex to set it if reuqired
87 * @uses PMA_DBI_select_db()
89 function _checkAccess()
91 foreach ($this->items
as $key => $db) {
92 if (! @PMA_DBI_select_db
($db, $this->_db_link_user
)) {
93 unset($this->items
[$key]);
98 $this->_need_to_reindex
= true;
102 * checks if the configuration wants to hide some databases
104 * @todo temporaly use this docblock to test how to doc $GLOBALS
106 * @uses PMA_List_Database::$items
107 * @uses PMA_List_Database::$_need_to_reindex to set it if reuqired
109 * @uses $cfg['Server']['hide_db']
111 function _checkHideDatabase()
113 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
117 foreach ($this->items
as $key => $db) {
118 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
119 unset($this->items
[$key]);
123 $this->_need_to_reindex
= true;
127 * retrieves database list from server
129 * @todo we could also search mysql tables if all fail?
131 * @uses PMA_List_Database::$_show_databases_disabled for not retrying if SHOW DATABASES is disabled
132 * @uses PMA_List_Database::$_db_link
133 * @uses PMA_List_Database::$_db_link_control in case of SHOW DATABASES is disabled for userlink
134 * @uses PMA_DBI_fetch_result()
135 * @uses PMA_DBI_getError()
136 * @uses $GLOBALS['error_showdatabases']
137 * @uses $GLOBALS['errno']
138 * @param string $like_db_name usally a db_name containing wildcards
140 function _retrieve($like_db_name = '')
142 if ($this->_show_databases_disabled
) {
146 if (! empty($like_db_name)) {
147 $like = " LIKE '" . $like_db_name . "';";
152 $database_list = PMA_DBI_fetch_result('SHOW DATABASES' . $like, null, null, $this->_db_link
);
155 if ($GLOBALS['errno'] !== 0) {
156 // failed to get database list, try the control user
157 // (hopefully there is one and he has SHOW DATABASES right)
158 $this->_db_link
= $this->_db_link_control
;
159 $database_list = PMA_DBI_fetch_result('SHOW DATABASES' . $like, null, null, $this->_db_link
);
163 if ($GLOBALS['errno'] !== 0) {
164 // failed! we will display a warning that phpMyAdmin could not safely
165 // retrieve database list, the admin has to setup a control user or
166 // allow SHOW DATABASES
167 $GLOBALS['error_showdatabases'] = true;
168 $this->_show_databases_disabled
= true;
172 return $database_list;
178 * @uses PMA_List_Database::$items to initialize it
179 * @uses PMA_List_Database::$_need_to_reindex
180 * @uses PMA_List_Database::_checkOnlyDatabase()
181 * @uses PMA_List_Database::_retrieve()
182 * @uses PMA_List_Database::_checkHideDatabase()
183 * @uses PMA_List_Database::_checkAccess()
184 * @uses PMA_MYSQL_INT_VERSION
185 * @uses array_values()
187 * @uses $cfg['NaturalOrder']
191 $this->items
= array();
193 if (! $this->_checkOnlyDatabase()) {
194 $this->items
= $this->_retrieve();
195 if ($GLOBALS['cfg']['NaturalOrder']) {
196 natsort($this->items
);
197 $this->_need_to_reindex
= true;
201 $this->_checkHideDatabase();
203 // Before MySQL 4.0.2, SHOW DATABASES could send the
204 // whole list, so check if we really have access:
205 if (PMA_MYSQL_INT_VERSION
< 40002) {
206 $this->_checkAccess();
209 if ($this->_need_to_reindex
) {
210 $this->items
= array_values($this->items
);
215 * checks the only_db configuration
217 * @uses PMA_List_Database::$_show_databases_disabled
218 * @uses PMA_List_Database::$items
219 * @uses PMA_List_Database::_retrieve()
220 * @uses PMA_unescape_mysql_wildcards()
223 * @uses array_merge()
227 * @uses $cfg['Server']['only_db']
228 * @return boolean false if there is no only_db, otherwise true
230 function _checkOnlyDatabase()
232 if (is_string($GLOBALS['cfg']['Server']['only_db'])
233 && strlen($GLOBALS['cfg']['Server']['only_db'])) {
234 $GLOBALS['cfg']['Server']['only_db'] = array(
235 $GLOBALS['cfg']['Server']['only_db']
239 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
243 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
244 if ($each_only_db === '*' && ! $this->_show_databases_disabled
) {
245 // append all not already listed dbs to the list
246 $this->items
= array_merge($this->items
,
247 array_diff($this->_retrieve(), $this->items
));
248 // there can only be one '*', and this can only be last
252 // check if the db name contains wildcard,
253 // thus containing not escaped _ or %
254 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
255 // ... not contains wildcard
256 $this->items
[] = PMA_unescape_mysql_wildcards($each_only_db);
260 if (! $this->_show_databases_disabled
) {
261 $this->items
= array_merge($this->items
, $this->_retrieve($each_only_db));
265 // @todo induce error, about not using wildcards with SHOW DATABASE disabled?
272 * returns default item
274 * @uses PMA_List::getEmpty()
275 * @uses $GLOBALS['db']
277 * @return string default item
279 function getDefault()
281 if (strlen($GLOBALS['db'])) {
282 return $GLOBALS['db'];
285 return $this->getEmpty();
289 * returns array with dbs grouped with extended infos
291 * @uses $GLOBALS['PMA_List_Database']
292 * @uses $GLOBALS['cfgRelation']['commwork']
293 * @uses $cfg['ShowTooltip']
294 * @uses $cfg['LeftFrameDBTree']
295 * @uses $cfg['LeftFrameDBSeparator']
296 * @uses $cfg['ShowTooltipAliasDB']
297 * @uses PMA_getTableCount()
298 * @uses PMA_getComments()
303 * @param integer $offset
304 * @param integer $count
305 * @return array db list
307 function getGroupedDetails($offset, $count)
311 foreach ($this->getLimitedItems($offset, $count) as $key => $db) {
312 // garvin: Get comments from PMA comments table
314 if ($GLOBALS['cfg']['ShowTooltip']
315 && $GLOBALS['cfgRelation']['commwork']) {
316 $_db_tooltip = PMA_getComments($db);
317 if (is_array($_db_tooltip)) {
318 $db_tooltip = implode(' ', $_db_tooltip);
322 if ($GLOBALS['cfg']['LeftFrameDBTree']
323 && $GLOBALS['cfg']['LeftFrameDBSeparator']
324 && strstr($db, $GLOBALS['cfg']['LeftFrameDBSeparator']))
326 // use strpos instead of strrpos; it seems more common to
327 // have the db name, the separator, then the rest which
328 // might contain a separator
329 // like dbname_the_rest
330 $pos = strpos($db, $GLOBALS['cfg']['LeftFrameDBSeparator']);
331 $group = substr($db, 0, $pos);
332 $disp_name_cut = substr($db, $pos);
335 $disp_name_cut = $db;
339 if ($db_tooltip && $GLOBALS['cfg']['ShowTooltipAliasDB']) {
340 $disp_name = $db_tooltip;
341 $disp_name_cut = $db_tooltip;
345 $dbgroups[$group][$db] = array(
347 'disp_name_cut' => $disp_name_cut,
348 'disp_name' => $disp_name,
349 'comment' => $db_tooltip,
350 'num_tables' => PMA_getTableCount($db),
352 } // end foreach ($GLOBALS['PMA_List_Database']->items as $db)
357 * returns a part of the items
359 * @uses PMA_List_Database::$items
360 * @uses array_slice()
361 * @param integer $offset
362 * @param integer $count
363 * @return array some items
365 function getLimitedItems($offset, $count)
367 return(array_slice($this->items
, $offset, $count));
371 * returns html code for list with dbs
373 * @return string html code list
375 function getHtmlListGrouped($selected = '', $offset, $count)
377 if (true === $selected) {
378 $selected = $this->getDefault();
381 $return = '<ul id="databaseList" xml:lang="en" dir="ltr">' . "\n";
382 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
383 if (count($dbs) > 1) {
384 $return .= '<li>' . $group . '<ul>' . "\n";
385 // wether display db_name cuted by the group part
391 foreach ($dbs as $db) {
393 if ($db['name'] == $selected) {
394 $return .= ' class="selected"';
396 $return .= '><a' . (! empty($db['comment']) ?
' title="' . $db['comment'] . '"' : '') . ' href="index.php?' . PMA_generate_common_url($db['name']) . '" target="_parent">';
397 $return .= ($cut ?
$db['disp_name_cut'] : $db['disp_name'])
398 .' (' . $db['num_tables'] . ')';
399 $return .= '</a></li>' . "\n";
401 if (count($dbs) > 1) {
402 $return .= '</ul></li>' . "\n";
411 * returns html code for select form element with dbs
413 * @todo IE can not handle different text directions in select boxes so,
414 * as mostly names will be in english, we set the whole selectbox to LTR
417 * @return string html code select
419 function getHtmlSelectGrouped($selected = '', $offset, $count)
421 if (true === $selected) {
422 $selected = $this->getDefault();
425 $return = '<select name="db" id="lightm_db" xml:lang="en" dir="ltr"'
426 . ' onchange="if (this.value != \'\') window.parent.openDb(this.value);">' . "\n"
427 . '<option value="" dir="' . $GLOBALS['text_dir'] . '">'
428 . '(' . $GLOBALS['strDatabases'] . ') ...</option>' . "\n";
429 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
430 if (count($dbs) > 1) {
431 $return .= '<optgroup label="' . htmlspecialchars($group)
433 // wether display db_name cuted by the group part
439 foreach ($dbs as $db) {
440 $return .= '<option value="' . htmlspecialchars($db['name']) . '"'
441 .' title="' . htmlspecialchars($db['comment']) . '"';
442 if ($db['name'] == $selected) {
443 $return .= ' selected="selected"';
445 $return .= '>' . htmlspecialchars($cut ?
$db['disp_name_cut'] : $db['disp_name'])
446 .' (' . $db['num_tables'] . ')</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
463 function _checkAgainstPrivTables()
465 // 1. get allowed dbs from the "mysql.db" table
466 // lem9: User can be blank (anonymous user)
468 SELECT DISTINCT `Db` FROM `mysql`.`db`
469 WHERE `Select_priv` = 'Y'
471 IN ('" . PMA_sqlAddslashes($GLOBALS['cfg']['Server']['user']) . "', '')";
472 $tmp_mydbs = PMA_DBI_fetch_result($local_query, null, null,
473 $GLOBALS['controllink']);
475 // Will use as associative array of the following 2 code
477 // the 1st is the only line intact from before
479 // the 2nd replaces $dblist[] = $row['Db'];
481 // Code following those 2 lines in correction continues
482 // populating $dblist[], as previous code did. But it is
483 // now populated with actual database names instead of
484 // with regular expressions.
485 var_dump($tmp_mydbs);
486 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $GLOBALS['controllink']);
487 // loic1: all databases cases - part 2
488 if (isset($tmp_mydbs['%'])) {
489 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
490 $dblist[] = $tmp_row[0];
493 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
494 $tmp_db = $tmp_row[0];
495 if (isset($tmp_mydbs[$tmp_db]) && $tmp_mydbs[$tmp_db] == 1) {
497 $tmp_mydbs[$tmp_db] = 0;
498 } elseif (!isset($dblist[$tmp_db])) {
499 foreach ($tmp_mydbs as $tmp_matchpattern => $tmp_value) {
500 // loic1: fixed bad regexp
501 // TODO: db names may contain characters
502 // that are regexp instructions
503 $re = '(^|(\\\\\\\\)+|[^\])';
504 $tmp_regex = ereg_replace($re . '%', '\\1.*', ereg_replace($re . '_', '\\1.{1}', $tmp_matchpattern));
505 // Fixed db name matching
506 // 2000-08-28 -- Benjamin Gandon
507 if (ereg('^' . $tmp_regex . '$', $tmp_db)) {
512 } // end if ... elseif ...
515 PMA_DBI_free_result($tmp_alldbs);
519 // 2. get allowed dbs from the "mysql.tables_priv" table
520 $local_query = 'SELECT DISTINCT Db FROM mysql.tables_priv WHERE Table_priv LIKE \'%Select%\' AND User = \'' . PMA_sqlAddslashes($GLOBALS['cfg']['Server']['user']) . '\'';
521 $rs = PMA_DBI_try_query($local_query, $GLOBALS['controllink']);
522 if ($rs && @PMA_DBI_num_rows
($rs)) {
523 while ($row = PMA_DBI_fetch_assoc($rs)) {
524 if (!in_array($row['Db'], $dblist)) {
525 $dblist[] = $row['Db'];
528 PMA_DBI_free_result($rs);