export.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Main export handling code
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\Core;
  9. use PhpMyAdmin\Encoding;
  10. use PhpMyAdmin\Export;
  11. use PhpMyAdmin\Plugins;
  12. use PhpMyAdmin\Plugins\ExportPlugin;
  13. use PhpMyAdmin\Relation;
  14. use PhpMyAdmin\Sanitize;
  15. use PhpMyAdmin\Url;
  16. use PhpMyAdmin\Util;
  17. use PhpMyAdmin\Response;
  18. /**
  19. * Get the variables sent or posted to this script and a core script
  20. */
  21. include_once 'libraries/common.inc.php';
  22. $response = Response::getInstance();
  23. $header = $response->getHeader();
  24. $scripts = $header->getScripts();
  25. $scripts->addFile('export_output.js');
  26. //check if it's the GET request to check export time out
  27. if (isset($_GET['check_time_out'])) {
  28. if (isset($_SESSION['pma_export_error'])) {
  29. $err = $_SESSION['pma_export_error'];
  30. unset($_SESSION['pma_export_error']);
  31. echo "timeout";
  32. } else {
  33. echo "success";
  34. }
  35. exit;
  36. }
  37. /**
  38. * Sets globals from $_POST
  39. *
  40. * - Please keep the parameters in order of their appearance in the form
  41. * - Some of these parameters are not used, as the code below directly
  42. * verifies from the superglobal $_POST or $_REQUEST
  43. * TODO: this should be removed to avoid passing user input to GLOBALS
  44. * without checking
  45. */
  46. $post_params = array(
  47. 'db',
  48. 'table',
  49. 'what',
  50. 'single_table',
  51. 'export_type',
  52. 'export_method',
  53. 'quick_or_custom',
  54. 'db_select',
  55. 'table_select',
  56. 'table_structure',
  57. 'table_data',
  58. 'limit_to',
  59. 'limit_from',
  60. 'allrows',
  61. 'lock_tables',
  62. 'output_format',
  63. 'filename_template',
  64. 'maxsize',
  65. 'remember_template',
  66. 'charset',
  67. 'compression',
  68. 'as_separate_files',
  69. 'knjenc',
  70. 'xkana',
  71. 'htmlword_structure_or_data',
  72. 'htmlword_null',
  73. 'htmlword_columns',
  74. 'mediawiki_headers',
  75. 'mediawiki_structure_or_data',
  76. 'mediawiki_caption',
  77. 'pdf_structure_or_data',
  78. 'odt_structure_or_data',
  79. 'odt_relation',
  80. 'odt_comments',
  81. 'odt_mime',
  82. 'odt_columns',
  83. 'odt_null',
  84. 'codegen_structure_or_data',
  85. 'codegen_format',
  86. 'excel_null',
  87. 'excel_removeCRLF',
  88. 'excel_columns',
  89. 'excel_edition',
  90. 'excel_structure_or_data',
  91. 'yaml_structure_or_data',
  92. 'ods_null',
  93. 'ods_structure_or_data',
  94. 'ods_columns',
  95. 'json_structure_or_data',
  96. 'json_pretty_print',
  97. 'json_unicode',
  98. 'xml_structure_or_data',
  99. 'xml_export_events',
  100. 'xml_export_functions',
  101. 'xml_export_procedures',
  102. 'xml_export_tables',
  103. 'xml_export_triggers',
  104. 'xml_export_views',
  105. 'xml_export_contents',
  106. 'texytext_structure_or_data',
  107. 'texytext_columns',
  108. 'texytext_null',
  109. 'phparray_structure_or_data',
  110. 'sql_include_comments',
  111. 'sql_header_comment',
  112. 'sql_dates',
  113. 'sql_relation',
  114. 'sql_mime',
  115. 'sql_use_transaction',
  116. 'sql_disable_fk',
  117. 'sql_compatibility',
  118. 'sql_structure_or_data',
  119. 'sql_create_database',
  120. 'sql_drop_table',
  121. 'sql_procedure_function',
  122. 'sql_create_table',
  123. 'sql_create_view',
  124. 'sql_create_trigger',
  125. 'sql_if_not_exists',
  126. 'sql_auto_increment',
  127. 'sql_backquotes',
  128. 'sql_truncate',
  129. 'sql_delayed',
  130. 'sql_ignore',
  131. 'sql_type',
  132. 'sql_insert_syntax',
  133. 'sql_max_query_size',
  134. 'sql_hex_for_binary',
  135. 'sql_utc_time',
  136. 'sql_drop_database',
  137. 'sql_views_as_tables',
  138. 'sql_metadata',
  139. 'csv_separator',
  140. 'csv_enclosed',
  141. 'csv_escaped',
  142. 'csv_terminated',
  143. 'csv_null',
  144. 'csv_removeCRLF',
  145. 'csv_columns',
  146. 'csv_structure_or_data',
  147. // csv_replace should have been here but we use it directly from $_POST
  148. 'latex_caption',
  149. 'latex_structure_or_data',
  150. 'latex_structure_caption',
  151. 'latex_structure_continued_caption',
  152. 'latex_structure_label',
  153. 'latex_relation',
  154. 'latex_comments',
  155. 'latex_mime',
  156. 'latex_columns',
  157. 'latex_data_caption',
  158. 'latex_data_continued_caption',
  159. 'latex_data_label',
  160. 'latex_null',
  161. 'aliases'
  162. );
  163. foreach ($post_params as $one_post_param) {
  164. if (isset($_POST[$one_post_param])) {
  165. $GLOBALS[$one_post_param] = $_POST[$one_post_param];
  166. }
  167. }
  168. $table = $GLOBALS['table'];
  169. PhpMyAdmin\Util::checkParameters(array('what', 'export_type'));
  170. // sanitize this parameter which will be used below in a file inclusion
  171. $what = Core::securePath($_POST['what']);
  172. // export class instance, not array of properties, as before
  173. /* @var $export_plugin ExportPlugin */
  174. $export_plugin = Plugins::getPlugin(
  175. "export",
  176. $what,
  177. 'libraries/classes/Plugins/Export/',
  178. array(
  179. 'export_type' => $export_type,
  180. 'single_table' => isset($single_table)
  181. )
  182. );
  183. // Check export type
  184. if (empty($export_plugin)) {
  185. Core::fatalError(__('Bad type!'));
  186. }
  187. /**
  188. * valid compression methods
  189. */
  190. $compression_methods = array(
  191. 'zip',
  192. 'gzip'
  193. );
  194. /**
  195. * init and variable checking
  196. */
  197. $compression = false;
  198. $onserver = false;
  199. $save_on_server = false;
  200. $buffer_needed = false;
  201. $back_button = '';
  202. $refreshButton = '';
  203. $save_filename = '';
  204. $file_handle = '';
  205. $err_url = '';
  206. $filename = '';
  207. $separate_files = '';
  208. // Is it a quick or custom export?
  209. if (isset($_POST['quick_or_custom'])
  210. && $_POST['quick_or_custom'] == 'quick'
  211. ) {
  212. $quick_export = true;
  213. } else {
  214. $quick_export = false;
  215. }
  216. if ($_POST['output_format'] == 'astext') {
  217. $asfile = false;
  218. } else {
  219. $asfile = true;
  220. if (isset($_POST['as_separate_files'])
  221. && ! empty($_POST['as_separate_files'])
  222. ) {
  223. if (isset($_POST['compression'])
  224. && ! empty($_POST['compression'])
  225. && $_POST['compression'] == 'zip'
  226. ) {
  227. $separate_files = $_POST['as_separate_files'];
  228. }
  229. }
  230. if (in_array($_POST['compression'], $compression_methods)) {
  231. $compression = $_POST['compression'];
  232. $buffer_needed = true;
  233. }
  234. if (($quick_export && ! empty($_POST['quick_export_onserver']))
  235. || (! $quick_export && ! empty($_POST['onserver']))
  236. ) {
  237. if ($quick_export) {
  238. $onserver = $_POST['quick_export_onserver'];
  239. } else {
  240. $onserver = $_POST['onserver'];
  241. }
  242. // Will we save dump on server?
  243. $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
  244. }
  245. }
  246. /**
  247. * If we are sending the export file (as opposed to just displaying it
  248. * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
  249. */
  250. if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit' && ! $save_on_server) {
  251. $response->disable();
  252. //Disable all active buffers (see: ob_get_status(true) at this point)
  253. while (@ob_end_clean());
  254. }
  255. // Generate error url and check for needed variables
  256. if ($export_type == 'server') {
  257. $err_url = 'server_export.php' . Url::getCommon();
  258. } elseif ($export_type == 'database' && strlen($db) > 0) {
  259. $err_url = 'db_export.php' . Url::getCommon(array('db' => $db));
  260. // Check if we have something to export
  261. if (isset($table_select)) {
  262. $tables = $table_select;
  263. } else {
  264. $tables = array();
  265. }
  266. } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
  267. $err_url = 'tbl_export.php' . Url::getCommon(
  268. array(
  269. 'db' => $db, 'table' => $table
  270. )
  271. );
  272. } else {
  273. Core::fatalError(__('Bad parameters!'));
  274. }
  275. // Merge SQL Query aliases with Export aliases from
  276. // export page, Export page aliases are given more
  277. // preference over SQL Query aliases.
  278. $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
  279. $aliases = array();
  280. if ((!empty($parser->statements[0]))
  281. && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
  282. ) {
  283. $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
  284. }
  285. if (!empty($_POST['aliases'])) {
  286. $aliases = Export::mergeAliases($aliases, $_POST['aliases']);
  287. $_SESSION['tmpval']['aliases'] = $_POST['aliases'];
  288. }
  289. /**
  290. * Increase time limit for script execution and initializes some variables
  291. */
  292. Util::setTimeLimit();
  293. if (! empty($cfg['MemoryLimit'])) {
  294. ini_set('memory_limit', $cfg['MemoryLimit']);
  295. }
  296. register_shutdown_function('PhpMyAdmin\Export::shutdown');
  297. // Start with empty buffer
  298. $dump_buffer = '';
  299. $dump_buffer_len = 0;
  300. // Array of dump_buffers - used in separate file exports
  301. $dump_buffer_objects = array();
  302. // We send fake headers to avoid browser timeout when buffering
  303. $time_start = time();
  304. // Defines the default <CR><LF> format.
  305. // For SQL always use \n as MySQL wants this on all platforms.
  306. if ($what == 'sql') {
  307. $crlf = "\n";
  308. } else {
  309. $crlf = PHP_EOL;
  310. }
  311. $output_kanji_conversion = Encoding::canConvertKanji();
  312. // Do we need to convert charset?
  313. $output_charset_conversion = $asfile
  314. && Encoding::isSupported()
  315. && isset($charset) && $charset != 'utf-8';
  316. // Use on the fly compression?
  317. $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
  318. && $compression == 'gzip';
  319. if ($GLOBALS['onfly_compression']) {
  320. $GLOBALS['memory_limit'] = Export::getMemoryLimit();
  321. }
  322. // Generate filename and mime type if needed
  323. if ($asfile) {
  324. if (empty($remember_template)) {
  325. $remember_template = '';
  326. }
  327. list($filename, $mime_type) = Export::getFilenameAndMimetype(
  328. $export_type, $remember_template, $export_plugin, $compression,
  329. $filename_template
  330. );
  331. } else {
  332. $mime_type = '';
  333. }
  334. // Open file on server if needed
  335. if ($save_on_server) {
  336. list($save_filename, $message, $file_handle) = Export::openFile(
  337. $filename, $quick_export
  338. );
  339. // problem opening export file on server?
  340. if (! empty($message)) {
  341. Export::showPage($db, $table, $export_type);
  342. }
  343. } else {
  344. /**
  345. * Send headers depending on whether the user chose to download a dump file
  346. * or not
  347. */
  348. if ($asfile) {
  349. // Download
  350. // (avoid rewriting data containing HTML with anchors and forms;
  351. // this was reported to happen under Plesk)
  352. ini_set('url_rewriter.tags', '');
  353. $filename = Sanitize::sanitizeFilename($filename);
  354. Core::downloadHeader($filename, $mime_type);
  355. } else {
  356. // HTML
  357. if ($export_type == 'database') {
  358. $num_tables = count($tables);
  359. if ($num_tables == 0) {
  360. $message = PhpMyAdmin\Message::error(
  361. __('No tables found in database.')
  362. );
  363. $active_page = 'db_export.php';
  364. include 'db_export.php';
  365. exit();
  366. }
  367. }
  368. list($html, $back_button, $refreshButton) = Export::getHtmlForDisplayedExportHeader(
  369. $export_type, $db, $table
  370. );
  371. echo $html;
  372. unset($html);
  373. } // end download
  374. }
  375. $relation = new Relation();
  376. // Fake loop just to allow skip of remain of this code by break, I'd really
  377. // need exceptions here :-)
  378. do {
  379. // Re - initialize
  380. $dump_buffer = '';
  381. $dump_buffer_len = 0;
  382. // Add possibly some comments to export
  383. if (! $export_plugin->exportHeader()) {
  384. break;
  385. }
  386. // Will we need relation & co. setup?
  387. $do_relation = isset($GLOBALS[$what . '_relation']);
  388. $do_comments = isset($GLOBALS[$what . '_include_comments'])
  389. || isset($GLOBALS[$what . '_comments']);
  390. $do_mime = isset($GLOBALS[$what . '_mime']);
  391. if ($do_relation || $do_comments || $do_mime) {
  392. $cfgRelation = $relation->getRelationsParam();
  393. }
  394. // Include dates in export?
  395. $do_dates = isset($GLOBALS[$what . '_dates']);
  396. $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
  397. /**
  398. * Builds the dump
  399. */
  400. if ($export_type == 'server') {
  401. if (! isset($db_select)) {
  402. $db_select = '';
  403. }
  404. Export::exportServer(
  405. $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
  406. $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
  407. $aliases, $separate_files
  408. );
  409. } elseif ($export_type == 'database') {
  410. if (!isset($table_structure) || !is_array($table_structure)) {
  411. $table_structure = array();
  412. }
  413. if (!isset($table_data) || !is_array($table_data)) {
  414. $table_data = array();
  415. }
  416. if (!empty($_POST['structure_or_data_forced'])) {
  417. $table_structure = $tables;
  418. $table_data = $tables;
  419. }
  420. if (isset($lock_tables)) {
  421. Export::lockTables($db, $tables, "READ");
  422. try {
  423. Export::exportDatabase(
  424. $db, $tables, $whatStrucOrData, $table_structure,
  425. $table_data, $export_plugin, $crlf, $err_url, $export_type,
  426. $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
  427. $separate_files
  428. );
  429. } finally {
  430. Export::unlockTables();
  431. }
  432. } else {
  433. Export::exportDatabase(
  434. $db, $tables, $whatStrucOrData, $table_structure, $table_data,
  435. $export_plugin, $crlf, $err_url, $export_type, $do_relation,
  436. $do_comments, $do_mime, $do_dates, $aliases, $separate_files
  437. );
  438. }
  439. } else {
  440. // We export just one table
  441. // $allrows comes from the form when "Dump all rows" has been selected
  442. if (! isset($allrows)) {
  443. $allrows = '';
  444. }
  445. if (! isset($limit_to)) {
  446. $limit_to = 0;
  447. }
  448. if (! isset($limit_from)) {
  449. $limit_from = 0;
  450. }
  451. if (isset($lock_tables)) {
  452. try {
  453. Export::lockTables($db, array($table), "READ");
  454. Export::exportTable(
  455. $db, $table, $whatStrucOrData, $export_plugin, $crlf,
  456. $err_url, $export_type, $do_relation, $do_comments,
  457. $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
  458. $sql_query, $aliases
  459. );
  460. } finally {
  461. Export::unlockTables();
  462. }
  463. } else {
  464. Export::exportTable(
  465. $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
  466. $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
  467. $allrows, $limit_to, $limit_from, $sql_query, $aliases
  468. );
  469. }
  470. }
  471. if (! $export_plugin->exportFooter()) {
  472. break;
  473. }
  474. } while (false);
  475. // End of fake loop
  476. if ($save_on_server && ! empty($message)) {
  477. Export::showPage($db, $table, $export_type);
  478. }
  479. /**
  480. * Send the dump as a file...
  481. */
  482. if (empty($asfile)) {
  483. echo Export::getHtmlForDisplayedExportFooter($back_button, $refreshButton);
  484. return;
  485. } // end if
  486. // Convert the charset if required.
  487. if ($output_charset_conversion) {
  488. $dump_buffer = Encoding::convertString(
  489. 'utf-8',
  490. $GLOBALS['charset'],
  491. $dump_buffer
  492. );
  493. }
  494. // Compression needed?
  495. if ($compression) {
  496. if (! empty($separate_files)) {
  497. $dump_buffer = Export::compress(
  498. $dump_buffer_objects, $compression, $filename
  499. );
  500. } else {
  501. $dump_buffer = Export::compress($dump_buffer, $compression, $filename);
  502. }
  503. }
  504. /* If we saved on server, we have to close file now */
  505. if ($save_on_server) {
  506. $message = Export::closeFile(
  507. $file_handle, $dump_buffer, $save_filename
  508. );
  509. Export::showPage($db, $table, $export_type);
  510. } else {
  511. echo $dump_buffer;
  512. }