PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/db_operations.php

https://gitlab.com/luyxtran264/myproject
PHP | 315 lines | 197 code | 44 blank | 74 comment | 46 complexity | 6777c4cb1d8dcddf5e2858c37e5045fe MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * handles miscellaneous db operations:
  5. * - move/rename
  6. * - copy
  7. * - changing collation
  8. * - changing comment
  9. * - adding tables
  10. * - viewing PDF schemas
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. use PMA\libraries\plugins\export\ExportSql;
  15. /**
  16. * requirements
  17. */
  18. require_once 'libraries/common.inc.php';
  19. require_once 'libraries/mysql_charsets.inc.php';
  20. require_once 'libraries/display_create_table.lib.php';
  21. /**
  22. * functions implementation for this script
  23. */
  24. require_once 'libraries/check_user_privileges.lib.php';
  25. require_once 'libraries/operations.lib.php';
  26. // add a javascript file for jQuery functions to handle Ajax actions
  27. $response = PMA\libraries\Response::getInstance();
  28. $header = $response->getHeader();
  29. $scripts = $header->getScripts();
  30. $scripts->addFile('db_operations.js');
  31. $sql_query = '';
  32. /**
  33. * Rename/move or copy database
  34. */
  35. if (mb_strlen($GLOBALS['db'])
  36. && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
  37. ) {
  38. if (! empty($_REQUEST['db_rename'])) {
  39. $move = true;
  40. } else {
  41. $move = false;
  42. }
  43. if (! isset($_REQUEST['newname'])
  44. || ! mb_strlen($_REQUEST['newname'])
  45. ) {
  46. $message = PMA\libraries\Message::error(__('The database name is empty!'));
  47. } else {
  48. $_error = false;
  49. if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
  50. PMA_createDbBeforeCopy();
  51. }
  52. // here I don't use DELIMITER because it's not part of the
  53. // language; I have to send each statement one by one
  54. // to avoid selecting alternatively the current and new db
  55. // we would need to modify the CREATE definitions to qualify
  56. // the db name
  57. PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
  58. // go back to current db, just in case
  59. $GLOBALS['dbi']->selectDb($GLOBALS['db']);
  60. $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
  61. include_once "libraries/plugin_interface.lib.php";
  62. // remove all foreign key constraints, otherwise we can get errors
  63. /* @var $export_sql_plugin ExportSql */
  64. $export_sql_plugin = PMA_getPlugin(
  65. "export",
  66. "sql",
  67. 'libraries/plugins/export/',
  68. array(
  69. 'single_table' => isset($single_table),
  70. 'export_type' => 'database'
  71. )
  72. );
  73. // create stand-in tables for views
  74. $views = PMA_getViewsAndCreateSqlViewStandIn(
  75. $tables_full, $export_sql_plugin, $GLOBALS['db']
  76. );
  77. // copy tables
  78. $sqlConstratints = PMA_copyTables(
  79. $tables_full, $move, $GLOBALS['db']
  80. );
  81. // handle the views
  82. if (! $_error) {
  83. PMA_handleTheViews($views, $move, $GLOBALS['db']);
  84. }
  85. unset($views);
  86. // now that all tables exist, create all the accumulated constraints
  87. if (! $_error && count($sqlConstratints) > 0) {
  88. PMA_createAllAccumulatedConstraints($sqlConstratints);
  89. }
  90. unset($sqlConstratints);
  91. if (PMA_MYSQL_INT_VERSION >= 50100) {
  92. // here DELIMITER is not used because it's not part of the
  93. // language; each statement is sent one by one
  94. PMA_runEventDefinitionsForDb($GLOBALS['db']);
  95. }
  96. // go back to current db, just in case
  97. $GLOBALS['dbi']->selectDb($GLOBALS['db']);
  98. // Duplicate the bookmarks for this db (done once for each db)
  99. PMA_duplicateBookmarks($_error, $GLOBALS['db']);
  100. if (! $_error && $move) {
  101. if (isset($_REQUEST['adjust_privileges'])
  102. && ! empty($_REQUEST['adjust_privileges'])
  103. ) {
  104. PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
  105. }
  106. /**
  107. * cleanup pmadb stuff for this db
  108. */
  109. include_once 'libraries/relation_cleanup.lib.php';
  110. PMA_relationsCleanupDatabase($GLOBALS['db']);
  111. // if someday the RENAME DATABASE reappears, do not DROP
  112. $local_query = 'DROP DATABASE '
  113. . PMA\libraries\Util::backquote($GLOBALS['db']) . ';';
  114. $sql_query .= "\n" . $local_query;
  115. $GLOBALS['dbi']->query($local_query);
  116. $message = PMA\libraries\Message::success(
  117. __('Database %1$s has been renamed to %2$s.')
  118. );
  119. $message->addParam($GLOBALS['db']);
  120. $message->addParam($_REQUEST['newname']);
  121. } elseif (! $_error) {
  122. if (isset($_REQUEST['adjust_privileges'])
  123. && ! empty($_REQUEST['adjust_privileges'])
  124. ) {
  125. PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
  126. }
  127. $message = PMA\libraries\Message::success(
  128. __('Database %1$s has been copied to %2$s.')
  129. );
  130. $message->addParam($GLOBALS['db']);
  131. $message->addParam($_REQUEST['newname']);
  132. } else {
  133. $message = PMA\libraries\Message::error();
  134. }
  135. $reload = true;
  136. /* Change database to be used */
  137. if (! $_error && $move) {
  138. $GLOBALS['db'] = $_REQUEST['newname'];
  139. } elseif (! $_error) {
  140. if (isset($_REQUEST['switch_to_new'])
  141. && $_REQUEST['switch_to_new'] == 'true'
  142. ) {
  143. $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
  144. $GLOBALS['db'] = $_REQUEST['newname'];
  145. } else {
  146. $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
  147. }
  148. }
  149. }
  150. /**
  151. * Database has been successfully renamed/moved. If in an Ajax request,
  152. * generate the output with {@link PMA\libraries\Response} and exit
  153. */
  154. if ($GLOBALS['is_ajax_request'] == true) {
  155. $response = PMA\libraries\Response::getInstance();
  156. $response->setRequestStatus($message->isSuccess());
  157. $response->addJSON('message', $message);
  158. $response->addJSON('newname', $_REQUEST['newname']);
  159. $response->addJSON(
  160. 'sql_query',
  161. PMA\libraries\Util::getMessage(null, $sql_query)
  162. );
  163. $response->addJSON('db', $GLOBALS['db']);
  164. exit;
  165. }
  166. }
  167. /**
  168. * Settings for relations stuff
  169. */
  170. $cfgRelation = PMA_getRelationsParam();
  171. /**
  172. * Check if comments were updated
  173. * (must be done before displaying the menu tabs)
  174. */
  175. if (isset($_REQUEST['comment'])) {
  176. PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
  177. }
  178. require 'libraries/db_common.inc.php';
  179. $url_query .= '&amp;goto=db_operations.php';
  180. // Gets the database structure
  181. $sub_part = '_structure';
  182. list(
  183. $tables,
  184. $num_tables,
  185. $total_num_tables,
  186. $sub_part,
  187. $is_show_stats,
  188. $db_is_system_schema,
  189. $tooltip_truename,
  190. $tooltip_aliasname,
  191. $pos
  192. ) = PMA\libraries\Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
  193. echo "\n";
  194. if (isset($message)) {
  195. echo PMA\libraries\Util::getMessage($message, $sql_query);
  196. unset($message);
  197. }
  198. $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
  199. $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
  200. $response->addHTML('<div id="boxContainer" data-box-width="300">');
  201. if (!$is_information_schema) {
  202. if ($cfgRelation['commwork']) {
  203. /**
  204. * database comment
  205. */
  206. $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
  207. }
  208. $response->addHTML('<div class="operations_half_width">');
  209. $response->addHTML(PMA_getHtmlForCreateTable($db));
  210. $response->addHTML('</div>');
  211. /**
  212. * rename database
  213. */
  214. if ($GLOBALS['db'] != 'mysql') {
  215. $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
  216. }
  217. // Drop link if allowed
  218. // Don't even try to drop information_schema.
  219. // You won't be able to. Believe me. You won't.
  220. // Don't allow to easily drop mysql database, RFE #1327514.
  221. if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
  222. && ! $db_is_system_schema
  223. && $GLOBALS['db'] != 'mysql'
  224. ) {
  225. $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
  226. }
  227. /**
  228. * Copy database
  229. */
  230. $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
  231. /**
  232. * Change database charset
  233. */
  234. $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
  235. if (! $cfgRelation['allworks']
  236. && $cfg['PmaNoRelation_DisableWarning'] == false
  237. ) {
  238. $message = PMA\libraries\Message::notice(
  239. __(
  240. 'The phpMyAdmin configuration storage has been deactivated. ' .
  241. '%sFind out why%s.'
  242. )
  243. );
  244. $message->addParam(
  245. '<a href="'
  246. . './chk_rel.php' . $url_query . '">',
  247. false
  248. );
  249. $message->addParam('</a>', false);
  250. /* Show error if user has configured something, notice elsewhere */
  251. if (!empty($cfg['Servers'][$server]['pmadb'])) {
  252. $message->isError(true);
  253. }
  254. } // end if
  255. } // end if (!$is_information_schema)
  256. $response->addHTML('</div>');
  257. // not sure about displaying the PDF dialog in case db is information_schema
  258. if ($cfgRelation['pdfwork'] && $num_tables > 0) {
  259. // We only show this if we find something in the new pdf_pages table
  260. $test_query = '
  261. SELECT *
  262. FROM ' . PMA\libraries\Util::backquote($GLOBALS['cfgRelation']['db'])
  263. . '.' . PMA\libraries\Util::backquote($cfgRelation['pdf_pages']) . '
  264. WHERE db_name = \'' . PMA\libraries\Util::sqlAddSlashes($GLOBALS['db'])
  265. . '\'';
  266. $test_rs = PMA_queryAsControlUser(
  267. $test_query,
  268. false,
  269. PMA\libraries\DatabaseInterface::QUERY_STORE
  270. );
  271. } // end if