ServerDatabasesController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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($_REQUEST['dbstats']) ? false : true;
  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. /**
  113. * Builds and executes the db creation sql query
  114. */
  115. $sql_query = 'CREATE DATABASE ' . Util::backquote($_POST['new_db']);
  116. if (! empty($_POST['db_collation'])) {
  117. list($db_charset) = explode('_', $_POST['db_collation']);
  118. $charsets = Charsets::getMySQLCharsets(
  119. $GLOBALS['dbi'],
  120. $GLOBALS['cfg']['Server']['DisableIS']
  121. );
  122. $collations = Charsets::getMySQLCollations(
  123. $GLOBALS['dbi'],
  124. $GLOBALS['cfg']['Server']['DisableIS']
  125. );
  126. if (in_array($db_charset, $charsets)
  127. && in_array($_POST['db_collation'], $collations[$db_charset])
  128. ) {
  129. $sql_query .= ' DEFAULT'
  130. . Util::getCharsetQueryPart($_POST['db_collation']);
  131. }
  132. }
  133. $sql_query .= ';';
  134. $result = $GLOBALS['dbi']->tryQuery($sql_query);
  135. if (! $result) {
  136. // avoid displaying the not-created db name in header or navi panel
  137. $GLOBALS['db'] = '';
  138. $message = Message::rawError($GLOBALS['dbi']->getError());
  139. $this->response->setRequestStatus(false);
  140. $this->response->addJSON('message', $message);
  141. } else {
  142. $GLOBALS['db'] = $_POST['new_db'];
  143. $message = Message::success(__('Database %1$s has been created.'));
  144. $message->addParam($_POST['new_db']);
  145. $this->response->addJSON('message', $message);
  146. $this->response->addJSON(
  147. 'sql_query', Util::getMessage(null, $sql_query, 'success')
  148. );
  149. $this->response->addJSON(
  150. 'url_query',
  151. Util::getScriptNameForOption(
  152. $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
  153. )
  154. . Url::getCommon(array('db' => $_POST['new_db']))
  155. );
  156. }
  157. }
  158. /**
  159. * Handles dropping multiple databases
  160. *
  161. * @return void
  162. */
  163. public function dropDatabasesAction()
  164. {
  165. if (! isset($_POST['selected_dbs'])) {
  166. $message = Message::error(__('No databases selected.'));
  167. } else {
  168. $action = 'server_databases.php';
  169. $err_url = $action . Url::getCommon();
  170. $GLOBALS['submit_mult'] = 'drop_db';
  171. $GLOBALS['mult_btn'] = __('Yes');
  172. include 'libraries/mult_submits.inc.php';
  173. if (empty($message)) { // no error message
  174. $number_of_databases = count($selected);
  175. $message = Message::success(
  176. _ngettext(
  177. '%1$d database has been dropped successfully.',
  178. '%1$d databases have been dropped successfully.',
  179. $number_of_databases
  180. )
  181. );
  182. $message->addParam($number_of_databases);
  183. }
  184. }
  185. if ($message instanceof Message) {
  186. $this->response->setRequestStatus($message->isSuccess());
  187. $this->response->addJSON('message', $message);
  188. }
  189. }
  190. /**
  191. * Extracts parameters $sort_order and $sort_by
  192. *
  193. * @return void
  194. */
  195. private function _setSortDetails()
  196. {
  197. if (empty($_REQUEST['sort_by'])) {
  198. $this->_sort_by = 'SCHEMA_NAME';
  199. } else {
  200. $sort_by_whitelist = array(
  201. 'SCHEMA_NAME',
  202. 'DEFAULT_COLLATION_NAME',
  203. 'SCHEMA_TABLES',
  204. 'SCHEMA_TABLE_ROWS',
  205. 'SCHEMA_DATA_LENGTH',
  206. 'SCHEMA_INDEX_LENGTH',
  207. 'SCHEMA_LENGTH',
  208. 'SCHEMA_DATA_FREE'
  209. );
  210. if (in_array($_REQUEST['sort_by'], $sort_by_whitelist)) {
  211. $this->_sort_by = $_REQUEST['sort_by'];
  212. } else {
  213. $this->_sort_by = 'SCHEMA_NAME';
  214. }
  215. }
  216. if (isset($_REQUEST['sort_order'])
  217. && mb_strtolower($_REQUEST['sort_order']) == 'desc'
  218. ) {
  219. $this->_sort_order = 'desc';
  220. } else {
  221. $this->_sort_order = 'asc';
  222. }
  223. }
  224. /**
  225. * Returns the html for Database List
  226. *
  227. * @param array $replication_types replication types
  228. *
  229. * @return string
  230. */
  231. private function _getHtmlForDatabases(array $replication_types)
  232. {
  233. $first_database = reset($this->_databases);
  234. // table col order
  235. $column_order = $this->_getColumnOrder();
  236. // calculate aggregate stats to display in footer
  237. foreach ($this->_databases as $current) {
  238. foreach ($column_order as $stat_name => $stat) {
  239. if (array_key_exists($stat_name, $current)
  240. && is_numeric($stat['footer'])
  241. ) {
  242. $column_order[$stat_name]['footer'] += $current[$stat_name];
  243. }
  244. }
  245. }
  246. $_url_params = array(
  247. 'pos' => $this->_pos,
  248. 'dbstats' => $this->_dbstats,
  249. 'sort_by' => $this->_sort_by,
  250. 'sort_order' => $this->_sort_order,
  251. );
  252. $html = Template::get('server/databases/databases_header')->render([
  253. 'database_count' => $this->_database_count,
  254. 'pos' => $this->_pos,
  255. 'url_params' => $_url_params,
  256. 'max_db_list' => $GLOBALS['cfg']['MaxDbList'],
  257. 'sort_by' => $this->_sort_by,
  258. 'sort_order' => $this->_sort_order,
  259. 'column_order' => $column_order,
  260. 'first_database' => $first_database,
  261. 'master_replication' => $GLOBALS['replication_info']['master']['status'],
  262. 'slave_replication' => $GLOBALS['replication_info']['slave']['status'],
  263. 'is_superuser' => $GLOBALS['dbi']->isSuperuser(),
  264. 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'],
  265. ]);
  266. $html .= $this->_getHtmlForTableBody($column_order, $replication_types);
  267. $html .= Template::get('server/databases/databases_footer')->render([
  268. 'column_order' => $column_order,
  269. 'first_database' => $first_database,
  270. 'master_replication' => $GLOBALS['replication_info']['master']['status'],
  271. 'slave_replication' => $GLOBALS['replication_info']['slave']['status'],
  272. 'database_count' => $this->_database_count,
  273. 'is_superuser' => $GLOBALS['dbi']->isSuperuser(),
  274. 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'],
  275. 'pma_theme_image' => $GLOBALS['pmaThemeImage'],
  276. 'text_dir' => $GLOBALS['text_dir'],
  277. 'dbstats' => $this->_dbstats,
  278. ]);
  279. return $html;
  280. }
  281. /**
  282. * Prepares the $column_order array
  283. *
  284. * @return array
  285. */
  286. private function _getColumnOrder()
  287. {
  288. $column_order = array();
  289. $column_order['DEFAULT_COLLATION_NAME'] = array(
  290. 'disp_name' => __('Collation'),
  291. 'description_function' => array(Charsets::class, 'getCollationDescr'),
  292. 'format' => 'string',
  293. 'footer' => $this->dbi->getServerCollation(),
  294. );
  295. $column_order['SCHEMA_TABLES'] = array(
  296. 'disp_name' => __('Tables'),
  297. 'format' => 'number',
  298. 'footer' => 0,
  299. );
  300. $column_order['SCHEMA_TABLE_ROWS'] = array(
  301. 'disp_name' => __('Rows'),
  302. 'format' => 'number',
  303. 'footer' => 0,
  304. );
  305. $column_order['SCHEMA_DATA_LENGTH'] = array(
  306. 'disp_name' => __('Data'),
  307. 'format' => 'byte',
  308. 'footer' => 0,
  309. );
  310. $column_order['SCHEMA_INDEX_LENGTH'] = array(
  311. 'disp_name' => __('Indexes'),
  312. 'format' => 'byte',
  313. 'footer' => 0,
  314. );
  315. $column_order['SCHEMA_LENGTH'] = array(
  316. 'disp_name' => __('Total'),
  317. 'format' => 'byte',
  318. 'footer' => 0,
  319. );
  320. $column_order['SCHEMA_DATA_FREE'] = array(
  321. 'disp_name' => __('Overhead'),
  322. 'format' => 'byte',
  323. 'footer' => 0,
  324. );
  325. return $column_order;
  326. }
  327. /**
  328. * Returns the html for Database List
  329. *
  330. * @param array $column_order column order
  331. * @param array $replication_types replication types
  332. *
  333. * @return string
  334. */
  335. private function _getHtmlForTableBody(array $column_order, array $replication_types)
  336. {
  337. $html = '<tbody>' . "\n";
  338. foreach ($this->_databases as $current) {
  339. $tr_class = ' db-row';
  340. if ($this->dbi->isSystemSchema($current['SCHEMA_NAME'], true)) {
  341. $tr_class .= ' noclick';
  342. }
  343. $generated_html = $this->_buildHtmlForDb(
  344. $current,
  345. $column_order,
  346. $replication_types,
  347. $GLOBALS['replication_info'],
  348. $tr_class
  349. );
  350. $html .= $generated_html;
  351. } // end foreach ($this->_databases as $key => $current)
  352. $html .= '</tbody>';
  353. return $html;
  354. }
  355. /**
  356. * Builds the HTML for one database to display in the list
  357. * of databases from server_databases.php
  358. *
  359. * @param array $current current database
  360. * @param array $column_order column order
  361. * @param array $replication_types replication types
  362. * @param array $replication_info replication info
  363. * @param string $tr_class HTMl class for the row
  364. *
  365. * @return array $column_order, $out
  366. */
  367. function _buildHtmlForDb(
  368. array $current, array $column_order,
  369. array $replication_types, array $replication_info, $tr_class = ''
  370. ) {
  371. $master_replication = $slave_replication = '';
  372. foreach ($replication_types as $type) {
  373. if ($replication_info[$type]['status']) {
  374. $out = '';
  375. $key = array_search(
  376. $current["SCHEMA_NAME"],
  377. $replication_info[$type]['Ignore_DB']
  378. );
  379. if (strlen($key) > 0) {
  380. $out = Util::getIcon(
  381. 's_cancel',
  382. __('Not replicated')
  383. );
  384. } else {
  385. $key = array_search(
  386. $current["SCHEMA_NAME"], $replication_info[$type]['Do_DB']
  387. );
  388. if (strlen($key) > 0
  389. || count($replication_info[$type]['Do_DB']) == 0
  390. ) {
  391. // if ($key != null) did not work for index "0"
  392. $out = Util::getIcon(
  393. 's_success',
  394. __('Replicated')
  395. );
  396. }
  397. }
  398. if ($type == 'master') {
  399. $master_replication = $out;
  400. } elseif ($type == 'slave') {
  401. $slave_replication = $out;
  402. }
  403. }
  404. }
  405. return Template::get('server/databases/table_row')->render([
  406. 'current' => $current,
  407. 'tr_class' => $tr_class,
  408. 'column_order' => $column_order,
  409. 'master_replication_status' => $GLOBALS['replication_info']['master']['status'],
  410. 'master_replication' => $master_replication,
  411. 'slave_replication_status' => $GLOBALS['replication_info']['slave']['status'],
  412. 'slave_replication' => $slave_replication,
  413. 'is_superuser' => $GLOBALS['dbi']->isSuperuser(),
  414. 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'],
  415. 'is_system_schema' => $GLOBALS['dbi']->isSystemSchema($current['SCHEMA_NAME'], true),
  416. 'default_tab_database' => $GLOBALS['cfg']['DefaultTabDatabase'],
  417. ]);
  418. }
  419. }