export.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Main export handling code
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Get the variables sent or posted to this script and a core script
  10. */
  11. if (!defined('TESTSUITE')) {
  12. /**
  13. * If we are sending the export file (as opposed to just displaying it
  14. * as text), we have to bypass the usual PMA_Response mechanism
  15. */
  16. if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
  17. define('PMA_BYPASS_GET_INSTANCE', 1);
  18. }
  19. include_once 'libraries/common.inc.php';
  20. include_once 'libraries/zip.lib.php';
  21. include_once 'libraries/plugin_interface.lib.php';
  22. include_once 'libraries/export.lib.php';
  23. //check if it's the GET request to check export time out
  24. if (isset($_GET['check_time_out'])) {
  25. if (isset($_SESSION['pma_export_error'])) {
  26. $err = $_SESSION['pma_export_error'];
  27. unset($_SESSION['pma_export_error']);
  28. echo "timeout";
  29. } else {
  30. echo "success";
  31. }
  32. exit;
  33. }
  34. /**
  35. * Sets globals from $_POST
  36. *
  37. * - Please keep the parameters in order of their appearance in the form
  38. * - Some of these parameters are not used, as the code below directly
  39. * verifies from the superglobal $_POST or $_REQUEST
  40. */
  41. $post_params = array(
  42. 'db',
  43. 'table',
  44. 'single_table',
  45. 'export_type',
  46. 'export_method',
  47. 'quick_or_custom',
  48. 'db_select',
  49. 'table_select',
  50. 'limit_to',
  51. 'limit_from',
  52. 'allrows',
  53. 'output_format',
  54. 'filename_template',
  55. 'maxsize',
  56. 'remember_template',
  57. 'charset_of_file',
  58. 'compression',
  59. 'what',
  60. 'knjenc',
  61. 'xkana',
  62. 'htmlword_structure_or_data',
  63. 'htmlword_null',
  64. 'htmlword_columns',
  65. 'mediawiki_headers',
  66. 'mediawiki_structure_or_data',
  67. 'mediawiki_caption',
  68. 'pdf_report_title',
  69. 'pdf_structure_or_data',
  70. 'odt_structure_or_data',
  71. 'odt_relation',
  72. 'odt_comments',
  73. 'odt_mime',
  74. 'odt_columns',
  75. 'odt_null',
  76. 'codegen_structure_or_data',
  77. 'codegen_format',
  78. 'excel_null',
  79. 'excel_removeCRLF',
  80. 'excel_columns',
  81. 'excel_edition',
  82. 'excel_structure_or_data',
  83. 'yaml_structure_or_data',
  84. 'ods_null',
  85. 'ods_structure_or_data',
  86. 'ods_columns',
  87. 'json_structure_or_data',
  88. 'xml_structure_or_data',
  89. 'xml_export_functions',
  90. 'xml_export_procedures',
  91. 'xml_export_tables',
  92. 'xml_export_triggers',
  93. 'xml_export_views',
  94. 'xml_export_contents',
  95. 'texytext_structure_or_data',
  96. 'texytext_columns',
  97. 'texytext_null',
  98. 'phparray_structure_or_data',
  99. 'sql_include_comments',
  100. 'sql_header_comment',
  101. 'sql_dates',
  102. 'sql_relation',
  103. 'sql_mime',
  104. 'sql_use_transaction',
  105. 'sql_disable_fk',
  106. 'sql_compatibility',
  107. 'sql_structure_or_data',
  108. 'sql_create_database',
  109. 'sql_drop_table',
  110. 'sql_procedure_function',
  111. 'sql_create_table_statements',
  112. 'sql_create_table',
  113. 'sql_create_view',
  114. 'sql_create_trigger',
  115. 'sql_if_not_exists',
  116. 'sql_auto_increment',
  117. 'sql_backquotes',
  118. 'sql_truncate',
  119. 'sql_delayed',
  120. 'sql_ignore',
  121. 'sql_type',
  122. 'sql_insert_syntax',
  123. 'sql_max_query_size',
  124. 'sql_hex_for_binary',
  125. 'sql_utc_time',
  126. 'sql_drop_database',
  127. 'sql_views_as_tables',
  128. 'csv_separator',
  129. 'csv_enclosed',
  130. 'csv_escaped',
  131. 'csv_terminated',
  132. 'csv_null',
  133. 'csv_removeCRLF',
  134. 'csv_columns',
  135. 'csv_structure_or_data',
  136. // csv_replace should have been here but we use it directly from $_POST
  137. 'latex_caption',
  138. 'latex_structure_or_data',
  139. 'latex_structure_caption',
  140. 'latex_structure_continued_caption',
  141. 'latex_structure_label',
  142. 'latex_relation',
  143. 'latex_comments',
  144. 'latex_mime',
  145. 'latex_columns',
  146. 'latex_data_caption',
  147. 'latex_data_continued_caption',
  148. 'latex_data_label',
  149. 'latex_null'
  150. );
  151. foreach ($post_params as $one_post_param) {
  152. if (isset($_POST[$one_post_param])) {
  153. $GLOBALS[$one_post_param] = $_POST[$one_post_param];
  154. }
  155. }
  156. // sanitize this parameter which will be used below in a file inclusion
  157. $what = PMA_securePath($what);
  158. PMA_Util::checkParameters(array('what', 'export_type'));
  159. // export class instance, not array of properties, as before
  160. $export_plugin = PMA_getPlugin(
  161. "export",
  162. $what,
  163. 'libraries/plugins/export/',
  164. array(
  165. 'export_type' => $export_type,
  166. 'single_table' => isset($single_table)
  167. )
  168. );
  169. // Backward compatibility
  170. $type = $what;
  171. // Check export type
  172. if (! isset($export_plugin)) {
  173. PMA_fatalError(__('Bad type!'));
  174. }
  175. /**
  176. * valid compression methods
  177. */
  178. $compression_methods = array(
  179. 'zip',
  180. 'gzip'
  181. );
  182. /**
  183. * init and variable checking
  184. */
  185. $compression = false;
  186. $onserver = false;
  187. $save_on_server = false;
  188. $buffer_needed = false;
  189. // Is it a quick or custom export?
  190. if ($_REQUEST['quick_or_custom'] == 'quick') {
  191. $quick_export = true;
  192. } else {
  193. $quick_export = false;
  194. }
  195. if ($_REQUEST['output_format'] == 'astext') {
  196. $asfile = false;
  197. } else {
  198. $asfile = true;
  199. if (in_array($_REQUEST['compression'], $compression_methods)) {
  200. $compression = $_REQUEST['compression'];
  201. $buffer_needed = true;
  202. }
  203. if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
  204. || (! $quick_export && ! empty($_REQUEST['onserver']))
  205. ) {
  206. if ($quick_export) {
  207. $onserver = $_REQUEST['quick_export_onserver'];
  208. } else {
  209. $onserver = $_REQUEST['onserver'];
  210. }
  211. // Will we save dump on server?
  212. $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
  213. }
  214. }
  215. // Generate error url and check for needed variables
  216. if ($export_type == 'server') {
  217. $err_url = 'server_export.php?' . PMA_URL_getCommon();
  218. } elseif ($export_type == 'database' && strlen($db)) {
  219. $err_url = 'db_export.php?' . PMA_URL_getCommon($db);
  220. // Check if we have something to export
  221. if (isset($table_select)) {
  222. $tables = $table_select;
  223. } else {
  224. $tables = array();
  225. }
  226. } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
  227. $err_url = 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
  228. } else {
  229. PMA_fatalError(__('Bad parameters!'));
  230. }
  231. /**
  232. * Increase time limit for script execution and initializes some variables
  233. */
  234. @set_time_limit($cfg['ExecTimeLimit']);
  235. if (! empty($cfg['MemoryLimit'])) {
  236. @ini_set('memory_limit', $cfg['MemoryLimit']);
  237. }
  238. register_shutdown_function('PMA_shutdownDuringExport');
  239. // Start with empty buffer
  240. $dump_buffer = '';
  241. $dump_buffer_len = 0;
  242. // We send fake headers to avoid browser timeout when buffering
  243. $time_start = time();
  244. }
  245. // Defines the default <CR><LF> format.
  246. // For SQL always use \n as MySQL wants this on all platforms.
  247. if (!defined('TESTSUITE')) {
  248. if ($what == 'sql') {
  249. $crlf = "\n";
  250. } else {
  251. $crlf = PMA_Util::whichCrlf();
  252. }
  253. $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
  254. && $type != 'xls';
  255. // Do we need to convert charset?
  256. $output_charset_conversion = $asfile
  257. && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
  258. && isset($charset_of_file) && $charset_of_file != 'utf-8'
  259. && $type != 'xls';
  260. // Use on the fly compression?
  261. $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
  262. && $compression == 'gzip';
  263. if ($GLOBALS['onfly_compression']) {
  264. $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
  265. }
  266. // Generate filename and mime type if needed
  267. if ($asfile) {
  268. if (empty($remember_template)) {
  269. $remember_template = '';
  270. }
  271. list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
  272. $export_type, $remember_template, $export_plugin, $compression,
  273. $filename_template
  274. );
  275. }
  276. // Open file on server if needed
  277. if ($save_on_server) {
  278. list($save_filename, $message, $file_handle) = PMA_openExportFile(
  279. $filename, $quick_export
  280. );
  281. // problem opening export file on server?
  282. if (! empty($message)) {
  283. if ($export_type == 'server') {
  284. $active_page = 'server_export.php';
  285. include 'server_export.php';
  286. } elseif ($export_type == 'database') {
  287. $active_page = 'db_export.php';
  288. include 'db_export.php';
  289. } else {
  290. $active_page = 'tbl_export.php';
  291. include 'tbl_export.php';
  292. }
  293. exit();
  294. }
  295. }
  296. /**
  297. * Send headers depending on whether the user chose to download a dump file
  298. * or not
  299. */
  300. if (! $save_on_server) {
  301. if ($asfile) {
  302. // Download
  303. // (avoid rewriting data containing HTML with anchors and forms;
  304. // this was reported to happen under Plesk)
  305. @ini_set('url_rewriter.tags', '');
  306. $filename = PMA_sanitizeFilename($filename);
  307. PMA_downloadHeader($filename, $mime_type);
  308. } else {
  309. // HTML
  310. if ($export_type == 'database') {
  311. $num_tables = count($tables);
  312. if ($num_tables == 0) {
  313. $message = PMA_Message::error(
  314. __('No tables found in database.')
  315. );
  316. $active_page = 'db_export.php';
  317. include 'db_export.php';
  318. exit();
  319. }
  320. }
  321. list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
  322. $export_type, $db, $table
  323. );
  324. echo $html;
  325. unset($html);
  326. } // end download
  327. }
  328. // Fake loop just to allow skip of remain of this code by break, I'd really
  329. // need exceptions here :-)
  330. do {
  331. // Add possibly some comments to export
  332. if (! $export_plugin->exportHeader($db)) {
  333. break;
  334. }
  335. // Will we need relation & co. setup?
  336. $do_relation = isset($GLOBALS[$what . '_relation']);
  337. $do_comments = isset($GLOBALS[$what . '_include_comments'])
  338. || isset($GLOBALS[$what . '_comments']) ;
  339. $do_mime = isset($GLOBALS[$what . '_mime']);
  340. if ($do_relation || $do_comments || $do_mime) {
  341. $cfgRelation = PMA_getRelationsParam();
  342. }
  343. if ($do_mime) {
  344. include_once 'libraries/transformations.lib.php';
  345. }
  346. // Include dates in export?
  347. $do_dates = isset($GLOBALS[$what . '_dates']);
  348. $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
  349. /**
  350. * Builds the dump
  351. */
  352. if ($export_type == 'server') {
  353. if (! isset($db_select)) {
  354. $db_select = '';
  355. }
  356. PMA_exportServer(
  357. $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
  358. $export_type, $do_relation, $do_comments, $do_mime, $do_dates
  359. );
  360. } elseif ($export_type == 'database') {
  361. PMA_exportDatabase(
  362. $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
  363. $export_type, $do_relation, $do_comments, $do_mime, $do_dates
  364. );
  365. } else {
  366. // We export just one table
  367. // $allrows comes from the form when "Dump all rows" has been selected
  368. if (! isset($allrows)) {
  369. $allrows = '';
  370. }
  371. if (! isset($limit_to)) {
  372. $limit_to = 0;
  373. }
  374. if (! isset($limit_from)) {
  375. $limit_from = 0;
  376. }
  377. PMA_exportTable(
  378. $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
  379. $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
  380. $allrows, $limit_to, $limit_from, $sql_query
  381. );
  382. }
  383. if (! $export_plugin->exportFooter()) {
  384. break;
  385. }
  386. } while (false);
  387. // End of fake loop
  388. if ($save_on_server && ! empty($message)) {
  389. if ($export_type == 'server') {
  390. $active_page = 'server_export.php';
  391. include 'server_export.php';
  392. } elseif ($export_type == 'database') {
  393. $active_page = 'db_export.php';
  394. include 'db_export.php';
  395. } else {
  396. $active_page = 'tbl_export.php';
  397. include 'tbl_export.php';
  398. }
  399. exit();
  400. }
  401. /**
  402. * Send the dump as a file...
  403. */
  404. if (! empty($asfile)) {
  405. // Convert the charset if required.
  406. if ($output_charset_conversion) {
  407. $dump_buffer = PMA_convertString(
  408. 'utf-8',
  409. $GLOBALS['charset_of_file'],
  410. $dump_buffer
  411. );
  412. }
  413. // Compression needed?
  414. if ($compression) {
  415. $dump_buffer
  416. = PMA_compressExport($dump_buffer, $compression, $filename);
  417. }
  418. /* If we saved on server, we have to close file now */
  419. if ($save_on_server) {
  420. $message = PMA_closeExportFile(
  421. $file_handle, $dump_buffer, $save_filename
  422. );
  423. if ($export_type == 'server') {
  424. $active_page = 'server_export.php';
  425. include_once 'server_export.php';
  426. } elseif ($export_type == 'database') {
  427. $active_page = 'db_export.php';
  428. include_once 'db_export.php';
  429. } else {
  430. $active_page = 'tbl_export.php';
  431. include_once 'tbl_export.php';
  432. }
  433. exit();
  434. } else {
  435. echo $dump_buffer;
  436. }
  437. } else {
  438. echo PMA_getHtmlForDisplayedExportFooter($back_button);
  439. } // end if
  440. }
  441. ?>