Upgrade phpmyadmin project (commit 2 of 3; This commits brings in phpmyadmin version...
[openemr.git] / phpmyadmin / export.php
blob5a236146b0087eb271bad7de952774e2bb8d7762
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 'output_format',
56 'filename_template',
57 'maxsize',
58 'remember_template',
59 'charset_of_file',
60 'compression',
61 'what',
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_report_title',
71 'pdf_structure_or_data',
72 'odt_structure_or_data',
73 'odt_relation',
74 'odt_comments',
75 'odt_mime',
76 'odt_columns',
77 'odt_null',
78 'codegen_structure_or_data',
79 'codegen_format',
80 'excel_null',
81 'excel_removeCRLF',
82 'excel_columns',
83 'excel_edition',
84 'excel_structure_or_data',
85 'yaml_structure_or_data',
86 'ods_null',
87 'ods_structure_or_data',
88 'ods_columns',
89 'json_structure_or_data',
90 'xml_structure_or_data',
91 'xml_export_functions',
92 'xml_export_procedures',
93 'xml_export_tables',
94 'xml_export_triggers',
95 'xml_export_views',
96 'xml_export_contents',
97 'texytext_structure_or_data',
98 'texytext_columns',
99 'texytext_null',
100 'phparray_structure_or_data',
101 'sql_include_comments',
102 'sql_header_comment',
103 'sql_dates',
104 'sql_relation',
105 'sql_mime',
106 'sql_use_transaction',
107 'sql_disable_fk',
108 'sql_compatibility',
109 'sql_structure_or_data',
110 'sql_create_database',
111 'sql_drop_table',
112 'sql_procedure_function',
113 'sql_create_table_statements',
114 'sql_create_table',
115 'sql_create_view',
116 'sql_create_trigger',
117 'sql_if_not_exists',
118 'sql_auto_increment',
119 'sql_backquotes',
120 'sql_truncate',
121 'sql_delayed',
122 'sql_ignore',
123 'sql_type',
124 'sql_insert_syntax',
125 'sql_max_query_size',
126 'sql_hex_for_binary',
127 'sql_utc_time',
128 'sql_drop_database',
129 'sql_views_as_tables',
130 'csv_separator',
131 'csv_enclosed',
132 'csv_escaped',
133 'csv_terminated',
134 'csv_null',
135 'csv_removeCRLF',
136 'csv_columns',
137 'csv_structure_or_data',
138 // csv_replace should have been here but we use it directly from $_POST
139 'latex_caption',
140 'latex_structure_or_data',
141 'latex_structure_caption',
142 'latex_structure_continued_caption',
143 'latex_structure_label',
144 'latex_relation',
145 'latex_comments',
146 'latex_mime',
147 'latex_columns',
148 'latex_data_caption',
149 'latex_data_continued_caption',
150 'latex_data_label',
151 'latex_null'
154 foreach ($post_params as $one_post_param) {
155 if (isset($_POST[$one_post_param])) {
156 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
160 // sanitize this parameter which will be used below in a file inclusion
161 $what = PMA_securePath($what);
163 PMA_Util::checkParameters(array('what', 'export_type'));
165 // export class instance, not array of properties, as before
166 $export_plugin = PMA_getPlugin(
167 "export",
168 $what,
169 'libraries/plugins/export/',
170 array(
171 'export_type' => $export_type,
172 'single_table' => isset($single_table)
176 // Backward compatibility
177 $type = $what;
179 // Check export type
180 if (! isset($export_plugin)) {
181 PMA_fatalError(__('Bad type!'));
185 * valid compression methods
187 $compression_methods = array(
188 'zip',
189 'gzip'
193 * init and variable checking
195 $compression = false;
196 $onserver = false;
197 $save_on_server = false;
198 $buffer_needed = false;
200 // Is it a quick or custom export?
201 if ($_REQUEST['quick_or_custom'] == 'quick') {
202 $quick_export = true;
203 } else {
204 $quick_export = false;
207 if ($_REQUEST['output_format'] == 'astext') {
208 $asfile = false;
209 } else {
210 $asfile = true;
211 if (in_array($_REQUEST['compression'], $compression_methods)) {
212 $compression = $_REQUEST['compression'];
213 $buffer_needed = true;
215 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
216 || (! $quick_export && ! empty($_REQUEST['onserver']))
218 if ($quick_export) {
219 $onserver = $_REQUEST['quick_export_onserver'];
220 } else {
221 $onserver = $_REQUEST['onserver'];
223 // Will we save dump on server?
224 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
228 // Generate error url and check for needed variables
229 if ($export_type == 'server') {
230 $err_url = 'server_export.php?' . PMA_URL_getCommon();
231 } elseif ($export_type == 'database' && strlen($db)) {
232 $err_url = 'db_export.php?' . PMA_URL_getCommon($db);
233 // Check if we have something to export
234 if (isset($table_select)) {
235 $tables = $table_select;
236 } else {
237 $tables = array();
239 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
240 $err_url = 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
241 } else {
242 PMA_fatalError(__('Bad parameters!'));
246 * Increase time limit for script execution and initializes some variables
248 @set_time_limit($cfg['ExecTimeLimit']);
249 if (! empty($cfg['MemoryLimit'])) {
250 @ini_set('memory_limit', $cfg['MemoryLimit']);
252 register_shutdown_function('PMA_shutdownDuringExport');
253 // Start with empty buffer
254 $dump_buffer = '';
255 $dump_buffer_len = 0;
257 // We send fake headers to avoid browser timeout when buffering
258 $time_start = time();
261 // Defines the default <CR><LF> format.
262 // For SQL always use \n as MySQL wants this on all platforms.
263 if (!defined('TESTSUITE')) {
264 if ($what == 'sql') {
265 $crlf = "\n";
266 } else {
267 $crlf = PMA_Util::whichCrlf();
270 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
271 && $type != 'xls';
273 // Do we need to convert charset?
274 $output_charset_conversion = $asfile
275 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
276 && isset($charset_of_file) && $charset_of_file != 'utf-8'
277 && $type != 'xls';
279 // Use on the fly compression?
280 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
281 && $compression == 'gzip';
282 if ($GLOBALS['onfly_compression']) {
283 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
286 // Generate filename and mime type if needed
287 if ($asfile) {
288 if (empty($remember_template)) {
289 $remember_template = '';
291 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
292 $export_type, $remember_template, $export_plugin, $compression,
293 $filename_template
297 // Open file on server if needed
298 if ($save_on_server) {
299 list($save_filename, $message, $file_handle) = PMA_openExportFile(
300 $filename, $quick_export
303 // problem opening export file on server?
304 if (! empty($message)) {
305 if ($export_type == 'server') {
306 $active_page = 'server_export.php';
307 include 'server_export.php';
308 } elseif ($export_type == 'database') {
309 $active_page = 'db_export.php';
310 include 'db_export.php';
311 } else {
312 $active_page = 'tbl_export.php';
313 include 'tbl_export.php';
315 exit();
320 * Send headers depending on whether the user chose to download a dump file
321 * or not
323 if (! $save_on_server) {
324 if ($asfile) {
325 // Download
326 // (avoid rewriting data containing HTML with anchors and forms;
327 // this was reported to happen under Plesk)
328 @ini_set('url_rewriter.tags', '');
329 $filename = PMA_sanitizeFilename($filename);
331 PMA_downloadHeader($filename, $mime_type);
332 } else {
333 // HTML
334 if ($export_type == 'database') {
335 $num_tables = count($tables);
336 if ($num_tables == 0) {
337 $message = PMA_Message::error(
338 __('No tables found in database.')
340 $active_page = 'db_export.php';
341 include 'db_export.php';
342 exit();
345 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
346 $export_type, $db, $table
348 echo $html;
349 unset($html);
350 } // end download
353 // Fake loop just to allow skip of remain of this code by break, I'd really
354 // need exceptions here :-)
355 do {
357 // Add possibly some comments to export
358 if (! $export_plugin->exportHeader($db)) {
359 break;
362 // Will we need relation & co. setup?
363 $do_relation = isset($GLOBALS[$what . '_relation']);
364 $do_comments = isset($GLOBALS[$what . '_include_comments'])
365 || isset($GLOBALS[$what . '_comments']) ;
366 $do_mime = isset($GLOBALS[$what . '_mime']);
367 if ($do_relation || $do_comments || $do_mime) {
368 $cfgRelation = PMA_getRelationsParam();
370 if ($do_mime) {
371 include_once 'libraries/transformations.lib.php';
374 // Include dates in export?
375 $do_dates = isset($GLOBALS[$what . '_dates']);
377 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
380 * Builds the dump
382 if ($export_type == 'server') {
383 if (! isset($db_select)) {
384 $db_select = '';
386 PMA_exportServer(
387 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
388 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
390 } elseif ($export_type == 'database') {
391 PMA_exportDatabase(
392 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
393 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
395 } else {
396 // We export just one table
397 // $allrows comes from the form when "Dump all rows" has been selected
398 if (! isset($allrows)) {
399 $allrows = '';
401 if (! isset($limit_to)) {
402 $limit_to = 0;
404 if (! isset($limit_from)) {
405 $limit_from = 0;
407 PMA_exportTable(
408 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
409 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
410 $allrows, $limit_to, $limit_from, $sql_query
413 if (! $export_plugin->exportFooter()) {
414 break;
417 } while (false);
418 // End of fake loop
420 if ($save_on_server && ! empty($message)) {
421 if ($export_type == 'server') {
422 $active_page = 'server_export.php';
423 include 'server_export.php';
424 } elseif ($export_type == 'database') {
425 $active_page = 'db_export.php';
426 include 'db_export.php';
427 } else {
428 $active_page = 'tbl_export.php';
429 include 'tbl_export.php';
431 exit();
435 * Send the dump as a file...
437 if (! empty($asfile)) {
438 // Convert the charset if required.
439 if ($output_charset_conversion) {
440 $dump_buffer = PMA_convertString(
441 'utf-8',
442 $GLOBALS['charset_of_file'],
443 $dump_buffer
447 // Compression needed?
448 if ($compression) {
449 $dump_buffer
450 = PMA_compressExport($dump_buffer, $compression, $filename);
453 /* If we saved on server, we have to close file now */
454 if ($save_on_server) {
455 $message = PMA_closeExportFile(
456 $file_handle, $dump_buffer, $save_filename
458 if ($export_type == 'server') {
459 $active_page = 'server_export.php';
460 include_once 'server_export.php';
461 } elseif ($export_type == 'database') {
462 $active_page = 'db_export.php';
463 include_once 'db_export.php';
464 } else {
465 $active_page = 'tbl_export.php';
466 include_once 'tbl_export.php';
468 exit();
469 } else {
470 echo $dump_buffer;
472 } else {
473 echo PMA_getHtmlForDisplayedExportFooter($back_button);
474 } // end if