mult_submits.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Helper for multi submit forms
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\CentralColumns;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\MultSubmits;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\Sql;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\Util;
  15. if (! defined('PHPMYADMIN')) {
  16. exit;
  17. }
  18. $request_params = array(
  19. 'clause_is_unique',
  20. 'from_prefix',
  21. 'goto',
  22. 'mult_btn',
  23. 'original_sql_query',
  24. 'query_type',
  25. 'reload',
  26. 'selected',
  27. 'selected_fld',
  28. 'selected_recent_table',
  29. 'sql_query',
  30. 'submit_mult',
  31. 'table_type',
  32. 'to_prefix',
  33. 'url_query'
  34. );
  35. foreach ($request_params as $one_request_param) {
  36. if (isset($_POST[$one_request_param])) {
  37. $GLOBALS[$one_request_param] = $_POST[$one_request_param];
  38. }
  39. }
  40. $response = Response::getInstance();
  41. global $db, $table, $clause_is_unique, $from_prefix, $goto,
  42. $mult_btn, $original_sql_query, $query_type, $reload,
  43. $selected, $selected_fld, $selected_recent_table, $sql_query,
  44. $submit_mult, $table_type, $to_prefix, $url_query, $pmaThemeImage;
  45. $multSubmits = new MultSubmits();
  46. /**
  47. * Prepares the work and runs some other scripts if required
  48. */
  49. if (! empty($submit_mult)
  50. && $submit_mult != __('With selected:')
  51. && (! empty($_POST['selected_dbs'])
  52. || ! empty($_POST['selected_tbl'])
  53. || ! empty($selected_fld)
  54. || ! empty($_POST['rows_to_delete']))
  55. ) {
  56. define('PMA_SUBMIT_MULT', 1);
  57. if (! empty($_POST['selected_dbs'])) {
  58. // coming from server database view - do something with
  59. // selected databases
  60. $selected = $_POST['selected_dbs'];
  61. $query_type = 'drop_db';
  62. } elseif (! empty($_POST['selected_tbl'])) {
  63. // coming from database structure view - do something with
  64. // selected tables
  65. $selected = $_POST['selected_tbl'];
  66. $centralColumns = new CentralColumns($GLOBALS['dbi']);
  67. switch ($submit_mult) {
  68. case 'add_prefix_tbl':
  69. case 'replace_prefix_tbl':
  70. case 'copy_tbl_change_prefix':
  71. case 'drop_db':
  72. case 'drop_tbl':
  73. case 'empty_tbl':
  74. $what = $submit_mult;
  75. break;
  76. case 'check_tbl':
  77. case 'optimize_tbl':
  78. case 'repair_tbl':
  79. case 'analyze_tbl':
  80. case 'checksum_tbl':
  81. $query_type = $submit_mult;
  82. unset($submit_mult);
  83. $mult_btn = __('Yes');
  84. break;
  85. case 'export':
  86. unset($submit_mult);
  87. include 'db_export.php';
  88. exit;
  89. case 'copy_tbl':
  90. $views = $GLOBALS['dbi']->getVirtualTables($db);
  91. list($full_query, $reload, $full_query_views)
  92. = $multSubmits->getQueryFromSelected(
  93. $submit_mult, $table, $selected, $views
  94. );
  95. $_url_params = $multSubmits->getUrlParams(
  96. $submit_mult, $reload, $action, $db, $table, $selected, $views,
  97. isset($original_sql_query)? $original_sql_query : null,
  98. isset($original_url_query)? $original_url_query : null
  99. );
  100. $response->disable();
  101. $response->addHTML(
  102. $multSubmits->getHtmlForCopyMultipleTables($action, $_url_params)
  103. );
  104. exit;
  105. case 'show_create':
  106. $show_create = Template::get(
  107. 'database/structure/show_create'
  108. )
  109. ->render(
  110. array(
  111. 'db' => $GLOBALS['db'],
  112. 'db_objects' => $selected,
  113. 'dbi' => $GLOBALS['dbi'],
  114. )
  115. );
  116. // Send response to client.
  117. $response->addJSON('message', $show_create);
  118. exit;
  119. case 'sync_unique_columns_central_list':
  120. $centralColsError = $centralColumns->syncUniqueColumns(
  121. $selected
  122. );
  123. break;
  124. case 'delete_unique_columns_central_list':
  125. $centralColsError = $centralColumns->deleteColumnsFromList(
  126. $selected
  127. );
  128. break;
  129. case 'make_consistent_with_central_list':
  130. $centralColsError = $centralColumns->makeConsistentWithList(
  131. $GLOBALS['db'],
  132. $selected
  133. );
  134. break;
  135. } // end switch
  136. } elseif (isset($selected_fld) && !empty($selected_fld)) {
  137. // coming from table structure view - do something with
  138. // selected columns
  139. // handled in StructrueController
  140. } else {
  141. // coming from browsing - do something with selected rows
  142. $what = 'row_delete';
  143. $selected = $_REQUEST['rows_to_delete'];
  144. }
  145. } // end if
  146. if (empty($db)) {
  147. $db = '';
  148. }
  149. if (empty($table)) {
  150. $table = '';
  151. }
  152. $views = $GLOBALS['dbi']->getVirtualTables($db);
  153. /**
  154. * Displays the confirmation form if required
  155. */
  156. if (!empty($submit_mult) && !empty($what)) {
  157. unset($message);
  158. if (strlen($table) > 0) {
  159. include './libraries/tbl_common.inc.php';
  160. $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
  161. } elseif (strlen($db) > 0) {
  162. include './libraries/db_common.inc.php';
  163. list(
  164. $tables,
  165. $num_tables,
  166. $total_num_tables,
  167. $sub_part,
  168. $is_show_stats,
  169. $db_is_system_schema,
  170. $tooltip_truename,
  171. $tooltip_aliasname,
  172. $pos
  173. ) = Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
  174. } else {
  175. include_once './libraries/server_common.inc.php';
  176. }
  177. // Builds the query
  178. list($full_query, $reload, $full_query_views)
  179. = $multSubmits->getQueryFromSelected(
  180. $what, $table, $selected, $views
  181. );
  182. // Displays the confirmation form
  183. $_url_params = $multSubmits->getUrlParams(
  184. $what, $reload, $action, $db, $table, $selected, $views,
  185. isset($original_sql_query)? $original_sql_query : null,
  186. isset($original_url_query)? $original_url_query : null
  187. );
  188. if ($what == 'replace_prefix_tbl' || $what == 'copy_tbl_change_prefix') {
  189. $response->disable();
  190. $response->addHTML(
  191. $multSubmits->getHtmlForReplacePrefixTable($action, $_url_params)
  192. );
  193. } elseif ($what == 'add_prefix_tbl') {
  194. $response->disable();
  195. $response->addHTML($multSubmits->getHtmlForAddPrefixTable($action, $_url_params));
  196. } else {
  197. $response->addHTML(
  198. $multSubmits->getHtmlForOtherActions($what, $action, $_url_params, $full_query)
  199. );
  200. }
  201. exit;
  202. } elseif (! empty($mult_btn) && $mult_btn == __('Yes')) {
  203. /**
  204. * Executes the query - dropping rows, columns/fields, tables or dbs
  205. */
  206. if ($query_type == 'primary_fld') {
  207. // Gets table primary key
  208. $GLOBALS['dbi']->selectDb($db);
  209. $result = $GLOBALS['dbi']->query(
  210. 'SHOW KEYS FROM ' . Util::backquote($table) . ';'
  211. );
  212. $primary = '';
  213. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  214. // Backups the list of primary keys
  215. if ($row['Key_name'] == 'PRIMARY') {
  216. $primary .= $row['Column_name'] . ', ';
  217. }
  218. } // end while
  219. $GLOBALS['dbi']->freeResult($result);
  220. }
  221. if ($query_type == 'drop_tbl'
  222. || $query_type == 'empty_tbl'
  223. || $query_type == 'row_delete'
  224. ) {
  225. $default_fk_check_value = Util::handleDisableFKCheckInit();
  226. }
  227. list(
  228. $result, $rebuild_database_list, $reload_ret,
  229. $run_parts, $execute_query_later, $sql_query, $sql_query_views
  230. ) = $multSubmits->buildOrExecuteQuery(
  231. $query_type, $selected, $db, $table, $views,
  232. isset($primary) ? $primary : null,
  233. isset($from_prefix) ? $from_prefix : null,
  234. isset($to_prefix) ? $to_prefix : null
  235. );
  236. //update the existed variable
  237. if (isset($reload_ret)) {
  238. $reload = $reload_ret;
  239. }
  240. if ($query_type == 'drop_tbl') {
  241. if (!empty($sql_query)) {
  242. $sql_query .= ';';
  243. } elseif (!empty($sql_query_views)) {
  244. $sql_query = $sql_query_views . ';';
  245. unset($sql_query_views);
  246. }
  247. }
  248. // Unset cache values for tables count, issue #14205
  249. if ($query_type === 'drop_tbl' && isset($_SESSION['tmpval'])) {
  250. if (isset($_SESSION['tmpval']['table_limit_offset'])) {
  251. unset($_SESSION['tmpval']['table_limit_offset']);
  252. }
  253. if (isset($_SESSION['tmpval']['table_limit_offset_db'])) {
  254. unset($_SESSION['tmpval']['table_limit_offset_db']);
  255. }
  256. }
  257. if ($execute_query_later) {
  258. $sql = new Sql();
  259. $sql->executeQueryAndSendQueryResponse(
  260. null, // analyzed_sql_results
  261. false, // is_gotofile
  262. $db, // db
  263. $table, // table
  264. null, // find_real_end
  265. null, // sql_query_for_bookmark
  266. null, // extra_data
  267. null, // message_to_show
  268. null, // message
  269. null, // sql_data
  270. $goto, // goto
  271. $pmaThemeImage, // pmaThemeImage
  272. null, // disp_query
  273. null, // disp_message
  274. $query_type, // query_type
  275. $sql_query, // sql_query
  276. $selected, // selectedTables
  277. null // complete_query
  278. );
  279. } elseif (!$run_parts) {
  280. $GLOBALS['dbi']->selectDb($db);
  281. $result = $GLOBALS['dbi']->tryQuery($sql_query);
  282. if ($result && !empty($sql_query_views)) {
  283. $sql_query .= ' ' . $sql_query_views . ';';
  284. $result = $GLOBALS['dbi']->tryQuery($sql_query_views);
  285. unset($sql_query_views);
  286. }
  287. if (! $result) {
  288. $message = Message::error($GLOBALS['dbi']->getError());
  289. }
  290. }
  291. if ($query_type == 'drop_tbl'
  292. || $query_type == 'empty_tbl'
  293. || $query_type == 'row_delete'
  294. ) {
  295. Util::handleDisableFKCheckCleanup($default_fk_check_value);
  296. }
  297. if ($rebuild_database_list) {
  298. // avoid a problem with the database list navigator
  299. // when dropping a db from server_databases
  300. $GLOBALS['dblist']->databases->build();
  301. }
  302. } else {
  303. if (isset($submit_mult)
  304. && ($submit_mult == 'sync_unique_columns_central_list'
  305. || $submit_mult == 'delete_unique_columns_central_list'
  306. || $submit_mult == 'add_to_central_columns'
  307. || $submit_mult == 'remove_from_central_columns'
  308. || $submit_mult == 'make_consistent_with_central_list')
  309. ) {
  310. if (isset($centralColsError) && $centralColsError !== true) {
  311. $message = $centralColsError;
  312. } else {
  313. $message = Message::success(__('Success!'));
  314. }
  315. } else {
  316. $message = Message::success(__('No change'));
  317. }
  318. }