Translated using Weblate (Slovenian)
[phpmyadmin.git] / export.php
blobabbfd782b8f9dfa933cf566a1608983a2ddc25cd
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 'single_table',
47 'export_type',
48 'export_method',
49 'quick_or_custom',
50 'db_select',
51 'table_select',
52 'limit_to',
53 'limit_from',
54 'allrows',
55 'lock_tables',
56 'output_format',
57 'filename_template',
58 'maxsize',
59 'remember_template',
60 'charset_of_file',
61 'compression',
62 'knjenc',
63 'xkana',
64 'htmlword_structure_or_data',
65 'htmlword_null',
66 'htmlword_columns',
67 'mediawiki_headers',
68 'mediawiki_structure_or_data',
69 'mediawiki_caption',
70 'pdf_structure_or_data',
71 'odt_structure_or_data',
72 'odt_relation',
73 'odt_comments',
74 'odt_mime',
75 'odt_columns',
76 'odt_null',
77 'codegen_structure_or_data',
78 'codegen_format',
79 'excel_null',
80 'excel_removeCRLF',
81 'excel_columns',
82 'excel_edition',
83 'excel_structure_or_data',
84 'yaml_structure_or_data',
85 'ods_null',
86 'ods_structure_or_data',
87 'ods_columns',
88 'json_structure_or_data',
89 'json_pretty_print',
90 'xml_structure_or_data',
91 'xml_export_events',
92 'xml_export_functions',
93 'xml_export_procedures',
94 'xml_export_tables',
95 'xml_export_triggers',
96 'xml_export_views',
97 'xml_export_contents',
98 'texytext_structure_or_data',
99 'texytext_columns',
100 'texytext_null',
101 'phparray_structure_or_data',
102 'sql_include_comments',
103 'sql_header_comment',
104 'sql_dates',
105 'sql_relation',
106 'sql_mime',
107 'sql_use_transaction',
108 'sql_disable_fk',
109 'sql_compatibility',
110 'sql_structure_or_data',
111 'sql_create_database',
112 'sql_drop_table',
113 'sql_procedure_function',
114 'sql_create_table_statements',
115 'sql_create_table',
116 'sql_create_view',
117 'sql_create_trigger',
118 'sql_if_not_exists',
119 'sql_auto_increment',
120 'sql_backquotes',
121 'sql_truncate',
122 'sql_delayed',
123 'sql_ignore',
124 'sql_type',
125 'sql_insert_syntax',
126 'sql_max_query_size',
127 'sql_hex_for_binary',
128 'sql_utc_time',
129 'sql_drop_database',
130 'sql_views_as_tables',
131 'sql_metadata',
132 'csv_separator',
133 'csv_enclosed',
134 'csv_escaped',
135 'csv_terminated',
136 'csv_null',
137 'csv_removeCRLF',
138 'csv_columns',
139 'csv_structure_or_data',
140 // csv_replace should have been here but we use it directly from $_POST
141 'latex_caption',
142 'latex_structure_or_data',
143 'latex_structure_caption',
144 'latex_structure_continued_caption',
145 'latex_structure_label',
146 'latex_relation',
147 'latex_comments',
148 'latex_mime',
149 'latex_columns',
150 'latex_data_caption',
151 'latex_data_continued_caption',
152 'latex_data_label',
153 'latex_null',
154 'aliases'
157 foreach ($post_params as $one_post_param) {
158 if (isset($_POST[$one_post_param])) {
159 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
163 $table = $GLOBALS['table'];
164 // sanitize this parameter which will be used below in a file inclusion
165 $what = PMA_securePath($_POST['what']);
167 PMA_Util::checkParameters(array('what', 'export_type'));
169 // export class instance, not array of properties, as before
170 $export_plugin = PMA_getPlugin(
171 "export",
172 $what,
173 'libraries/plugins/export/',
174 array(
175 'export_type' => $export_type,
176 'single_table' => isset($single_table)
180 // Backward compatibility
181 $type = $what;
183 // Check export type
184 if (! isset($export_plugin)) {
185 PMA_fatalError(__('Bad type!'));
188 // Avoid warning from PHP Analyzer
189 if (is_null($export_plugin)) {
190 $export_plugin = new stdClass();
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 = '';
214 // Is it a quick or custom export?
215 if ($_REQUEST['quick_or_custom'] == 'quick') {
216 $quick_export = true;
217 } else {
218 $quick_export = false;
221 if ($_REQUEST['output_format'] == 'astext') {
222 $asfile = false;
223 } else {
224 $asfile = true;
225 if (in_array($_REQUEST['compression'], $compression_methods)) {
226 $compression = $_REQUEST['compression'];
227 $buffer_needed = true;
229 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
230 || (! $quick_export && ! empty($_REQUEST['onserver']))
232 if ($quick_export) {
233 $onserver = $_REQUEST['quick_export_onserver'];
234 } else {
235 $onserver = $_REQUEST['onserver'];
237 // Will we save dump on server?
238 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
242 // Generate error url and check for needed variables
243 /** @var PMA_String $pmaString */
244 $pmaString = $GLOBALS['PMA_String'];
245 if ($export_type == 'server') {
246 $err_url = 'server_export.php' . PMA_URL_getCommon();
247 } elseif ($export_type == 'database'
248 && /*overload*/mb_strlen($db)
250 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
251 // Check if we have something to export
252 if (isset($table_select)) {
253 $tables = $table_select;
254 } else {
255 $tables = array();
257 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
258 && /*overload*/mb_strlen($table)
260 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
261 array(
262 'db' => $db, 'table' => $table
265 } else {
266 PMA_fatalError(__('Bad parameters!'));
269 // Merge SQL Query aliases with Export aliases from
270 // export page, Export page aliases are given more
271 // preference over SQL Query aliases.
272 if (!empty($_REQUEST['aliases'])) {
273 $aliases = PMA_mergeAliases(
274 PMA_SQP_getAliasesFromQuery($sql_query, $db),
275 $_REQUEST['aliases']
277 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
278 } else {
279 $aliases = PMA_SQP_getAliasesFromQuery($sql_query, $db);
283 * Increase time limit for script execution and initializes some variables
285 @set_time_limit($cfg['ExecTimeLimit']);
286 if (! empty($cfg['MemoryLimit'])) {
287 @ini_set('memory_limit', $cfg['MemoryLimit']);
289 register_shutdown_function('PMA_shutdownDuringExport');
290 // Start with empty buffer
291 $dump_buffer = '';
292 $dump_buffer_len = 0;
294 // We send fake headers to avoid browser timeout when buffering
295 $time_start = time();
297 // Defines the default <CR><LF> format.
298 // For SQL always use \n as MySQL wants this on all platforms.
299 if ($what == 'sql') {
300 $crlf = "\n";
301 } else {
302 $crlf = PMA_Util::whichCrlf();
305 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
306 && $type != 'xls';
308 // Do we need to convert charset?
309 $output_charset_conversion = $asfile
310 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
311 && isset($charset_of_file) && $charset_of_file != 'utf-8'
312 && $type != 'xls';
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'] = PMA_getMemoryLimitForExport();
321 // Generate filename and mime type if needed
322 if ($asfile) {
323 if (empty($remember_template)) {
324 $remember_template = '';
326 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
327 $export_type, $remember_template, $export_plugin, $compression,
328 $filename_template
330 } else {
331 $mime_type = '';
334 // Open file on server if needed
335 if ($save_on_server) {
336 list($save_filename, $message, $file_handle) = PMA_openExportFile(
337 $filename, $quick_export
340 // problem opening export file on server?
341 if (! empty($message)) {
342 PMA_showExportPage($db, $table, $export_type);
344 } else {
346 * Send headers depending on whether the user chose to download a dump file
347 * or not
349 if ($asfile) {
350 // Download
351 // (avoid rewriting data containing HTML with anchors and forms;
352 // this was reported to happen under Plesk)
353 @ini_set('url_rewriter.tags', '');
354 $filename = PMA_sanitizeFilename($filename);
356 PMA_downloadHeader($filename, $mime_type);
357 } else {
358 // HTML
359 if ($export_type == 'database') {
360 $num_tables = count($tables);
361 if ($num_tables == 0) {
362 $message = PMA_Message::error(
363 __('No tables found in database.')
365 $active_page = 'db_export.php';
366 include 'db_export.php';
367 exit();
370 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
371 $export_type, $db, $table
373 echo $html;
374 unset($html);
375 } // end download
378 // Fake loop just to allow skip of remain of this code by break, I'd really
379 // need exceptions here :-)
380 do {
382 // Add possibly some comments to export
383 if (! $export_plugin->exportHeader($db)) {
384 break;
387 // Will we need relation & co. setup?
388 $do_relation = isset($GLOBALS[$what . '_relation']);
389 $do_comments = isset($GLOBALS[$what . '_include_comments'])
390 || isset($GLOBALS[$what . '_comments']) ;
391 $do_mime = isset($GLOBALS[$what . '_mime']);
392 if ($do_relation || $do_comments || $do_mime) {
393 $cfgRelation = PMA_getRelationsParam();
395 if ($do_mime) {
396 include_once 'libraries/transformations.lib.php';
399 // Include dates in export?
400 $do_dates = isset($GLOBALS[$what . '_dates']);
402 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
405 * Builds the dump
407 if ($export_type == 'server') {
408 if (! isset($db_select)) {
409 $db_select = '';
411 PMA_exportServer(
412 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
413 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
414 $aliases
416 } elseif ($export_type == 'database') {
417 if (isset($lock_tables)) {
418 PMA_lockTables($db, $tables, "READ");
419 try {
420 PMA_exportDatabase(
421 $db, $tables, $whatStrucOrData, $export_plugin, $crlf,
422 $err_url, $export_type, $do_relation, $do_comments,
423 $do_mime, $do_dates, $aliases
425 PMA_unlockTables();
426 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
427 PMA_unlockTables();
428 throw $e;
430 } else {
431 PMA_exportDatabase(
432 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
433 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
434 $aliases
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 = '';
443 if (! isset($limit_to)) {
444 $limit_to = 0;
446 if (! isset($limit_from)) {
447 $limit_from = 0;
449 if (isset($lock_tables)) {
450 try {
451 PMA_lockTables($db, array($table), "READ");
452 PMA_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
458 PMA_unlockTables();
459 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
460 PMA_unlockTables();
461 throw $e;
463 } else {
464 PMA_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
471 if (! $export_plugin->exportFooter()) {
472 break;
475 } while (false);
476 // End of fake loop
478 if ($save_on_server && ! empty($message)) {
479 PMA_showExportPage($db, $table, $export_type);
483 * Send the dump as a file...
485 if (! empty($asfile)) {
486 // Convert the charset if required.
487 if ($output_charset_conversion) {
488 $dump_buffer = PMA_convertString(
489 'utf-8',
490 $GLOBALS['charset_of_file'],
491 $dump_buffer
495 // Compression needed?
496 if ($compression) {
497 $dump_buffer
498 = PMA_compressExport($dump_buffer, $compression, $filename);
501 /* If we saved on server, we have to close file now */
502 if ($save_on_server) {
503 $message = PMA_closeExportFile(
504 $file_handle, $dump_buffer, $save_filename
506 PMA_showExportPage($db, $table, $export_type);
507 } else {
508 echo $dump_buffer;
510 } else {
511 echo PMA_getHtmlForDisplayedExportFooter($back_button);
512 } // end if