Patch #2984893 - InnoDB storage page emits warning
[phpmyadmin-themes.git] / export.php
blobdae4cf4f02682f69d04cd472ef06f1fb304011d0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @todo too much die here, or?
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 /**
10 * Get the variables sent or posted to this script and a core script
12 require_once './libraries/common.inc.php';
13 require_once './libraries/zip.lib.php';
14 require_once './libraries/plugin_interface.lib.php';
16 PMA_checkParameters(array('what', 'export_type'));
18 // Scan plugins
19 $export_list = PMA_getPlugins('./libraries/export/', array('export_type' => $export_type, 'single_table' => isset($single_table)));
21 // Backward compatbility
22 $type = $what;
24 // Check export type
25 if (!isset($export_list[$type])) {
26 die('Bad type!');
29 /**
30 * valid compression methods
32 $compression_methods = array(
33 'zip',
34 'gzip',
35 'bzip',
38 /**
39 * init and variable checking
41 $compression = false;
42 $onserver = false;
43 $save_on_server = false;
44 $buffer_needed = false;
45 if (empty($_REQUEST['asfile'])) {
46 $asfile = false;
47 } else {
48 $asfile = true;
49 if (in_array($_REQUEST['compression'], $compression_methods)) {
50 $compression = $_REQUEST['compression'];
51 $buffer_needed = true;
53 if (!empty($_REQUEST['onserver'])) {
54 $onserver = $_REQUEST['onserver'];
55 // Will we save dump on server?
56 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
60 // Does export require to be into file?
61 if (isset($export_list[$type]['force_file']) && ! $asfile) {
62 $message = PMA_Message::error('strExportMustBeFile');
63 $GLOBALS['js_include'][] = 'functions.js';
64 require_once './libraries/header.inc.php';
65 if ($export_type == 'server') {
66 $active_page = 'server_export.php';
67 require './server_export.php';
68 } elseif ($export_type == 'database') {
69 $active_page = 'db_export.php';
70 require './db_export.php';
71 } else {
72 $active_page = 'tbl_export.php';
73 require './tbl_export.php';
75 exit();
78 // Generate error url and check for needed variables
79 if ($export_type == 'server') {
80 $err_url = 'server_export.php?' . PMA_generate_common_url();
81 } elseif ($export_type == 'database' && strlen($db)) {
82 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
83 // Check if we have something to export
84 if (isset($table_select)) {
85 $tables = $table_select;
86 } else {
87 $tables = array();
89 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
90 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
91 } else {
92 die('Bad parameters!');
95 // Get the functions specific to the export type
96 require './libraries/export/' . PMA_securePath($type) . '.php';
98 /**
99 * Increase time limit for script execution and initializes some variables
101 @set_time_limit($cfg['ExecTimeLimit']);
102 if (!empty($cfg['MemoryLimit'])) {
103 @ini_set('memory_limit', $cfg['MemoryLimit']);
106 // Start with empty buffer
107 $dump_buffer = '';
108 $dump_buffer_len = 0;
110 // We send fake headers to avoid browser timeout when buffering
111 $time_start = time();
115 * Output handler for all exports, if needed buffering, it stores data into
116 * $dump_buffer, otherwise it prints thems out.
118 * @param string the insert statement
120 * @return bool Whether output suceeded
122 function PMA_exportOutputHandler($line)
124 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
126 // Kanji encoding convert feature
127 if ($GLOBALS['output_kanji_conversion']) {
128 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
130 // If we have to buffer data, we will perform everything at once at the end
131 if ($GLOBALS['buffer_needed']) {
133 $dump_buffer .= $line;
134 if ($GLOBALS['onfly_compression']) {
136 $dump_buffer_len += strlen($line);
138 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
139 if ($GLOBALS['output_charset_conversion']) {
140 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
142 // as bzipped
143 if ($GLOBALS['compression'] == 'bzip' && @function_exists('bzcompress')) {
144 $dump_buffer = bzcompress($dump_buffer);
146 // as a gzipped file
147 elseif ($GLOBALS['compression'] == 'gzip' && @function_exists('gzencode')) {
148 // without the optional parameter level because it bug
149 $dump_buffer = gzencode($dump_buffer);
151 if ($GLOBALS['save_on_server']) {
152 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
153 if (!$write_result || ($write_result != strlen($dump_buffer))) {
154 $GLOBALS['message'] = PMA_Message::error('strNoSpace');
155 $GLOBALS['message']->addParam($save_filename);
156 return false;
158 } else {
159 echo $dump_buffer;
161 $dump_buffer = '';
162 $dump_buffer_len = 0;
164 } else {
165 $time_now = time();
166 if ($time_start >= $time_now + 30) {
167 $time_start = $time_now;
168 header('X-pmaPing: Pong');
169 } // end if
171 } else {
172 if ($GLOBALS['asfile']) {
173 if ($GLOBALS['output_charset_conversion']) {
174 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
176 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
177 $write_result = @fwrite($GLOBALS['file_handle'], $line);
178 if (!$write_result || ($write_result != strlen($line))) {
179 $GLOBALS['message'] = PMA_Message::error('strNoSpace');
180 $GLOBALS['message']->addParam($save_filename);
181 return false;
183 $time_now = time();
184 if ($time_start >= $time_now + 30) {
185 $time_start = $time_now;
186 header('X-pmaPing: Pong');
187 } // end if
188 } else {
189 // We export as file - output normally
190 echo $line;
192 } else {
193 // We export as html - replace special chars
194 echo htmlspecialchars($line);
197 return true;
198 } // end of the 'PMA_exportOutputHandler()' function
200 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
201 if ($what == 'sql') {
202 $crlf = "\n";
203 } else {
204 $crlf = PMA_whichCrlf();
207 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
209 // Do we need to convert charset?
210 $output_charset_conversion = $asfile && $cfg['AllowAnywhereRecoding']
211 && isset($charset_of_file) && $charset_of_file != $charset
212 && $type != 'xls';
214 // Use on the fly compression?
215 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && ($compression == 'gzip' || $compression == 'bzip');
216 if ($onfly_compression) {
217 $memory_limit = trim(@ini_get('memory_limit'));
218 // 2 MB as default
219 if (empty($memory_limit)) {
220 $memory_limit = 2 * 1024 * 1024;
223 if (strtolower(substr($memory_limit, -1)) == 'm') {
224 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
225 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
226 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
227 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
228 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
229 } else {
230 $memory_limit = (int)$memory_limit;
233 // Some of memory is needed for other thins and as treshold.
234 // Nijel: During export I had allocated (see memory_get_usage function)
235 // approx 1.2MB so this comes from that.
236 if ($memory_limit > 1500000) {
237 $memory_limit -= 1500000;
240 // Some memory is needed for compression, assume 1/3
241 $memory_limit /= 8;
244 // Generate filename and mime type if needed
245 if ($asfile) {
246 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
247 if ($export_type == 'server') {
248 if (isset($remember_template)) {
249 PMA_setCookie('pma_server_filename_template', $filename_template);
251 $filename = str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template));
252 } elseif ($export_type == 'database') {
253 if (isset($remember_template)) {
254 PMA_setCookie('pma_db_filename_template', $filename_template);
256 $filename = str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)));
257 } else {
258 if (isset($remember_template)) {
259 PMA_setCookie('pma_table_filename_template', $filename_template);
261 $filename = str_replace('__TABLE__', $table, str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template))));
264 // convert filename to iso-8859-1, it is safer
265 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] )) {
266 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
267 } else {
268 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
271 // Grab basic dump extension and mime type
272 $filename .= '.' . $export_list[$type]['extension'];
273 $mime_type = $export_list[$type]['mime_type'];
275 // If dump is going to be compressed, set correct mime_type and add
276 // compression to extension
277 if ($compression == 'bzip') {
278 $filename .= '.bz2';
279 $mime_type = 'application/x-bzip2';
280 } elseif ($compression == 'gzip') {
281 $filename .= '.gz';
282 $mime_type = 'application/x-gzip';
283 } elseif ($compression == 'zip') {
284 $filename .= '.zip';
285 $mime_type = 'application/zip';
289 // Open file on server if needed
290 if ($save_on_server) {
291 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
292 unset($message);
293 if (file_exists($save_filename) && empty($onserverover)) {
294 $message = PMA_Message::error('strFileAlreadyExists');
295 $message->addParam($save_filename);
296 } else {
297 if (is_file($save_filename) && !is_writable($save_filename)) {
298 $message = PMA_Message::error('strNoPermission');
299 $message->addParam($save_filename);
300 } else {
301 if (!$file_handle = @fopen($save_filename, 'w')) {
302 $message = PMA_Message::error('strNoPermission');
303 $message->addParam($save_filename);
307 if (isset($message)) {
308 $GLOBALS['js_include'][] = 'functions.js';
309 require_once './libraries/header.inc.php';
310 if ($export_type == 'server') {
311 $active_page = 'server_export.php';
312 require './server_export.php';
313 } elseif ($export_type == 'database') {
314 $active_page = 'db_export.php';
315 require './db_export.php';
316 } else {
317 $active_page = 'tbl_export.php';
318 require './tbl_export.php';
320 exit();
325 * Send headers depending on whether the user chose to download a dump file
326 * or not
328 if (!$save_on_server) {
329 if ($asfile) {
330 // Download
331 // (avoid rewriting data containing HTML with anchors and forms;
332 // this was reported to happen under Plesk)
333 @ini_set('url_rewriter.tags','');
335 header('Content-Type: ' . $mime_type);
336 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
337 // lem9: Tested behavior of
338 // IE 5.50.4807.2300
339 // IE 6.0.2800.1106 (small glitch, asks twice when I click Open)
340 // IE 6.0.2900.2180
341 // Firefox 1.0.6
342 // in http and https
343 header('Content-Disposition: attachment; filename="' . $filename . '"');
344 if (PMA_USR_BROWSER_AGENT == 'IE') {
345 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
346 header('Pragma: public');
347 } else {
348 header('Pragma: no-cache');
349 // test case: exporting a database into a .gz file with Safari
350 // would produce files not having the current time
351 // (added this header for Safari but should not harm other browsers)
352 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
354 } else {
355 // HTML
356 if ($export_type == 'database') {
357 $num_tables = count($tables);
358 if ($num_tables == 0) {
359 $message = PMA_Message::error('strNoTablesFound');
360 $GLOBALS['js_include'][] = 'functions.js';
361 require_once './libraries/header.inc.php';
362 $active_page = 'db_export.php';
363 require './db_export.php';
364 exit();
367 $backup_cfgServer = $cfg['Server'];
368 require_once './libraries/header.inc.php';
369 $cfg['Server'] = $backup_cfgServer;
370 unset($backup_cfgServer);
371 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
372 //echo ' <pre>' . "\n";
373 echo ' <form name="nofunction">' . "\n"
374 // remove auto-select for now: there is no way to select
375 // only a part of the text; anyway, it should obey
376 // $cfg['TextareaAutoSelect']
377 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
378 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
379 } // end download
382 // Fake loop just to allow skip of remain of this code by break, I'd really
383 // need exceptions here :-)
384 do {
386 // Add possibly some comments to export
387 if (!PMA_exportHeader()) {
388 break;
391 // Will we need relation & co. setup?
392 $do_relation = isset($GLOBALS[$what . '_relation']);
393 $do_comments = isset($GLOBALS[$what . '_comments']);
394 $do_mime = isset($GLOBALS[$what . '_mime']);
395 if ($do_relation || $do_comments || $do_mime) {
396 require_once './libraries/relation.lib.php';
397 $cfgRelation = PMA_getRelationsParam();
399 if ($do_mime) {
400 require_once './libraries/transformations.lib.php';
403 // Include dates in export?
404 $do_dates = isset($GLOBALS[$what . '_dates']);
407 * Builds the dump
409 // Gets the number of tables if a dump of a database has been required
410 if ($export_type == 'server') {
411 if (isset($db_select)) {
412 $tmp_select = implode($db_select, '|');
413 $tmp_select = '|' . $tmp_select . '|';
415 // Walk over databases
416 foreach ($GLOBALS['pma']->databases as $current_db) {
417 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
418 || !isset($tmp_select)) {
419 if (!PMA_exportDBHeader($current_db)) {
420 break 2;
422 if (!PMA_exportDBCreate($current_db)) {
423 break 2;
425 $tables = PMA_DBI_get_tables($current_db);
426 $views = array();
427 foreach ($tables as $table) {
428 // if this is a view, collect it for later; views must be exported
429 // after the tables
430 $is_view = PMA_Table::isView($current_db, $table);
431 if ($is_view) {
432 $views[] = $table;
434 if (isset($GLOBALS[$what . '_structure'])) {
435 // for a view, export a stand-in definition of the table
436 // to resolve view dependencies
437 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)) {
438 break 3;
441 // if this is a view or a merge table, don't export data
442 if (isset($GLOBALS[$what . '_data']) && !($is_view || PMA_Table::isMerge($current_db, $table))) {
443 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
444 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
445 break 3;
448 // now export the triggers (needs to be done after the data because
449 // triggers can modify already imported tables)
450 if (isset($GLOBALS[$what . '_structure'])) {
451 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
452 break 2;
456 foreach($views as $view) {
457 // no data export for a view
458 if (isset($GLOBALS[$what . '_structure'])) {
459 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
460 break 3;
464 if (!PMA_exportDBFooter($current_db)) {
465 break 2;
469 } elseif ($export_type == 'database') {
470 if (!PMA_exportDBHeader($db)) {
471 break;
473 $i = 0;
474 $views = array();
475 // $tables contains the choices from the user (via $table_select)
476 foreach ($tables as $table) {
477 // if this is a view, collect it for later; views must be exported after
478 // the tables
479 $is_view = PMA_Table::isView($db, $table);
480 if ($is_view) {
481 $views[] = $table;
483 if (isset($GLOBALS[$what . '_structure'])) {
484 // for a view, export a stand-in definition of the table
485 // to resolve view dependencies
486 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ? 'stand_in' : 'create_table', $export_type)) {
487 break 2;
490 // if this is a view or a merge table, don't export data
491 if (isset($GLOBALS[$what . '_data']) && !($is_view || PMA_Table::isMerge($db, $table))) {
492 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
493 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
494 break 2;
497 // now export the triggers (needs to be done after the data because
498 // triggers can modify already imported tables)
499 if (isset($GLOBALS[$what . '_structure'])) {
500 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
501 break 2;
505 foreach ($views as $view) {
506 // no data export for a view
507 if (isset($GLOBALS[$what . '_structure'])) {
508 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
509 break 2;
514 if (!PMA_exportDBFooter($db)) {
515 break;
517 } else {
518 if (!PMA_exportDBHeader($db)) {
519 break;
521 // We export just one table
522 // $allrows comes from the form when "Dump all rows" has been selected
523 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
524 $add_query = ' LIMIT '
525 . (($limit_from > 0) ? $limit_from . ', ' : '')
526 . $limit_to;
527 } else {
528 $add_query = '';
531 $is_view = PMA_Table::isView($db, $table);
532 if (isset($GLOBALS[$what . '_structure'])) {
533 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ? 'create_view' : 'create_table', $export_type)) {
534 break;
537 // If this is an export of a single view, we have to export data;
538 // for example, a PDF report
539 // if it is a merge table, no data is exported
540 if (isset($GLOBALS[$what . '_data']) && ! PMA_Table::isMerge($db, $table)) {
541 if (!empty($sql_query)) {
542 // only preg_replace if needed
543 if (!empty($add_query)) {
544 // remove trailing semicolon before adding a LIMIT
545 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
547 $local_query = $sql_query . $add_query;
548 PMA_DBI_select_db($db);
549 } else {
550 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
552 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
553 break;
556 // now export the triggers (needs to be done after the data because
557 // triggers can modify already imported tables)
558 if (isset($GLOBALS[$what . '_structure'])) {
559 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
560 break 2;
563 if (!PMA_exportDBFooter($db)) {
564 break;
567 if (!PMA_exportFooter()) {
568 break;
571 } while (false);
572 // End of fake loop
574 if ($save_on_server && isset($message)) {
575 $GLOBALS['js_include'][] = 'functions.js';
576 require_once './libraries/header.inc.php';
577 if ($export_type == 'server') {
578 $active_page = 'server_export.php';
579 require './server_export.php';
580 } elseif ($export_type == 'database') {
581 $active_page = 'db_export.php';
582 require './db_export.php';
583 } else {
584 $active_page = 'tbl_export.php';
585 require './tbl_export.php';
587 exit();
591 * Send the dump as a file...
593 if (!empty($asfile)) {
594 // Convert the charset if required.
595 if ($output_charset_conversion) {
596 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
599 // Do the compression
600 // 1. as a zipped file
601 if ($compression == 'zip') {
602 if (@function_exists('gzcompress')) {
603 $zipfile = new zipfile();
604 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
605 $dump_buffer = $zipfile -> file();
608 // 2. as a bzipped file
609 elseif ($compression == 'bzip') {
610 if (@function_exists('bzcompress')) {
611 $dump_buffer = bzcompress($dump_buffer);
614 // 3. as a gzipped file
615 elseif ($compression == 'gzip') {
616 if (@function_exists('gzencode') && !@ini_get('zlib.output_compression')) {
617 // without the optional parameter level because it bug
618 $dump_buffer = gzencode($dump_buffer);
622 /* If ve saved on server, we have to close file now */
623 if ($save_on_server) {
624 $write_result = @fwrite($file_handle, $dump_buffer);
625 fclose($file_handle);
626 if (strlen($dump_buffer) !=0 && (!$write_result || ($write_result != strlen($dump_buffer)))) {
627 $message = new PMA_Message('strNoSpace', PMA_Message::ERROR, $save_filename);
628 } else {
629 $message = new PMA_Message('strDumpSaved', PMA_Message::SUCCESS, $save_filename);
632 $GLOBALS['js_include'][] = 'functions.js';
633 require_once './libraries/header.inc.php';
634 if ($export_type == 'server') {
635 $active_page = 'server_export.php';
636 require_once './server_export.php';
637 } elseif ($export_type == 'database') {
638 $active_page = 'db_export.php';
639 require_once './db_export.php';
640 } else {
641 $active_page = 'tbl_export.php';
642 require_once './tbl_export.php';
644 exit();
645 } else {
646 echo $dump_buffer;
650 * Displays the dump...
652 else {
654 * Close the html tags and add the footers in dump is displayed on screen
656 //echo ' </pre>' . "\n";
657 echo '</textarea>' . "\n"
658 . ' </form>' . "\n";
659 echo '</div>' . "\n";
660 echo "\n";
662 <script type="text/javascript">
663 //<![CDATA[
664 var bodyWidth=null; var bodyHeight=null;
665 if (document.getElementById('textSQLDUMP')) {
666 bodyWidth = self.innerWidth;
667 bodyHeight = self.innerHeight;
668 if (!bodyWidth && !bodyHeight) {
669 if (document.compatMode && document.compatMode == "BackCompat") {
670 bodyWidth = document.body.clientWidth;
671 bodyHeight = document.body.clientHeight;
672 } else if (document.compatMode && document.compatMode == "CSS1Compat") {
673 bodyWidth = document.documentElement.clientWidth;
674 bodyHeight = document.documentElement.clientHeight;
677 document.getElementById('textSQLDUMP').style.width=(bodyWidth-50) + 'px';
678 document.getElementById('textSQLDUMP').style.height=(bodyHeight-100) + 'px';
680 //]]>
681 </script>
682 <?php
683 require_once './libraries/footer.inc.php';
684 } // end if