2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export hanling 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';
15 PMA_checkParameters(array('what', 'export_type'));
18 $export_list = PMA_getPlugins(
19 './libraries/export/',
21 'export_type' => $export_type,
22 'single_table' => isset($single_table)));
24 // Backward compatbility
28 if (! isset($export_list[$type])) {
33 * valid compression methods
35 $compression_methods = array(
42 * init and variable checking
46 $save_on_server = false;
47 $buffer_needed = false;
49 // Is it a quick or custom export?
50 if ($_REQUEST['quick_or_custom'] == 'quick') {
53 $quick_export = false;
56 if ($_REQUEST['output_format'] == 'astext') {
60 if (in_array($_REQUEST['compression'], $compression_methods)) {
61 $compression = $_REQUEST['compression'];
62 $buffer_needed = true;
64 if (($quick_export && !empty($_REQUEST['quick_export_onserver'])) ||
(!$quick_export && !empty($_REQUEST['onserver']))) {
66 $onserver = $_REQUEST['quick_export_onserver'];
68 $onserver = $_REQUEST['onserver'];
70 // Will we save dump on server?
71 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
75 // Does export require to be into file?
76 if (isset($export_list[$type]['force_file']) && ! $asfile) {
77 $message = PMA_Message
::error(__('Selected export type has to be saved in file!'));
78 include_once './libraries/header.inc.php';
79 if ($export_type == 'server') {
80 $active_page = 'server_export.php';
81 include './server_export.php';
82 } elseif ($export_type == 'database') {
83 $active_page = 'db_export.php';
84 include './db_export.php';
86 $active_page = 'tbl_export.php';
87 include './tbl_export.php';
92 // Generate error url and check for needed variables
93 if ($export_type == 'server') {
94 $err_url = 'server_export.php?' . PMA_generate_common_url();
95 } elseif ($export_type == 'database' && strlen($db)) {
96 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
97 // Check if we have something to export
98 if (isset($table_select)) {
99 $tables = $table_select;
103 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
104 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
106 die(__('Bad parameters!'));
109 // Get the functions specific to the export type
110 require './libraries/export/' . PMA_securePath($type) . '.php';
113 * Increase time limit for script execution and initializes some variables
115 @set_time_limit
($cfg['ExecTimeLimit']);
116 if (!empty($cfg['MemoryLimit'])) {
117 @ini_set
('memory_limit', $cfg['MemoryLimit']);
120 // Start with empty buffer
122 $dump_buffer_len = 0;
124 // We send fake headers to avoid browser timeout when buffering
125 $time_start = time();
129 * Output handler for all exports, if needed buffering, it stores data into
130 * $dump_buffer, otherwise it prints thems out.
132 * @param string $line the insert statement
133 * @return bool Whether output suceeded
135 function PMA_exportOutputHandler($line)
137 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
139 // Kanji encoding convert feature
140 if ($GLOBALS['output_kanji_conversion']) {
141 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ?
$GLOBALS['xkana'] : '');
143 // If we have to buffer data, we will perform everything at once at the end
144 if ($GLOBALS['buffer_needed']) {
146 $dump_buffer .= $line;
147 if ($GLOBALS['onfly_compression']) {
149 $dump_buffer_len +
= strlen($line);
151 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
152 if ($GLOBALS['output_charset_conversion']) {
153 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
156 if ($GLOBALS['compression'] == 'bzip' && @function_exists
('bzcompress')) {
157 $dump_buffer = bzcompress($dump_buffer);
158 } elseif ($GLOBALS['compression'] == 'gzip' && @function_exists
('gzencode')) {
160 // without the optional parameter level because it bug
161 $dump_buffer = gzencode($dump_buffer);
163 if ($GLOBALS['save_on_server']) {
164 $write_result = @fwrite
($GLOBALS['file_handle'], $dump_buffer);
165 if (!$write_result ||
($write_result != strlen($dump_buffer))) {
166 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
167 $GLOBALS['message']->addParam($save_filename);
174 $dump_buffer_len = 0;
178 if ($time_start >= $time_now +
30) {
179 $time_start = $time_now;
180 header('X-pmaPing: Pong');
184 if ($GLOBALS['asfile']) {
185 if ($GLOBALS['output_charset_conversion']) {
186 $line = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $line);
188 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
189 $write_result = @fwrite
($GLOBALS['file_handle'], $line);
190 if (!$write_result ||
($write_result != strlen($line))) {
191 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
192 $GLOBALS['message']->addParam($save_filename);
196 if ($time_start >= $time_now +
30) {
197 $time_start = $time_now;
198 header('X-pmaPing: Pong');
201 // We export as file - output normally
205 // We export as html - replace special chars
206 echo htmlspecialchars($line);
210 } // end of the 'PMA_exportOutputHandler()' function
212 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
213 if ($what == 'sql') {
216 $crlf = PMA_whichCrlf();
219 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
221 // Do we need to convert charset?
222 $output_charset_conversion = $asfile && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
223 && isset($charset_of_file) && $charset_of_file != 'utf-8'
226 // Use on the fly compression?
227 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && ($compression == 'gzip' ||
$compression == 'bzip');
228 if ($onfly_compression) {
229 $memory_limit = trim(@ini_get
('memory_limit'));
231 if (empty($memory_limit)) {
232 $memory_limit = 2 * 1024 * 1024;
235 if (strtolower(substr($memory_limit, -1)) == 'm') {
236 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
237 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
238 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
239 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
240 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
242 $memory_limit = (int)$memory_limit;
245 // Some of memory is needed for other thins and as treshold.
246 // Nijel: During export I had allocated (see memory_get_usage function)
247 // approx 1.2MB so this comes from that.
248 if ($memory_limit > 1500000) {
249 $memory_limit -= 1500000;
252 // Some memory is needed for compression, assume 1/3
256 // Generate filename and mime type if needed
258 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
259 if ($export_type == 'server') {
260 if (isset($remember_template)) {
261 $GLOBALS['PMA_Config']->setUserValue('pma_server_filename_template',
262 'Export/file_template_server', $filename_template);
264 } elseif ($export_type == 'database') {
265 if (isset($remember_template)) {
266 $GLOBALS['PMA_Config']->setUserValue('pma_db_filename_template',
267 'Export/file_template_database', $filename_template);
270 if (isset($remember_template)) {
271 $GLOBALS['PMA_Config']->setUserValue('pma_table_filename_template',
272 'Export/file_template_table', $filename_template);
275 $filename = PMA_expandUserString($filename_template);
276 $filename = PMA_sanitize_filename($filename);
278 // Grab basic dump extension and mime type
279 // Check if the user already added extension; get the substring where the extension would be if it was included
280 $extension_start_pos = strlen($filename) - strlen($export_list[$type]['extension']) - 1;
281 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
282 $required_extension = "." . $export_list[$type]['extension'];
283 if (strtolower($user_extension) != $required_extension) {
284 $filename .= $required_extension;
286 $mime_type = $export_list[$type]['mime_type'];
288 // If dump is going to be compressed, set correct mime_type and add
289 // compression to extension
290 if ($compression == 'bzip') {
292 $mime_type = 'application/x-bzip2';
293 } elseif ($compression == 'gzip') {
295 $mime_type = 'application/x-gzip';
296 } elseif ($compression == 'zip') {
298 $mime_type = 'application/zip';
302 // Open file on server if needed
303 if ($save_on_server) {
304 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
306 if (file_exists($save_filename) && ((!$quick_export && empty($onserverover)) ||
($quick_export && $_REQUEST['quick_export_onserverover'] != 'saveitover'))) {
307 $message = PMA_Message
::error(__('File %s already exists on server, change filename or check overwrite option.'));
308 $message->addParam($save_filename);
310 if (is_file($save_filename) && !is_writable($save_filename)) {
311 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
312 $message->addParam($save_filename);
314 if (!$file_handle = @fopen
($save_filename, 'w')) {
315 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
316 $message->addParam($save_filename);
320 if (isset($message)) {
321 include_once './libraries/header.inc.php';
322 if ($export_type == 'server') {
323 $active_page = 'server_export.php';
324 include './server_export.php';
325 } elseif ($export_type == 'database') {
326 $active_page = 'db_export.php';
327 include './db_export.php';
329 $active_page = 'tbl_export.php';
330 include './tbl_export.php';
337 * Send headers depending on whether the user chose to download a dump file
340 if (!$save_on_server) {
343 // (avoid rewriting data containing HTML with anchors and forms;
344 // this was reported to happen under Plesk)
345 @ini_set
('url_rewriter.tags','');
346 $filename = PMA_sanitize_filename($filename);
348 PMA_download_header($filename, $mime_type);
351 if ($export_type == 'database') {
352 $num_tables = count($tables);
353 if ($num_tables == 0) {
354 $message = PMA_Message
::error(__('No tables found in database.'));
355 include_once './libraries/header.inc.php';
356 $active_page = 'db_export.php';
357 include './db_export.php';
361 $backup_cfgServer = $cfg['Server'];
362 include_once './libraries/header.inc.php';
363 $cfg['Server'] = $backup_cfgServer;
364 unset($backup_cfgServer);
365 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
366 //echo ' <pre>' . "\n";
369 * Displays a back button with all the $_REQUEST data in the URL (store in a variable to also display after the textarea)
371 $back_button = '<p>[ <a href="';
372 if ($export_type == 'server') {
373 $back_button .= 'server_export.php?' . PMA_generate_common_url();
374 } elseif ($export_type == 'database') {
375 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
377 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
380 // Convert the multiple select elements from an array to a string
381 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
382 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
383 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
384 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
387 foreach ($_REQUEST as $name => $value) {
388 $back_button .= '&' . urlencode($name) . '=' . urlencode($value);
390 $back_button .= '&repopulate=1">Back</a> ]</p>';
393 echo ' <form name="nofunction">' . "\n"
394 // remove auto-select for now: there is no way to select
395 // only a part of the text; anyway, it should obey
396 // $cfg['TextareaAutoSelect']
397 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
398 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
402 // Fake loop just to allow skip of remain of this code by break, I'd really
403 // need exceptions here :-)
406 // Add possibly some comments to export
407 if (!PMA_exportHeader()) {
411 // Will we need relation & co. setup?
412 $do_relation = isset($GLOBALS[$what . '_relation']);
413 $do_comments = isset($GLOBALS[$what . '_include_comments']);
414 $do_mime = isset($GLOBALS[$what . '_mime']);
415 if ($do_relation ||
$do_comments ||
$do_mime) {
416 $cfgRelation = PMA_getRelationsParam();
419 include_once './libraries/transformations.lib.php';
422 // Include dates in export?
423 $do_dates = isset($GLOBALS[$what . '_dates']);
428 // Gets the number of tables if a dump of a database has been required
429 if ($export_type == 'server') {
430 if (isset($db_select)) {
431 $tmp_select = implode($db_select, '|');
432 $tmp_select = '|' . $tmp_select . '|';
434 // Walk over databases
435 foreach ($GLOBALS['pma']->databases
as $current_db) {
436 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
437 ||
! isset($tmp_select)) {
438 if (!PMA_exportDBHeader($current_db)) {
441 if (!PMA_exportDBCreate($current_db)) {
444 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
445 PMA_exportRoutines($current_db);
448 $tables = PMA_DBI_get_tables($current_db);
450 foreach ($tables as $table) {
451 // if this is a view, collect it for later; views must be exported
453 $is_view = PMA_Table
::isView($current_db, $table);
457 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
458 // for a view, export a stand-in definition of the table
459 // to resolve view dependencies
460 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)) {
464 // if this is a view or a merge table, don't export data
465 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($current_db, $table))) {
466 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
467 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
471 // now export the triggers (needs to be done after the data because
472 // triggers can modify already imported tables)
473 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
474 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
479 foreach ($views as $view) {
480 // no data export for a view
481 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
482 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
487 if (!PMA_exportDBFooter($current_db)) {
492 } elseif ($export_type == 'database') {
493 if (!PMA_exportDBHeader($db)) {
497 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
498 PMA_exportRoutines($db);
503 // $tables contains the choices from the user (via $table_select)
504 foreach ($tables as $table) {
505 // if this is a view, collect it for later; views must be exported after
507 $is_view = PMA_Table
::isView($db, $table);
511 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
512 // for a view, export a stand-in definition of the table
513 // to resolve view dependencies
514 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
518 // if this is a view or a merge table, don't export data
519 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($db, $table))) {
520 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
521 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
525 // now export the triggers (needs to be done after the data because
526 // triggers can modify already imported tables)
527 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
528 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
533 foreach ($views as $view) {
534 // no data export for a view
535 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
536 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
542 if (!PMA_exportDBFooter($db)) {
546 if (!PMA_exportDBHeader($db)) {
549 // We export just one table
550 // $allrows comes from the form when "Dump all rows" has been selected
551 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
552 $add_query = ' LIMIT '
553 . (($limit_from > 0) ?
$limit_from . ', ' : '')
559 $is_view = PMA_Table
::isView($db, $table);
560 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
561 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'create_view' : 'create_table', $export_type)) {
565 // If this is an export of a single view, we have to export data;
566 // for example, a PDF report
567 // if it is a merge table, no data is exported
568 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && ! PMA_Table
::isMerge($db, $table)) {
569 if (!empty($sql_query)) {
570 // only preg_replace if needed
571 if (!empty($add_query)) {
572 // remove trailing semicolon before adding a LIMIT
573 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
575 $local_query = $sql_query . $add_query;
576 PMA_DBI_select_db($db);
578 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
580 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
584 // now export the triggers (needs to be done after the data because
585 // triggers can modify already imported tables)
586 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
587 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
591 if (!PMA_exportDBFooter($db)) {
595 if (!PMA_exportFooter()) {
602 if ($save_on_server && isset($message)) {
603 include_once './libraries/header.inc.php';
604 if ($export_type == 'server') {
605 $active_page = 'server_export.php';
606 include './server_export.php';
607 } elseif ($export_type == 'database') {
608 $active_page = 'db_export.php';
609 include './db_export.php';
611 $active_page = 'tbl_export.php';
612 include './tbl_export.php';
618 * Send the dump as a file...
620 if (!empty($asfile)) {
621 // Convert the charset if required.
622 if ($output_charset_conversion) {
623 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
626 // Do the compression
627 // 1. as a zipped file
628 if ($compression == 'zip') {
629 if (@function_exists
('gzcompress')) {
630 $zipfile = new zipfile();
631 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
632 $dump_buffer = $zipfile -> file();
634 } elseif ($compression == 'bzip') {
635 // 2. as a bzipped file
636 if (@function_exists
('bzcompress')) {
637 $dump_buffer = bzcompress($dump_buffer);
639 } elseif ($compression == 'gzip') {
640 // 3. as a gzipped file
641 if (@function_exists
('gzencode') && !@ini_get
('zlib.output_compression')) {
642 // without the optional parameter level because it bug
643 $dump_buffer = gzencode($dump_buffer);
647 /* If ve saved on server, we have to close file now */
648 if ($save_on_server) {
649 $write_result = @fwrite
($file_handle, $dump_buffer);
650 fclose($file_handle);
651 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
652 $message = new PMA_Message(__('Insufficient space to save the file %s.'), PMA_Message
::ERROR
, $save_filename);
654 $message = new PMA_Message(__('Dump has been saved to file %s.'), PMA_Message
::SUCCESS
, $save_filename);
657 include_once './libraries/header.inc.php';
658 if ($export_type == 'server') {
659 $active_page = 'server_export.php';
660 include_once './server_export.php';
661 } elseif ($export_type == 'database') {
662 $active_page = 'db_export.php';
663 include_once './db_export.php';
665 $active_page = 'tbl_export.php';
666 include_once './tbl_export.php';
674 * Displays the dump...
676 * Close the html tags and add the footers if dump is displayed on screen
678 echo '</textarea>' . "\n"
683 echo '</div>' . "\n";
686 <script type
="text/javascript">
688 var bodyWidth
=null; var bodyHeight
=null;
689 if (document
.getElementById('textSQLDUMP')) {
690 bodyWidth
= self
.innerWidth
;
691 bodyHeight
= self
.innerHeight
;
692 if (!bodyWidth
&& !bodyHeight
) {
693 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
694 bodyWidth
= document
.body
.clientWidth
;
695 bodyHeight
= document
.body
.clientHeight
;
696 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
697 bodyWidth
= document
.documentElement
.clientWidth
;
698 bodyHeight
= document
.documentElement
.clientHeight
;
701 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
702 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
707 include './libraries/footer.inc.php';