Menu.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Generates and renders the top menu
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Class for generating the top menu
  13. *
  14. * @package PhpMyAdmin
  15. */
  16. class PMA_Menu
  17. {
  18. /**
  19. * Server id
  20. *
  21. * @access private
  22. * @var string
  23. */
  24. private $_server;
  25. /**
  26. * Database name
  27. *
  28. * @access private
  29. * @var string
  30. */
  31. private $_db;
  32. /**
  33. * Table name
  34. *
  35. * @access private
  36. * @var string
  37. */
  38. private $_table;
  39. /**
  40. * Creates a new instance of PMA_Menu
  41. *
  42. * @param int $server Server id
  43. * @param string $db Database name
  44. * @param string $table Table name
  45. */
  46. public function __construct($server, $db, $table)
  47. {
  48. $this->_server = $server;
  49. $this->_db = $db;
  50. $this->_table = $table;
  51. }
  52. /**
  53. * Prints the menu and the breadcrumbs
  54. *
  55. * @return void
  56. */
  57. public function display()
  58. {
  59. echo $this->getDisplay();
  60. }
  61. /**
  62. * Returns the menu and the breadcrumbs as a string
  63. *
  64. * @return string
  65. */
  66. public function getDisplay()
  67. {
  68. $retval = $this->_getBreadcrumbs();
  69. $retval .= $this->_getMenu();
  70. return $retval;
  71. }
  72. /**
  73. * Returns hash for the menu and the breadcrumbs
  74. *
  75. * @return string
  76. */
  77. public function getHash()
  78. {
  79. return substr(
  80. md5($this->_getMenu() . $this->_getBreadcrumbs()),
  81. 0,
  82. 8
  83. );
  84. }
  85. /**
  86. * Returns the menu as HTML
  87. *
  88. * @return string HTML formatted menubar
  89. */
  90. private function _getMenu()
  91. {
  92. $tabs = array();
  93. $url_params = array('db' => $this->_db);
  94. $level = '';
  95. if (strlen($this->_table)) {
  96. $tabs = $this->_getTableTabs();
  97. $url_params['table'] = $this->_table;
  98. $level = 'table';
  99. } else if (strlen($this->_db)) {
  100. $tabs = $this->_getDbTabs();
  101. $level = 'db';
  102. } else {
  103. $tabs = $this->_getServerTabs();
  104. $level = 'server';
  105. }
  106. $allowedTabs = $this->_getAllowedTabs($level);
  107. foreach ($tabs as $key => $value) {
  108. if (! array_key_exists($key, $allowedTabs)) {
  109. unset($tabs[$key]);
  110. }
  111. }
  112. return PMA_Util::getHtmlTabs($tabs, $url_params, 'topmenu', true);
  113. }
  114. /**
  115. * Returns a list of allowed tabs for the current user for the given level
  116. *
  117. * @param string $level 'server', 'db' or 'table' level
  118. *
  119. * @return array list of allowed tabs
  120. */
  121. private function _getAllowedTabs($level)
  122. {
  123. $allowedTabs = PMA_Util::getMenuTabList($level);
  124. $cfgRelation = PMA_getRelationsParam();
  125. if ($cfgRelation['menuswork']) {
  126. $groupTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  127. . "."
  128. . PMA_Util::backquote($GLOBALS['cfg']['Server']['usergroups']);
  129. $userTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  130. . "." . PMA_Util::backquote($GLOBALS['cfg']['Server']['users']);
  131. $sql_query = "SELECT `tab` FROM " . $groupTable
  132. . " WHERE `allowed` = 'N'"
  133. . " AND `tab` LIKE '" . $level . "%'"
  134. . " AND `usergroup` = (SELECT usergroup FROM "
  135. . $userTable . " WHERE `username` = '"
  136. . PMA_Util::sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . "')";
  137. $result = PMA_queryAsControlUser($sql_query, false);
  138. if ($result) {
  139. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  140. $tabName = substr($row['tab'], strpos($row['tab'], '_') + 1);
  141. unset($allowedTabs[$tabName]);
  142. }
  143. }
  144. }
  145. return $allowedTabs;
  146. }
  147. /**
  148. * Returns the breadcrumbs as HTML
  149. *
  150. * @return string HTML formatted breadcrumbs
  151. */
  152. private function _getBreadcrumbs()
  153. {
  154. $retval = '';
  155. $tbl_is_view = PMA_Table::isView($this->_db, $this->_table);
  156. $server_info = ! empty($GLOBALS['cfg']['Server']['verbose'])
  157. ? $GLOBALS['cfg']['Server']['verbose']
  158. : $GLOBALS['cfg']['Server']['host'];
  159. $server_info .= empty($GLOBALS['cfg']['Server']['port'])
  160. ? ''
  161. : ':' . $GLOBALS['cfg']['Server']['port'];
  162. $separator = "<span class='separator item'>&nbsp;»</span>";
  163. $item = '<a href="%1$s?%2$s" class="item">';
  164. if (PMA_Util::showText('TabsMode')) {
  165. $item .= '%4$s: ';
  166. }
  167. $item .= '%3$s</a>';
  168. $retval .= "<div id='floating_menubar'></div>";
  169. $retval .= "<div id='serverinfo'>";
  170. if (PMA_Util::showIcons('TabsMode')) {
  171. $retval .= PMA_Util::getImage(
  172. 's_host.png',
  173. '',
  174. array('class' => 'item')
  175. );
  176. }
  177. $retval .= sprintf(
  178. $item,
  179. $GLOBALS['cfg']['DefaultTabServer'],
  180. PMA_URL_getCommon(),
  181. htmlspecialchars($server_info),
  182. __('Server')
  183. );
  184. if (strlen($this->_db)) {
  185. $retval .= $separator;
  186. if (PMA_Util::showIcons('TabsMode')) {
  187. $retval .= PMA_Util::getImage(
  188. 's_db.png',
  189. '',
  190. array('class' => 'item')
  191. );
  192. }
  193. $retval .= sprintf(
  194. $item,
  195. $GLOBALS['cfg']['DefaultTabDatabase'],
  196. PMA_URL_getCommon($this->_db),
  197. htmlspecialchars($this->_db),
  198. __('Database')
  199. );
  200. // if the table is being dropped, $_REQUEST['purge'] is set to '1'
  201. // so do not display the table name in upper div
  202. if (strlen($this->_table)
  203. && ! (isset($_REQUEST['purge']) && $_REQUEST['purge'] == '1')
  204. ) {
  205. include './libraries/tbl_info.inc.php';
  206. $retval .= $separator;
  207. if (PMA_Util::showIcons('TabsMode')) {
  208. $icon = $tbl_is_view ? 'b_views.png' : 's_tbl.png';
  209. $retval .= PMA_Util::getImage(
  210. $icon,
  211. '',
  212. array('class' => 'item')
  213. );
  214. }
  215. $retval .= sprintf(
  216. $item,
  217. $GLOBALS['cfg']['DefaultTabTable'],
  218. PMA_URL_getCommon($this->_db, $this->_table),
  219. str_replace(' ', '&nbsp;', htmlspecialchars($this->_table)),
  220. $tbl_is_view ? __('View') : __('Table')
  221. );
  222. /**
  223. * Displays table comment
  224. */
  225. if (! empty($show_comment)
  226. && ! isset($GLOBALS['avoid_show_comment'])
  227. ) {
  228. if (strstr($show_comment, '; InnoDB free')) {
  229. $show_comment = preg_replace(
  230. '@; InnoDB free:.*?$@',
  231. '',
  232. $show_comment
  233. );
  234. }
  235. $retval .= '<span class="table_comment"';
  236. $retval .= ' id="span_table_comment">&quot;';
  237. $retval .= htmlspecialchars($show_comment);
  238. $retval .= '&quot;</span>';
  239. } // end if
  240. } else {
  241. // no table selected, display database comment if present
  242. $cfgRelation = PMA_getRelationsParam();
  243. // Get additional information about tables for tooltip is done
  244. // in libraries/db_info.inc.php only once
  245. if ($cfgRelation['commwork']) {
  246. $comment = PMA_getDbComment($this->_db);
  247. /**
  248. * Displays table comment
  249. */
  250. if (! empty($comment)) {
  251. $retval .= '<span class="table_comment"'
  252. . ' id="span_table_comment">&quot;'
  253. . htmlspecialchars($comment)
  254. . '&quot;</span>';
  255. } // end if
  256. }
  257. }
  258. }
  259. $retval .= '<div class="clearfloat"></div>';
  260. $retval .= '</div>';
  261. return $retval;
  262. }
  263. /**
  264. * Returns the table tabs as an array
  265. *
  266. * @return array Data for generating table tabs
  267. */
  268. private function _getTableTabs()
  269. {
  270. $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($this->_db);
  271. $tbl_is_view = PMA_Table::isView($this->_db, $this->_table);
  272. $is_superuser = $GLOBALS['dbi']->isSuperuser();
  273. $tabs = array();
  274. $tabs['browse']['icon'] = 'b_browse.png';
  275. $tabs['browse']['text'] = __('Browse');
  276. $tabs['browse']['link'] = 'sql.php';
  277. $tabs['browse']['args']['pos'] = 0;
  278. $tabs['structure']['icon'] = 'b_props.png';
  279. $tabs['structure']['link'] = 'tbl_structure.php';
  280. $tabs['structure']['text'] = __('Structure');
  281. $tabs['sql']['icon'] = 'b_sql.png';
  282. $tabs['sql']['link'] = 'tbl_sql.php';
  283. $tabs['sql']['text'] = __('SQL');
  284. $tabs['search']['icon'] = 'b_search.png';
  285. $tabs['search']['text'] = __('Search');
  286. $tabs['search']['link'] = 'tbl_select.php';
  287. $tabs['search']['active'] = in_array(
  288. basename($GLOBALS['PMA_PHP_SELF']),
  289. array('tbl_select.php', 'tbl_zoom_select.php', 'tbl_find_replace.php')
  290. );
  291. if (! $db_is_system_schema) {
  292. $tabs['insert']['icon'] = 'b_insrow.png';
  293. $tabs['insert']['link'] = 'tbl_change.php';
  294. $tabs['insert']['text'] = __('Insert');
  295. }
  296. $tabs['export']['icon'] = 'b_tblexport.png';
  297. $tabs['export']['link'] = 'tbl_export.php';
  298. $tabs['export']['args']['single_table'] = 'true';
  299. $tabs['export']['text'] = __('Export');
  300. /**
  301. * Don't display "Import" for views and information_schema
  302. */
  303. if (! $tbl_is_view && ! $db_is_system_schema) {
  304. $tabs['import']['icon'] = 'b_tblimport.png';
  305. $tabs['import']['link'] = 'tbl_import.php';
  306. $tabs['import']['text'] = __('Import');
  307. }
  308. if ($is_superuser && ! PMA_DRIZZLE && ! $db_is_system_schema) {
  309. $tabs['privileges']['link'] = 'server_privileges.php';
  310. $tabs['privileges']['args']['checkprivsdb'] = $this->_db;
  311. $tabs['privileges']['args']['checkprivstable'] = $this->_table;
  312. // stay on table view
  313. $tabs['privileges']['args']['viewing_mode'] = 'table';
  314. $tabs['privileges']['text'] = __('Privileges');
  315. $tabs['privileges']['icon'] = 's_rights.png';
  316. }
  317. /**
  318. * Don't display "Operations" for views and information_schema
  319. */
  320. if (! $tbl_is_view && ! $db_is_system_schema) {
  321. $tabs['operation']['icon'] = 'b_tblops.png';
  322. $tabs['operation']['link'] = 'tbl_operations.php';
  323. $tabs['operation']['text'] = __('Operations');
  324. }
  325. if (PMA_Tracker::isActive()) {
  326. $tabs['tracking']['icon'] = 'eye.png';
  327. $tabs['tracking']['text'] = __('Tracking');
  328. $tabs['tracking']['link'] = 'tbl_tracking.php';
  329. }
  330. if (! $db_is_system_schema
  331. && ! PMA_DRIZZLE
  332. && PMA_Util::currentUserHasPrivilege(
  333. 'TRIGGER',
  334. $this->_db,
  335. $this->_table
  336. )
  337. && ! $tbl_is_view
  338. ) {
  339. $tabs['triggers']['link'] = 'tbl_triggers.php';
  340. $tabs['triggers']['text'] = __('Triggers');
  341. $tabs['triggers']['icon'] = 'b_triggers.png';
  342. }
  343. /**
  344. * Views support a limited number of operations
  345. */
  346. if ($tbl_is_view && ! $db_is_system_schema) {
  347. $tabs['operation']['icon'] = 'b_tblops.png';
  348. $tabs['operation']['link'] = 'view_operations.php';
  349. $tabs['operation']['text'] = __('Operations');
  350. }
  351. return $tabs;
  352. }
  353. /**
  354. * Returns the db tabs as an array
  355. *
  356. * @return array Data for generating db tabs
  357. */
  358. private function _getDbTabs()
  359. {
  360. $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($this->_db);
  361. $num_tables = count($GLOBALS['dbi']->getTables($this->_db));
  362. $is_superuser = $GLOBALS['dbi']->isSuperuser();
  363. /**
  364. * Gets the relation settings
  365. */
  366. $cfgRelation = PMA_getRelationsParam();
  367. $tabs = array();
  368. $tabs['structure']['link'] = 'db_structure.php';
  369. $tabs['structure']['text'] = __('Structure');
  370. $tabs['structure']['icon'] = 'b_props.png';
  371. $tabs['sql']['link'] = 'db_sql.php';
  372. $tabs['sql']['text'] = __('SQL');
  373. $tabs['sql']['icon'] = 'b_sql.png';
  374. $tabs['search']['text'] = __('Search');
  375. $tabs['search']['icon'] = 'b_search.png';
  376. $tabs['search']['link'] = 'db_search.php';
  377. if ($num_tables == 0) {
  378. $tabs['search']['warning'] = __('Database seems to be empty!');
  379. }
  380. $tabs['qbe']['text'] = __('Query');
  381. $tabs['qbe']['icon'] = 's_db.png';
  382. $tabs['qbe']['link'] = 'db_qbe.php';
  383. if ($num_tables == 0) {
  384. $tabs['qbe']['warning'] = __('Database seems to be empty!');
  385. }
  386. $tabs['export']['text'] = __('Export');
  387. $tabs['export']['icon'] = 'b_export.png';
  388. $tabs['export']['link'] = 'db_export.php';
  389. if ($num_tables == 0) {
  390. $tabs['export']['warning'] = __('Database seems to be empty!');
  391. }
  392. if (! $db_is_system_schema) {
  393. $tabs['import']['link'] = 'db_import.php';
  394. $tabs['import']['text'] = __('Import');
  395. $tabs['import']['icon'] = 'b_import.png';
  396. $tabs['operation']['link'] = 'db_operations.php';
  397. $tabs['operation']['text'] = __('Operations');
  398. $tabs['operation']['icon'] = 'b_tblops.png';
  399. if ($is_superuser && ! PMA_DRIZZLE) {
  400. $tabs['privileges']['link'] = 'server_privileges.php';
  401. $tabs['privileges']['args']['checkprivsdb'] = $this->_db;
  402. // stay on database view
  403. $tabs['privileges']['args']['viewing_mode'] = 'db';
  404. $tabs['privileges']['text'] = __('Privileges');
  405. $tabs['privileges']['icon'] = 's_rights.png';
  406. }
  407. if (! PMA_DRIZZLE) {
  408. $tabs['routines']['link'] = 'db_routines.php';
  409. $tabs['routines']['text'] = __('Routines');
  410. $tabs['routines']['icon'] = 'b_routines.png';
  411. }
  412. if (PMA_MYSQL_INT_VERSION >= 50106
  413. && ! PMA_DRIZZLE
  414. && PMA_Util::currentUserHasPrivilege('EVENT', $this->_db)
  415. ) {
  416. $tabs['events']['link'] = 'db_events.php';
  417. $tabs['events']['text'] = __('Events');
  418. $tabs['events']['icon'] = 'b_events.png';
  419. }
  420. if (! PMA_DRIZZLE
  421. && PMA_Util::currentUserHasPrivilege('TRIGGER', $this->_db)
  422. ) {
  423. $tabs['triggers']['link'] = 'db_triggers.php';
  424. $tabs['triggers']['text'] = __('Triggers');
  425. $tabs['triggers']['icon'] = 'b_triggers.png';
  426. }
  427. }
  428. if (PMA_Tracker::isActive()) {
  429. $tabs['tracking']['text'] = __('Tracking');
  430. $tabs['tracking']['icon'] = 'eye.png';
  431. $tabs['tracking']['link'] = 'db_tracking.php';
  432. }
  433. if (! $db_is_system_schema && $cfgRelation['designerwork']) {
  434. $tabs['designer']['text'] = __('Designer');
  435. $tabs['designer']['icon'] = 'b_relations.png';
  436. $tabs['designer']['link'] = 'pmd_general.php';
  437. }
  438. return $tabs;
  439. }
  440. /**
  441. * Returns the server tabs as an array
  442. *
  443. * @return array Data for generating server tabs
  444. */
  445. private function _getServerTabs()
  446. {
  447. $is_superuser = isset($GLOBALS['dbi']) && $GLOBALS['dbi']->isSuperuser();
  448. $binary_logs = null;
  449. $notDrizzle = ! defined('PMA_DRIZZLE')
  450. || (defined('PMA_DRIZZLE') && ! PMA_DRIZZLE);
  451. if (isset($GLOBALS['dbi']) && $notDrizzle) {
  452. $binary_logs = $GLOBALS['dbi']->fetchResult(
  453. 'SHOW MASTER LOGS',
  454. 'Log_name',
  455. null,
  456. null,
  457. PMA_DatabaseInterface::QUERY_STORE
  458. );
  459. }
  460. $tabs = array();
  461. $tabs['databases']['icon'] = 's_db.png';
  462. $tabs['databases']['link'] = 'server_databases.php';
  463. $tabs['databases']['text'] = __('Databases');
  464. $tabs['sql']['icon'] = 'b_sql.png';
  465. $tabs['sql']['link'] = 'server_sql.php';
  466. $tabs['sql']['text'] = __('SQL');
  467. $tabs['status']['icon'] = 's_status.png';
  468. $tabs['status']['link'] = 'server_status.php';
  469. $tabs['status']['text'] = __('Status');
  470. $tabs['status']['active'] = in_array(
  471. basename($GLOBALS['PMA_PHP_SELF']),
  472. array(
  473. 'server_status.php',
  474. 'server_status_advisor.php',
  475. 'server_status_monitor.php',
  476. 'server_status_queries.php',
  477. 'server_status_variables.php'
  478. )
  479. );
  480. if ($is_superuser && ! PMA_DRIZZLE) {
  481. $tabs['rights']['icon'] = 's_rights.png';
  482. $tabs['rights']['link'] = 'server_privileges.php';
  483. $tabs['rights']['text'] = __('Users');
  484. $tabs['rights']['active'] = in_array(
  485. basename($GLOBALS['PMA_PHP_SELF']),
  486. array('server_privileges.php', 'server_user_groups.php')
  487. );
  488. $tabs['rights']['args']['viewing_mode'] = 'server';
  489. }
  490. $tabs['export']['icon'] = 'b_export.png';
  491. $tabs['export']['link'] = 'server_export.php';
  492. $tabs['export']['text'] = __('Export');
  493. $tabs['import']['icon'] = 'b_import.png';
  494. $tabs['import']['link'] = 'server_import.php';
  495. $tabs['import']['text'] = __('Import');
  496. $tabs['settings']['icon'] = 'b_tblops.png';
  497. $tabs['settings']['link'] = 'prefs_manage.php';
  498. $tabs['settings']['text'] = __('Settings');
  499. $tabs['settings']['active'] = in_array(
  500. basename($GLOBALS['PMA_PHP_SELF']),
  501. array('prefs_forms.php', 'prefs_manage.php')
  502. );
  503. if (! empty($binary_logs)) {
  504. $tabs['binlog']['icon'] = 's_tbl.png';
  505. $tabs['binlog']['link'] = 'server_binlog.php';
  506. $tabs['binlog']['text'] = __('Binary log');
  507. }
  508. if ($is_superuser && ! PMA_DRIZZLE) {
  509. $tabs['replication']['icon'] = 's_replication.png';
  510. $tabs['replication']['link'] = 'server_replication.php';
  511. $tabs['replication']['text'] = __('Replication');
  512. }
  513. $tabs['vars']['icon'] = 's_vars.png';
  514. $tabs['vars']['link'] = 'server_variables.php';
  515. $tabs['vars']['text'] = __('Variables');
  516. $tabs['charset']['icon'] = 's_asci.png';
  517. $tabs['charset']['link'] = 'server_collations.php';
  518. $tabs['charset']['text'] = __('Charsets');
  519. if (defined('PMA_DRIZZLE') && PMA_DRIZZLE) {
  520. $tabs['plugins']['icon'] = 'b_engine.png';
  521. $tabs['plugins']['link'] = 'server_plugins.php';
  522. $tabs['plugins']['text'] = __('Plugins');
  523. } else {
  524. $tabs['engine']['icon'] = 'b_engine.png';
  525. $tabs['engine']['link'] = 'server_engines.php';
  526. $tabs['engine']['text'] = __('Engines');
  527. }
  528. return $tabs;
  529. }
  530. }
  531. ?>