Translation update done using Pootle.
[phpmyadmin.git] / libraries / List_Database.class.php
blob1e1b31fe46bb7d3b8964799875312cf39edd5372
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
93 * @return array
95 protected function _retrieve($like_db_name = null)
97 if ($this->_show_databases_disabled) {
98 return array();
101 if (null !== $like_db_name) {
102 $command = "SHOW DATABASES LIKE '" . $like_db_name . "'";
103 } elseif (null === $this->_command) {
104 $command = str_replace('#user#', $GLOBALS['cfg']['Server']['user'],
105 $GLOBALS['cfg']['Server']['ShowDatabasesCommand']);
106 $this->_command = $command;
107 } else {
108 $command = $this->_command;
111 $database_list = PMA_DBI_fetch_result($command, null, null, $this->_db_link);
112 PMA_DBI_getError();
114 if ($GLOBALS['errno'] !== 0) {
115 // failed to get database list, try the control user
116 // (hopefully there is one and he has SHOW DATABASES right)
117 $this->_db_link = $this->_db_link_control;
118 $database_list = PMA_DBI_fetch_result($command, null, null, $this->_db_link);
120 PMA_DBI_getError();
122 if ($GLOBALS['errno'] !== 0) {
123 // failed! we will display a warning that phpMyAdmin could not safely
124 // retrieve database list, the admin has to setup a control user or
125 // allow SHOW DATABASES
126 $GLOBALS['error_showdatabases'] = true;
127 $this->_show_databases_disabled = true;
131 if ($GLOBALS['cfg']['NaturalOrder']) {
132 natsort($database_list);
133 } else {
134 // need to sort anyway, otherwise information_schema
135 // goes at the top
136 sort($database_list);
139 return $database_list;
143 * builds up the list
146 public function build()
148 if (! $this->_checkOnlyDatabase()) {
149 $items = $this->_retrieve();
150 $this->exchangeArray($items);
153 $this->_checkHideDatabase();
157 * checks the only_db configuration
159 * @return boolean false if there is no only_db, otherwise true
161 protected function _checkOnlyDatabase()
163 if (is_string($GLOBALS['cfg']['Server']['only_db'])
164 && strlen($GLOBALS['cfg']['Server']['only_db'])) {
165 $GLOBALS['cfg']['Server']['only_db'] = array(
166 $GLOBALS['cfg']['Server']['only_db']
170 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
171 return false;
174 $items = array();
176 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
177 if ($each_only_db === '*' && ! $this->_show_databases_disabled) {
178 // append all not already listed dbs to the list
179 $items = array_merge($items,
180 array_diff($this->_retrieve(), $items));
181 // there can only be one '*', and this can only be last
182 break;
185 // check if the db name contains wildcard,
186 // thus containing not escaped _ or %
187 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
188 // ... not contains wildcard
189 $items[] = PMA_unescape_mysql_wildcards($each_only_db);
190 continue;
193 if (! $this->_show_databases_disabled) {
194 $items = array_merge($items, $this->_retrieve($each_only_db));
195 continue;
198 // @todo induce error, about not using wildcards with SHOW DATABASE disabled?
201 $this->exchangeArray($items);
203 return true;
207 * returns default item
209 * @return string default item
211 public function getDefault()
213 if (strlen($GLOBALS['db'])) {
214 return $GLOBALS['db'];
217 return $this->getEmpty();
221 * returns array with dbs grouped with extended infos
223 * @param integer $offset
224 * @param integer $count
225 * @return array db list
227 public function getGroupedDetails($offset, $count)
229 $dbgroups = array();
231 if ($GLOBALS['cfg']['ShowTooltip']
232 && $GLOBALS['cfgRelation']['commwork']) {
233 $db_tooltips = PMA_getDbComments();
236 if (!$GLOBALS['cfg']['LeftFrameDBTree']) {
237 $separators = array();
238 } elseif (is_array($GLOBALS['cfg']['LeftFrameDBSeparator'])) {
239 $separators = $GLOBALS['cfg']['LeftFrameDBSeparator'];
240 } elseif (!empty($GLOBALS['cfg']['LeftFrameDBSeparator'])) {
241 $separators = array($GLOBALS['cfg']['LeftFrameDBSeparator']);
242 } else {
243 $separators = array();
246 foreach ($this->getLimitedItems($offset, $count) as $db) {
247 // Get comments from PMA comments table
248 $db_tooltip = '';
250 if (isset($db_tooltips[$db])) {
251 $db_tooltip = $db_tooltips[$db];
254 $pos = false;
256 foreach ($separators as $separator) {
257 // use strpos instead of strrpos; it seems more common to
258 // have the db name, the separator, then the rest which
259 // might contain a separator
260 // like dbname_the_rest
261 $pos = strpos($db, $separator, 1);
263 if ($pos !== false) {
264 break;
268 if ($pos !== false) {
269 $group = substr($db, 0, $pos);
270 $disp_name_cut = substr($db, $pos);
271 } else {
272 $group = $db;
273 $disp_name_cut = $db;
276 $disp_name = $db;
277 if ($db_tooltip && $GLOBALS['cfg']['ShowTooltipAliasDB']) {
278 $disp_name = $db_tooltip;
279 $disp_name_cut = $db_tooltip;
280 $db_tooltip = $db;
283 $dbgroups[$group][$db] = array(
284 'name' => $db,
285 'disp_name_cut' => $disp_name_cut,
286 'disp_name' => $disp_name,
287 'comment' => $db_tooltip,
290 if ($GLOBALS['cfg']['Server']['CountTables']) {
291 $dbgroups[$group][$db]['num_tables'] = PMA_getTableCount($db);
293 } // end foreach ($GLOBALS['PMA_List_Database']->items as $db)
294 return $dbgroups;
298 * returns a part of the items
300 * @param integer $offset
301 * @param integer $count
302 * @return array some items
304 public function getLimitedItems($offset, $count)
306 return array_slice($this->getArrayCopy(), $offset, $count);
310 * returns html code for list with dbs
312 * @return string html code list
314 public function getHtmlListGrouped($selected = '', $offset, $count)
316 if (true === $selected) {
317 $selected = $this->getDefault();
320 $return = '<ul id="databaseList" xml:lang="en" dir="ltr">' . "\n";
321 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
322 if (count($dbs) > 1) {
323 $return .= '<li class="group"><span>' . htmlspecialchars($group) . '</span><ul>' . "\n";
324 // whether display db_name cut by the group part
325 $cut = true;
326 } else {
327 // .. or full
328 $cut = false;
330 foreach ($dbs as $db) {
331 $return .= '<li';
332 if ($db['name'] == $selected) {
333 $return .= ' class="selected"';
335 $return .= '><a';
336 if (! empty($db['comment'])) {
337 $return .= ' title="' . htmlspecialchars($db['comment']) . '"';
339 $return .= ' href="index.php?' . PMA_generate_common_url($db['name'])
340 . '" target="_parent">';
341 if ($cut) {
342 $return .= htmlspecialchars($db['disp_name_cut']);
343 } else {
344 $return .= htmlspecialchars($db['disp_name']);
347 if (! empty($db['num_tables'])) {
348 $return .= ' (' . $db['num_tables'] . ')';
350 $return .= '</a></li>' . "\n";
352 if (count($dbs) > 1) {
353 $return .= '</ul></li>' . "\n";
356 $return .= '</ul>';
358 return $return;
362 * returns html code for select form element with dbs
364 * @todo IE can not handle different text directions in select boxes so,
365 * as mostly names will be in english, we set the whole selectbox to LTR
366 * and EN
368 * @return string html code select
370 public function getHtmlSelectGrouped($selected = '', $offset, $count)
372 if (true === $selected) {
373 $selected = $this->getDefault();
376 $return = '<select name="db" id="lightm_db" xml:lang="en" dir="ltr"'
377 . ' onchange="if (this.value != \'\') window.parent.openDb(this.value);">' . "\n"
378 . '<option value="" dir="' . $GLOBALS['text_dir'] . '">'
379 . '(' . __('Databases') . ') ...</option>' . "\n";
380 foreach ($this->getGroupedDetails($offset, $count) as $group => $dbs) {
381 if (count($dbs) > 1) {
382 $return .= '<optgroup label="' . htmlspecialchars($group)
383 . '">' . "\n";
384 // whether display db_name cuted by the group part
385 $cut = true;
386 } else {
387 // .. or full
388 $cut = false;
390 foreach ($dbs as $db) {
391 $return .= '<option value="' . htmlspecialchars($db['name']) . '"'
392 .' title="' . htmlspecialchars($db['comment']) . '"';
393 if ($db['name'] == $selected || (PMA_DRIZZLE && strtolower($db['name']) == strtolower($selected))) {
394 $return .= ' selected="selected"';
396 $return .= '>' . htmlspecialchars($cut ? $db['disp_name_cut'] : $db['disp_name']);
397 if (! empty($db['num_tables'])) {
398 $return .= ' (' . $db['num_tables'] . ')';
400 $return .= '</option>' . "\n";
402 if (count($dbs) > 1) {
403 $return .= '</optgroup>' . "\n";
406 $return .= '</select>';
408 return $return;
412 * this is just a backup, if all is fine this can be deleted later
414 * @deprecated
416 protected function _checkAgainstPrivTables()
418 // 1. get allowed dbs from the "mysql.db" table
419 // User can be blank (anonymous user)
420 $local_query = "
421 SELECT DISTINCT `Db` FROM `mysql`.`db`
422 WHERE `Select_priv` = 'Y'
423 AND `User`
424 IN ('" . PMA_sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . "', '')";
425 $tmp_mydbs = PMA_DBI_fetch_result($local_query, null, null,
426 $GLOBALS['controllink']);
427 if ($tmp_mydbs) {
428 // Will use as associative array of the following 2 code
429 // lines:
430 // the 1st is the only line intact from before
431 // correction,
432 // the 2nd replaces $dblist[] = $row['Db'];
434 // Code following those 2 lines in correction continues
435 // populating $dblist[], as previous code did. But it is
436 // now populated with actual database names instead of
437 // with regular expressions.
438 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $GLOBALS['controllink']);
439 // all databases cases - part 2
440 if (isset($tmp_mydbs['%'])) {
441 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
442 $dblist[] = $tmp_row[0];
443 } // end while
444 } else {
445 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
446 $tmp_db = $tmp_row[0];
447 if (isset($tmp_mydbs[$tmp_db]) && $tmp_mydbs[$tmp_db] == 1) {
448 $dblist[] = $tmp_db;
449 $tmp_mydbs[$tmp_db] = 0;
450 } elseif (! isset($dblist[$tmp_db])) {
451 foreach ($tmp_mydbs as $tmp_matchpattern => $tmp_value) {
452 // fixed bad regexp
453 // TODO: db names may contain characters
454 // that are regexp instructions
455 $re = '(^|(\\\\\\\\)+|[^\])';
456 $tmp_regex = preg_replace('/' . addcslashes($re,'/') . '%/', '\\1.*', preg_replace('/' . addcslashes($re,'/') . '_/', '\\1.{1}', $tmp_matchpattern));
457 // Fixed db name matching
458 // 2000-08-28 -- Benjamin Gandon
459 if (preg_match('/^' . addcslashes($tmp_regex,'/') . '$/', $tmp_db)) {
460 $dblist[] = $tmp_db;
461 break;
463 } // end while
464 } // end if ... elseif ...
465 } // end while
466 } // end else
467 PMA_DBI_free_result($tmp_alldbs);
468 unset($tmp_mydbs);
469 } // end if
471 // 2. get allowed dbs from the "mysql.tables_priv" table
472 $local_query = 'SELECT DISTINCT Db FROM mysql.tables_priv WHERE Table_priv LIKE \'%Select%\' AND User = \'' . PMA_sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . '\'';
473 $rs = PMA_DBI_try_query($local_query, $GLOBALS['controllink']);
474 if ($rs && @PMA_DBI_num_rows($rs)) {
475 while ($row = PMA_DBI_fetch_assoc($rs)) {
476 if (!in_array($row['Db'], $dblist)) {
477 $dblist[] = $row['Db'];
479 } // end while
480 PMA_DBI_free_result($rs);
481 } // end if