export.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. }
  253. // Generate error url and check for needed variables
  254. if ($export_type == 'server') {
  255. $err_url = 'server_export.php' . Url::getCommon();
  256. } elseif ($export_type == 'database' && strlen($db) > 0) {
  257. $err_url = 'db_export.php' . Url::getCommon(array('db' => $db));
  258. // Check if we have something to export
  259. if (isset($table_select)) {
  260. $tables = $table_select;
  261. } else {
  262. $tables = array();
  263. }
  264. } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
  265. $err_url = 'tbl_export.php' . Url::getCommon(
  266. array(
  267. 'db' => $db, 'table' => $table
  268. )
  269. );
  270. } else {
  271. Core::fatalError(__('Bad parameters!'));
  272. }
  273. // Merge SQL Query aliases with Export aliases from
  274. // export page, Export page aliases are given more
  275. // preference over SQL Query aliases.
  276. $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
  277. $aliases = array();
  278. if ((!empty($parser->statements[0]))
  279. && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
  280. ) {
  281. $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
  282. }
  283. if (!empty($_POST['aliases'])) {
  284. $aliases = Export::mergeAliases($aliases, $_POST['aliases']);
  285. $_SESSION['tmpval']['aliases'] = $_POST['aliases'];
  286. }
  287. /**
  288. * Increase time limit for script execution and initializes some variables
  289. */
  290. Util::setTimeLimit();
  291. if (! empty($cfg['MemoryLimit'])) {
  292. ini_set('memory_limit', $cfg['MemoryLimit']);
  293. }
  294. register_shutdown_function('PhpMyAdmin\Export::shutdown');
  295. // Start with empty buffer
  296. $dump_buffer = '';
  297. $dump_buffer_len = 0;
  298. // Array of dump_buffers - used in separate file exports
  299. $dump_buffer_objects = array();
  300. // We send fake headers to avoid browser timeout when buffering
  301. $time_start = time();
  302. // Defines the default <CR><LF> format.
  303. // For SQL always use \n as MySQL wants this on all platforms.
  304. if ($what == 'sql') {
  305. $crlf = "\n";
  306. } else {
  307. $crlf = PHP_EOL;
  308. }
  309. $output_kanji_conversion = Encoding::canConvertKanji();
  310. // Do we need to convert charset?
  311. $output_charset_conversion = $asfile
  312. && Encoding::isSupported()
  313. && isset($charset) && $charset != 'utf-8';
  314. // Use on the fly compression?
  315. $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
  316. && $compression == 'gzip';
  317. if ($GLOBALS['onfly_compression']) {
  318. $GLOBALS['memory_limit'] = Export::getMemoryLimit();
  319. }
  320. // Generate filename and mime type if needed
  321. if ($asfile) {
  322. if (empty($remember_template)) {
  323. $remember_template = '';
  324. }
  325. list($filename, $mime_type) = Export::getFilenameAndMimetype(
  326. $export_type, $remember_template, $export_plugin, $compression,
  327. $filename_template
  328. );
  329. } else {
  330. $mime_type = '';
  331. }
  332. // Open file on server if needed
  333. if ($save_on_server) {
  334. list($save_filename, $message, $file_handle) = Export::openFile(
  335. $filename, $quick_export
  336. );
  337. // problem opening export file on server?
  338. if (! empty($message)) {
  339. Export::showPage($db, $table, $export_type);
  340. }
  341. } else {
  342. /**
  343. * Send headers depending on whether the user chose to download a dump file
  344. * or not
  345. */
  346. if ($asfile) {
  347. // Download
  348. // (avoid rewriting data containing HTML with anchors and forms;
  349. // this was reported to happen under Plesk)
  350. ini_set('url_rewriter.tags', '');
  351. $filename = Sanitize::sanitizeFilename($filename);
  352. Core::downloadHeader($filename, $mime_type);
  353. } else {
  354. // HTML
  355. if ($export_type == 'database') {
  356. $num_tables = count($tables);
  357. if ($num_tables == 0) {
  358. $message = PhpMyAdmin\Message::error(
  359. __('No tables found in database.')
  360. );
  361. $active_page = 'db_export.php';
  362. include 'db_export.php';
  363. exit();
  364. }
  365. }
  366. list($html, $back_button, $refreshButton) = Export::getHtmlForDisplayedExportHeader(
  367. $export_type, $db, $table
  368. );
  369. echo $html;
  370. unset($html);
  371. } // end download
  372. }
  373. $relation = new Relation();
  374. // Fake loop just to allow skip of remain of this code by break, I'd really
  375. // need exceptions here :-)
  376. do {
  377. // Re - initialize
  378. $dump_buffer = '';
  379. $dump_buffer_len = 0;
  380. // Add possibly some comments to export
  381. if (! $export_plugin->exportHeader()) {
  382. break;
  383. }
  384. // Will we need relation & co. setup?
  385. $do_relation = isset($GLOBALS[$what . '_relation']);
  386. $do_comments = isset($GLOBALS[$what . '_include_comments'])
  387. || isset($GLOBALS[$what . '_comments']);
  388. $do_mime = isset($GLOBALS[$what . '_mime']);
  389. if ($do_relation || $do_comments || $do_mime) {
  390. $cfgRelation = $relation->getRelationsParam();
  391. }
  392. // Include dates in export?
  393. $do_dates = isset($GLOBALS[$what . '_dates']);
  394. $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
  395. /**
  396. * Builds the dump
  397. */
  398. if ($export_type == 'server') {
  399. if (! isset($db_select)) {
  400. $db_select = '';
  401. }
  402. Export::exportServer(
  403. $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
  404. $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
  405. $aliases, $separate_files
  406. );
  407. } elseif ($export_type == 'database') {
  408. if (!isset($table_structure) || !is_array($table_structure)) {
  409. $table_structure = array();
  410. }
  411. if (!isset($table_data) || !is_array($table_data)) {
  412. $table_data = array();
  413. }
  414. if (!empty($_POST['structure_or_data_forced'])) {
  415. $table_structure = $tables;
  416. $table_data = $tables;
  417. }
  418. if (isset($lock_tables)) {
  419. Export::lockTables($db, $tables, "READ");
  420. try {
  421. Export::exportDatabase(
  422. $db, $tables, $whatStrucOrData, $table_structure,
  423. $table_data, $export_plugin, $crlf, $err_url, $export_type,
  424. $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
  425. $separate_files
  426. );
  427. } finally {
  428. Export::unlockTables();
  429. }
  430. } else {
  431. Export::exportDatabase(
  432. $db, $tables, $whatStrucOrData, $table_structure, $table_data,
  433. $export_plugin, $crlf, $err_url, $export_type, $do_relation,
  434. $do_comments, $do_mime, $do_dates, $aliases, $separate_files
  435. );
  436. }
  437. } else {
  438. // We export just one table
  439. // $allrows comes from the form when "Dump all rows" has been selected
  440. if (! isset($allrows)) {
  441. $allrows = '';
  442. }
  443. if (! isset($limit_to)) {
  444. $limit_to = 0;
  445. }
  446. if (! isset($limit_from)) {
  447. $limit_from = 0;
  448. }
  449. if (isset($lock_tables)) {
  450. try {
  451. Export::lockTables($db, array($table), "READ");
  452. Export::exportTable(
  453. $db, $table, $whatStrucOrData, $export_plugin, $crlf,
  454. $err_url, $export_type, $do_relation, $do_comments,
  455. $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
  456. $sql_query, $aliases
  457. );
  458. } finally {
  459. Export::unlockTables();
  460. }
  461. } else {
  462. Export::exportTable(
  463. $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
  464. $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
  465. $allrows, $limit_to, $limit_from, $sql_query, $aliases
  466. );
  467. }
  468. }
  469. if (! $export_plugin->exportFooter()) {
  470. break;
  471. }
  472. } while (false);
  473. // End of fake loop
  474. if ($save_on_server && ! empty($message)) {
  475. Export::showPage($db, $table, $export_type);
  476. }
  477. /**
  478. * Send the dump as a file...
  479. */
  480. if (empty($asfile)) {
  481. echo Export::getHtmlForDisplayedExportFooter($back_button, $refreshButton);
  482. return;
  483. } // end if
  484. // Convert the charset if required.
  485. if ($output_charset_conversion) {
  486. $dump_buffer = Encoding::convertString(
  487. 'utf-8',
  488. $GLOBALS['charset'],
  489. $dump_buffer
  490. );
  491. }
  492. // Compression needed?
  493. if ($compression) {
  494. if (! empty($separate_files)) {
  495. $dump_buffer = Export::compress(
  496. $dump_buffer_objects, $compression, $filename
  497. );
  498. } else {
  499. $dump_buffer = Export::compress($dump_buffer, $compression, $filename);
  500. }
  501. }
  502. /* If we saved on server, we have to close file now */
  503. if ($save_on_server) {
  504. $message = Export::closeFile(
  505. $file_handle, $dump_buffer, $save_filename
  506. );
  507. Export::showPage($db, $table, $export_type);
  508. } else {
  509. echo $dump_buffer;
  510. }