Translated using Weblate (Albanian)
[phpmyadmin.git] / libraries / Menu.php
blobc712e27f2414032259bbc7e985a51bf08df8ee8a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Generates and renders the top menu
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 use PMA\libraries\URL;
12 /**
13 * Class for generating the top menu
15 * @package PhpMyAdmin
17 class Menu
19 /**
20 * Server id
22 * @access private
23 * @var int
25 private $_server;
26 /**
27 * Database name
29 * @access private
30 * @var string
32 private $_db;
33 /**
34 * Table name
36 * @access private
37 * @var string
39 private $_table;
41 /**
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;
51 $this->_db = $db;
52 $this->_table = $table;
55 /**
56 * Prints the menu and the breadcrumbs
58 * @return void
60 public function display()
62 echo $this->getDisplay();
65 /**
66 * Returns the menu and the breadcrumbs as a string
68 * @return string
70 public function getDisplay()
72 $retval = $this->_getBreadcrumbs();
73 $retval .= $this->_getMenu();
74 return $retval;
77 /**
78 * Returns hash for the menu and the breadcrumbs
80 * @return string
82 public function getHash()
84 return substr(
85 md5($this->_getMenu() . $this->_getBreadcrumbs()),
91 /**
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;
103 $level = 'table';
104 } else if (strlen($this->_db) > 0) {
105 $tabs = $this->_getDbTabs();
106 $level = 'db';
107 } else {
108 $tabs = $this->_getServerTabs();
109 $level = 'server';
112 $allowedTabs = $this->_getAllowedTabs($level);
113 foreach ($tabs as $key => $value) {
114 if (! array_key_exists($key, $allowedTabs)) {
115 unset($tabs[$key]);
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'])
138 . "."
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);
151 if ($result) {
152 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
153 $tabName = mb_substr(
154 $row['tab'],
155 mb_strpos($row['tab'], '_') + 1
157 unset($allowedTabs[$tabName]);
161 Util::cacheSet($cache_key, $allowedTabs);
162 return $allowedTabs;
166 * Returns the breadcrumbs as HTML
168 * @return string HTML formatted breadcrumbs
170 private function _getBreadcrumbs()
172 $retval = '';
173 $tbl_is_view = $GLOBALS['dbi']->getTable($this->_db, $this->_table)
174 ->isView();
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'])
182 ? ''
183 : ':' . $GLOBALS['cfg']['Server']['port'];
185 $separator = "<span class='separator item'>&nbsp;ยป</span>";
186 $item = '<a href="%1$s%2$s" class="item">';
188 if (Util::showText('TabsMode')) {
189 $item .= '%4$s: ';
191 $item .= '%3$s</a>';
192 $retval .= "<div id='floating_menubar'></div>";
193 $retval .= "<div id='serverinfo'>";
194 if (Util::showIcons('TabsMode')) {
195 $retval .= Util::getImage(
196 's_host.png',
198 array('class' => 'item')
201 $retval .= sprintf(
202 $item,
203 Util::getScriptNameForOption(
204 $GLOBALS['cfg']['DefaultTabServer'], 'server'
206 URL::getCommon(),
207 htmlspecialchars($server_info),
208 __('Server')
211 if (strlen($this->_db) > 0) {
212 $retval .= $separator;
213 if (Util::showIcons('TabsMode')) {
214 $retval .= Util::getImage(
215 's_db.png',
217 array('class' => 'item')
220 $retval .= sprintf(
221 $item,
222 Util::getScriptNameForOption(
223 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
225 URL::getCommon(array('db' => $this->_db)),
226 htmlspecialchars($this->_db),
227 __('Database')
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(
240 $icon,
242 array('class' => 'item')
245 $retval .= sprintf(
246 $item,
247 Util::getScriptNameForOption(
248 $GLOBALS['cfg']['DefaultTabTable'], 'table'
250 URL::getCommon(
251 array(
252 'db' => $this->_db, 'table' => $this->_table
255 str_replace(' ', '&nbsp;', 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:.*?$@',
269 $show_comment
272 $retval .= '<span class="table_comment"';
273 $retval .= ' id="span_table_comment">&quot;';
274 $retval .= htmlspecialchars($show_comment);
275 $retval .= '&quot;</span>';
276 } // end if
277 } else {
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">&quot;'
291 . htmlspecialchars($comment)
292 . '&quot;</span>';
293 } // end if
297 $retval .= '<div class="clearfloat"></div>';
298 $retval .= '</div>';
299 return $retval;
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)
311 ->isView();
312 $updatable_view = false;
313 if ($tbl_is_view) {
314 $updatable_view = $GLOBALS['dbi']->getTable($this->_db, $this->_table)
315 ->isUpdatableView();
317 $is_superuser = $GLOBALS['dbi']->isSuperuser();
318 $isCreateOrGrantUser = $GLOBALS['dbi']->isUserType('grant')
319 || $GLOBALS['dbi']->isUserType('create');
321 $tabs = array();
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(
402 'TRIGGER',
403 $this->_db,
404 $this->_table
406 && ! $tbl_is_view
408 $tabs['triggers']['link'] = 'tbl_triggers.php';
409 $tabs['triggers']['text'] = __('Triggers');
410 $tabs['triggers']['icon'] = 'b_triggers.png';
413 return $tabs;
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();
434 $tabs = array();
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';
520 return $tabs;
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');
535 } else {
536 $binary_logs = $GLOBALS['dbi']->fetchResult(
537 'SHOW MASTER LOGS',
538 'Log_name',
539 null,
540 null,
541 DatabaseInterface::QUERY_STORE
543 Util::cacheSet('binary_logs', $binary_logs);
546 $tabs = array();
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']),
561 array(
562 'server_status.php',
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');
604 if ($is_superuser) {
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');
626 return $tabs;
630 * Set current table
632 * @param string $table Current table
634 * @return $this
636 public function setTable($table)
638 $this->_table = $table;
639 return $this;