server_databases.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * functions for displaying server databases
  5. *
  6. * @usedby server_databases.php
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. if (! defined('PHPMYADMIN')) {
  11. exit;
  12. }
  13. /**
  14. * Returns the html for Database List
  15. *
  16. * @param Array $databases GBI return databases
  17. * @param int $databases_count database count
  18. * @param int $pos display pos
  19. * @param Array $dbstats database status
  20. * @param string $sort_by sort by string
  21. * @param string $sort_order sort order string
  22. * @param bool $is_superuser User status
  23. * @param Array $cfg configuration
  24. * @param string $replication_types replication types
  25. * @param string $replication_info replication info
  26. * @param string $url_query url query
  27. *
  28. * @return string
  29. */
  30. function PMA_getHtmlForDatabase(
  31. $databases, $databases_count, $pos, $dbstats,
  32. $sort_by, $sort_order, $is_superuser, $cfg,
  33. $replication_types, $replication_info, $url_query
  34. ) {
  35. $html = '<div id="tableslistcontainer">';
  36. reset($databases);
  37. $first_database = current($databases);
  38. // table col order
  39. $column_order = PMA_getColumnOrder();
  40. $_url_params = array(
  41. 'pos' => $pos,
  42. 'dbstats' => $dbstats,
  43. 'sort_by' => $sort_by,
  44. 'sort_order' => $sort_order,
  45. );
  46. $html .= PMA_Util::getListNavigator(
  47. $databases_count, $pos, $_url_params, 'server_databases.php',
  48. 'frame_content', $GLOBALS['cfg']['MaxDbList']
  49. );
  50. $_url_params['pos'] = $pos;
  51. $html .= '<form class="ajax" action="server_databases.php" ';
  52. $html .= 'method="post" name="dbStatsForm" id="dbStatsForm">' . "\n";
  53. $html .= PMA_URL_getHiddenInputs($_url_params);
  54. $_url_params['sort_by'] = 'SCHEMA_NAME';
  55. $_url_params['sort_order']
  56. = ($sort_by == 'SCHEMA_NAME' && $sort_order == 'asc') ? 'desc' : 'asc';
  57. $html .= '<table id="tabledatabases" class="data">' . "\n"
  58. . '<thead>' . "\n"
  59. . '<tr>' . "\n";
  60. $html .= PMA_getHtmlForColumnOrderWithSort(
  61. $is_superuser,
  62. $cfg['AllowUserDropDatabase'],
  63. $_url_params,
  64. $sort_by,
  65. $sort_order,
  66. $column_order,
  67. $first_database
  68. );
  69. $html .= PMA_getHtmlForReplicationType(
  70. $is_superuser,
  71. $replication_types,
  72. $cfg['ActionLinksMode']
  73. );
  74. $html .= '</tr>' . "\n"
  75. . '</thead>' . "\n";
  76. list($output, $column_order) = PMA_getHtmlAndColumnOrderForDatabaseList(
  77. $databases,
  78. $is_superuser,
  79. $url_query,
  80. $column_order,
  81. $replication_types,
  82. $replication_info
  83. );
  84. $html .= $output;
  85. unset($output);
  86. $html .= PMA_getHtmlForTableFooter(
  87. $cfg['AllowUserDropDatabase'],
  88. $is_superuser,
  89. $databases_count,
  90. $column_order,
  91. $replication_types,
  92. $first_database
  93. );
  94. $html .= '</table>' . "\n";
  95. $html .= PMA_getHtmlForTableFooterButtons(
  96. $cfg['AllowUserDropDatabase'],
  97. $is_superuser
  98. );
  99. if (empty($dbstats)) {
  100. //we should put notice above database list
  101. $html = PMA_getHtmlForNoticeEnableStatistics($url_query, $html);
  102. }
  103. $html .= '</form>';
  104. $html .= '</div>';
  105. return $html;
  106. }
  107. /**
  108. * Returns the html for Table footer buttons
  109. *
  110. * @param bool $is_allowUserDropDb Allow user drop database
  111. * @param bool $is_superuser User status
  112. *
  113. * @return string
  114. */
  115. function PMA_getHtmlForTableFooterButtons($is_allowUserDropDb, $is_superuser)
  116. {
  117. if (!$is_superuser && !$is_allowUserDropDb) {
  118. return '';
  119. }
  120. $html = '<img class="selectallarrow" src="'
  121. . $GLOBALS['pmaThemeImage'] . 'arrow_' . $GLOBALS['text_dir'] . '.png"'
  122. . ' width="38" height="22" alt="' . __('With selected:') . '" />' . "\n"
  123. . '<input type="checkbox" id="dbStatsForm_checkall" '
  124. . 'class="checkall_box" title="' . __('Check All') . '" /> '
  125. . '<label for="dbStatsForm_checkall">' . __('Check All') . '</label> '
  126. . '<i style="margin-left: 2em">' . __('With selected:') . '</i>' . "\n";
  127. $html .= PMA_Util::getButtonOrImage(
  128. '',
  129. 'mult_submit' . ' ajax',
  130. 'drop_selected_dbs',
  131. __('Drop'), 'b_deltbl.png'
  132. );
  133. return $html;
  134. }
  135. /**
  136. * Returns the html for Table footer
  137. *
  138. * @param bool $is_allowUserDropDb Allow user drop database
  139. * @param bool $is_superuser User status
  140. * @param Array $databases_count Database count
  141. * @param string $column_order column order
  142. * @param array $replication_types replication types
  143. * @param string $first_database First database
  144. *
  145. * @return string
  146. */
  147. function PMA_getHtmlForTableFooter(
  148. $is_allowUserDropDb, $is_superuser,
  149. $databases_count, $column_order,
  150. $replication_types, $first_database
  151. ) {
  152. $html = '<tfoot><tr>' . "\n";
  153. if ($is_superuser || $is_allowUserDropDb) {
  154. $html .= ' <th></th>' . "\n";
  155. }
  156. $html .= ' <th>' . __('Total') . ': <span id="databases_count">'
  157. . $databases_count . '</span></th>' . "\n";
  158. $html .= PMA_getHtmlForColumnOrder($column_order, $first_database);
  159. foreach ($replication_types as $type) {
  160. if ($GLOBALS["server_" . $type . "_status"]) {
  161. $html .= ' <th></th>' . "\n";
  162. }
  163. }
  164. if ($is_superuser) {
  165. $html .= ' <th></th>' . "\n";
  166. }
  167. $html .= '</tr>' . "\n";
  168. $html .= '</tfoot>' . "\n";
  169. return $html;
  170. }
  171. /**
  172. * Returns the html for Database List and Column order
  173. *
  174. * @param array $databases GBI return databases
  175. * @param bool $is_superuser User status
  176. * @param Array $url_query Url query
  177. * @param string $column_order column order
  178. * @param string $replication_types replication types
  179. * @param string $replication_info replication info
  180. *
  181. * @return Array
  182. */
  183. function PMA_getHtmlAndColumnOrderForDatabaseList(
  184. $databases, $is_superuser, $url_query,
  185. $column_order, $replication_types, $replication_info
  186. ) {
  187. $odd_row = true;
  188. $html = '<tbody>' . "\n";
  189. foreach ($databases as $current) {
  190. $tr_class = $odd_row ? 'odd' : 'even';
  191. if ($GLOBALS['dbi']->isSystemSchema($current['SCHEMA_NAME'], true)) {
  192. $tr_class .= ' noclick';
  193. }
  194. $html .= '<tr class="' . $tr_class . '">' . "\n";
  195. $odd_row = ! $odd_row;
  196. list($column_order, $generated_html) = PMA_buildHtmlForDb(
  197. $current,
  198. $is_superuser,
  199. $url_query,
  200. $column_order,
  201. $replication_types,
  202. $replication_info
  203. );
  204. $html .= $generated_html;
  205. $html .= '</tr>' . "\n";
  206. } // end foreach ($databases as $key => $current)
  207. unset($current, $odd_row);
  208. $html .= '</tbody>';
  209. return array($html, $column_order);
  210. }
  211. /**
  212. * Returns the html for Column Order
  213. *
  214. * @param array $column_order Column order
  215. * @param array $first_database The first display database
  216. *
  217. * @return string
  218. */
  219. function PMA_getHtmlForColumnOrder($column_order, $first_database)
  220. {
  221. $html = "";
  222. foreach ($column_order as $stat_name => $stat) {
  223. if (array_key_exists($stat_name, $first_database)) {
  224. if ($stat['format'] === 'byte') {
  225. list($value, $unit)
  226. = PMA_Util::formatByteDown($stat['footer'], 3, 1);
  227. } elseif ($stat['format'] === 'number') {
  228. $value = PMA_Util::formatNumber($stat['footer'], 0);
  229. } else {
  230. $value = htmlentities($stat['footer'], 0);
  231. }
  232. $html .= ' <th class="value">';
  233. if (isset($stat['description_function'])) {
  234. $html .= '<dfn title="'
  235. . $stat['description_function']($stat['footer']) . '">';
  236. }
  237. $html .= $value;
  238. if (isset($stat['description_function'])) {
  239. $html .= '</dfn>';
  240. }
  241. $html .= '</th>' . "\n";
  242. if ($stat['format'] === 'byte') {
  243. $html .= ' <th class="unit">' . $unit . '</th>' . "\n";
  244. }
  245. }
  246. }
  247. return $html;
  248. }
  249. /**
  250. * Returns the html for Column Order with Sort
  251. *
  252. * @param bool $is_superuser User status
  253. * @param bool $is_allowUserDropDb Allow user drop database
  254. * @param Array $_url_params Url params
  255. * @param string $sort_by sort colume name
  256. * @param string $sort_order order
  257. * @param array $column_order column order
  258. * @param array $first_database database to show
  259. *
  260. * @return string
  261. */
  262. function PMA_getHtmlForColumnOrderWithSort(
  263. $is_superuser, $is_allowUserDropDb,
  264. $_url_params, $sort_by, $sort_order,
  265. $column_order, $first_database
  266. ) {
  267. $html = ($is_superuser || $is_allowUserDropDb
  268. ? ' <th></th>' . "\n"
  269. : '')
  270. . ' <th><a href="server_databases.php'
  271. . PMA_URL_getCommon($_url_params) . '">' . "\n"
  272. . ' ' . __('Database') . "\n"
  273. . ($sort_by == 'SCHEMA_NAME'
  274. ? ' ' . PMA_Util::getImage(
  275. 's_' . $sort_order . '.png',
  276. ($sort_order == 'asc' ? __('Ascending') : __('Descending'))
  277. ) . "\n"
  278. : ''
  279. )
  280. . ' </a></th>' . "\n";
  281. $table_columns = 3;
  282. foreach ($column_order as $stat_name => $stat) {
  283. if (!array_key_exists($stat_name, $first_database)) {
  284. continue;
  285. }
  286. if ($stat['format'] === 'byte') {
  287. $table_columns += 2;
  288. $colspan = ' colspan="2"';
  289. } else {
  290. $table_columns++;
  291. $colspan = '';
  292. }
  293. $_url_params['sort_by'] = $stat_name;
  294. $_url_params['sort_order']
  295. = ($sort_by == $stat_name && $sort_order == 'desc') ? 'asc' : 'desc';
  296. $html .= ' <th' . $colspan . '>'
  297. . '<a href="server_databases.php'
  298. . PMA_URL_getCommon($_url_params) . '">' . "\n"
  299. . ' ' . $stat['disp_name'] . "\n"
  300. . ($sort_by == $stat_name
  301. ? ' ' . PMA_Util::getImage(
  302. 's_' . $sort_order . '.png',
  303. ($sort_order == 'asc' ? __('Ascending') : __('Descending'))
  304. ) . "\n"
  305. : ''
  306. )
  307. . ' </a></th>' . "\n";
  308. }
  309. return $html;
  310. }
  311. /**
  312. * Returns the html for Enable Statistics
  313. *
  314. * @param bool $url_query Url query
  315. * @param string $html html for database list
  316. *
  317. * @return string
  318. */
  319. function PMA_getHtmlForNoticeEnableStatistics($url_query, $html)
  320. {
  321. $notice = PMA_Message::notice(
  322. __(
  323. 'Note: Enabling the database statistics here might cause '
  324. . 'heavy traffic between the web server and the MySQL server.'
  325. )
  326. )->getDisplay();
  327. //we should put notice above database list
  328. $html = $notice . $html;
  329. $html .= '<ul><li id="li_switch_dbstats"><strong>' . "\n";
  330. $html .= '<a href="server_databases.php?' . $url_query . '&amp;dbstats=1"'
  331. . ' title="' . __('Enable Statistics') . '">' . "\n"
  332. . ' ' . __('Enable Statistics');
  333. $html .= '</a></strong><br />' . "\n";
  334. $html .= '</li>' . "\n" . '</ul>' . "\n";
  335. return $html;
  336. }
  337. /**
  338. * Returns the html for database replication types
  339. *
  340. * @param bool $is_superuser User status
  341. * @param Array $replication_types replication types
  342. * @param bool $cfg_inconic cfg about Properties Iconic
  343. *
  344. * @return string
  345. */
  346. function PMA_getHtmlForReplicationType(
  347. $is_superuser, $replication_types, $cfg_inconic
  348. ) {
  349. $html = '';
  350. foreach ($replication_types as $type) {
  351. if ($type == "master") {
  352. $name = __('Master replication');
  353. } elseif ($type == "slave") {
  354. $name = __('Slave replication');
  355. }
  356. if ($GLOBALS["server_{$type}_status"]) {
  357. $html .= ' <th>' . $name . '</th>' . "\n";
  358. }
  359. }
  360. if ($is_superuser && ! PMA_DRIZZLE) {
  361. $html .= ' <th>' . ($cfg_inconic ? '' : __('Action')) . "\n"
  362. . ' </th>' . "\n";
  363. }
  364. return $html;
  365. }
  366. /**
  367. * Returns the array about $sort_order and $sort_by
  368. *
  369. * @return Array
  370. */
  371. function PMA_getListForSortDatabase()
  372. {
  373. /**
  374. * avoids 'undefined index' errors
  375. */
  376. $sort_by = '';
  377. $sort_order = '';
  378. if (empty($_REQUEST['sort_by'])) {
  379. $sort_by = 'SCHEMA_NAME';
  380. } else {
  381. $sort_by_whitelist = array(
  382. 'SCHEMA_NAME',
  383. 'DEFAULT_COLLATION_NAME',
  384. 'SCHEMA_TABLES',
  385. 'SCHEMA_TABLE_ROWS',
  386. 'SCHEMA_DATA_LENGTH',
  387. 'SCHEMA_INDEX_LENGTH',
  388. 'SCHEMA_LENGTH',
  389. 'SCHEMA_DATA_FREE'
  390. );
  391. if (in_array($_REQUEST['sort_by'], $sort_by_whitelist)) {
  392. $sort_by = $_REQUEST['sort_by'];
  393. } else {
  394. $sort_by = 'SCHEMA_NAME';
  395. }
  396. }
  397. if (isset($_REQUEST['sort_order'])
  398. && strtolower($_REQUEST['sort_order']) == 'desc'
  399. ) {
  400. $sort_order = 'desc';
  401. } else {
  402. $sort_order = 'asc';
  403. }
  404. return array($sort_by, $sort_order);
  405. }
  406. /**
  407. * Deal with Drops multiple databases
  408. *
  409. * @return null
  410. */
  411. function PMA_dropMultiDatabases()
  412. {
  413. if (! isset($_REQUEST['selected_dbs']) && ! isset($_REQUEST['query_type'])) {
  414. $message = PMA_Message::error(__('No databases selected.'));
  415. } else {
  416. $action = 'server_databases.php';
  417. $submit_mult = 'drop_db';
  418. $err_url = 'server_databases.php?' . PMA_URL_getCommon();
  419. if (isset($_REQUEST['selected_dbs'])
  420. && !isset($_REQUEST['is_js_confirmed'])
  421. ) {
  422. $selected_db = $_REQUEST['selected_dbs'];
  423. }
  424. if (isset($_REQUEST['is_js_confirmed'])) {
  425. $_REQUEST = array(
  426. 'query_type' => $submit_mult,
  427. 'selected' => $_REQUEST['selected_dbs'],
  428. 'mult_btn' => __('Yes'),
  429. 'db' => $GLOBALS['db'],
  430. 'table' => $GLOBALS['table']);
  431. }
  432. //the following variables will be used on mult_submits.inc.php
  433. global $query_type, $selected, $mult_btn;
  434. include 'libraries/mult_submits.inc.php';
  435. unset($action, $submit_mult, $err_url, $selected_db, $GLOBALS['db']);
  436. if (empty($message)) {
  437. if ($mult_btn == __('Yes')) {
  438. $number_of_databases = count($selected);
  439. } else {
  440. $number_of_databases = 0;
  441. }
  442. $message = PMA_Message::success(
  443. _ngettext(
  444. '%1$d database has been dropped successfully.',
  445. '%1$d databases have been dropped successfully.',
  446. $number_of_databases
  447. )
  448. );
  449. $message->addParam($number_of_databases);
  450. }
  451. }
  452. if ($GLOBALS['is_ajax_request'] && $message instanceof PMA_Message) {
  453. $response = PMA_Response::getInstance();
  454. $response->isSuccess($message->isSuccess());
  455. $response->addJSON('message', $message);
  456. exit;
  457. }
  458. }
  459. ?>