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) {
20 $GLOBALS[$one_post_param] = $one_post_value;
23 PMA_checkParameters(array('what', 'export_type'));
26 $export_list = PMA_getPlugins(
29 'export_type' => $export_type,
30 'single_table' => isset($single_table)
34 // Backward compatbility
38 if (! isset($export_list[$type])) {
43 * valid compression methods
45 $compression_methods = array(
52 * init and variable checking
56 $save_on_server = false;
57 $buffer_needed = false;
59 // Is it a quick or custom export?
60 if ($_REQUEST['quick_or_custom'] == 'quick') {
63 $quick_export = false;
66 if ($_REQUEST['output_format'] == 'astext') {
70 if (in_array($_REQUEST['compression'], $compression_methods)) {
71 $compression = $_REQUEST['compression'];
72 $buffer_needed = true;
74 if (($quick_export && !empty($_REQUEST['quick_export_onserver'])) ||
(!$quick_export && !empty($_REQUEST['onserver']))) {
76 $onserver = $_REQUEST['quick_export_onserver'];
78 $onserver = $_REQUEST['onserver'];
80 // Will we save dump on server?
81 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
85 // Does export require to be into file?
86 if (isset($export_list[$type]['force_file']) && ! $asfile) {
87 $message = PMA_Message
::error(__('Selected export type has to be saved in file!'));
88 include_once 'libraries/header.inc.php';
89 if ($export_type == 'server') {
90 $active_page = 'server_export.php';
91 include 'server_export.php';
92 } elseif ($export_type == 'database') {
93 $active_page = 'db_export.php';
94 include 'db_export.php';
96 $active_page = 'tbl_export.php';
97 include 'tbl_export.php';
102 // Generate error url and check for needed variables
103 if ($export_type == 'server') {
104 $err_url = 'server_export.php?' . PMA_generate_common_url();
105 } elseif ($export_type == 'database' && strlen($db)) {
106 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
107 // Check if we have something to export
108 if (isset($table_select)) {
109 $tables = $table_select;
113 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
114 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
116 die(__('Bad parameters!'));
119 // Get the functions specific to the export type
120 require 'libraries/export/' . PMA_securePath($type) . '.php';
123 * Increase time limit for script execution and initializes some variables
125 @set_time_limit
($cfg['ExecTimeLimit']);
126 if (!empty($cfg['MemoryLimit'])) {
127 @ini_set
('memory_limit', $cfg['MemoryLimit']);
130 // Start with empty buffer
132 $dump_buffer_len = 0;
134 // We send fake headers to avoid browser timeout when buffering
135 $time_start = time();
139 * Output handler for all exports, if needed buffering, it stores data into
140 * $dump_buffer, otherwise it prints thems out.
142 * @param string $line the insert statement
144 * @return bool Whether output succeeded
146 function PMA_exportOutputHandler($line)
148 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
150 // Kanji encoding convert feature
151 if ($GLOBALS['output_kanji_conversion']) {
152 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ?
$GLOBALS['xkana'] : '');
154 // If we have to buffer data, we will perform everything at once at the end
155 if ($GLOBALS['buffer_needed']) {
157 $dump_buffer .= $line;
158 if ($GLOBALS['onfly_compression']) {
160 $dump_buffer_len +
= strlen($line);
162 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
163 if ($GLOBALS['output_charset_conversion']) {
164 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
167 if ($GLOBALS['compression'] == 'bzip2' && @function_exists
('bzcompress')) {
168 $dump_buffer = bzcompress($dump_buffer);
169 } elseif ($GLOBALS['compression'] == 'gzip' && @function_exists
('gzencode')) {
171 // without the optional parameter level because it bug
172 $dump_buffer = gzencode($dump_buffer);
174 if ($GLOBALS['save_on_server']) {
175 $write_result = @fwrite
($GLOBALS['file_handle'], $dump_buffer);
176 if (!$write_result ||
($write_result != strlen($dump_buffer))) {
177 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
178 $GLOBALS['message']->addParam($save_filename);
185 $dump_buffer_len = 0;
189 if ($time_start >= $time_now +
30) {
190 $time_start = $time_now;
191 header('X-pmaPing: Pong');
195 if ($GLOBALS['asfile']) {
196 if ($GLOBALS['output_charset_conversion']) {
197 $line = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $line);
199 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
200 $write_result = @fwrite
($GLOBALS['file_handle'], $line);
201 if (!$write_result ||
($write_result != strlen($line))) {
202 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
203 $GLOBALS['message']->addParam($save_filename);
207 if ($time_start >= $time_now +
30) {
208 $time_start = $time_now;
209 header('X-pmaPing: Pong');
212 // We export as file - output normally
216 // We export as html - replace special chars
217 echo htmlspecialchars($line);
221 } // end of the 'PMA_exportOutputHandler()' function
223 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
224 if ($what == 'sql') {
227 $crlf = PMA_whichCrlf();
230 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
232 // Do we need to convert charset?
233 $output_charset_conversion = $asfile && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
234 && isset($charset_of_file) && $charset_of_file != 'utf-8'
237 // Use on the fly compression?
238 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && ($compression == 'gzip' ||
$compression == 'bzip2');
239 if ($onfly_compression) {
240 $memory_limit = trim(@ini_get
('memory_limit'));
242 if (empty($memory_limit)) {
243 $memory_limit = 2 * 1024 * 1024;
246 if (strtolower(substr($memory_limit, -1)) == 'm') {
247 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
248 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
249 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
250 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
251 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
253 $memory_limit = (int)$memory_limit;
256 // Some of memory is needed for other thins and as treshold.
257 // Nijel: During export I had allocated (see memory_get_usage function)
258 // approx 1.2MB so this comes from that.
259 if ($memory_limit > 1500000) {
260 $memory_limit -= 1500000;
263 // Some memory is needed for compression, assume 1/3
267 // Generate filename and mime type if needed
269 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
270 if ($export_type == 'server') {
271 if (isset($remember_template)) {
272 $GLOBALS['PMA_Config']->setUserValue(
273 'pma_server_filename_template',
274 'Export/file_template_server',
278 } elseif ($export_type == 'database') {
279 if (isset($remember_template)) {
280 $GLOBALS['PMA_Config']->setUserValue(
281 'pma_db_filename_template',
282 'Export/file_template_database',
287 if (isset($remember_template)) {
288 $GLOBALS['PMA_Config']->setUserValue(
289 'pma_table_filename_template',
290 'Export/file_template_table',
295 $filename = PMA_expandUserString($filename_template);
296 $filename = PMA_sanitize_filename($filename);
298 // Grab basic dump extension and mime type
299 // Check if the user already added extension; get the substring where the extension would be if it was included
300 $extension_start_pos = strlen($filename) - strlen($export_list[$type]['extension']) - 1;
301 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
302 $required_extension = "." . $export_list[$type]['extension'];
303 if (strtolower($user_extension) != $required_extension) {
304 $filename .= $required_extension;
306 $mime_type = $export_list[$type]['mime_type'];
308 // If dump is going to be compressed, set correct mime_type and add
309 // compression to extension
310 if ($compression == 'bzip2') {
312 $mime_type = 'application/x-bzip2';
313 } elseif ($compression == 'gzip') {
315 $mime_type = 'application/x-gzip';
316 } elseif ($compression == 'zip') {
318 $mime_type = 'application/zip';
322 // Open file on server if needed
323 if ($save_on_server) {
324 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
326 if (file_exists($save_filename) && ((!$quick_export && empty($onserverover)) ||
($quick_export && $_REQUEST['quick_export_onserverover'] != 'saveitover'))) {
327 $message = PMA_Message
::error(__('File %s already exists on server, change filename or check overwrite option.'));
328 $message->addParam($save_filename);
330 if (is_file($save_filename) && !is_writable($save_filename)) {
331 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
332 $message->addParam($save_filename);
334 if (!$file_handle = @fopen
($save_filename, 'w')) {
335 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
336 $message->addParam($save_filename);
340 if (isset($message)) {
341 include_once 'libraries/header.inc.php';
342 if ($export_type == 'server') {
343 $active_page = 'server_export.php';
344 include 'server_export.php';
345 } elseif ($export_type == 'database') {
346 $active_page = 'db_export.php';
347 include 'db_export.php';
349 $active_page = 'tbl_export.php';
350 include 'tbl_export.php';
357 * Send headers depending on whether the user chose to download a dump file
360 if (!$save_on_server) {
363 // (avoid rewriting data containing HTML with anchors and forms;
364 // this was reported to happen under Plesk)
365 @ini_set
('url_rewriter.tags', '');
366 $filename = PMA_sanitize_filename($filename);
368 PMA_download_header($filename, $mime_type);
371 if ($export_type == 'database') {
372 $num_tables = count($tables);
373 if ($num_tables == 0) {
374 $message = PMA_Message
::error(__('No tables found in database.'));
375 include_once 'libraries/header.inc.php';
376 $active_page = 'db_export.php';
377 include 'db_export.php';
381 $backup_cfgServer = $cfg['Server'];
382 include_once 'libraries/header.inc.php';
383 $cfg['Server'] = $backup_cfgServer;
384 unset($backup_cfgServer);
385 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
386 //echo ' <pre>' . "\n";
389 * Displays a back button with all the $_REQUEST data in the URL (store in a variable to also display after the textarea)
391 $back_button = '<p>[ <a href="';
392 if ($export_type == 'server') {
393 $back_button .= 'server_export.php?' . PMA_generate_common_url();
394 } elseif ($export_type == 'database') {
395 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
397 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
400 // Convert the multiple select elements from an array to a string
401 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
402 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
403 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
404 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
407 foreach ($_REQUEST as $name => $value) {
408 $back_button .= '&' . urlencode($name) . '=' . urlencode($value);
410 $back_button .= '&repopulate=1">Back</a> ]</p>';
413 echo ' <form name="nofunction">' . "\n"
414 // remove auto-select for now: there is no way to select
415 // only a part of the text; anyway, it should obey
416 // $cfg['TextareaAutoSelect']
417 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
418 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
422 // Fake loop just to allow skip of remain of this code by break, I'd really
423 // need exceptions here :-)
426 // Add possibly some comments to export
427 if (!PMA_exportHeader()) {
431 // Will we need relation & co. setup?
432 $do_relation = isset($GLOBALS[$what . '_relation']);
433 $do_comments = isset($GLOBALS[$what . '_include_comments']);
434 $do_mime = isset($GLOBALS[$what . '_mime']);
435 if ($do_relation ||
$do_comments ||
$do_mime) {
436 $cfgRelation = PMA_getRelationsParam();
439 include_once 'libraries/transformations.lib.php';
442 // Include dates in export?
443 $do_dates = isset($GLOBALS[$what . '_dates']);
448 // Gets the number of tables if a dump of a database has been required
449 if ($export_type == 'server') {
450 if (isset($db_select)) {
451 $tmp_select = implode($db_select, '|');
452 $tmp_select = '|' . $tmp_select . '|';
454 // Walk over databases
455 foreach ($GLOBALS['pma']->databases
as $current_db) {
456 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
457 ||
! isset($tmp_select)
459 if (!PMA_exportDBHeader($current_db)) {
462 if (!PMA_exportDBCreate($current_db)) {
465 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
466 PMA_exportRoutines($current_db);
469 $tables = PMA_DBI_get_tables($current_db);
471 foreach ($tables as $table) {
472 // if this is a view, collect it for later; views must be exported
474 $is_view = PMA_Table
::isView($current_db, $table);
478 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
479 // for a view, export a stand-in definition of the table
480 // to resolve view dependencies
481 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)) {
485 // if this is a view or a merge table, don't export data
486 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($current_db, $table))) {
487 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
488 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
492 // now export the triggers (needs to be done after the data because
493 // triggers can modify already imported tables)
494 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
495 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
500 foreach ($views as $view) {
501 // no data export for a view
502 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
503 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
508 if (!PMA_exportDBFooter($current_db)) {
513 } elseif ($export_type == 'database') {
514 if (!PMA_exportDBHeader($db)) {
518 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
519 PMA_exportRoutines($db);
524 // $tables contains the choices from the user (via $table_select)
525 foreach ($tables as $table) {
526 // if this is a view, collect it for later; views must be exported after
528 $is_view = PMA_Table
::isView($db, $table);
532 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
533 // for a view, export a stand-in definition of the table
534 // to resolve view dependencies
535 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
539 // if this is a view or a merge table, don't export data
540 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($db, $table))) {
541 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
542 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
546 // now export the triggers (needs to be done after the data because
547 // triggers can modify already imported tables)
548 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
549 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
554 foreach ($views as $view) {
555 // no data export for a view
556 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
557 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
563 if (!PMA_exportDBFooter($db)) {
567 if (!PMA_exportDBHeader($db)) {
570 // We export just one table
571 // $allrows comes from the form when "Dump all rows" has been selected
572 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
573 $add_query = ' LIMIT '
574 . (($limit_from > 0) ?
$limit_from . ', ' : '')
580 $is_view = PMA_Table
::isView($db, $table);
581 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
582 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'create_view' : 'create_table', $export_type)) {
586 // If this is an export of a single view, we have to export data;
587 // for example, a PDF report
588 // if it is a merge table, no data is exported
589 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && ! PMA_Table
::isMerge($db, $table)) {
590 if (!empty($sql_query)) {
591 // only preg_replace if needed
592 if (!empty($add_query)) {
593 // remove trailing semicolon before adding a LIMIT
594 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
596 $local_query = $sql_query . $add_query;
597 PMA_DBI_select_db($db);
599 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
601 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
605 // now export the triggers (needs to be done after the data because
606 // triggers can modify already imported tables)
607 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
608 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
612 if (!PMA_exportDBFooter($db)) {
616 if (!PMA_exportFooter()) {
623 if ($save_on_server && isset($message)) {
624 include_once 'libraries/header.inc.php';
625 if ($export_type == 'server') {
626 $active_page = 'server_export.php';
627 include 'server_export.php';
628 } elseif ($export_type == 'database') {
629 $active_page = 'db_export.php';
630 include 'db_export.php';
632 $active_page = 'tbl_export.php';
633 include 'tbl_export.php';
639 * Send the dump as a file...
641 if (!empty($asfile)) {
642 // Convert the charset if required.
643 if ($output_charset_conversion) {
644 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
647 // Do the compression
648 // 1. as a zipped file
649 if ($compression == 'zip') {
650 if (@function_exists
('gzcompress')) {
651 $zipfile = new zipfile();
652 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
653 $dump_buffer = $zipfile -> file();
655 } elseif ($compression == 'bzip2') {
656 // 2. as a bzipped file
657 if (@function_exists
('bzcompress')) {
658 $dump_buffer = bzcompress($dump_buffer);
660 } elseif ($compression == 'gzip') {
661 // 3. as a gzipped file
662 if (@function_exists
('gzencode') && !@ini_get
('zlib.output_compression')) {
663 // without the optional parameter level because it bug
664 $dump_buffer = gzencode($dump_buffer);
668 /* If ve saved on server, we have to close file now */
669 if ($save_on_server) {
670 $write_result = @fwrite
($file_handle, $dump_buffer);
671 fclose($file_handle);
672 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
673 $message = new PMA_Message(__('Insufficient space to save the file %s.'), PMA_Message
::ERROR
, $save_filename);
675 $message = new PMA_Message(__('Dump has been saved to file %s.'), PMA_Message
::SUCCESS
, $save_filename);
678 include_once 'libraries/header.inc.php';
679 if ($export_type == 'server') {
680 $active_page = 'server_export.php';
681 include_once 'server_export.php';
682 } elseif ($export_type == 'database') {
683 $active_page = 'db_export.php';
684 include_once 'db_export.php';
686 $active_page = 'tbl_export.php';
687 include_once 'tbl_export.php';
695 * Displays the dump...
697 * Close the html tags and add the footers if dump is displayed on screen
699 echo '</textarea>' . "\n"
704 echo '</div>' . "\n";
707 <script type
="text/javascript">
709 var bodyWidth
=null; var bodyHeight
=null;
710 if (document
.getElementById('textSQLDUMP')) {
711 bodyWidth
= self
.innerWidth
;
712 bodyHeight
= self
.innerHeight
;
713 if (!bodyWidth
&& !bodyHeight
) {
714 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
715 bodyWidth
= document
.body
.clientWidth
;
716 bodyHeight
= document
.body
.clientHeight
;
717 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
718 bodyWidth
= document
.documentElement
.clientWidth
;
719 bodyHeight
= document
.documentElement
.clientHeight
;
722 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
723 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
728 include 'libraries/footer.inc.php';