ExportController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Export;
  4. use PhpMyAdmin\Controllers\AbstractController;
  5. use PhpMyAdmin\Controllers\Database\ExportController as DatabaseExportController;
  6. use PhpMyAdmin\Core;
  7. use PhpMyAdmin\Encoding;
  8. use PhpMyAdmin\Exceptions\ExportException;
  9. use PhpMyAdmin\Export;
  10. use PhpMyAdmin\Http\ServerRequest;
  11. use PhpMyAdmin\Message;
  12. use PhpMyAdmin\Plugins;
  13. use PhpMyAdmin\Plugins\ExportPlugin;
  14. use PhpMyAdmin\ResponseRenderer;
  15. use PhpMyAdmin\Sanitize;
  16. use PhpMyAdmin\SqlParser\Parser;
  17. use PhpMyAdmin\SqlParser\Statements\SelectStatement;
  18. use PhpMyAdmin\SqlParser\Utils\Misc;
  19. use PhpMyAdmin\Template;
  20. use PhpMyAdmin\Url;
  21. use PhpMyAdmin\Util;
  22. use function __;
  23. use function count;
  24. use function function_exists;
  25. use function in_array;
  26. use function ini_set;
  27. use function is_array;
  28. use function ob_end_clean;
  29. use function ob_get_length;
  30. use function ob_get_level;
  31. use function register_shutdown_function;
  32. use function strlen;
  33. use function time;
  34. use const PHP_EOL;
  35. final class ExportController extends AbstractController
  36. {
  37. /** @var Export */
  38. private $export;
  39. public function __construct(ResponseRenderer $response, Template $template, Export $export)
  40. {
  41. parent::__construct($response, $template);
  42. $this->export = $export;
  43. }
  44. public function __invoke(ServerRequest $request): void
  45. {
  46. global $containerBuilder, $db, $export_type, $filename_template, $sql_query, $errorUrl, $message;
  47. global $compression, $crlf, $asfile, $buffer_needed, $save_on_server, $file_handle, $separate_files;
  48. global $output_charset_conversion, $output_kanji_conversion, $table, $what, $export_plugin, $single_table;
  49. global $compression_methods, $onserver, $back_button, $refreshButton, $save_filename, $filename;
  50. global $quick_export, $cfg, $tables, $table_select, $aliases;
  51. global $time_start, $charset, $remember_template, $mime_type, $num_tables;
  52. global $active_page, $do_relation, $do_comments, $do_mime, $do_dates, $whatStrucOrData, $db_select;
  53. global $table_structure, $table_data, $lock_tables, $allrows, $limit_to, $limit_from;
  54. /** @var array<string, string> $postParams */
  55. $postParams = $request->getParsedBody();
  56. /** @var string $whatParam */
  57. $whatParam = $request->getParsedBodyParam('what', '');
  58. /** @var string|null $quickOrCustom */
  59. $quickOrCustom = $request->getParsedBodyParam('quick_or_custom');
  60. /** @var string|null $outputFormat */
  61. $outputFormat = $request->getParsedBodyParam('output_format');
  62. /** @var string $compressionParam */
  63. $compressionParam = $request->getParsedBodyParam('compression', '');
  64. /** @var string|null $asSeparateFiles */
  65. $asSeparateFiles = $request->getParsedBodyParam('as_separate_files');
  66. /** @var string|null $quickExportOnServer */
  67. $quickExportOnServer = $request->getParsedBodyParam('quick_export_onserver');
  68. /** @var string|null $onServerParam */
  69. $onServerParam = $request->getParsedBodyParam('onserver');
  70. /** @var array|null $aliasesParam */
  71. $aliasesParam = $request->getParsedBodyParam('aliases');
  72. /** @var string|null $structureOrDataForced */
  73. $structureOrDataForced = $request->getParsedBodyParam('structure_or_data_forced');
  74. $this->addScriptFiles(['export_output.js']);
  75. /**
  76. * Sets globals from $_POST
  77. *
  78. * - Please keep the parameters in order of their appearance in the form
  79. * - Some of these parameters are not used, as the code below directly
  80. * verifies from the superglobal $_POST or $_REQUEST
  81. * TODO: this should be removed to avoid passing user input to GLOBALS
  82. * without checking
  83. */
  84. $allowedPostParams = [
  85. 'db',
  86. 'table',
  87. 'what',
  88. 'single_table',
  89. 'export_type',
  90. 'export_method',
  91. 'quick_or_custom',
  92. 'db_select',
  93. 'table_select',
  94. 'table_structure',
  95. 'table_data',
  96. 'limit_to',
  97. 'limit_from',
  98. 'allrows',
  99. 'lock_tables',
  100. 'output_format',
  101. 'filename_template',
  102. 'maxsize',
  103. 'remember_template',
  104. 'charset',
  105. 'compression',
  106. 'as_separate_files',
  107. 'knjenc',
  108. 'xkana',
  109. 'htmlword_structure_or_data',
  110. 'htmlword_null',
  111. 'htmlword_columns',
  112. 'mediawiki_headers',
  113. 'mediawiki_structure_or_data',
  114. 'mediawiki_caption',
  115. 'pdf_structure_or_data',
  116. 'odt_structure_or_data',
  117. 'odt_relation',
  118. 'odt_comments',
  119. 'odt_mime',
  120. 'odt_columns',
  121. 'odt_null',
  122. 'codegen_structure_or_data',
  123. 'codegen_format',
  124. 'excel_null',
  125. 'excel_removeCRLF',
  126. 'excel_columns',
  127. 'excel_edition',
  128. 'excel_structure_or_data',
  129. 'yaml_structure_or_data',
  130. 'ods_null',
  131. 'ods_structure_or_data',
  132. 'ods_columns',
  133. 'json_structure_or_data',
  134. 'json_pretty_print',
  135. 'json_unicode',
  136. 'xml_structure_or_data',
  137. 'xml_export_events',
  138. 'xml_export_functions',
  139. 'xml_export_procedures',
  140. 'xml_export_tables',
  141. 'xml_export_triggers',
  142. 'xml_export_views',
  143. 'xml_export_contents',
  144. 'texytext_structure_or_data',
  145. 'texytext_columns',
  146. 'texytext_null',
  147. 'phparray_structure_or_data',
  148. 'sql_include_comments',
  149. 'sql_header_comment',
  150. 'sql_dates',
  151. 'sql_relation',
  152. 'sql_mime',
  153. 'sql_use_transaction',
  154. 'sql_disable_fk',
  155. 'sql_compatibility',
  156. 'sql_structure_or_data',
  157. 'sql_create_database',
  158. 'sql_drop_table',
  159. 'sql_procedure_function',
  160. 'sql_create_table',
  161. 'sql_create_view',
  162. 'sql_create_trigger',
  163. 'sql_view_current_user',
  164. 'sql_simple_view_export',
  165. 'sql_if_not_exists',
  166. 'sql_or_replace_view',
  167. 'sql_auto_increment',
  168. 'sql_backquotes',
  169. 'sql_truncate',
  170. 'sql_delayed',
  171. 'sql_ignore',
  172. 'sql_type',
  173. 'sql_insert_syntax',
  174. 'sql_max_query_size',
  175. 'sql_hex_for_binary',
  176. 'sql_utc_time',
  177. 'sql_drop_database',
  178. 'sql_views_as_tables',
  179. 'sql_metadata',
  180. 'csv_separator',
  181. 'csv_enclosed',
  182. 'csv_escaped',
  183. 'csv_terminated',
  184. 'csv_null',
  185. 'csv_removeCRLF',
  186. 'csv_columns',
  187. 'csv_structure_or_data',
  188. // csv_replace should have been here but we use it directly from $_POST
  189. 'latex_caption',
  190. 'latex_structure_or_data',
  191. 'latex_structure_caption',
  192. 'latex_structure_continued_caption',
  193. 'latex_structure_label',
  194. 'latex_relation',
  195. 'latex_comments',
  196. 'latex_mime',
  197. 'latex_columns',
  198. 'latex_data_caption',
  199. 'latex_data_continued_caption',
  200. 'latex_data_label',
  201. 'latex_null',
  202. 'aliases',
  203. ];
  204. foreach ($allowedPostParams as $param) {
  205. if (! isset($postParams[$param])) {
  206. continue;
  207. }
  208. $GLOBALS[$param] = $postParams[$param];
  209. }
  210. Util::checkParameters(['what', 'export_type']);
  211. // sanitize this parameter which will be used below in a file inclusion
  212. $what = Core::securePath($whatParam);
  213. // export class instance, not array of properties, as before
  214. /** @var ExportPlugin $export_plugin */
  215. $export_plugin = Plugins::getPlugin('export', $what, [
  216. 'export_type' => (string) $export_type,
  217. 'single_table' => isset($single_table),
  218. ]);
  219. // Check export type
  220. if (empty($export_plugin)) {
  221. Core::fatalError(__('Bad type!'));
  222. }
  223. /**
  224. * valid compression methods
  225. */
  226. $compression_methods = [];
  227. if ($GLOBALS['cfg']['ZipDump'] && function_exists('gzcompress')) {
  228. $compression_methods[] = 'zip';
  229. }
  230. if ($GLOBALS['cfg']['GZipDump'] && function_exists('gzencode')) {
  231. $compression_methods[] = 'gzip';
  232. }
  233. /**
  234. * init and variable checking
  235. */
  236. $compression = '';
  237. $onserver = false;
  238. $save_on_server = false;
  239. $buffer_needed = false;
  240. $back_button = '';
  241. $refreshButton = '';
  242. $save_filename = '';
  243. $file_handle = '';
  244. $errorUrl = '';
  245. $filename = '';
  246. $separate_files = '';
  247. // Is it a quick or custom export?
  248. if ($quickOrCustom === 'quick') {
  249. $quick_export = true;
  250. } else {
  251. $quick_export = false;
  252. }
  253. if ($outputFormat === 'astext') {
  254. $asfile = false;
  255. } else {
  256. $asfile = true;
  257. if ($asSeparateFiles && $compressionParam === 'zip') {
  258. $separate_files = $asSeparateFiles;
  259. }
  260. if (in_array($compressionParam, $compression_methods)) {
  261. $compression = $compressionParam;
  262. $buffer_needed = true;
  263. }
  264. if (($quick_export && $quickExportOnServer) || (! $quick_export && $onServerParam)) {
  265. if ($quick_export) {
  266. $onserver = $quickExportOnServer;
  267. } else {
  268. $onserver = $onServerParam;
  269. }
  270. // Will we save dump on server?
  271. $save_on_server = ! empty($cfg['SaveDir']);
  272. }
  273. }
  274. /**
  275. * If we are sending the export file (as opposed to just displaying it
  276. * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
  277. */
  278. if ($outputFormat === 'sendit' && ! $save_on_server) {
  279. $this->response->disable();
  280. //Disable all active buffers (see: ob_get_status(true) at this point)
  281. do {
  282. if (ob_get_length() > 0 || ob_get_level() > 0) {
  283. $hasBuffer = ob_end_clean();
  284. } else {
  285. $hasBuffer = false;
  286. }
  287. } while ($hasBuffer);
  288. }
  289. $tables = [];
  290. // Generate error url and check for needed variables
  291. if ($export_type === 'server') {
  292. $errorUrl = Url::getFromRoute('/server/export');
  293. } elseif ($export_type === 'database' && strlen($db) > 0) {
  294. $errorUrl = Url::getFromRoute('/database/export', ['db' => $db]);
  295. // Check if we have something to export
  296. $tables = $table_select ?? [];
  297. } elseif ($export_type === 'table' && strlen($db) > 0 && strlen($table) > 0) {
  298. $errorUrl = Url::getFromRoute('/table/export', [
  299. 'db' => $db,
  300. 'table' => $table,
  301. ]);
  302. } elseif ($export_type === 'raw') {
  303. $errorUrl = Url::getFromRoute('/server/export', ['sql_query' => $sql_query]);
  304. } else {
  305. Core::fatalError(__('Bad parameters!'));
  306. }
  307. // Merge SQL Query aliases with Export aliases from
  308. // export page, Export page aliases are given more
  309. // preference over SQL Query aliases.
  310. $parser = new Parser($sql_query);
  311. $aliases = [];
  312. if (! empty($parser->statements[0]) && ($parser->statements[0] instanceof SelectStatement)) {
  313. $aliases = Misc::getAliases($parser->statements[0], $db);
  314. }
  315. if (! empty($aliasesParam)) {
  316. $aliases = $this->export->mergeAliases($aliases, $aliasesParam);
  317. $_SESSION['tmpval']['aliases'] = $aliasesParam;
  318. }
  319. /**
  320. * Increase time limit for script execution and initializes some variables
  321. */
  322. Util::setTimeLimit();
  323. if (! empty($cfg['MemoryLimit'])) {
  324. ini_set('memory_limit', $cfg['MemoryLimit']);
  325. }
  326. register_shutdown_function([$this->export, 'shutdown']);
  327. // Start with empty buffer
  328. $this->export->dumpBuffer = '';
  329. $this->export->dumpBufferLength = 0;
  330. // Array of dump buffers - used in separate file exports
  331. $this->export->dumpBufferObjects = [];
  332. // We send fake headers to avoid browser timeout when buffering
  333. $time_start = time();
  334. // Defines the default <CR><LF> format.
  335. // For SQL always use \n as MySQL wants this on all platforms.
  336. if ($what === 'sql') {
  337. $crlf = "\n";
  338. } else {
  339. $crlf = PHP_EOL;
  340. }
  341. $output_kanji_conversion = Encoding::canConvertKanji();
  342. // Do we need to convert charset?
  343. $output_charset_conversion = $asfile
  344. && Encoding::isSupported()
  345. && isset($charset) && $charset !== 'utf-8'
  346. && in_array($charset, Encoding::listEncodings(), true);
  347. // Use on the fly compression?
  348. $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
  349. && $compression === 'gzip';
  350. if ($GLOBALS['onfly_compression']) {
  351. $GLOBALS['memory_limit'] = $this->export->getMemoryLimit();
  352. }
  353. // Generate filename and mime type if needed
  354. if ($asfile) {
  355. if (empty($remember_template)) {
  356. $remember_template = '';
  357. }
  358. [$filename, $mime_type] = $this->export->getFilenameAndMimetype(
  359. $export_type,
  360. $remember_template,
  361. $export_plugin,
  362. $compression,
  363. $filename_template
  364. );
  365. } else {
  366. $mime_type = '';
  367. }
  368. // For raw query export, filename will be export.extension
  369. if ($export_type === 'raw') {
  370. [$filename] = $this->export->getFinalFilenameAndMimetypeForFilename($export_plugin, $compression, 'export');
  371. }
  372. // Open file on server if needed
  373. if ($save_on_server) {
  374. [$save_filename, $message, $file_handle] = $this->export->openFile($filename, $quick_export);
  375. // problem opening export file on server?
  376. if (! empty($message)) {
  377. $this->export->showPage($export_type);
  378. return;
  379. }
  380. } else {
  381. /**
  382. * Send headers depending on whether the user chose to download a dump file
  383. * or not
  384. */
  385. if ($asfile) {
  386. // Download
  387. // (avoid rewriting data containing HTML with anchors and forms;
  388. // this was reported to happen under Plesk)
  389. ini_set('url_rewriter.tags', '');
  390. $filename = Sanitize::sanitizeFilename($filename);
  391. Core::downloadHeader($filename, $mime_type);
  392. } else {
  393. // HTML
  394. if ($export_type === 'database') {
  395. $num_tables = count($tables);
  396. if ($num_tables === 0) {
  397. $message = Message::error(
  398. __('No tables found in database.')
  399. );
  400. $active_page = Url::getFromRoute('/database/export');
  401. /** @var DatabaseExportController $controller */
  402. $controller = $containerBuilder->get(DatabaseExportController::class);
  403. $controller();
  404. exit;
  405. }
  406. }
  407. [$html, $back_button, $refreshButton] = $this->export->getHtmlForDisplayedExportHeader(
  408. $export_type,
  409. $db,
  410. $table
  411. );
  412. echo $html;
  413. unset($html);
  414. }
  415. }
  416. try {
  417. // Re - initialize
  418. $this->export->dumpBuffer = '';
  419. $this->export->dumpBufferLength = 0;
  420. // Add possibly some comments to export
  421. if (! $export_plugin->exportHeader()) {
  422. throw new ExportException('Failure during header export.');
  423. }
  424. // Will we need relation & co. setup?
  425. $do_relation = isset($GLOBALS[$what . '_relation']);
  426. $do_comments = isset($GLOBALS[$what . '_include_comments'])
  427. || isset($GLOBALS[$what . '_comments']);
  428. $do_mime = isset($GLOBALS[$what . '_mime']);
  429. // Include dates in export?
  430. $do_dates = isset($GLOBALS[$what . '_dates']);
  431. $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'] ?? null;
  432. if (! in_array($whatStrucOrData, ['structure', 'data', 'structure_and_data'], true)) {
  433. $whatStrucOrData = 'data';
  434. /** @var mixed $whatStrucOrDataDefaultValue */
  435. $whatStrucOrDataDefaultValue = $cfg['Export'][$what . '_structure_or_data'] ?? null;
  436. if (in_array($whatStrucOrDataDefaultValue, ['structure', 'data', 'structure_and_data'], true)) {
  437. $whatStrucOrData = $whatStrucOrDataDefaultValue;
  438. }
  439. $GLOBALS[$what . '_structure_or_data'] = $whatStrucOrData;
  440. }
  441. if ($export_type === 'raw') {
  442. $whatStrucOrData = 'raw';
  443. }
  444. /**
  445. * Builds the dump
  446. */
  447. if ($export_type === 'server') {
  448. if (! isset($db_select)) {
  449. $db_select = '';
  450. }
  451. $this->export->exportServer(
  452. $db_select,
  453. $whatStrucOrData,
  454. $export_plugin,
  455. $crlf,
  456. $errorUrl,
  457. $export_type,
  458. $do_relation,
  459. $do_comments,
  460. $do_mime,
  461. $do_dates,
  462. $aliases,
  463. $separate_files
  464. );
  465. } elseif ($export_type === 'database') {
  466. if (! isset($table_structure) || ! is_array($table_structure)) {
  467. $table_structure = [];
  468. }
  469. if (! isset($table_data) || ! is_array($table_data)) {
  470. $table_data = [];
  471. }
  472. if ($structureOrDataForced) {
  473. $table_structure = $tables;
  474. $table_data = $tables;
  475. }
  476. if (isset($lock_tables)) {
  477. $this->export->lockTables($db, $tables, 'READ');
  478. try {
  479. $this->export->exportDatabase(
  480. $db,
  481. $tables,
  482. $whatStrucOrData,
  483. $table_structure,
  484. $table_data,
  485. $export_plugin,
  486. $crlf,
  487. $errorUrl,
  488. $export_type,
  489. $do_relation,
  490. $do_comments,
  491. $do_mime,
  492. $do_dates,
  493. $aliases,
  494. $separate_files
  495. );
  496. } finally {
  497. $this->export->unlockTables();
  498. }
  499. } else {
  500. $this->export->exportDatabase(
  501. $db,
  502. $tables,
  503. $whatStrucOrData,
  504. $table_structure,
  505. $table_data,
  506. $export_plugin,
  507. $crlf,
  508. $errorUrl,
  509. $export_type,
  510. $do_relation,
  511. $do_comments,
  512. $do_mime,
  513. $do_dates,
  514. $aliases,
  515. $separate_files
  516. );
  517. }
  518. } elseif ($export_type === 'raw') {
  519. Export::exportRaw($whatStrucOrData, $export_plugin, $crlf, $errorUrl, $db, $sql_query, $export_type);
  520. } else {
  521. // We export just one table
  522. // $allrows comes from the form when "Dump all rows" has been selected
  523. if (! isset($allrows)) {
  524. $allrows = '';
  525. }
  526. if (! isset($limit_to)) {
  527. $limit_to = '0';
  528. }
  529. if (! isset($limit_from)) {
  530. $limit_from = '0';
  531. }
  532. if (isset($lock_tables)) {
  533. try {
  534. $this->export->lockTables($db, [$table], 'READ');
  535. $this->export->exportTable(
  536. $db,
  537. $table,
  538. $whatStrucOrData,
  539. $export_plugin,
  540. $crlf,
  541. $errorUrl,
  542. $export_type,
  543. $do_relation,
  544. $do_comments,
  545. $do_mime,
  546. $do_dates,
  547. $allrows,
  548. $limit_to,
  549. $limit_from,
  550. $sql_query,
  551. $aliases
  552. );
  553. } finally {
  554. $this->export->unlockTables();
  555. }
  556. } else {
  557. $this->export->exportTable(
  558. $db,
  559. $table,
  560. $whatStrucOrData,
  561. $export_plugin,
  562. $crlf,
  563. $errorUrl,
  564. $export_type,
  565. $do_relation,
  566. $do_comments,
  567. $do_mime,
  568. $do_dates,
  569. $allrows,
  570. $limit_to,
  571. $limit_from,
  572. $sql_query,
  573. $aliases
  574. );
  575. }
  576. }
  577. if (! $export_plugin->exportFooter()) {
  578. throw new ExportException('Failure during footer export.');
  579. }
  580. } catch (ExportException $e) {
  581. // Ignore
  582. }
  583. if ($save_on_server && ! empty($message)) {
  584. $this->export->showPage($export_type);
  585. return;
  586. }
  587. /**
  588. * Send the dump as a file...
  589. */
  590. if (empty($asfile)) {
  591. echo $this->export->getHtmlForDisplayedExportFooter($back_button, $refreshButton);
  592. return;
  593. }
  594. // Convert the charset if required.
  595. if ($output_charset_conversion) {
  596. $this->export->dumpBuffer = Encoding::convertString(
  597. 'utf-8',
  598. $GLOBALS['charset'],
  599. $this->export->dumpBuffer
  600. );
  601. }
  602. // Compression needed?
  603. if ($compression) {
  604. if (! empty($separate_files)) {
  605. $this->export->dumpBuffer = $this->export->compress(
  606. $this->export->dumpBufferObjects,
  607. $compression,
  608. $filename
  609. );
  610. } else {
  611. $this->export->dumpBuffer = $this->export->compress($this->export->dumpBuffer, $compression, $filename);
  612. }
  613. }
  614. /* If we saved on server, we have to close file now */
  615. if ($save_on_server) {
  616. $message = $this->export->closeFile($file_handle, $this->export->dumpBuffer, $save_filename);
  617. $this->export->showPage($export_type);
  618. return;
  619. }
  620. echo $this->export->dumpBuffer;
  621. }
  622. }