Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / List_Database.class.php
blob748a3e73928bc06ae90d3861ce09f0bdfa7c45ad
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * holds the PMA_List_Database class
6 * @package phpMyAdmin
7 */
9 /**
10 * the list base class
12 require_once './libraries/List.class.php';
14 /**
15 * handles database lists
17 * <code>
18 * $PMA_List_Database = new PMA_List_Database($userlink, $controllink);
19 * </code>
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
24 * @access public
25 * @since phpMyAdmin 2.9.10
26 * @package phpMyAdmin
28 /*public*/ class PMA_List_Database extends PMA_List
30 /**
31 * @var mixed database link resource|object to be used
33 protected $_db_link = null;
35 /**
36 * @var mixed user database link resource|object
38 protected $_db_link_user = null;
40 /**
41 * @var mixed controluser database link resource|object
43 protected $_db_link_control = null;
45 /**
46 * @var boolean whether SHOW DATABASES is disabled or not
47 * @access protected
49 protected $_show_databases_disabled = false;
51 /**
52 * @var string command to retrieve databases from server
54 protected $_command = null;
56 /**
57 * Constructor
59 * @param mixed $db_link_user user database link resource|object
60 * @param mixed $db_link_control control database link resource|object
62 public 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;
68 parent::__construct();
69 $this->build();
72 /**
73 * checks if the configuration wants to hide some databases
75 protected function _checkHideDatabase()
77 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
78 return;
81 foreach ($this->getArrayCopy() as $key => $db) {
82 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
83 $this->offsetUnset($key);
88 /**
89 * retrieves database list from server
91 * @todo we could also search mysql tables if all fail?
92 * @param string $like_db_name usally a db_name containing wildcards
94 protected function _retrieve($like_db_name = null)
96 if ($this->_show_databases_disabled) {
97 return array();
100 if (null !== $like_db_name) {
101 $command = "SHOW DATABASES LIKE '" . $like_db_name . "'";
102 } elseif (null === $this->_command) {
103 $command = str_replace('#user#', $GLOBALS['cfg']['Server']['user'],
104 $GLOBALS['cfg']['Server']['ShowDatabasesCommand']);
105 $this->_command = $command;
106 } else {
107 $command = $this->_command;
110 $database_list = PMA_DBI_fetch_result($command, null, null, $this->_db_link);
111 PMA_DBI_getError();
113 if ($GLOBALS['errno'] !== 0) {
114 // failed to get database list, try the control user
115 // (hopefully there is one and he has SHOW DATABASES right)
116 $this->_db_link = $this->_db_link_control;
117 $database_list = PMA_DBI_fetch_result($command, null, null, $this->_db_link);
119 PMA_DBI_getError();
121 if ($GLOBALS['errno'] !== 0) {
122 // failed! we will display a warning that phpMyAdmin could not safely
123 // retrieve database list, the admin has to setup a control user or
124 // allow SHOW DATABASES
125 $GLOBALS['error_showdatabases'] = true;
126 $this->_show_databases_disabled = true;
130 if ($GLOBALS['cfg']['NaturalOrder']) {
131 natsort($database_list);
132 } else {
133 // need to sort anyway, otherwise information_schema
134 // goes at the top
135 sort($database_list);
138 return $database_list;
142 * builds up the list
145 public function build()
147 if (! $this->_checkOnlyDatabase()) {
148 $items = $this->_retrieve();
149 $this->exchangeArray($items);
152 $this->_checkHideDatabase();
156 * checks the only_db configuration
158 * @return boolean false if there is no only_db, otherwise true
160 protected function _checkOnlyDatabase()
162 if (is_string($GLOBALS['cfg']['Server']['only_db'])
163 && strlen($GLOBALS['cfg']['Server']['only_db'])) {
164 $GLOBALS['cfg']['Server']['only_db'] = array(
165 $GLOBALS['cfg']['Server']['only_db']
169 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
170 return false;
173 $items = array();
175 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
176 if ($each_only_db === '*' && ! $this->_show_databases_disabled) {
177 // append all not already listed dbs to the list
178 $items = array_merge($items,
179 array_diff($this->_retrieve(), $items));
180 // there can only be one '*', and this can only be last
181 break;
184 // check if the db name contains wildcard,
185 // thus containing not escaped _ or %
186 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
187 // ... not contains wildcard
188 $items[] = PMA_unescape_mysql_wildcards($each_only_db);
189 continue;
192 if (! $this->_show_databases_disabled) {
193 $items = array_merge($items, $this->_retrieve($each_only_db));
194 continue;
197 // @todo induce error, about not using wildcards with SHOW DATABASE disabled?
200 $this->exchangeArray($items);
202 return true;
206 * returns default item
208 * @return string default item
210 public function getDefault()
212 if (strlen($GLOBALS['db'])) {
213 return $GLOBALS['db'];
216 return $this->getEmpty();
220 * returns array with dbs grouped with extended infos
222 * @param integer $offset
223 * @param integer $count
224 * @return array db list
226 public function getGroupedDetails($offset, $count)
228 $dbgroups = array();
230 if ($GLOBALS['cfg']['ShowTooltip']
231 && $GLOBALS['cfgRelation']['commwork']) {
232 $db_tooltips = PMA_getDbComments();
235 if (!$GLOBALS['cfg']['LeftFrameDBTree']) {
236 $separators = array();
237 } elseif (is_array($GLOBALS['cfg']['LeftFrameDBSeparator'])) {
238 $separators = $GLOBALS['cfg']['LeftFrameDBSeparator'];
239 } elseif (!empty($GLOBALS['cfg']['LeftFrameDBSeparator'])) {
240 $separators = array($GLOBALS['cfg']['LeftFrameDBSeparator']);
241 } else {
242 $separators = array();
245 foreach ($this->getLimitedItems($offset, $count) as $db) {
246 // Get comments from PMA comments table
247 $db_tooltip = '';
249 if (isset($db_tooltips[$db])) {
250 $db_tooltip = $db_tooltips[$db];
253 $pos = false;
255 foreach ($separators as $separator) {
256 // use strpos instead of strrpos; it seems more common to
257 // have the db name, the separator, then the rest which
258 // might contain a separator
259 // like dbname_the_rest
260 $pos = strpos($db, $separator, 1);
262 if ($pos !== false) {
263 break;
267 if ($pos !== false) {
268 $group = substr($db, 0, $pos);
269 $disp_name_cut = substr($db, $pos);
270 } else {
271 $group = $db;
272 $disp_name_cut = $db;
275 $disp_name = $db;
276 if ($db_tooltip && $GLOBALS['cfg']['ShowTooltipAliasDB']) {
277 $disp_name = $db_tooltip;
278 $disp_name_cut = $db_tooltip;
279 $db_tooltip = $db;
282 $dbgroups[$group][$db] = array(
283 'name' => $db,
284 'disp_name_cut' => $disp_name_cut,
285 'disp_name' => $disp_name,
286 'comment' => $db_tooltip,
289 if ($GLOBALS['cfg']['Server']['CountTables']) {
290 $dbgroups[$group][$db]['num_tables'] = PMA_getTableCount($db);
292 } // end foreach ($GLOBALS['PMA_List_Database']->items as $db)
293 return $dbgroups;
297 * returns a part of the items
299 * @param integer $offset
300 * @param integer $count
301 * @return array some items
303 public function getLimitedItems($offset, $count)
305 return array_slice($this->getArrayCopy(), $offset, $count);
309 * returns html code for list with dbs
311 * @return string html code list
313 public function getHtmlListGrouped($selected = '', $offset, $count)
315 if (true === $selected) {
316 $selected = $this->getDefault();
319 $return = '<ul id="databaseList" xml:lang="en" dir="ltr">' . "\n";
320 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
321 if (count($dbs) > 1) {
322 $return .= '<li class="group"><span>' . htmlspecialchars($group) . '</span><ul>' . "\n";
323 // whether display db_name cut by the group part
324 $cut = true;
325 } else {
326 // .. or full
327 $cut = false;
329 foreach ($dbs as $db) {
330 $return .= '<li';
331 if ($db['name'] == $selected) {
332 $return .= ' class="selected"';
334 $return .= '><a';
335 if (! empty($db['comment'])) {
336 $return .= ' title="' . htmlspecialchars($db['comment']) . '"';
338 $return .= ' href="index.php?' . PMA_generate_common_url($db['name'])
339 . '" target="_parent">';
340 if ($cut) {
341 $return .= htmlspecialchars($db['disp_name_cut']);
342 } else {
343 $return .= htmlspecialchars($db['disp_name']);
346 if (! empty($db['num_tables'])) {
347 $return .= ' (' . $db['num_tables'] . ')';
349 $return .= '</a></li>' . "\n";
351 if (count($dbs) > 1) {
352 $return .= '</ul></li>' . "\n";
355 $return .= '</ul>';
357 return $return;
361 * returns html code for select form element with dbs
363 * @todo IE can not handle different text directions in select boxes so,
364 * as mostly names will be in english, we set the whole selectbox to LTR
365 * and EN
367 * @return string html code select
369 public function getHtmlSelectGrouped($selected = '', $offset, $count)
371 if (true === $selected) {
372 $selected = $this->getDefault();
375 $return = '<select name="db" id="lightm_db" xml:lang="en" dir="ltr"'
376 . ' onchange="if (this.value != \'\') window.parent.openDb(this.value);">' . "\n"
377 . '<option value="" dir="' . $GLOBALS['text_dir'] . '">'
378 . '(' . __('Databases') . ') ...</option>' . "\n";
379 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
380 if (count($dbs) > 1) {
381 $return .= '<optgroup label="' . htmlspecialchars($group)
382 . '">' . "\n";
383 // whether display db_name cuted by the group part
384 $cut = true;
385 } else {
386 // .. or full
387 $cut = false;
389 foreach ($dbs as $db) {
390 $return .= '<option value="' . htmlspecialchars($db['name']) . '"'
391 .' title="' . htmlspecialchars($db['comment']) . '"';
392 if ($db['name'] == $selected || (PMA_DRIZZLE && strtolower($db['name']) == strtolower($selected))) {
393 $return .= ' selected="selected"';
395 $return .= '>' . htmlspecialchars($cut ? $db['disp_name_cut'] : $db['disp_name']);
396 if (! empty($db['num_tables'])) {
397 $return .= ' (' . $db['num_tables'] . ')';
399 $return .= '</option>' . "\n";
401 if (count($dbs) > 1) {
402 $return .= '</optgroup>' . "\n";
405 $return .= '</select>';
407 return $return;
411 * this is just a backup, if all is fine this can be deleted later
413 * @deprecated
415 protected function _checkAgainstPrivTables()
417 // 1. get allowed dbs from the "mysql.db" table
418 // User can be blank (anonymous user)
419 $local_query = "
420 SELECT DISTINCT `Db` FROM `mysql`.`db`
421 WHERE `Select_priv` = 'Y'
422 AND `User`
423 IN ('" . PMA_sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . "', '')";
424 $tmp_mydbs = PMA_DBI_fetch_result($local_query, null, null,
425 $GLOBALS['controllink']);
426 if ($tmp_mydbs) {
427 // Will use as associative array of the following 2 code
428 // lines:
429 // the 1st is the only line intact from before
430 // correction,
431 // the 2nd replaces $dblist[] = $row['Db'];
433 // Code following those 2 lines in correction continues
434 // populating $dblist[], as previous code did. But it is
435 // now populated with actual database names instead of
436 // with regular expressions.
437 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $GLOBALS['controllink']);
438 // all databases cases - part 2
439 if (isset($tmp_mydbs['%'])) {
440 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
441 $dblist[] = $tmp_row[0];
442 } // end while
443 } else {
444 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
445 $tmp_db = $tmp_row[0];
446 if (isset($tmp_mydbs[$tmp_db]) && $tmp_mydbs[$tmp_db] == 1) {
447 $dblist[] = $tmp_db;
448 $tmp_mydbs[$tmp_db] = 0;
449 } elseif (! isset($dblist[$tmp_db])) {
450 foreach ($tmp_mydbs as $tmp_matchpattern => $tmp_value) {
451 // fixed bad regexp
452 // TODO: db names may contain characters
453 // that are regexp instructions
454 $re = '(^|(\\\\\\\\)+|[^\])';
455 $tmp_regex = preg_replace('/' . addcslashes($re,'/') . '%/', '\\1.*', preg_replace('/' . addcslashes($re,'/') . '_/', '\\1.{1}', $tmp_matchpattern));
456 // Fixed db name matching
457 // 2000-08-28 -- Benjamin Gandon
458 if (preg_match('/^' . addcslashes($tmp_regex,'/') . '$/', $tmp_db)) {
459 $dblist[] = $tmp_db;
460 break;
462 } // end while
463 } // end if ... elseif ...
464 } // end while
465 } // end else
466 PMA_DBI_free_result($tmp_alldbs);
467 unset($tmp_mydbs);
468 } // end if
470 // 2. get allowed dbs from the "mysql.tables_priv" table
471 $local_query = 'SELECT DISTINCT Db FROM mysql.tables_priv WHERE Table_priv LIKE \'%Select%\' AND User = \'' . PMA_sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . '\'';
472 $rs = PMA_DBI_try_query($local_query, $GLOBALS['controllink']);
473 if ($rs && @PMA_DBI_num_rows($rs)) {
474 while ($row = PMA_DBI_fetch_assoc($rs)) {
475 if (!in_array($row['Db'], $dblist)) {
476 $dblist[] = $row['Db'];
478 } // end while
479 PMA_DBI_free_result($rs);
480 } // end if