ExportController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Database;
  4. use PhpMyAdmin\Config\PageSettings;
  5. use PhpMyAdmin\Export;
  6. use PhpMyAdmin\Export\Options;
  7. use PhpMyAdmin\Message;
  8. use PhpMyAdmin\Plugins;
  9. use PhpMyAdmin\Response;
  10. use PhpMyAdmin\Template;
  11. use PhpMyAdmin\Url;
  12. use PhpMyAdmin\Util;
  13. use function array_merge;
  14. use function is_array;
  15. final class ExportController extends AbstractController
  16. {
  17. /** @var Export */
  18. private $export;
  19. /** @var Options */
  20. private $exportOptions;
  21. /**
  22. * @param Response $response
  23. * @param string $db Database name.
  24. */
  25. public function __construct($response, Template $template, $db, Export $export, Options $exportOptions)
  26. {
  27. parent::__construct($response, $template, $db);
  28. $this->export = $export;
  29. $this->exportOptions = $exportOptions;
  30. }
  31. public function index(): void
  32. {
  33. global $db, $table, $sub_part, $url_params, $sql_query;
  34. global $tables, $num_tables, $total_num_tables, $tooltip_truename;
  35. global $tooltip_aliasname, $pos, $table_select, $unlim_num_rows, $cfg, $err_url;
  36. $pageSettings = new PageSettings('Export');
  37. $pageSettingsErrorHtml = $pageSettings->getErrorHTML();
  38. $pageSettingsHtml = $pageSettings->getHTML();
  39. $this->addScriptFiles(['export.js']);
  40. // $sub_part is used in Util::getDbInfo() to see if we are coming from
  41. // /database/export, in which case we don't obey $cfg['MaxTableList']
  42. $sub_part = '_export';
  43. Util::checkParameters(['db']);
  44. $err_url = Util::getScriptNameForOption($cfg['DefaultTabDatabase'], 'database');
  45. $err_url .= Url::getCommon(['db' => $db], '&');
  46. if (! $this->hasDatabase()) {
  47. return;
  48. }
  49. $url_params['goto'] = Url::getFromRoute('/database/export');
  50. [
  51. $tables,
  52. $num_tables,
  53. $total_num_tables,
  54. $sub_part,,,
  55. $tooltip_truename,
  56. $tooltip_aliasname,
  57. $pos,
  58. ] = Util::getDbInfo($db, $sub_part ?? '');
  59. // exit if no tables in db found
  60. if ($num_tables < 1) {
  61. $this->response->addHTML(
  62. Message::error(__('No tables found in database.'))->getDisplay()
  63. );
  64. return;
  65. }
  66. if (! empty($_POST['selected_tbl']) && empty($table_select)) {
  67. $table_select = $_POST['selected_tbl'];
  68. }
  69. $tablesForMultiValues = [];
  70. foreach ($tables as $each_table) {
  71. if (isset($_POST['table_select']) && is_array($_POST['table_select'])) {
  72. $is_checked = $this->export->getCheckedClause(
  73. $each_table['Name'],
  74. $_POST['table_select']
  75. );
  76. } elseif (isset($table_select)) {
  77. $is_checked = $this->export->getCheckedClause(
  78. $each_table['Name'],
  79. $table_select
  80. );
  81. } else {
  82. $is_checked = true;
  83. }
  84. if (isset($_POST['table_structure']) && is_array($_POST['table_structure'])) {
  85. $structure_checked = $this->export->getCheckedClause(
  86. $each_table['Name'],
  87. $_POST['table_structure']
  88. );
  89. } else {
  90. $structure_checked = $is_checked;
  91. }
  92. if (isset($_POST['table_data']) && is_array($_POST['table_data'])) {
  93. $data_checked = $this->export->getCheckedClause(
  94. $each_table['Name'],
  95. $_POST['table_data']
  96. );
  97. } else {
  98. $data_checked = $is_checked;
  99. }
  100. $tablesForMultiValues[] = [
  101. 'name' => $each_table['Name'],
  102. 'is_checked_select' => $is_checked,
  103. 'is_checked_structure' => $structure_checked,
  104. 'is_checked_data' => $data_checked,
  105. ];
  106. }
  107. if (! isset($sql_query)) {
  108. $sql_query = '';
  109. }
  110. if (! isset($unlim_num_rows)) {
  111. $unlim_num_rows = 0;
  112. }
  113. $isReturnBackFromRawExport = isset($_POST['export_type']) && $_POST['export_type'] === 'raw';
  114. if (isset($_POST['raw_query']) || $isReturnBackFromRawExport) {
  115. $export_type = 'raw';
  116. } else {
  117. $export_type = 'database';
  118. }
  119. $GLOBALS['single_table'] = $_POST['single_table'] ?? $_GET['single_table'] ?? $GLOBALS['single_table'] ?? null;
  120. $exportList = Plugins::getExport($export_type, isset($GLOBALS['single_table']));
  121. if (empty($exportList)) {
  122. $this->response->addHTML(Message::error(
  123. __('Could not load export plugins, please check your installation!')
  124. )->getDisplay());
  125. return;
  126. }
  127. $options = $this->exportOptions->getOptions(
  128. $export_type,
  129. $db,
  130. $table,
  131. $sql_query,
  132. $num_tables,
  133. $unlim_num_rows,
  134. $exportList
  135. );
  136. $this->render('database/export/index', array_merge($options, [
  137. 'page_settings_error_html' => $pageSettingsErrorHtml,
  138. 'page_settings_html' => $pageSettingsHtml,
  139. 'structure_or_data_forced' => $_POST['structure_or_data_forced'] ?? 0,
  140. 'tables' => $tablesForMultiValues,
  141. ]));
  142. }
  143. public function tables(): void
  144. {
  145. if (empty($_POST['selected_tbl'])) {
  146. $this->response->setRequestStatus(false);
  147. $this->response->addJSON('message', __('No table selected.'));
  148. return;
  149. }
  150. $this->index();
  151. }
  152. }