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);
278 // convert filename to iso-8859-1, it is safer
279 $filename = PMA_convert_string('utf-8', 'iso-8859-1', $filename);
281 // Grab basic dump extension and mime type
282 // Check if the user already added extension; get the substring where the extension would be if it was included
283 $extension_start_pos = strlen($filename) - strlen($export_list[$type]['extension']) - 1;
284 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
285 $required_extension = "." . $export_list[$type]['extension'];
286 if (strtolower($user_extension) != $required_extension) {
287 $filename .= $required_extension;
289 $mime_type = $export_list[$type]['mime_type'];
291 // If dump is going to be compressed, set correct mime_type and add
292 // compression to extension
293 if ($compression == 'bzip') {
295 $mime_type = 'application/x-bzip2';
296 } elseif ($compression == 'gzip') {
298 $mime_type = 'application/x-gzip';
299 } elseif ($compression == 'zip') {
301 $mime_type = 'application/zip';
305 // Open file on server if needed
306 if ($save_on_server) {
307 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
309 if (file_exists($save_filename) && ((!$quick_export && empty($onserverover)) ||
($quick_export && $_REQUEST['quick_export_onserverover'] != 'saveitover'))) {
310 $message = PMA_Message
::error(__('File %s already exists on server, change filename or check overwrite option.'));
311 $message->addParam($save_filename);
313 if (is_file($save_filename) && !is_writable($save_filename)) {
314 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
315 $message->addParam($save_filename);
317 if (!$file_handle = @fopen
($save_filename, 'w')) {
318 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
319 $message->addParam($save_filename);
323 if (isset($message)) {
324 require_once './libraries/header.inc.php';
325 if ($export_type == 'server') {
326 $active_page = 'server_export.php';
327 require './server_export.php';
328 } elseif ($export_type == 'database') {
329 $active_page = 'db_export.php';
330 require './db_export.php';
332 $active_page = 'tbl_export.php';
333 require './tbl_export.php';
340 * Send headers depending on whether the user chose to download a dump file
343 if (!$save_on_server) {
346 // (avoid rewriting data containing HTML with anchors and forms;
347 // this was reported to happen under Plesk)
348 @ini_set
('url_rewriter.tags','');
350 PMA_download_header($filename, $mime_type);
353 if ($export_type == 'database') {
354 $num_tables = count($tables);
355 if ($num_tables == 0) {
356 $message = PMA_Message
::error(__('No tables found in database.'));
357 require_once './libraries/header.inc.php';
358 $active_page = 'db_export.php';
359 require './db_export.php';
363 $backup_cfgServer = $cfg['Server'];
364 require_once './libraries/header.inc.php';
365 $cfg['Server'] = $backup_cfgServer;
366 unset($backup_cfgServer);
367 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
368 //echo ' <pre>' . "\n";
371 * Displays a back button with all the $_REQUEST data in the URL (store in a variable to also display after the textarea)
373 $back_button = '<p>[ <a href="';
374 if ($export_type == 'server') {
375 $back_button .= 'server_export.php?' . PMA_generate_common_url();
376 } elseif ($export_type == 'database') {
377 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
379 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
382 // Convert the multiple select elements from an array to a string
383 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
384 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
385 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
386 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
389 foreach ($_REQUEST as $name => $value) {
390 $back_button .= '&' . urlencode($name) . '=' . urlencode($value);
392 $back_button .= '&repopulate=1">Back</a> ]</p>';
395 echo ' <form name="nofunction">' . "\n"
396 // remove auto-select for now: there is no way to select
397 // only a part of the text; anyway, it should obey
398 // $cfg['TextareaAutoSelect']
399 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
400 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
404 // Fake loop just to allow skip of remain of this code by break, I'd really
405 // need exceptions here :-)
408 // Add possibly some comments to export
409 if (!PMA_exportHeader()) {
413 // Will we need relation & co. setup?
414 $do_relation = isset($GLOBALS[$what . '_relation']);
415 $do_comments = isset($GLOBALS[$what . '_include_comments']);
416 $do_mime = isset($GLOBALS[$what . '_mime']);
417 if ($do_relation ||
$do_comments ||
$do_mime) {
418 $cfgRelation = PMA_getRelationsParam();
421 require_once './libraries/transformations.lib.php';
424 // Include dates in export?
425 $do_dates = isset($GLOBALS[$what . '_dates']);
430 // Gets the number of tables if a dump of a database has been required
431 if ($export_type == 'server') {
432 if (isset($db_select)) {
433 $tmp_select = implode($db_select, '|');
434 $tmp_select = '|' . $tmp_select . '|';
436 // Walk over databases
437 foreach ($GLOBALS['pma']->databases
as $current_db) {
438 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
439 ||
! isset($tmp_select)) {
440 if (!PMA_exportDBHeader($current_db)) {
443 if (!PMA_exportDBCreate($current_db)) {
446 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
447 PMA_exportRoutines($current_db);
450 $tables = PMA_DBI_get_tables($current_db);
452 foreach ($tables as $table) {
453 // if this is a view, collect it for later; views must be exported
455 $is_view = PMA_Table
::isView($current_db, $table);
459 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
460 // for a view, export a stand-in definition of the table
461 // to resolve view dependencies
462 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)) {
466 // if this is a view or a merge table, don't export data
467 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($current_db, $table))) {
468 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
469 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
473 // now export the triggers (needs to be done after the data because
474 // triggers can modify already imported tables)
475 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
476 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
481 foreach ($views as $view) {
482 // no data export for a view
483 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
484 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
489 if (!PMA_exportDBFooter($current_db)) {
494 } elseif ($export_type == 'database') {
495 if (!PMA_exportDBHeader($db)) {
499 if (function_exists('PMA_exportRoutines') && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false && isset($GLOBALS['sql_procedure_function'])) {
500 PMA_exportRoutines($db);
505 // $tables contains the choices from the user (via $table_select)
506 foreach ($tables as $table) {
507 // if this is a view, collect it for later; views must be exported after
509 $is_view = PMA_Table
::isView($db, $table);
513 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
514 // for a view, export a stand-in definition of the table
515 // to resolve view dependencies
516 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
520 // if this is a view or a merge table, don't export data
521 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($db, $table))) {
522 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
523 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
527 // now export the triggers (needs to be done after the data because
528 // triggers can modify already imported tables)
529 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
530 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
535 foreach ($views as $view) {
536 // no data export for a view
537 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
538 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
544 if (!PMA_exportDBFooter($db)) {
548 if (!PMA_exportDBHeader($db)) {
551 // We export just one table
552 // $allrows comes from the form when "Dump all rows" has been selected
553 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
554 $add_query = ' LIMIT '
555 . (($limit_from > 0) ?
$limit_from . ', ' : '')
561 $is_view = PMA_Table
::isView($db, $table);
562 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
563 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'create_view' : 'create_table', $export_type)) {
567 // If this is an export of a single view, we have to export data;
568 // for example, a PDF report
569 // if it is a merge table, no data is exported
570 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && ! PMA_Table
::isMerge($db, $table)) {
571 if (!empty($sql_query)) {
572 // only preg_replace if needed
573 if (!empty($add_query)) {
574 // remove trailing semicolon before adding a LIMIT
575 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
577 $local_query = $sql_query . $add_query;
578 PMA_DBI_select_db($db);
580 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
582 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
586 // now export the triggers (needs to be done after the data because
587 // triggers can modify already imported tables)
588 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
589 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
593 if (!PMA_exportDBFooter($db)) {
597 if (!PMA_exportFooter()) {
604 if ($save_on_server && isset($message)) {
605 require_once './libraries/header.inc.php';
606 if ($export_type == 'server') {
607 $active_page = 'server_export.php';
608 require './server_export.php';
609 } elseif ($export_type == 'database') {
610 $active_page = 'db_export.php';
611 require './db_export.php';
613 $active_page = 'tbl_export.php';
614 require './tbl_export.php';
620 * Send the dump as a file...
622 if (!empty($asfile)) {
623 // Convert the charset if required.
624 if ($output_charset_conversion) {
625 $dump_buffer = PMA_convert_string('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
628 // Do the compression
629 // 1. as a zipped file
630 if ($compression == 'zip') {
631 if (@function_exists
('gzcompress')) {
632 $zipfile = new zipfile();
633 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
634 $dump_buffer = $zipfile -> file();
637 // 2. as a bzipped file
638 elseif ($compression == 'bzip') {
639 if (@function_exists
('bzcompress')) {
640 $dump_buffer = bzcompress($dump_buffer);
643 // 3. as a gzipped file
644 elseif ($compression == 'gzip') {
645 if (@function_exists
('gzencode') && !@ini_get
('zlib.output_compression')) {
646 // without the optional parameter level because it bug
647 $dump_buffer = gzencode($dump_buffer);
651 /* If ve saved on server, we have to close file now */
652 if ($save_on_server) {
653 $write_result = @fwrite
($file_handle, $dump_buffer);
654 fclose($file_handle);
655 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
656 $message = new PMA_Message(__('Insufficient space to save the file %s.'), PMA_Message
::ERROR
, $save_filename);
658 $message = new PMA_Message(__('Dump has been saved to file %s.'), PMA_Message
::SUCCESS
, $save_filename);
661 require_once './libraries/header.inc.php';
662 if ($export_type == 'server') {
663 $active_page = 'server_export.php';
664 require_once './server_export.php';
665 } elseif ($export_type == 'database') {
666 $active_page = 'db_export.php';
667 require_once './db_export.php';
669 $active_page = 'tbl_export.php';
670 require_once './tbl_export.php';
678 * Displays the dump...
682 * Close the html tags and add the footers in dump is displayed on screen
684 echo '</textarea>' . "\n"
689 echo '</div>' . "\n";
692 <script type
="text/javascript">
694 var bodyWidth
=null; var bodyHeight
=null;
695 if (document
.getElementById('textSQLDUMP')) {
696 bodyWidth
= self
.innerWidth
;
697 bodyHeight
= self
.innerHeight
;
698 if (!bodyWidth
&& !bodyHeight
) {
699 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
700 bodyWidth
= document
.body
.clientWidth
;
701 bodyHeight
= document
.body
.clientHeight
;
702 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
703 bodyWidth
= document
.documentElement
.clientWidth
;
704 bodyHeight
= document
.documentElement
.clientHeight
;
707 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
708 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
713 require './libraries/footer.inc.php';