Translated using Weblate (Slovenian)
[phpmyadmin.git] / export.php
blob3d4a625daf0678cb1ed576f7748c3c5c660ca1fd
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 'csv_separator',
132 'csv_enclosed',
133 'csv_escaped',
134 'csv_terminated',
135 'csv_null',
136 'csv_removeCRLF',
137 'csv_columns',
138 'csv_structure_or_data',
139 // csv_replace should have been here but we use it directly from $_POST
140 'latex_caption',
141 'latex_structure_or_data',
142 'latex_structure_caption',
143 'latex_structure_continued_caption',
144 'latex_structure_label',
145 'latex_relation',
146 'latex_comments',
147 'latex_mime',
148 'latex_columns',
149 'latex_data_caption',
150 'latex_data_continued_caption',
151 'latex_data_label',
152 'latex_null',
153 'aliases'
156 foreach ($post_params as $one_post_param) {
157 if (isset($_POST[$one_post_param])) {
158 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
162 $table = $GLOBALS['table'];
163 // sanitize this parameter which will be used below in a file inclusion
164 $what = PMA_securePath($_POST['what']);
166 PMA_Util::checkParameters(array('what', 'export_type'));
168 // export class instance, not array of properties, as before
169 $export_plugin = PMA_getPlugin(
170 "export",
171 $what,
172 'libraries/plugins/export/',
173 array(
174 'export_type' => $export_type,
175 'single_table' => isset($single_table)
179 // Backward compatibility
180 $type = $what;
182 // Check export type
183 if (! isset($export_plugin)) {
184 PMA_fatalError(__('Bad type!'));
187 // Avoid warning from PHP Analyzer
188 if (is_null($export_plugin)) {
189 $export_plugin = new stdClass();
193 * valid compression methods
195 $compression_methods = array(
196 'zip',
197 'gzip'
201 * init and variable checking
203 $compression = false;
204 $onserver = false;
205 $save_on_server = false;
206 $buffer_needed = false;
207 $back_button = '';
208 $save_filename = '';
209 $file_handle = '';
210 $err_url = '';
211 $filename = '';
213 // Is it a quick or custom export?
214 if ($_REQUEST['quick_or_custom'] == 'quick') {
215 $quick_export = true;
216 } else {
217 $quick_export = false;
220 if ($_REQUEST['output_format'] == 'astext') {
221 $asfile = false;
222 } else {
223 $asfile = true;
224 if (in_array($_REQUEST['compression'], $compression_methods)) {
225 $compression = $_REQUEST['compression'];
226 $buffer_needed = true;
228 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
229 || (! $quick_export && ! empty($_REQUEST['onserver']))
231 if ($quick_export) {
232 $onserver = $_REQUEST['quick_export_onserver'];
233 } else {
234 $onserver = $_REQUEST['onserver'];
236 // Will we save dump on server?
237 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
241 // Generate error url and check for needed variables
242 /** @var PMA_String $pmaString */
243 $pmaString = $GLOBALS['PMA_String'];
244 if ($export_type == 'server') {
245 $err_url = 'server_export.php' . PMA_URL_getCommon();
246 } elseif ($export_type == 'database'
247 && /*overload*/mb_strlen($db)
249 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
250 // Check if we have something to export
251 if (isset($table_select)) {
252 $tables = $table_select;
253 } else {
254 $tables = array();
256 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
257 && /*overload*/mb_strlen($table)
259 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
260 array(
261 'db' => $db, 'table' => $table
264 } else {
265 PMA_fatalError(__('Bad parameters!'));
268 // Merge SQL Query aliases with Export aliases from
269 // export page, Export page aliases are given more
270 // preference over SQL Query aliases.
271 if (!empty($_REQUEST['aliases'])) {
272 $aliases = PMA_mergeAliases(
273 PMA_SQP_getAliasesFromQuery($sql_query, $db),
274 $_REQUEST['aliases']
276 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
277 } else {
278 $aliases = PMA_SQP_getAliasesFromQuery($sql_query, $db);
282 * Increase time limit for script execution and initializes some variables
284 @set_time_limit($cfg['ExecTimeLimit']);
285 if (! empty($cfg['MemoryLimit'])) {
286 @ini_set('memory_limit', $cfg['MemoryLimit']);
288 register_shutdown_function('PMA_shutdownDuringExport');
289 // Start with empty buffer
290 $dump_buffer = '';
291 $dump_buffer_len = 0;
293 // We send fake headers to avoid browser timeout when buffering
294 $time_start = time();
296 // Defines the default <CR><LF> format.
297 // For SQL always use \n as MySQL wants this on all platforms.
298 if ($what == 'sql') {
299 $crlf = "\n";
300 } else {
301 $crlf = PMA_Util::whichCrlf();
304 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
305 && $type != 'xls';
307 // Do we need to convert charset?
308 $output_charset_conversion = $asfile
309 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
310 && isset($charset_of_file) && $charset_of_file != 'utf-8'
311 && $type != 'xls';
313 // Use on the fly compression?
314 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
315 && $compression == 'gzip';
316 if ($GLOBALS['onfly_compression']) {
317 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
320 // Generate filename and mime type if needed
321 if ($asfile) {
322 if (empty($remember_template)) {
323 $remember_template = '';
325 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
326 $export_type, $remember_template, $export_plugin, $compression,
327 $filename_template
329 } else {
330 $mime_type = '';
333 // Open file on server if needed
334 if ($save_on_server) {
335 list($save_filename, $message, $file_handle) = PMA_openExportFile(
336 $filename, $quick_export
339 // problem opening export file on server?
340 if (! empty($message)) {
341 PMA_showExportPage($db, $table, $export_type);
343 } else {
345 * Send headers depending on whether the user chose to download a dump file
346 * or not
348 if ($asfile) {
349 // Download
350 // (avoid rewriting data containing HTML with anchors and forms;
351 // this was reported to happen under Plesk)
352 @ini_set('url_rewriter.tags', '');
353 $filename = PMA_sanitizeFilename($filename);
355 PMA_downloadHeader($filename, $mime_type);
356 } else {
357 // HTML
358 if ($export_type == 'database') {
359 $num_tables = count($tables);
360 if ($num_tables == 0) {
361 $message = PMA_Message::error(
362 __('No tables found in database.')
364 $active_page = 'db_export.php';
365 include 'db_export.php';
366 exit();
369 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
370 $export_type, $db, $table
372 echo $html;
373 unset($html);
374 } // end download
377 // Fake loop just to allow skip of remain of this code by break, I'd really
378 // need exceptions here :-)
379 do {
381 // Add possibly some comments to export
382 if (! $export_plugin->exportHeader($db)) {
383 break;
386 // Will we need relation & co. setup?
387 $do_relation = isset($GLOBALS[$what . '_relation']);
388 $do_comments = isset($GLOBALS[$what . '_include_comments'])
389 || isset($GLOBALS[$what . '_comments']) ;
390 $do_mime = isset($GLOBALS[$what . '_mime']);
391 if ($do_relation || $do_comments || $do_mime) {
392 $cfgRelation = PMA_getRelationsParam();
394 if ($do_mime) {
395 include_once 'libraries/transformations.lib.php';
398 // Include dates in export?
399 $do_dates = isset($GLOBALS[$what . '_dates']);
401 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
404 * Builds the dump
406 if ($export_type == 'server') {
407 if (! isset($db_select)) {
408 $db_select = '';
410 PMA_exportServer(
411 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
412 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
413 $aliases
415 } elseif ($export_type == 'database') {
416 if (isset($lock_tables)) {
417 PMA_lockTables($db, $tables, "READ");
418 try {
419 PMA_exportDatabase(
420 $db, $tables, $whatStrucOrData, $export_plugin, $crlf,
421 $err_url, $export_type, $do_relation, $do_comments,
422 $do_mime, $do_dates, $aliases
424 PMA_unlockTables();
425 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
426 PMA_unlockTables();
427 throw $e;
429 } else {
430 PMA_exportDatabase(
431 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
432 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
433 $aliases
436 } else {
437 // We export just one table
438 // $allrows comes from the form when "Dump all rows" has been selected
439 if (! isset($allrows)) {
440 $allrows = '';
442 if (! isset($limit_to)) {
443 $limit_to = 0;
445 if (! isset($limit_from)) {
446 $limit_from = 0;
448 if (isset($lock_tables)) {
449 try {
450 PMA_lockTables($db, array($table), "READ");
451 PMA_exportTable(
452 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
453 $err_url, $export_type, $do_relation, $do_comments,
454 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
455 $sql_query, $aliases
457 PMA_unlockTables();
458 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
459 PMA_unlockTables();
460 throw $e;
462 } else {
463 PMA_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
470 if (! $export_plugin->exportFooter()) {
471 break;
474 } while (false);
475 // End of fake loop
477 if ($save_on_server && ! empty($message)) {
478 PMA_showExportPage($db, $table, $export_type);
482 * Send the dump as a file...
484 if (! empty($asfile)) {
485 // Convert the charset if required.
486 if ($output_charset_conversion) {
487 $dump_buffer = PMA_convertString(
488 'utf-8',
489 $GLOBALS['charset_of_file'],
490 $dump_buffer
494 // Compression needed?
495 if ($compression) {
496 $dump_buffer
497 = PMA_compressExport($dump_buffer, $compression, $filename);
500 /* If we saved on server, we have to close file now */
501 if ($save_on_server) {
502 $message = PMA_closeExportFile(
503 $file_handle, $dump_buffer, $save_filename
505 PMA_showExportPage($db, $table, $export_type);
506 } else {
507 echo $dump_buffer;
509 } else {
510 echo PMA_getHtmlForDisplayedExportFooter($back_button);
511 } // end if