path fix for public images
[openemr.git] / phpmyadmin / export.php
blobe50f4fdd4c9f90e9a5987d733ef961a999b56eee
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
9 /**
10 * Get the variables sent or posted to this script and a core script
12 if (!defined('TESTSUITE')) {
13 /**
14 * If we are sending the export file (as opposed to just displaying it
15 * as text), we have to bypass the usual PMA_Response mechanism
17 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
18 define('PMA_BYPASS_GET_INSTANCE', 1);
20 include_once 'libraries/common.inc.php';
21 include_once 'libraries/zip.lib.php';
22 include_once 'libraries/plugin_interface.lib.php';
23 include_once 'libraries/export.lib.php';
25 //check if it's the GET request to check export time out
26 if (isset($_GET['check_time_out'])) {
27 if (isset($_SESSION['pma_export_error'])) {
28 $err = $_SESSION['pma_export_error'];
29 unset($_SESSION['pma_export_error']);
30 echo "timeout";
31 } else {
32 echo "success";
34 exit;
36 /**
37 * Sets globals from $_POST
39 * - Please keep the parameters in order of their appearance in the form
40 * - Some of these parameters are not used, as the code below directly
41 * verifies from the superglobal $_POST or $_REQUEST
43 $post_params = array(
44 'db',
45 'table',
46 'what',
47 'single_table',
48 'export_type',
49 'export_method',
50 'quick_or_custom',
51 'db_select',
52 'table_select',
53 'table_structure',
54 'table_data',
55 'limit_to',
56 'limit_from',
57 'allrows',
58 'lock_tables',
59 'output_format',
60 'filename_template',
61 'maxsize',
62 'remember_template',
63 'charset',
64 'compression',
65 'as_separate_files',
66 'knjenc',
67 'xkana',
68 'htmlword_structure_or_data',
69 'htmlword_null',
70 'htmlword_columns',
71 'mediawiki_headers',
72 'mediawiki_structure_or_data',
73 'mediawiki_caption',
74 'pdf_structure_or_data',
75 'odt_structure_or_data',
76 'odt_relation',
77 'odt_comments',
78 'odt_mime',
79 'odt_columns',
80 'odt_null',
81 'codegen_structure_or_data',
82 'codegen_format',
83 'excel_null',
84 'excel_removeCRLF',
85 'excel_columns',
86 'excel_edition',
87 'excel_structure_or_data',
88 'yaml_structure_or_data',
89 'ods_null',
90 'ods_structure_or_data',
91 'ods_columns',
92 'json_structure_or_data',
93 'json_pretty_print',
94 'xml_structure_or_data',
95 'xml_export_events',
96 'xml_export_functions',
97 'xml_export_procedures',
98 'xml_export_tables',
99 'xml_export_triggers',
100 'xml_export_views',
101 'xml_export_contents',
102 'texytext_structure_or_data',
103 'texytext_columns',
104 'texytext_null',
105 'phparray_structure_or_data',
106 'sql_include_comments',
107 'sql_header_comment',
108 'sql_dates',
109 'sql_relation',
110 'sql_mime',
111 'sql_use_transaction',
112 'sql_disable_fk',
113 'sql_compatibility',
114 'sql_structure_or_data',
115 'sql_create_database',
116 'sql_drop_table',
117 'sql_procedure_function',
118 'sql_create_table',
119 'sql_create_view',
120 'sql_create_trigger',
121 'sql_if_not_exists',
122 'sql_auto_increment',
123 'sql_backquotes',
124 'sql_truncate',
125 'sql_delayed',
126 'sql_ignore',
127 'sql_type',
128 'sql_insert_syntax',
129 'sql_max_query_size',
130 'sql_hex_for_binary',
131 'sql_utc_time',
132 'sql_drop_database',
133 'sql_views_as_tables',
134 'sql_metadata',
135 'csv_separator',
136 'csv_enclosed',
137 'csv_escaped',
138 'csv_terminated',
139 'csv_null',
140 'csv_removeCRLF',
141 'csv_columns',
142 'csv_structure_or_data',
143 // csv_replace should have been here but we use it directly from $_POST
144 'latex_caption',
145 'latex_structure_or_data',
146 'latex_structure_caption',
147 'latex_structure_continued_caption',
148 'latex_structure_label',
149 'latex_relation',
150 'latex_comments',
151 'latex_mime',
152 'latex_columns',
153 'latex_data_caption',
154 'latex_data_continued_caption',
155 'latex_data_label',
156 'latex_null',
157 'aliases'
160 foreach ($post_params as $one_post_param) {
161 if (isset($_POST[$one_post_param])) {
162 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
166 $table = $GLOBALS['table'];
168 PMA_Util::checkParameters(array('what', 'export_type'));
170 // sanitize this parameter which will be used below in a file inclusion
171 $what = PMA_securePath($_POST['what']);
173 // export class instance, not array of properties, as before
174 /* @var $export_plugin ExportPlugin */
175 $export_plugin = PMA_getPlugin(
176 "export",
177 $what,
178 'libraries/plugins/export/',
179 array(
180 'export_type' => $export_type,
181 'single_table' => isset($single_table)
185 // Backward compatibility
186 $type = $what;
188 // Check export type
189 if (empty($export_plugin)) {
190 PMA_fatalError(__('Bad type!'));
194 * valid compression methods
196 $compression_methods = array(
197 'zip',
198 'gzip'
202 * init and variable checking
204 $compression = false;
205 $onserver = false;
206 $save_on_server = false;
207 $buffer_needed = false;
208 $back_button = '';
209 $save_filename = '';
210 $file_handle = '';
211 $err_url = '';
212 $filename = '';
213 $separate_files = '';
215 // Is it a quick or custom export?
216 if (isset($_REQUEST['quick_or_custom']) && $_REQUEST['quick_or_custom'] == 'quick') {
217 $quick_export = true;
218 } else {
219 $quick_export = false;
222 if ($_REQUEST['output_format'] == 'astext') {
223 $asfile = false;
224 } else {
225 $asfile = true;
226 if (isset($_REQUEST['as_separate_files'])
227 && ! empty($_REQUEST['as_separate_files'])
229 if (isset($_REQUEST['compression'])
230 && ! empty($_REQUEST['compression'])
231 && $_REQUEST['compression'] == 'zip'
233 $separate_files = $_REQUEST['as_separate_files'];
236 if (in_array($_REQUEST['compression'], $compression_methods)) {
237 $compression = $_REQUEST['compression'];
238 $buffer_needed = true;
240 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
241 || (! $quick_export && ! empty($_REQUEST['onserver']))
243 if ($quick_export) {
244 $onserver = $_REQUEST['quick_export_onserver'];
245 } else {
246 $onserver = $_REQUEST['onserver'];
248 // Will we save dump on server?
249 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
253 // Generate error url and check for needed variables
254 /** @var PMA_String $pmaString */
255 $pmaString = $GLOBALS['PMA_String'];
256 if ($export_type == 'server') {
257 $err_url = 'server_export.php' . PMA_URL_getCommon();
258 } elseif ($export_type == 'database'
259 && /*overload*/mb_strlen($db)
261 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
262 // Check if we have something to export
263 if (isset($table_select)) {
264 $tables = $table_select;
265 } else {
266 $tables = array();
268 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
269 && /*overload*/mb_strlen($table)
271 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
272 array(
273 'db' => $db, 'table' => $table
276 } else {
277 PMA_fatalError(__('Bad parameters!'));
280 // Merge SQL Query aliases with Export aliases from
281 // export page, Export page aliases are given more
282 // preference over SQL Query aliases.
283 $parser = new SqlParser\Parser($sql_query);
284 $aliases = array();
285 if ((!empty($parser->statements[0]))
286 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
288 if (!empty($_REQUEST['aliases'])) {
289 $aliases = PMA_mergeAliases(
290 SqlParser\Utils\Misc::getAliases($parser->statements[0], $db),
291 $_REQUEST['aliases']
293 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
294 } else {
295 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
300 * Increase time limit for script execution and initializes some variables
302 @set_time_limit($cfg['ExecTimeLimit']);
303 if (! empty($cfg['MemoryLimit'])) {
304 @ini_set('memory_limit', $cfg['MemoryLimit']);
306 register_shutdown_function('PMA_shutdownDuringExport');
307 // Start with empty buffer
308 $dump_buffer = '';
309 $dump_buffer_len = 0;
311 // Array of dump_buffers - used in separate file exports
312 $dump_buffer_objects = array();
314 // We send fake headers to avoid browser timeout when buffering
315 $time_start = time();
317 // Defines the default <CR><LF> format.
318 // For SQL always use \n as MySQL wants this on all platforms.
319 if ($what == 'sql') {
320 $crlf = "\n";
321 } else {
322 $crlf = PMA_Util::whichCrlf();
325 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
326 && $type != 'xls';
328 // Do we need to convert charset?
329 $output_charset_conversion = $asfile
330 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
331 && isset($charset) && $charset != 'utf-8'
332 && $type != 'xls';
334 // Use on the fly compression?
335 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
336 && $compression == 'gzip';
337 if ($GLOBALS['onfly_compression']) {
338 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
341 // Generate filename and mime type if needed
342 if ($asfile) {
343 if (empty($remember_template)) {
344 $remember_template = '';
346 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
347 $export_type, $remember_template, $export_plugin, $compression,
348 $filename_template
350 } else {
351 $mime_type = '';
354 // Open file on server if needed
355 if ($save_on_server) {
356 list($save_filename, $message, $file_handle) = PMA_openExportFile(
357 $filename, $quick_export
360 // problem opening export file on server?
361 if (! empty($message)) {
362 PMA_showExportPage($db, $table, $export_type);
364 } else {
366 * Send headers depending on whether the user chose to download a dump file
367 * or not
369 if ($asfile) {
370 // Download
371 // (avoid rewriting data containing HTML with anchors and forms;
372 // this was reported to happen under Plesk)
373 @ini_set('url_rewriter.tags', '');
374 $filename = PMA_sanitizeFilename($filename);
376 PMA_downloadHeader($filename, $mime_type);
377 } else {
378 // HTML
379 if ($export_type == 'database') {
380 $num_tables = count($tables);
381 if ($num_tables == 0) {
382 $message = PMA_Message::error(
383 __('No tables found in database.')
385 $active_page = 'db_export.php';
386 include 'db_export.php';
387 exit();
390 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
391 $export_type, $db, $table
393 echo $html;
394 unset($html);
395 } // end download
398 // Fake loop just to allow skip of remain of this code by break, I'd really
399 // need exceptions here :-)
400 do {
401 // Re - initialize
402 $dump_buffer = '';
403 $dump_buffer_len = 0;
405 // Add possibly some comments to export
406 if (! $export_plugin->exportHeader()) {
407 break;
410 // Will we need relation & co. setup?
411 $do_relation = isset($GLOBALS[$what . '_relation']);
412 $do_comments = isset($GLOBALS[$what . '_include_comments'])
413 || isset($GLOBALS[$what . '_comments']) ;
414 $do_mime = isset($GLOBALS[$what . '_mime']);
415 if ($do_relation || $do_comments || $do_mime) {
416 $cfgRelation = PMA_getRelationsParam();
418 if ($do_mime) {
419 include_once 'libraries/transformations.lib.php';
422 // Include dates in export?
423 $do_dates = isset($GLOBALS[$what . '_dates']);
425 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
428 * Builds the dump
430 if ($export_type == 'server') {
431 if (! isset($db_select)) {
432 $db_select = '';
434 PMA_exportServer(
435 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
436 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
437 $aliases, $separate_files
439 } elseif ($export_type == 'database') {
440 if (!isset($table_structure) || !is_array($table_structure)) {
441 $table_structure = array();
443 if (!isset($table_data) || !is_array($table_data)) {
444 $table_data = array();
446 if (!empty($_REQUEST['structure_or_data_forced'])) {
447 $table_structure = $tables;
448 $table_data = $tables;
450 if (isset($lock_tables)) {
451 PMA_lockTables($db, $tables, "READ");
452 try {
453 PMA_exportDatabase(
454 $db, $tables, $whatStrucOrData, $table_structure,
455 $table_data, $export_plugin, $crlf, $err_url, $export_type,
456 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
457 $separate_files
459 PMA_unlockTables();
460 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
461 PMA_unlockTables();
462 throw $e;
464 } else {
465 PMA_exportDatabase(
466 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
467 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
468 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
471 } else {
472 // We export just one table
473 // $allrows comes from the form when "Dump all rows" has been selected
474 if (! isset($allrows)) {
475 $allrows = '';
477 if (! isset($limit_to)) {
478 $limit_to = 0;
480 if (! isset($limit_from)) {
481 $limit_from = 0;
483 if (isset($lock_tables)) {
484 try {
485 PMA_lockTables($db, array($table), "READ");
486 PMA_exportTable(
487 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
488 $err_url, $export_type, $do_relation, $do_comments,
489 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
490 $sql_query, $aliases
492 PMA_unlockTables();
493 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
494 PMA_unlockTables();
495 throw $e;
497 } else {
498 PMA_exportTable(
499 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
500 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
501 $allrows, $limit_to, $limit_from, $sql_query, $aliases
505 if (! $export_plugin->exportFooter()) {
506 break;
509 } while (false);
510 // End of fake loop
512 if ($save_on_server && ! empty($message)) {
513 PMA_showExportPage($db, $table, $export_type);
517 * Send the dump as a file...
519 if (empty($asfile)) {
520 echo PMA_getHtmlForDisplayedExportFooter($back_button);
521 return;
522 } // end if
524 // Convert the charset if required.
525 if ($output_charset_conversion) {
526 $dump_buffer = PMA_convertString(
527 'utf-8',
528 $GLOBALS['charset'],
529 $dump_buffer
533 // Compression needed?
534 if ($compression) {
535 if (! empty($separate_files)) {
536 $dump_buffer = PMA_compressExport(
537 $dump_buffer_objects, $compression, $filename
539 } else {
540 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
545 /* If we saved on server, we have to close file now */
546 if ($save_on_server) {
547 $message = PMA_closeExportFile(
548 $file_handle, $dump_buffer, $save_filename
550 PMA_showExportPage($db, $table, $export_type);
551 } else {
552 echo $dump_buffer;