ServerDatabasesController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Server\ServerDatabasesController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. namespace PhpMyAdmin\Controllers\Server;
  9. use PhpMyAdmin\Controllers\Controller;
  10. use PhpMyAdmin\Charsets;
  11. use PhpMyAdmin\DatabaseInterface;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Response;
  14. use PhpMyAdmin\Server\Common;
  15. use PhpMyAdmin\Template;
  16. use PhpMyAdmin\Url;
  17. use PhpMyAdmin\Util;
  18. /**
  19. * Handles viewing and creating and deleting databases
  20. *
  21. * @package PhpMyAdmin\Controllers
  22. */
  23. class ServerDatabasesController extends Controller
  24. {
  25. /**
  26. * @var array array of database details
  27. */
  28. private $_databases;
  29. /**
  30. * @var int number of databases
  31. */
  32. private $_database_count;
  33. /**
  34. * @var string sort by column
  35. */
  36. private $_sort_by;
  37. /**
  38. * @var string sort order of databases
  39. */
  40. private $_sort_order;
  41. /**
  42. * @var boolean whether to show database statistics
  43. */
  44. private $_dbstats;
  45. /**
  46. * @var int position in list navigation
  47. */
  48. private $_pos;
  49. /**
  50. * Index action
  51. *
  52. * @return void
  53. */
  54. public function indexAction()
  55. {
  56. include_once 'libraries/check_user_privileges.inc.php';
  57. $response = Response::getInstance();
  58. if (isset($_POST['drop_selected_dbs'])
  59. && $response->isAjax()
  60. && ($GLOBALS['dbi']->isSuperuser() || $GLOBALS['cfg']['AllowUserDropDatabase'])
  61. ) {
  62. $this->dropDatabasesAction();
  63. return;
  64. }
  65. include_once 'libraries/replication.inc.php';
  66. if (isset($_POST['new_db'])
  67. && $response->isAjax()
  68. ) {
  69. $this->createDatabaseAction();
  70. return;
  71. }
  72. include_once 'libraries/server_common.inc.php';
  73. $header = $this->response->getHeader();
  74. $scripts = $header->getScripts();
  75. $scripts->addFile('server_databases.js');
  76. $this->_setSortDetails();
  77. $this->_dbstats = ! empty($_POST['dbstats']);
  78. $this->_pos = empty($_REQUEST['pos']) ? 0 : (int) $_REQUEST['pos'];
  79. /**
  80. * Gets the databases list
  81. */
  82. if ($GLOBALS['server'] > 0) {
  83. $this->_databases = $this->dbi->getDatabasesFull(
  84. null, $this->_dbstats, DatabaseInterface::CONNECT_USER, $this->_sort_by,
  85. $this->_sort_order, $this->_pos, true
  86. );
  87. $this->_database_count = count($GLOBALS['dblist']->databases);
  88. } else {
  89. $this->_database_count = 0;
  90. }
  91. if ($this->_database_count > 0 && ! empty($this->_databases)) {
  92. $databases = $this->_getHtmlForDatabases($replication_types);
  93. }
  94. $this->response->addHTML(Template::get('server/databases/index')->render([
  95. 'show_create_db' => $GLOBALS['cfg']['ShowCreateDb'],
  96. 'is_create_db_priv' => $GLOBALS['is_create_db_priv'],
  97. 'dbstats' => $this->_dbstats,
  98. 'db_to_create' => $GLOBALS['db_to_create'],
  99. 'server_collation' => $GLOBALS['dbi']->getServerCollation(),
  100. 'databases' => isset($databases) ? $databases : null,
  101. 'dbi' => $GLOBALS['dbi'],
  102. 'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'],
  103. ]));
  104. }
  105. /**
  106. * Handles creating a new database
  107. *
  108. * @return void
  109. */
  110. public function createDatabaseAction()
  111. {
  112. // lower_case_table_names=1 `DB` becomes `db`
  113. if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
  114. $_POST['new_db'] = mb_strtolower(
  115. $_POST['new_db']
  116. );
  117. }
  118. /**
  119. * Builds and executes the db creation sql query
  120. */
  121. $sql_query = 'CREATE DATABASE ' . Util::backquote($_POST['new_db']);
  122. if (! empty($_POST['db_collation'])) {
  123. list($db_charset) = explode('_', $_POST['db_collation']);
  124. $charsets = Charsets::getMySQLCharsets(
  125. $GLOBALS['dbi'],
  126. $GLOBALS['cfg']['Server']['DisableIS']
  127. );
  128. $collations = Charsets::getMySQLCollations(
  129. $GLOBALS['dbi'],
  130. $GLOBALS['cfg']['Server']['DisableIS']
  131. );
  132. if (in_array($db_charset, $charsets)
  133. && in_array($_POST['db_collation'], $collations[$db_charset])
  134. ) {
  135. $sql_query .= ' DEFAULT'
  136. . Util::getCharsetQueryPart($_POST['db_collation']);
  137. }
  138. }
  139. $sql_query .= ';';
  140. $result = $GLOBALS['dbi']->tryQuery($sql_query);
  141. if (! $result) {
  142. // avoid displaying the not-created db name in header or navi panel
  143. $GLOBALS['db'] = '';
  144. $message = Message::rawError($GLOBALS['dbi']->getError());
  145. $this->response->setRequestStatus(false);
  146. $this->response->addJSON('message', $message);
  147. } else {
  148. $GLOBALS['db'] = $_POST['new_db'];
  149. $message = Message::success(__('Database %1$s has been created.'));
  150. $message->addParam($_POST['new_db']);
  151. $this->response->addJSON('message', $message);
  152. $this->response->addJSON(
  153. 'sql_query', Util::getMessage(null, $sql_query, 'success')
  154. );
  155. $this->response->addJSON(
  156. 'url_query',
  157. Util::getScriptNameForOption(
  158. $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
  159. )
  160. . Url::getCommon(array('db' => $_POST['new_db']))
  161. );
  162. }
  163. }
  164. /**
  165. * Handles dropping multiple databases
  166. *
  167. * @return void
  168. */
  169. public function dropDatabasesAction()
  170. {
  171. if (! isset($_POST['selected_dbs'])) {
  172. $message = Message::error(__('No databases selected.'));
  173. } else {
  174. $action = 'server_databases.php';
  175. $err_url = $action . Url::getCommon();
  176. $GLOBALS['submit_mult'] = 'drop_db';
  177. $GLOBALS['mult_btn'] = __('Yes');
  178. include 'libraries/mult_submits.inc.php';
  179. if (empty($message)) { // no error message
  180. $number_of_databases = count($selected);
  181. $message = Message::success(
  182. _ngettext(
  183. '%1$d database has been dropped successfully.',
  184. '%1$d databases have been dropped successfully.',
  185. $number_of_databases
  186. )
  187. );
  188. $message->addParam($number_of_databases);
  189. }
  190. }
  191. if ($message instanceof Message) {
  192. $this->response->setRequestStatus($message->isSuccess());
  193. $this->response->addJSON('message', $message);
  194. }
  195. }
  196. /**
  197. * Extracts parameters $sort_order and $sort_by
  198. *
  199. * @return void
  200. */
  201. private function _setSortDetails()
  202. {
  203. if (empty($_REQUEST['sort_by'])) {
  204. $this->_sort_by = 'SCHEMA_NAME';
  205. } else {
  206. $sort_by_whitelist = array(
  207. 'SCHEMA_NAME',
  208. 'DEFAULT_COLLATION_NAME',
  209. 'SCHEMA_TABLES',
  210. 'SCHEMA_TABLE_ROWS',
  211. 'SCHEMA_DATA_LENGTH',
  212. 'SCHEMA_INDEX_LENGTH',
  213. 'SCHEMA_LENGTH',
  214. 'SCHEMA_DATA_FREE'
  215. );
  216. if (in_array($_REQUEST['sort_by'], $sort_by_whitelist)) {
  217. $this->_sort_by = $_REQUEST['sort_by'];
  218. } else {
  219. $this->_sort_by = 'SCHEMA_NAME';
  220. }
  221. }
  222. if (isset($_REQUEST['sort_order'])
  223. && mb_strtolower($_REQUEST['sort_order']) == 'desc'
  224. ) {
  225. $this->_sort_order = 'desc';
  226. } else {
  227. $this->_sort_order = 'asc';
  228. }
  229. }
  230. /**
  231. * Returns the html for Database List
  232. *
  233. * @param array $replication_types replication types
  234. *
  235. * @return string
  236. */
  237. private function _getHtmlForDatabases(array $replication_types)
  238. {
  239. $first_database = reset($this->_databases);
  240. // table col order
  241. $column_order = $this->_getColumnOrder();
  242. // calculate aggregate stats to display in footer
  243. foreach ($this->_databases as $current) {
  244. foreach ($column_order as $stat_name => $stat) {
  245. if (array_key_exists($stat_name, $current)
  246. && is_numeric($stat['footer'])
  247. ) {
  248. $column_order[$stat_name]['footer'] += $current[$stat_name];
  249. }
  250. }
  251. }
  252. $_url_params = array(
  253. 'pos' => $this->_pos,
  254. 'dbstats' => $this->_dbstats,
  255. 'sort_by' => $this->_sort_by,
  256. 'sort_order' => $this->_sort_order,
  257. );
  258. $html = Template::get('server/databases/databases_header')->render([
  259. 'database_count' => $this->_database_count,
  260. 'pos' => $this->_pos,
  261. 'url_params' => $_url_params,
  262. 'max_db_list' => $GLOBALS['cfg']['MaxDbList'],
  263. 'sort_by' => $this->_sort_by,
  264. 'sort_order' => $this->_sort_order,
  265. 'column_order' => $column_order,
  266. 'first_database' => $first_database,
  267. 'master_replication' => $GLOBALS['replication_info']['master']['status'],
  268. 'slave_replication' => $GLOBALS['replication_info']['slave']['status'],
  269. 'is_superuser' => $GLOBALS['dbi']->isSuperuser(),
  270. 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'],
  271. ]);
  272. $html .= $this->_getHtmlForTableBody($column_order, $replication_types);
  273. $html .= Template::get('server/databases/databases_footer')->render([
  274. 'column_order' => $column_order,
  275. 'first_database' => $first_database,
  276. 'master_replication' => $GLOBALS['replication_info']['master']['status'],
  277. 'slave_replication' => $GLOBALS['replication_info']['slave']['status'],
  278. 'database_count' => $this->_database_count,
  279. 'is_superuser' => $GLOBALS['dbi']->isSuperuser(),
  280. 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'],
  281. 'pma_theme_image' => $GLOBALS['pmaThemeImage'],
  282. 'text_dir' => $GLOBALS['text_dir'],
  283. 'dbstats' => $this->_dbstats,
  284. ]);
  285. return $html;
  286. }
  287. /**
  288. * Prepares the $column_order array
  289. *
  290. * @return array
  291. */
  292. private function _getColumnOrder()
  293. {
  294. $column_order = array();
  295. $column_order['DEFAULT_COLLATION_NAME'] = array(
  296. 'disp_name' => __('Collation'),
  297. 'description_function' => array(Charsets::class, 'getCollationDescr'),
  298. 'format' => 'string',
  299. 'footer' => $this->dbi->getServerCollation(),
  300. );
  301. $column_order['SCHEMA_TABLES'] = array(
  302. 'disp_name' => __('Tables'),
  303. 'format' => 'number',
  304. 'footer' => 0,
  305. );
  306. $column_order['SCHEMA_TABLE_ROWS'] = array(
  307. 'disp_name' => __('Rows'),
  308. 'format' => 'number',
  309. 'footer' => 0,
  310. );
  311. $column_order['SCHEMA_DATA_LENGTH'] = array(
  312. 'disp_name' => __('Data'),
  313. 'format' => 'byte',
  314. 'footer' => 0,
  315. );
  316. $column_order['SCHEMA_INDEX_LENGTH'] = array(
  317. 'disp_name' => __('Indexes'),
  318. 'format' => 'byte',
  319. 'footer' => 0,
  320. );
  321. $column_order['SCHEMA_LENGTH'] = array(
  322. 'disp_name' => __('Total'),
  323. 'format' => 'byte',
  324. 'footer' => 0,
  325. );
  326. $column_order['SCHEMA_DATA_FREE'] = array(
  327. 'disp_name' => __('Overhead'),
  328. 'format' => 'byte',
  329. 'footer' => 0,
  330. );
  331. return $column_order;
  332. }
  333. /**
  334. * Returns the html for Database List
  335. *
  336. * @param array $column_order column order
  337. * @param array $replication_types replication types
  338. *
  339. * @return string
  340. */
  341. private function _getHtmlForTableBody(array $column_order, array $replication_types)
  342. {
  343. $html = '<tbody>' . "\n";
  344. foreach ($this->_databases as $current) {
  345. $tr_class = ' db-row';
  346. if ($this->dbi->isSystemSchema($current['SCHEMA_NAME'], true)) {
  347. $tr_class .= ' noclick';
  348. }
  349. $generated_html = $this->_buildHtmlForDb(
  350. $current,
  351. $column_order,
  352. $replication_types,
  353. $GLOBALS['replication_info'],
  354. $tr_class
  355. );
  356. $html .= $generated_html;
  357. } // end foreach ($this->_databases as $key => $current)
  358. $html .= '</tbody>';
  359. return $html;
  360. }
  361. /**
  362. * Builds the HTML for one database to display in the list
  363. * of databases from server_databases.php
  364. *
  365. * @param array $current current database
  366. * @param array $column_order column order
  367. * @param array $replication_types replication types
  368. * @param array $replication_info replication info
  369. * @param string $tr_class HTMl class for the row
  370. *
  371. * @return array $column_order, $out
  372. */
  373. function _buildHtmlForDb(
  374. array $current, array $column_order,
  375. array $replication_types, array $replication_info, $tr_class = ''
  376. ) {
  377. $master_replication = $slave_replication = '';
  378. foreach ($replication_types as $type) {
  379. if ($replication_info[$type]['status']) {
  380. $out = '';
  381. $key = array_search(
  382. $current["SCHEMA_NAME"],
  383. $replication_info[$type]['Ignore_DB']
  384. );
  385. if (strlen($key) > 0) {
  386. $out = Util::getIcon(
  387. 's_cancel',
  388. __('Not replicated')
  389. );
  390. } else {
  391. $key = array_search(
  392. $current["SCHEMA_NAME"], $replication_info[$type]['Do_DB']
  393. );
  394. if (strlen($key) > 0
  395. || count($replication_info[$type]['Do_DB']) == 0
  396. ) {
  397. // if ($key != null) did not work for index "0"
  398. $out = Util::getIcon(
  399. 's_success',
  400. __('Replicated')
  401. );
  402. }
  403. }
  404. if ($type == 'master') {
  405. $master_replication = $out;
  406. } elseif ($type == 'slave') {
  407. $slave_replication = $out;
  408. }
  409. }
  410. }
  411. return Template::get('server/databases/table_row')->render([
  412. 'current' => $current,
  413. 'tr_class' => $tr_class,
  414. 'column_order' => $column_order,
  415. 'master_replication_status' => $GLOBALS['replication_info']['master']['status'],
  416. 'master_replication' => $master_replication,
  417. 'slave_replication_status' => $GLOBALS['replication_info']['slave']['status'],
  418. 'slave_replication' => $slave_replication,
  419. 'is_superuser' => $GLOBALS['dbi']->isSuperuser(),
  420. 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'],
  421. 'is_system_schema' => $GLOBALS['dbi']->isSystemSchema($current['SCHEMA_NAME'], true),
  422. 'default_tab_database' => $GLOBALS['cfg']['DefaultTabDatabase'],
  423. ]);
  424. }
  425. }