ExportController.php 24 KB

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