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 require_once './libraries/header.inc.php';
79 if ($export_type == 'server') {
80 $active_page = 'server_export.php';
81 require './server_export.php';
82 } elseif ($export_type == 'database') {
83 $active_page = 'db_export.php';
84 require './db_export.php';
86 $active_page = 'tbl_export.php';
87 require './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);
160 elseif ($GLOBALS['compression'] == 'gzip' && @function_exists
('gzencode')) {
161 // without the optional parameter level because it bug
162 $dump_buffer = gzencode($dump_buffer);
164 if ($GLOBALS['save_on_server']) {
165 $write_result = @fwrite
($GLOBALS['file_handle'], $dump_buffer);
166 if (!$write_result ||
($write_result != strlen($dump_buffer))) {
167 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
168 $GLOBALS['message']->addParam($save_filename);
175 $dump_buffer_len = 0;
179 if ($time_start >= $time_now +
30) {
180 $time_start = $time_now;
181 header('X-pmaPing: Pong');
185 if ($GLOBALS['asfile']) {
186 if ($GLOBALS['output_charset_conversion']) {
187 $line = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $line);
189 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
190 $write_result = @fwrite
($GLOBALS['file_handle'], $line);
191 if (!$write_result ||
($write_result != strlen($line))) {
192 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
193 $GLOBALS['message']->addParam($save_filename);
197 if ($time_start >= $time_now +
30) {
198 $time_start = $time_now;
199 header('X-pmaPing: Pong');
202 // We export as file - output normally
206 // We export as html - replace special chars
207 echo htmlspecialchars($line);
211 } // end of the 'PMA_exportOutputHandler()' function
213 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
214 if ($what == 'sql') {
217 $crlf = PMA_whichCrlf();
220 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
222 // Do we need to convert charset?
223 $output_charset_conversion = $asfile && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
224 && isset($charset_of_file) && $charset_of_file != 'utf-8'
227 // Use on the fly compression?
228 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && ($compression == 'gzip' ||
$compression == 'bzip');
229 if ($onfly_compression) {
230 $memory_limit = trim(@ini_get
('memory_limit'));
232 if (empty($memory_limit)) {
233 $memory_limit = 2 * 1024 * 1024;
236 if (strtolower(substr($memory_limit, -1)) == 'm') {
237 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
238 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
239 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
240 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
241 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
243 $memory_limit = (int)$memory_limit;
246 // Some of memory is needed for other thins and as treshold.
247 // Nijel: During export I had allocated (see memory_get_usage function)
248 // approx 1.2MB so this comes from that.
249 if ($memory_limit > 1500000) {
250 $memory_limit -= 1500000;
253 // Some memory is needed for compression, assume 1/3
257 // Generate filename and mime type if needed
259 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
260 if ($export_type == 'server') {
261 if (isset($remember_template)) {
262 $GLOBALS['PMA_Config']->setUserValue('pma_server_filename_template',
263 'Export/file_template_server', $filename_template);
265 } elseif ($export_type == 'database') {
266 if (isset($remember_template)) {
267 $GLOBALS['PMA_Config']->setUserValue('pma_db_filename_template',
268 'Export/file_template_database', $filename_template);
271 if (isset($remember_template)) {
272 $GLOBALS['PMA_Config']->setUserValue('pma_table_filename_template',
273 'Export/file_template_table', $filename_template);
276 $filename = PMA_expandUserString($filename_template);
277 $filename = PMA_sanitize_filename($filename);
279 // Grab basic dump extension and mime type
280 // Check if the user already added extension; get the substring where the extension would be if it was included
281 $extension_start_pos = strlen($filename) - strlen($export_list[$type]['extension']) - 1;
282 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
283 $required_extension = "." . $export_list[$type]['extension'];
284 if (strtolower($user_extension) != $required_extension) {
285 $filename .= $required_extension;
287 $mime_type = $export_list[$type]['mime_type'];
289 // If dump is going to be compressed, set correct mime_type and add
290 // compression to extension
291 if ($compression == 'bzip') {
293 $mime_type = 'application/x-bzip2';
294 } elseif ($compression == 'gzip') {
296 $mime_type = 'application/x-gzip';
297 } elseif ($compression == 'zip') {
299 $mime_type = 'application/zip';
303 // Open file on server if needed
304 if ($save_on_server) {
305 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
307 if (file_exists($save_filename) && ((!$quick_export && empty($onserverover)) ||
($quick_export && $_REQUEST['quick_export_onserverover'] != 'saveitover'))) {
308 $message = PMA_Message
::error(__('File %s already exists on server, change filename or check overwrite option.'));
309 $message->addParam($save_filename);
311 if (is_file($save_filename) && !is_writable($save_filename)) {
312 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
313 $message->addParam($save_filename);
315 if (!$file_handle = @fopen
($save_filename, 'w')) {
316 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
317 $message->addParam($save_filename);
321 if (isset($message)) {
322 require_once './libraries/header.inc.php';
323 if ($export_type == 'server') {
324 $active_page = 'server_export.php';
325 require './server_export.php';
326 } elseif ($export_type == 'database') {
327 $active_page = 'db_export.php';
328 require './db_export.php';
330 $active_page = 'tbl_export.php';
331 require './tbl_export.php';
338 * Send headers depending on whether the user chose to download a dump file
341 if (!$save_on_server) {
344 // (avoid rewriting data containing HTML with anchors and forms;
345 // this was reported to happen under Plesk)
346 @ini_set
('url_rewriter.tags','');
347 $filename = PMA_sanitize_filename($filename);
349 PMA_download_header($filename, $mime_type);
352 if ($export_type == 'database') {
353 $num_tables = count($tables);
354 if ($num_tables == 0) {
355 $message = PMA_Message
::error(__('No tables found in database.'));
356 require_once './libraries/header.inc.php';
357 $active_page = 'db_export.php';
358 require './db_export.php';
362 $backup_cfgServer = $cfg['Server'];
363 require_once './libraries/header.inc.php';
364 $cfg['Server'] = $backup_cfgServer;
365 unset($backup_cfgServer);
366 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
367 //echo ' <pre>' . "\n";
370 * Displays a back button with all the $_REQUEST data in the URL (store in a variable to also display after the textarea)
372 $back_button = '<p>[ <a href="';
373 if ($export_type == 'server') {
374 $back_button .= 'server_export.php?' . PMA_generate_common_url();
375 } elseif ($export_type == 'database') {
376 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
378 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
381 // Convert the multiple select elements from an array to a string
382 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
383 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
384 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
385 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
388 foreach ($_REQUEST as $name => $value) {
389 $back_button .= '&' . urlencode($name) . '=' . urlencode($value);
391 $back_button .= '&repopulate=1">Back</a> ]</p>';
394 echo ' <form name="nofunction">' . "\n"
395 // remove auto-select for now: there is no way to select
396 // only a part of the text; anyway, it should obey
397 // $cfg['TextareaAutoSelect']
398 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
399 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
403 // Fake loop just to allow skip of remain of this code by break, I'd really
404 // need exceptions here :-)
407 // Add possibly some comments to export
408 if (!PMA_exportHeader()) {
412 // Will we need relation & co. setup?
413 $do_relation = isset($GLOBALS[$what . '_relation']);
414 $do_comments = isset($GLOBALS[$what . '_include_comments']);
415 $do_mime = isset($GLOBALS[$what . '_mime']);
416 if ($do_relation ||
$do_comments ||
$do_mime) {
417 $cfgRelation = PMA_getRelationsParam();
420 require_once './libraries/transformations.lib.php';
423 // Include dates in export?
424 $do_dates = isset($GLOBALS[$what . '_dates']);
429 // Gets the number of tables if a dump of a database has been required
430 if ($export_type == 'server') {
431 if (isset($db_select)) {
432 $tmp_select = implode($db_select, '|');
433 $tmp_select = '|' . $tmp_select . '|';
435 // Walk over databases
436 foreach ($GLOBALS['pma']->databases
as $current_db) {
437 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
438 ||
! isset($tmp_select)) {
439 if (!PMA_exportDBHeader($current_db)) {
442 if (!PMA_exportDBCreate($current_db)) {
445 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
446 PMA_exportRoutines($current_db);
449 $tables = PMA_DBI_get_tables($current_db);
451 foreach ($tables as $table) {
452 // if this is a view, collect it for later; views must be exported
454 $is_view = PMA_Table
::isView($current_db, $table);
458 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
459 // for a view, export a stand-in definition of the table
460 // to resolve view dependencies
461 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)) {
465 // if this is a view or a merge table, don't export data
466 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($current_db, $table))) {
467 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
468 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
472 // now export the triggers (needs to be done after the data because
473 // triggers can modify already imported tables)
474 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
475 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
480 foreach ($views as $view) {
481 // no data export for a view
482 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
483 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
488 if (!PMA_exportDBFooter($current_db)) {
493 } elseif ($export_type == 'database') {
494 if (!PMA_exportDBHeader($db)) {
498 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
499 PMA_exportRoutines($db);
504 // $tables contains the choices from the user (via $table_select)
505 foreach ($tables as $table) {
506 // if this is a view, collect it for later; views must be exported after
508 $is_view = PMA_Table
::isView($db, $table);
512 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
513 // for a view, export a stand-in definition of the table
514 // to resolve view dependencies
515 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
519 // if this is a view or a merge table, don't export data
520 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($db, $table))) {
521 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
522 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
526 // now export the triggers (needs to be done after the data because
527 // triggers can modify already imported tables)
528 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
529 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
534 foreach ($views as $view) {
535 // no data export for a view
536 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
537 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
543 if (!PMA_exportDBFooter($db)) {
547 if (!PMA_exportDBHeader($db)) {
550 // We export just one table
551 // $allrows comes from the form when "Dump all rows" has been selected
552 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
553 $add_query = ' LIMIT '
554 . (($limit_from > 0) ?
$limit_from . ', ' : '')
560 $is_view = PMA_Table
::isView($db, $table);
561 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
562 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'create_view' : 'create_table', $export_type)) {
566 // If this is an export of a single view, we have to export data;
567 // for example, a PDF report
568 // if it is a merge table, no data is exported
569 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && ! PMA_Table
::isMerge($db, $table)) {
570 if (!empty($sql_query)) {
571 // only preg_replace if needed
572 if (!empty($add_query)) {
573 // remove trailing semicolon before adding a LIMIT
574 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
576 $local_query = $sql_query . $add_query;
577 PMA_DBI_select_db($db);
579 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
581 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
585 // now export the triggers (needs to be done after the data because
586 // triggers can modify already imported tables)
587 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
588 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
592 if (!PMA_exportDBFooter($db)) {
596 if (!PMA_exportFooter()) {
603 if ($save_on_server && isset($message)) {
604 require_once './libraries/header.inc.php';
605 if ($export_type == 'server') {
606 $active_page = 'server_export.php';
607 require './server_export.php';
608 } elseif ($export_type == 'database') {
609 $active_page = 'db_export.php';
610 require './db_export.php';
612 $active_page = 'tbl_export.php';
613 require './tbl_export.php';
619 * Send the dump as a file...
621 if (!empty($asfile)) {
622 // Convert the charset if required.
623 if ($output_charset_conversion) {
624 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
627 // Do the compression
628 // 1. as a zipped file
629 if ($compression == 'zip') {
630 if (@function_exists
('gzcompress')) {
631 $zipfile = new zipfile();
632 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
633 $dump_buffer = $zipfile -> file();
636 // 2. as a bzipped file
637 elseif ($compression == 'bzip') {
638 if (@function_exists
('bzcompress')) {
639 $dump_buffer = bzcompress($dump_buffer);
642 // 3. as a gzipped file
643 elseif ($compression == 'gzip') {
644 if (@function_exists
('gzencode') && !@ini_get
('zlib.output_compression')) {
645 // without the optional parameter level because it bug
646 $dump_buffer = gzencode($dump_buffer);
650 /* If ve saved on server, we have to close file now */
651 if ($save_on_server) {
652 $write_result = @fwrite
($file_handle, $dump_buffer);
653 fclose($file_handle);
654 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
655 $message = new PMA_Message(__('Insufficient space to save the file %s.'), PMA_Message
::ERROR
, $save_filename);
657 $message = new PMA_Message(__('Dump has been saved to file %s.'), PMA_Message
::SUCCESS
, $save_filename);
660 require_once './libraries/header.inc.php';
661 if ($export_type == 'server') {
662 $active_page = 'server_export.php';
663 require_once './server_export.php';
664 } elseif ($export_type == 'database') {
665 $active_page = 'db_export.php';
666 require_once './db_export.php';
668 $active_page = 'tbl_export.php';
669 require_once './tbl_export.php';
677 * Displays the dump...
681 * Close the html tags and add the footers in dump is displayed on screen
683 echo '</textarea>' . "\n"
688 echo '</div>' . "\n";
691 <script type
="text/javascript">
693 var bodyWidth
=null; var bodyHeight
=null;
694 if (document
.getElementById('textSQLDUMP')) {
695 bodyWidth
= self
.innerWidth
;
696 bodyHeight
= self
.innerHeight
;
697 if (!bodyWidth
&& !bodyHeight
) {
698 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
699 bodyWidth
= document
.body
.clientWidth
;
700 bodyHeight
= document
.body
.clientHeight
;
701 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
702 bodyWidth
= document
.documentElement
.clientWidth
;
703 bodyHeight
= document
.documentElement
.clientHeight
;
706 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
707 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
712 require './libraries/footer.inc.php';