2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Generates and renders the top menu
8 namespace PMA\libraries
;
10 use PMA\libraries\URL
;
13 * Class for generating the top menu
42 * Creates a new instance of Menu
44 * @param int $server Server id
45 * @param string $db Database name
46 * @param string $table Table name
48 public function __construct($server, $db, $table)
50 $this->_server
= $server;
52 $this->_table
= $table;
56 * Prints the menu and the breadcrumbs
60 public function display()
62 echo $this->getDisplay();
66 * Returns the menu and the breadcrumbs as a string
70 public function getDisplay()
72 $retval = $this->_getBreadcrumbs();
73 $retval .= $this->_getMenu();
78 * Returns hash for the menu and the breadcrumbs
82 public function getHash()
85 md5($this->_getMenu() . $this->_getBreadcrumbs()),
92 * Returns the menu as HTML
94 * @return string HTML formatted menubar
96 private function _getMenu()
98 $url_params = array('db' => $this->_db
);
100 if (strlen($this->_table
) > 0) {
101 $tabs = $this->_getTableTabs();
102 $url_params['table'] = $this->_table
;
104 } else if (strlen($this->_db
) > 0) {
105 $tabs = $this->_getDbTabs();
108 $tabs = $this->_getServerTabs();
112 $allowedTabs = $this->_getAllowedTabs($level);
113 foreach ($tabs as $key => $value) {
114 if (! array_key_exists($key, $allowedTabs)) {
118 return Util
::getHtmlTabs($tabs, $url_params, 'topmenu', true);
122 * Returns a list of allowed tabs for the current user for the given level
124 * @param string $level 'server', 'db' or 'table' level
126 * @return array list of allowed tabs
128 private function _getAllowedTabs($level)
130 $cache_key = 'menu-levels-' . $level;
131 if (Util
::cacheExists($cache_key)) {
132 return Util
::cacheGet($cache_key);
134 $allowedTabs = Util
::getMenuTabList($level);
135 $cfgRelation = PMA_getRelationsParam();
136 if ($cfgRelation['menuswork']) {
137 $groupTable = Util
::backquote($cfgRelation['db'])
139 . Util
::backquote($cfgRelation['usergroups']);
140 $userTable = Util
::backquote($cfgRelation['db'])
141 . "." . Util
::backquote($cfgRelation['users']);
143 $sql_query = "SELECT `tab` FROM " . $groupTable
144 . " WHERE `allowed` = 'N'"
145 . " AND `tab` LIKE '" . $level . "%'"
146 . " AND `usergroup` = (SELECT usergroup FROM "
147 . $userTable . " WHERE `username` = '"
148 . $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['user']) . "')";
150 $result = PMA_queryAsControlUser($sql_query, false);
152 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
153 $tabName = mb_substr(
155 mb_strpos($row['tab'], '_') +
1
157 unset($allowedTabs[$tabName]);
161 Util
::cacheSet($cache_key, $allowedTabs);
166 * Returns the breadcrumbs as HTML
168 * @return string HTML formatted breadcrumbs
170 private function _getBreadcrumbs()
173 $tbl_is_view = $GLOBALS['dbi']->getTable($this->_db
, $this->_table
)
175 if (empty($GLOBALS['cfg']['Server']['host'])) {
176 $GLOBALS['cfg']['Server']['host'] = '';
178 $server_info = ! empty($GLOBALS['cfg']['Server']['verbose'])
179 ?
$GLOBALS['cfg']['Server']['verbose']
180 : $GLOBALS['cfg']['Server']['host'];
181 $server_info .= empty($GLOBALS['cfg']['Server']['port'])
183 : ':' . $GLOBALS['cfg']['Server']['port'];
185 $separator = "<span class='separator item'> ยป</span>";
186 $item = '<a href="%1$s%2$s" class="item">';
188 if (Util
::showText('TabsMode')) {
192 $retval .= "<div id='floating_menubar'></div>";
193 $retval .= "<div id='serverinfo'>";
194 if (Util
::showIcons('TabsMode')) {
195 $retval .= Util
::getImage(
198 array('class' => 'item')
203 Util
::getScriptNameForOption(
204 $GLOBALS['cfg']['DefaultTabServer'], 'server'
207 htmlspecialchars($server_info),
211 if (strlen($this->_db
) > 0) {
212 $retval .= $separator;
213 if (Util
::showIcons('TabsMode')) {
214 $retval .= Util
::getImage(
217 array('class' => 'item')
222 Util
::getScriptNameForOption(
223 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
225 URL
::getCommon(array('db' => $this->_db
)),
226 htmlspecialchars($this->_db
),
229 // if the table is being dropped, $_REQUEST['purge'] is set to '1'
230 // so do not display the table name in upper div
231 if (strlen($this->_table
) > 0
232 && ! (isset($_REQUEST['purge']) && $_REQUEST['purge'] == '1')
234 include './libraries/tbl_info.inc.php';
236 $retval .= $separator;
237 if (Util
::showIcons('TabsMode')) {
238 $icon = $tbl_is_view ?
'b_views.png' : 's_tbl.png';
239 $retval .= Util
::getImage(
242 array('class' => 'item')
247 Util
::getScriptNameForOption(
248 $GLOBALS['cfg']['DefaultTabTable'], 'table'
252 'db' => $this->_db
, 'table' => $this->_table
255 str_replace(' ', ' ', htmlspecialchars($this->_table
)),
256 $tbl_is_view ?
__('View') : __('Table')
260 * Displays table comment
262 if (! empty($show_comment)
263 && ! isset($GLOBALS['avoid_show_comment'])
265 if (mb_strstr($show_comment, '; InnoDB free')) {
266 $show_comment = preg_replace(
267 '@; InnoDB free:.*?$@',
272 $retval .= '<span class="table_comment"';
273 $retval .= ' id="span_table_comment">"';
274 $retval .= htmlspecialchars($show_comment);
275 $retval .= '"</span>';
278 // no table selected, display database comment if present
279 $cfgRelation = PMA_getRelationsParam();
281 // Get additional information about tables for tooltip is done
282 // in Util::getDbInfo() only once
283 if ($cfgRelation['commwork']) {
284 $comment = PMA_getDbComment($this->_db
);
286 * Displays table comment
288 if (! empty($comment)) {
289 $retval .= '<span class="table_comment"'
290 . ' id="span_table_comment">"'
291 . htmlspecialchars($comment)
297 $retval .= '<div class="clearfloat"></div>';
303 * Returns the table tabs as an array
305 * @return array Data for generating table tabs
307 private function _getTableTabs()
309 $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($this->_db
);
310 $tbl_is_view = $GLOBALS['dbi']->getTable($this->_db
, $this->_table
)
312 $updatable_view = false;
314 $updatable_view = $GLOBALS['dbi']->getTable($this->_db
, $this->_table
)
317 $is_superuser = $GLOBALS['dbi']->isSuperuser();
318 $isCreateOrGrantUser = $GLOBALS['dbi']->isUserType('grant')
319 ||
$GLOBALS['dbi']->isUserType('create');
323 $tabs['browse']['icon'] = 'b_browse.png';
324 $tabs['browse']['text'] = __('Browse');
325 $tabs['browse']['link'] = 'sql.php';
326 $tabs['browse']['args']['pos'] = 0;
328 $tabs['structure']['icon'] = 'b_props.png';
329 $tabs['structure']['link'] = 'tbl_structure.php';
330 $tabs['structure']['text'] = __('Structure');
331 $tabs['structure']['active'] = in_array(
332 basename($GLOBALS['PMA_PHP_SELF']),
333 array('tbl_structure.php', 'tbl_relation.php')
336 $tabs['sql']['icon'] = 'b_sql.png';
337 $tabs['sql']['link'] = 'tbl_sql.php';
338 $tabs['sql']['text'] = __('SQL');
340 $tabs['search']['icon'] = 'b_search.png';
341 $tabs['search']['text'] = __('Search');
342 $tabs['search']['link'] = 'tbl_select.php';
343 $tabs['search']['active'] = in_array(
344 basename($GLOBALS['PMA_PHP_SELF']),
345 array('tbl_select.php', 'tbl_zoom_select.php', 'tbl_find_replace.php')
348 if (! $db_is_system_schema && (! $tbl_is_view ||
$updatable_view)) {
349 $tabs['insert']['icon'] = 'b_insrow.png';
350 $tabs['insert']['link'] = 'tbl_change.php';
351 $tabs['insert']['text'] = __('Insert');
354 $tabs['export']['icon'] = 'b_tblexport.png';
355 $tabs['export']['link'] = 'tbl_export.php';
356 $tabs['export']['args']['single_table'] = 'true';
357 $tabs['export']['text'] = __('Export');
360 * Don't display "Import" for views and information_schema
362 if (! $tbl_is_view && ! $db_is_system_schema) {
363 $tabs['import']['icon'] = 'b_tblimport.png';
364 $tabs['import']['link'] = 'tbl_import.php';
365 $tabs['import']['text'] = __('Import');
367 if (($is_superuser ||
$isCreateOrGrantUser)
368 && ! $db_is_system_schema
370 $tabs['privileges']['link'] = 'server_privileges.php';
371 $tabs['privileges']['args']['checkprivsdb'] = $this->_db
;
372 $tabs['privileges']['args']['checkprivstable'] = $this->_table
;
373 // stay on table view
374 $tabs['privileges']['args']['viewing_mode'] = 'table';
375 $tabs['privileges']['text'] = __('Privileges');
376 $tabs['privileges']['icon'] = 's_rights.png';
379 * Don't display "Operations" for views and information_schema
381 if (! $tbl_is_view && ! $db_is_system_schema) {
382 $tabs['operation']['icon'] = 'b_tblops.png';
383 $tabs['operation']['link'] = 'tbl_operations.php';
384 $tabs['operation']['text'] = __('Operations');
387 * Views support a limited number of operations
389 if ($tbl_is_view && ! $db_is_system_schema) {
390 $tabs['operation']['icon'] = 'b_tblops.png';
391 $tabs['operation']['link'] = 'view_operations.php';
392 $tabs['operation']['text'] = __('Operations');
395 if (Tracker
::isActive() && ! $db_is_system_schema) {
396 $tabs['tracking']['icon'] = 'eye.png';
397 $tabs['tracking']['text'] = __('Tracking');
398 $tabs['tracking']['link'] = 'tbl_tracking.php';
400 if (! $db_is_system_schema
401 && Util
::currentUserHasPrivilege(
408 $tabs['triggers']['link'] = 'tbl_triggers.php';
409 $tabs['triggers']['text'] = __('Triggers');
410 $tabs['triggers']['icon'] = 'b_triggers.png';
417 * Returns the db tabs as an array
419 * @return array Data for generating db tabs
421 private function _getDbTabs()
423 $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($this->_db
);
424 $num_tables = count($GLOBALS['dbi']->getTables($this->_db
));
425 $is_superuser = $GLOBALS['dbi']->isSuperuser();
426 $isCreateOrGrantUser = $GLOBALS['dbi']->isUserType('grant')
427 ||
$GLOBALS['dbi']->isUserType('create');
430 * Gets the relation settings
432 $cfgRelation = PMA_getRelationsParam();
436 $tabs['structure']['link'] = 'db_structure.php';
437 $tabs['structure']['text'] = __('Structure');
438 $tabs['structure']['icon'] = 'b_props.png';
440 $tabs['sql']['link'] = 'db_sql.php';
441 $tabs['sql']['text'] = __('SQL');
442 $tabs['sql']['icon'] = 'b_sql.png';
444 $tabs['search']['text'] = __('Search');
445 $tabs['search']['icon'] = 'b_search.png';
446 $tabs['search']['link'] = 'db_search.php';
447 if ($num_tables == 0) {
448 $tabs['search']['warning'] = __('Database seems to be empty!');
451 $tabs['qbe']['text'] = __('Query');
452 $tabs['qbe']['icon'] = 's_db.png';
453 $tabs['qbe']['link'] = 'db_qbe.php';
454 if ($num_tables == 0) {
455 $tabs['qbe']['warning'] = __('Database seems to be empty!');
458 $tabs['export']['text'] = __('Export');
459 $tabs['export']['icon'] = 'b_export.png';
460 $tabs['export']['link'] = 'db_export.php';
461 if ($num_tables == 0) {
462 $tabs['export']['warning'] = __('Database seems to be empty!');
465 if (! $db_is_system_schema) {
466 $tabs['import']['link'] = 'db_import.php';
467 $tabs['import']['text'] = __('Import');
468 $tabs['import']['icon'] = 'b_import.png';
470 $tabs['operation']['link'] = 'db_operations.php';
471 $tabs['operation']['text'] = __('Operations');
472 $tabs['operation']['icon'] = 'b_tblops.png';
474 if (($is_superuser ||
$isCreateOrGrantUser)) {
475 $tabs['privileges']['link'] = 'server_privileges.php';
476 $tabs['privileges']['args']['checkprivsdb'] = $this->_db
;
477 // stay on database view
478 $tabs['privileges']['args']['viewing_mode'] = 'db';
479 $tabs['privileges']['text'] = __('Privileges');
480 $tabs['privileges']['icon'] = 's_rights.png';
483 $tabs['routines']['link'] = 'db_routines.php';
484 $tabs['routines']['text'] = __('Routines');
485 $tabs['routines']['icon'] = 'b_routines.png';
487 if (Util
::currentUserHasPrivilege('EVENT', $this->_db
)) {
488 $tabs['events']['link'] = 'db_events.php';
489 $tabs['events']['text'] = __('Events');
490 $tabs['events']['icon'] = 'b_events.png';
493 if (Util
::currentUserHasPrivilege('TRIGGER', $this->_db
)) {
494 $tabs['triggers']['link'] = 'db_triggers.php';
495 $tabs['triggers']['text'] = __('Triggers');
496 $tabs['triggers']['icon'] = 'b_triggers.png';
500 if (Tracker
::isActive() && ! $db_is_system_schema) {
501 $tabs['tracking']['text'] = __('Tracking');
502 $tabs['tracking']['icon'] = 'eye.png';
503 $tabs['tracking']['link'] = 'db_tracking.php';
506 if (! $db_is_system_schema) {
507 $tabs['designer']['text'] = __('Designer');
508 $tabs['designer']['icon'] = 'b_relations.png';
509 $tabs['designer']['link'] = 'db_designer.php';
510 $tabs['designer']['id'] = 'designer_tab';
513 if (! $db_is_system_schema
514 && $cfgRelation['centralcolumnswork']
516 $tabs['central_columns']['text'] = __('Central columns');
517 $tabs['central_columns']['icon'] = 'centralColumns.png';
518 $tabs['central_columns']['link'] = 'db_central_columns.php';
524 * Returns the server tabs as an array
526 * @return array Data for generating server tabs
528 private function _getServerTabs()
530 $is_superuser = $GLOBALS['dbi']->isSuperuser();
531 $isCreateOrGrantUser = $GLOBALS['dbi']->isUserType('grant')
532 ||
$GLOBALS['dbi']->isUserType('create');
533 if (Util
::cacheExists('binary_logs')) {
534 $binary_logs = Util
::cacheGet('binary_logs');
536 $binary_logs = $GLOBALS['dbi']->fetchResult(
541 DatabaseInterface
::QUERY_STORE
543 Util
::cacheSet('binary_logs', $binary_logs);
548 $tabs['databases']['icon'] = 's_db.png';
549 $tabs['databases']['link'] = 'server_databases.php';
550 $tabs['databases']['text'] = __('Databases');
552 $tabs['sql']['icon'] = 'b_sql.png';
553 $tabs['sql']['link'] = 'server_sql.php';
554 $tabs['sql']['text'] = __('SQL');
556 $tabs['status']['icon'] = 's_status.png';
557 $tabs['status']['link'] = 'server_status.php';
558 $tabs['status']['text'] = __('Status');
559 $tabs['status']['active'] = in_array(
560 basename($GLOBALS['PMA_PHP_SELF']),
563 'server_status_advisor.php',
564 'server_status_monitor.php',
565 'server_status_queries.php',
566 'server_status_variables.php',
567 'server_status_processes.php'
571 if ($is_superuser ||
$isCreateOrGrantUser) {
572 $tabs['rights']['icon'] = 's_rights.png';
573 $tabs['rights']['link'] = 'server_privileges.php';
574 $tabs['rights']['text'] = __('User accounts');
575 $tabs['rights']['active'] = in_array(
576 basename($GLOBALS['PMA_PHP_SELF']),
577 array('server_privileges.php', 'server_user_groups.php')
579 $tabs['rights']['args']['viewing_mode'] = 'server';
582 $tabs['export']['icon'] = 'b_export.png';
583 $tabs['export']['link'] = 'server_export.php';
584 $tabs['export']['text'] = __('Export');
586 $tabs['import']['icon'] = 'b_import.png';
587 $tabs['import']['link'] = 'server_import.php';
588 $tabs['import']['text'] = __('Import');
590 $tabs['settings']['icon'] = 'b_tblops.png';
591 $tabs['settings']['link'] = 'prefs_manage.php';
592 $tabs['settings']['text'] = __('Settings');
593 $tabs['settings']['active'] = in_array(
594 basename($GLOBALS['PMA_PHP_SELF']),
595 array('prefs_forms.php', 'prefs_manage.php')
598 if (! empty($binary_logs)) {
599 $tabs['binlog']['icon'] = 's_tbl.png';
600 $tabs['binlog']['link'] = 'server_binlog.php';
601 $tabs['binlog']['text'] = __('Binary log');
605 $tabs['replication']['icon'] = 's_replication.png';
606 $tabs['replication']['link'] = 'server_replication.php';
607 $tabs['replication']['text'] = __('Replication');
610 $tabs['vars']['icon'] = 's_vars.png';
611 $tabs['vars']['link'] = 'server_variables.php';
612 $tabs['vars']['text'] = __('Variables');
614 $tabs['charset']['icon'] = 's_asci.png';
615 $tabs['charset']['link'] = 'server_collations.php';
616 $tabs['charset']['text'] = __('Charsets');
618 $tabs['engine']['icon'] = 'b_engine.png';
619 $tabs['engine']['link'] = 'server_engines.php';
620 $tabs['engine']['text'] = __('Engines');
622 $tabs['plugins']['icon'] = 'b_plugin.png';
623 $tabs['plugins']['link'] = 'server_plugins.php';
624 $tabs['plugins']['text'] = __('Plugins');
632 * @param string $table Current table
636 public function setTable($table)
638 $this->_table
= $table;