2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
9 * Get the variables sent or posted to this script and a core script
11 require_once 'libraries/common.inc.php';
12 require_once 'libraries/zip.lib.php';
13 require_once 'libraries/plugin_interface.lib.php';
16 * Sets globals from all $_POST (in export.php only)
17 * Would it not be tiresome to list all export-plugin options here?
19 foreach ($_POST as $one_post_param => $one_post_value)
21 $GLOBALS[$one_post_param] = $one_post_value;
24 PMA_checkParameters(array('what', 'export_type'));
27 $export_list = PMA_getPlugins(
30 'export_type' => $export_type,
31 'single_table' => isset($single_table)
35 // Backward compatbility
39 if (! isset($export_list[$type])) {
44 * valid compression methods
46 $compression_methods = array(
53 * init and variable checking
57 $save_on_server = false;
58 $buffer_needed = false;
60 // Is it a quick or custom export?
61 if ($_REQUEST['quick_or_custom'] == 'quick') {
64 $quick_export = false;
67 if ($_REQUEST['output_format'] == 'astext') {
71 if (in_array($_REQUEST['compression'], $compression_methods)) {
72 $compression = $_REQUEST['compression'];
73 $buffer_needed = true;
75 if (($quick_export && !empty($_REQUEST['quick_export_onserver'])) ||
(!$quick_export && !empty($_REQUEST['onserver']))) {
77 $onserver = $_REQUEST['quick_export_onserver'];
79 $onserver = $_REQUEST['onserver'];
81 // Will we save dump on server?
82 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
86 // Does export require to be into file?
87 if (isset($export_list[$type]['force_file']) && ! $asfile) {
88 $message = PMA_Message
::error(__('Selected export type has to be saved in file!'));
89 include_once 'libraries/header.inc.php';
90 if ($export_type == 'server') {
91 $active_page = 'server_export.php';
92 include 'server_export.php';
93 } elseif ($export_type == 'database') {
94 $active_page = 'db_export.php';
95 include 'db_export.php';
97 $active_page = 'tbl_export.php';
98 include 'tbl_export.php';
103 // Generate error url and check for needed variables
104 if ($export_type == 'server') {
105 $err_url = 'server_export.php?' . PMA_generate_common_url();
106 } elseif ($export_type == 'database' && strlen($db)) {
107 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
108 // Check if we have something to export
109 if (isset($table_select)) {
110 $tables = $table_select;
114 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
115 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
117 die(__('Bad parameters!'));
120 // Get the functions specific to the export type
121 require 'libraries/export/' . PMA_securePath($type) . '.php';
124 * Increase time limit for script execution and initializes some variables
126 @set_time_limit
($cfg['ExecTimeLimit']);
127 if (!empty($cfg['MemoryLimit'])) {
128 @ini_set
('memory_limit', $cfg['MemoryLimit']);
131 // Start with empty buffer
133 $dump_buffer_len = 0;
135 // We send fake headers to avoid browser timeout when buffering
136 $time_start = time();
140 * Output handler for all exports, if needed buffering, it stores data into
141 * $dump_buffer, otherwise it prints thems out.
143 * @param string $line the insert statement
145 * @return bool Whether output succeeded
147 function PMA_exportOutputHandler($line)
149 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
151 // Kanji encoding convert feature
152 if ($GLOBALS['output_kanji_conversion']) {
153 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ?
$GLOBALS['xkana'] : '');
155 // If we have to buffer data, we will perform everything at once at the end
156 if ($GLOBALS['buffer_needed']) {
158 $dump_buffer .= $line;
159 if ($GLOBALS['onfly_compression']) {
161 $dump_buffer_len +
= strlen($line);
163 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
164 if ($GLOBALS['output_charset_conversion']) {
165 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
168 if ($GLOBALS['compression'] == 'bzip2' && @function_exists
('bzcompress')) {
169 $dump_buffer = bzcompress($dump_buffer);
170 } elseif ($GLOBALS['compression'] == 'gzip' && @function_exists
('gzencode')) {
172 // without the optional parameter level because it bug
173 $dump_buffer = gzencode($dump_buffer);
175 if ($GLOBALS['save_on_server']) {
176 $write_result = @fwrite
($GLOBALS['file_handle'], $dump_buffer);
177 if (!$write_result ||
($write_result != strlen($dump_buffer))) {
178 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
179 $GLOBALS['message']->addParam($save_filename);
186 $dump_buffer_len = 0;
190 if ($time_start >= $time_now +
30) {
191 $time_start = $time_now;
192 header('X-pmaPing: Pong');
196 if ($GLOBALS['asfile']) {
197 if ($GLOBALS['output_charset_conversion']) {
198 $line = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $line);
200 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
201 $write_result = @fwrite
($GLOBALS['file_handle'], $line);
202 if (!$write_result ||
($write_result != strlen($line))) {
203 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
204 $GLOBALS['message']->addParam($save_filename);
208 if ($time_start >= $time_now +
30) {
209 $time_start = $time_now;
210 header('X-pmaPing: Pong');
213 // We export as file - output normally
217 // We export as html - replace special chars
218 echo htmlspecialchars($line);
222 } // end of the 'PMA_exportOutputHandler()' function
224 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
225 if ($what == 'sql') {
228 $crlf = PMA_whichCrlf();
231 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
233 // Do we need to convert charset?
234 $output_charset_conversion = $asfile && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
235 && isset($charset_of_file) && $charset_of_file != 'utf-8'
238 // Use on the fly compression?
239 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && ($compression == 'gzip' ||
$compression == 'bzip2');
240 if ($onfly_compression) {
241 $memory_limit = trim(@ini_get
('memory_limit'));
243 if (empty($memory_limit)) {
244 $memory_limit = 2 * 1024 * 1024;
247 if (strtolower(substr($memory_limit, -1)) == 'm') {
248 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
249 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
250 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
251 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
252 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
254 $memory_limit = (int)$memory_limit;
257 // Some of memory is needed for other thins and as treshold.
258 // Nijel: During export I had allocated (see memory_get_usage function)
259 // approx 1.2MB so this comes from that.
260 if ($memory_limit > 1500000) {
261 $memory_limit -= 1500000;
264 // Some memory is needed for compression, assume 1/3
268 // Generate filename and mime type if needed
270 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
271 if ($export_type == 'server') {
272 if (isset($remember_template)) {
273 $GLOBALS['PMA_Config']->setUserValue(
274 'pma_server_filename_template',
275 'Export/file_template_server',
279 } elseif ($export_type == 'database') {
280 if (isset($remember_template)) {
281 $GLOBALS['PMA_Config']->setUserValue(
282 'pma_db_filename_template',
283 'Export/file_template_database',
288 if (isset($remember_template)) {
289 $GLOBALS['PMA_Config']->setUserValue(
290 'pma_table_filename_template',
291 'Export/file_template_table',
296 $filename = PMA_expandUserString($filename_template);
297 $filename = PMA_sanitize_filename($filename);
299 // Grab basic dump extension and mime type
300 // Check if the user already added extension; get the substring where the extension would be if it was included
301 $extension_start_pos = strlen($filename) - strlen($export_list[$type]['extension']) - 1;
302 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
303 $required_extension = "." . $export_list[$type]['extension'];
304 if (strtolower($user_extension) != $required_extension) {
305 $filename .= $required_extension;
307 $mime_type = $export_list[$type]['mime_type'];
309 // If dump is going to be compressed, set correct mime_type and add
310 // compression to extension
311 if ($compression == 'bzip2') {
313 $mime_type = 'application/x-bzip2';
314 } elseif ($compression == 'gzip') {
316 $mime_type = 'application/x-gzip';
317 } elseif ($compression == 'zip') {
319 $mime_type = 'application/zip';
323 // Open file on server if needed
324 if ($save_on_server) {
325 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
327 if (file_exists($save_filename) && ((!$quick_export && empty($onserverover)) ||
($quick_export && $_REQUEST['quick_export_onserverover'] != 'saveitover'))) {
328 $message = PMA_Message
::error(__('File %s already exists on server, change filename or check overwrite option.'));
329 $message->addParam($save_filename);
331 if (is_file($save_filename) && !is_writable($save_filename)) {
332 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
333 $message->addParam($save_filename);
335 if (!$file_handle = @fopen
($save_filename, 'w')) {
336 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
337 $message->addParam($save_filename);
341 if (isset($message)) {
342 include_once 'libraries/header.inc.php';
343 if ($export_type == 'server') {
344 $active_page = 'server_export.php';
345 include 'server_export.php';
346 } elseif ($export_type == 'database') {
347 $active_page = 'db_export.php';
348 include 'db_export.php';
350 $active_page = 'tbl_export.php';
351 include 'tbl_export.php';
358 * Send headers depending on whether the user chose to download a dump file
361 if (!$save_on_server) {
364 // (avoid rewriting data containing HTML with anchors and forms;
365 // this was reported to happen under Plesk)
366 @ini_set
('url_rewriter.tags', '');
367 $filename = PMA_sanitize_filename($filename);
369 PMA_download_header($filename, $mime_type);
372 if ($export_type == 'database') {
373 $num_tables = count($tables);
374 if ($num_tables == 0) {
375 $message = PMA_Message
::error(__('No tables found in database.'));
376 include_once 'libraries/header.inc.php';
377 $active_page = 'db_export.php';
378 include 'db_export.php';
382 $backup_cfgServer = $cfg['Server'];
383 include_once 'libraries/header.inc.php';
384 $cfg['Server'] = $backup_cfgServer;
385 unset($backup_cfgServer);
386 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
387 //echo ' <pre>' . "\n";
390 * Displays a back button with all the $_REQUEST data in the URL (store in a variable to also display after the textarea)
392 $back_button = '<p>[ <a href="';
393 if ($export_type == 'server') {
394 $back_button .= 'server_export.php?' . PMA_generate_common_url();
395 } elseif ($export_type == 'database') {
396 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
398 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
401 // Convert the multiple select elements from an array to a string
402 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
403 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
404 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
405 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
408 foreach ($_REQUEST as $name => $value) {
409 $back_button .= '&' . urlencode($name) . '=' . urlencode($value);
411 $back_button .= '&repopulate=1">Back</a> ]</p>';
414 echo ' <form name="nofunction">' . "\n"
415 // remove auto-select for now: there is no way to select
416 // only a part of the text; anyway, it should obey
417 // $cfg['TextareaAutoSelect']
418 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
419 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
423 // Fake loop just to allow skip of remain of this code by break, I'd really
424 // need exceptions here :-)
427 // Add possibly some comments to export
428 if (!PMA_exportHeader()) {
432 // Will we need relation & co. setup?
433 $do_relation = isset($GLOBALS[$what . '_relation']);
434 $do_comments = isset($GLOBALS[$what . '_include_comments']);
435 $do_mime = isset($GLOBALS[$what . '_mime']);
436 if ($do_relation ||
$do_comments ||
$do_mime) {
437 $cfgRelation = PMA_getRelationsParam();
440 include_once 'libraries/transformations.lib.php';
443 // Include dates in export?
444 $do_dates = isset($GLOBALS[$what . '_dates']);
449 // Gets the number of tables if a dump of a database has been required
450 if ($export_type == 'server') {
451 if (isset($db_select)) {
452 $tmp_select = implode($db_select, '|');
453 $tmp_select = '|' . $tmp_select . '|';
455 // Walk over databases
456 foreach ($GLOBALS['pma']->databases
as $current_db) {
457 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
458 ||
! isset($tmp_select)
460 if (!PMA_exportDBHeader($current_db)) {
463 if (!PMA_exportDBCreate($current_db)) {
466 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
467 PMA_exportRoutines($current_db);
470 $tables = PMA_DBI_get_tables($current_db);
472 foreach ($tables as $table) {
473 // if this is a view, collect it for later; views must be exported
475 $is_view = PMA_Table
::isView($current_db, $table);
479 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
480 // for a view, export a stand-in definition of the table
481 // to resolve view dependencies
482 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
486 // if this is a view or a merge table, don't export data
487 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($current_db, $table))) {
488 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
489 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
493 // now export the triggers (needs to be done after the data because
494 // triggers can modify already imported tables)
495 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
496 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
501 foreach ($views as $view) {
502 // no data export for a view
503 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
504 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
509 if (!PMA_exportDBFooter($current_db)) {
514 } elseif ($export_type == 'database') {
515 if (!PMA_exportDBHeader($db)) {
519 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
520 PMA_exportRoutines($db);
525 // $tables contains the choices from the user (via $table_select)
526 foreach ($tables as $table) {
527 // if this is a view, collect it for later; views must be exported after
529 $is_view = PMA_Table
::isView($db, $table);
533 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
534 // for a view, export a stand-in definition of the table
535 // to resolve view dependencies
536 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
540 // if this is a view or a merge table, don't export data
541 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($db, $table))) {
542 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
543 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
547 // now export the triggers (needs to be done after the data because
548 // triggers can modify already imported tables)
549 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
550 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
555 foreach ($views as $view) {
556 // no data export for a view
557 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
558 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
564 if (!PMA_exportDBFooter($db)) {
568 if (!PMA_exportDBHeader($db)) {
571 // We export just one table
572 // $allrows comes from the form when "Dump all rows" has been selected
573 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
574 $add_query = ' LIMIT '
575 . (($limit_from > 0) ?
$limit_from . ', ' : '')
581 $is_view = PMA_Table
::isView($db, $table);
582 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
583 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'create_view' : 'create_table', $export_type)) {
587 // If this is an export of a single view, we have to export data;
588 // for example, a PDF report
589 // if it is a merge table, no data is exported
590 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && ! PMA_Table
::isMerge($db, $table)) {
591 if (!empty($sql_query)) {
592 // only preg_replace if needed
593 if (!empty($add_query)) {
594 // remove trailing semicolon before adding a LIMIT
595 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
597 $local_query = $sql_query . $add_query;
598 PMA_DBI_select_db($db);
600 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
602 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
606 // now export the triggers (needs to be done after the data because
607 // triggers can modify already imported tables)
608 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
609 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
613 if (!PMA_exportDBFooter($db)) {
617 if (!PMA_exportFooter()) {
624 if ($save_on_server && isset($message)) {
625 include_once 'libraries/header.inc.php';
626 if ($export_type == 'server') {
627 $active_page = 'server_export.php';
628 include 'server_export.php';
629 } elseif ($export_type == 'database') {
630 $active_page = 'db_export.php';
631 include 'db_export.php';
633 $active_page = 'tbl_export.php';
634 include 'tbl_export.php';
640 * Send the dump as a file...
642 if (!empty($asfile)) {
643 // Convert the charset if required.
644 if ($output_charset_conversion) {
645 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
648 // Do the compression
649 // 1. as a zipped file
650 if ($compression == 'zip') {
651 if (@function_exists
('gzcompress')) {
652 $zipfile = new zipfile();
653 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
654 $dump_buffer = $zipfile -> file();
656 } elseif ($compression == 'bzip2') {
657 // 2. as a bzipped file
658 if (@function_exists
('bzcompress')) {
659 $dump_buffer = bzcompress($dump_buffer);
661 } elseif ($compression == 'gzip') {
662 // 3. as a gzipped file
663 if (@function_exists
('gzencode') && !@ini_get
('zlib.output_compression')) {
664 // without the optional parameter level because it bug
665 $dump_buffer = gzencode($dump_buffer);
669 /* If ve saved on server, we have to close file now */
670 if ($save_on_server) {
671 $write_result = @fwrite
($file_handle, $dump_buffer);
672 fclose($file_handle);
673 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
674 $message = new PMA_Message(__('Insufficient space to save the file %s.'), PMA_Message
::ERROR
, $save_filename);
676 $message = new PMA_Message(__('Dump has been saved to file %s.'), PMA_Message
::SUCCESS
, $save_filename);
679 include_once 'libraries/header.inc.php';
680 if ($export_type == 'server') {
681 $active_page = 'server_export.php';
682 include_once 'server_export.php';
683 } elseif ($export_type == 'database') {
684 $active_page = 'db_export.php';
685 include_once 'db_export.php';
687 $active_page = 'tbl_export.php';
688 include_once 'tbl_export.php';
696 * Displays the dump...
698 * Close the html tags and add the footers if dump is displayed on screen
700 echo '</textarea>' . "\n"
705 echo '</div>' . "\n";
708 <script type
="text/javascript">
710 var bodyWidth
=null; var bodyHeight
=null;
711 if (document
.getElementById('textSQLDUMP')) {
712 bodyWidth
= self
.innerWidth
;
713 bodyHeight
= self
.innerHeight
;
714 if (!bodyWidth
&& !bodyHeight
) {
715 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
716 bodyWidth
= document
.body
.clientWidth
;
717 bodyHeight
= document
.body
.clientHeight
;
718 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
719 bodyWidth
= document
.documentElement
.clientWidth
;
720 bodyHeight
= document
.documentElement
.clientHeight
;
723 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
724 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
729 include 'libraries/footer.inc.php';