export.php 16 KB

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