lang
[phpmyadmin/crack.git] / export.php3
blob87c6b7bd9bd4b37d35d00f66c40549b527bb4931
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Get the variables sent or posted to this script and a core script
7 */
8 require('./libraries/grab_globals.lib.php3');
9 require('./libraries/common.lib.php3');
10 require('./libraries/zip.lib.php3');
12 // What type of export are we doing?
13 if ($what == 'excel') {
14 $type = 'csv';
15 } else {
16 $type = $what;
19 /**
20 * Defines the url to return to in case of error in a sql statement
22 require('./libraries/export/' . $type . '.php3');
24 // Generate error url
25 if ($export_type == 'server') {
26 $err_url = 'server_export.php3?' . PMA_generate_common_url();
27 } elseif ($export_type == 'database') {
28 $err_url = 'db_details_export.php3?' . PMA_generate_common_url($db);
29 } else {
30 $err_url = 'tbl_properties_export.php3?' . PMA_generate_common_url($db, $table);
33 /**
34 * Increase time limit for script execution and initializes some variables
36 @set_time_limit($cfg['ExecTimeLimit']);
38 // Start with empty buffer
39 $dump_buffer = '';
41 // We send fake headers to avoid browser timeout when buffering
42 $time_start = time();
45 /**
46 * Output handler for all exports, if needed buffering, it stores data into
47 * $dump_buffer, otherwise it prints thems out.
49 * @param string the insert statement
51 * @return bool Whether output suceeded
53 function PMA_exportOutputHandler($line)
55 global $time_start;
57 // Kanji encoding convert feature
58 if (function_exists('PMA_kanji_str_conv')) {
59 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
61 // If we have to buffer data, we will perform everything at once at the end
62 if ($GLOBALS['buffer_needed']) {
63 $GLOBALS['dump_buffer'] .= $line;
65 $time_now = time();
66 if ($time_start >= $time_now + 30) {
67 $time_start = $time_now;
68 header('X-pmaPing: Pong');
69 } // end if
70 } else {
71 if ($GLOBALS['asfile']) {
72 if ($GLOBALS['save_on_server']) {
73 $write_result = @fwrite($GLOBALS['file_handle'], $line);
74 if (!$write_result || ($write_result != strlen($line))) {
75 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], $save_filename);
76 return FALSE;
78 $time_now = time();
79 if ($time_start >= $time_now + 30) {
80 $time_start = $time_now;
81 header('X-pmaPing: Pong');
82 } // end if
83 } else {
84 // We export as file - output normally
85 if ($GLOBALS['output_charset_conversion']) {
86 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
88 echo $line;
90 } else {
91 // We export as html - replace special chars
92 echo htmlspecialchars($line);
95 return TRUE;
96 } // end of the 'PMA_exportOutputHandler()' function
98 // Will we save dump on server?
99 $save_on_server = isset($cfg['SaveDir']) && !empty($cfg['SaveDir']) && !empty($onserver);
101 // Ensure compressed formats are associated with the download feature
102 if (empty($asfile)) {
103 if ($save_on_server) {
104 $asfile = TRUE;
105 } elseif (isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip')) {
106 $asfile = TRUE;
107 } else {
108 $asfile = FALSE;
110 } else {
111 $asfile = TRUE;
114 // Defines the default <CR><LF> format
115 $crlf = PMA_whichCrlf();
117 // Do we need to convert charset?
118 $output_charset_conversion = $asfile &&
119 $cfg['AllowAnywhereRecoding'] && $allow_recoding
120 && isset($charset_of_file) && $charset_of_file != $charset;
122 // Set whether we will need buffering
123 $buffer_needed = isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip');
125 // Generate filename and mime type if needed
126 if ($asfile) {
127 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
128 if ($export_type == 'server') {
129 if (isset($remember_template)) {
130 setcookie('pma_server_filename_template', $filename_template , 0,
131 substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')),
132 '', ($pma_uri_parts['scheme'] == 'https'));
134 $filename = str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template));
135 } elseif ($export_type == 'database') {
136 if (isset($remember_template)) {
137 setcookie('pma_db_filename_template', $filename_template , 0,
138 substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')),
139 '', ($pma_uri_parts['scheme'] == 'https'));
141 $filename = str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)));
142 } else {
143 if (isset($remember_template)) {
144 setcookie('pma_table_filename_template', $filename_template , 0,
145 substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')),
146 '', ($pma_uri_parts['scheme'] == 'https'));
148 $filename = str_replace('__TABLE__', $table, str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template))));
151 // convert filename to iso-8859-1, it is safer
152 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
153 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
154 } else {
155 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
158 // Generate basic dump extension
159 if ($type == 'csv') {
160 $filename .= '.csv';
161 $mime_type = 'text/x-csv';
162 } else if ($type == 'xml') {
163 $filename .= '.xml';
164 $mime_type = 'text/xml';
165 } else if ($type == 'latex') {
166 $filename .= '.tex';
167 $mime_type = 'application/x-tex';
168 } else {
169 $filename .= '.sql';
170 // loic1: 'application/octet-stream' is the registered IANA type but
171 // MSIE and Opera seems to prefer 'application/octetstream'
172 $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA')
173 ? 'application/octetstream'
174 : 'application/octet-stream';
177 // If dump is going to be compressed, set correct mime_type and add
178 // compression to extension
179 if (isset($compression) && $compression == 'bzip') {
180 $filename .= '.bz2';
181 $mime_type = 'application/x-bzip';
182 } else if (isset($compression) && $compression == 'gzip') {
183 $filename .= '.gz';
184 $mime_type = 'application/x-gzip';
185 } else if (isset($compression) && $compression == 'zip') {
186 $filename .= '.zip';
187 $mime_type = 'application/x-zip';
191 // Open file on server if needed
192 if ($save_on_server) {
193 $save_filename = $cfg['SaveDir'] . ereg_replace('[/\\]','_',$filename);
194 unset($message);
195 if (file_exists($save_filename) && empty($onserverover)) {
196 $message = sprintf($strFileAlreadyExists, $save_filename);
197 } else {
198 if (is_file($save_filename) && !is_writable($save_filename)) {
199 $message = sprintf($strNoPermission, $save_filename);
200 } else {
201 if (!$file_handle = @fopen($save_filename, 'w')) {
202 $message = sprintf($strNoPermission, $save_filename);
206 if (isset($message)) {
207 $js_to_run = 'functions.js';
208 include('./header.inc.php3');
209 if ($export_type == 'server') {
210 $active_page = 'server_export.php3';
211 include('./server_export.php3');
212 } elseif ($export_type == 'database') {
213 $active_page = 'db_details_export.php3';
214 include('./db_details_export.php3');
215 } else {
216 $active_page = 'tbl_properties_export.php3';
217 include('./tbl_properties_export.php3');
219 exit();
224 * Send headers depending on whether the user chose to download a dump file
225 * or not
227 if (!$save_on_server) {
228 if ($asfile ) {
229 // Download
230 header('Content-Type: ' . $mime_type);
231 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
232 // lem9 & loic1: IE need specific headers
233 if (PMA_USR_BROWSER_AGENT == 'IE') {
234 header('Content-Disposition: inline; filename="' . $filename . '"');
235 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
236 header('Pragma: public');
237 } else {
238 header('Content-Disposition: attachment; filename="' . $filename . '"');
239 header('Pragma: no-cache');
241 } else {
242 // HTML
243 $backup_cfgServer = $cfg['Server'];
244 include('./header.inc.php3');
245 $cfg['Server'] = $backup_cfgServer;
246 unset($backup_cfgServer);
247 echo '<div align="' . $cell_align_left . '">' . "\n";
248 echo ' <pre>' . "\n";
249 } // end download
252 // Check if we have something to export
253 if ($export_type == 'database') {
254 $tables = PMA_mysql_list_tables($db);
255 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
256 if ($num_tables == 0) {
257 $message = $strNoTablesFound;
258 $js_to_run = 'functions.js';
259 include('./header.inc.php3');
260 if ($export_type == 'server') {
261 $active_page = 'server_export.php3';
262 include('./server_export.php3');
263 } elseif ($export_type == 'database') {
264 $active_page = 'db_details_export.php3';
265 include('./db_details_export.php3');
266 } else {
267 $active_page = 'tbl_properties_export.php3';
268 include('./tbl_properties_export.php3');
270 exit();
274 // Add possibly some comments to export
275 PMA_exportHeader();
277 // Will we need relation & co. setup?
278 $do_relation = isset($GLOBALS[$what . '_relation']);
279 $do_comments = isset($GLOBALS[$what . '_comments']);
280 $do_mime = isset($GLOBALS[$what . '_mime']);
281 if ($do_relation || $do_comments || $do_mime) {
282 require('./libraries/relation.lib.php3');
283 $cfgRelation = PMA_getRelationsParam();
285 if ($do_mime) {
286 require('./libraries/transformations.lib.php3');
290 * Builds the dump
292 // Gets the number of tables if a dump of a database has been required
293 if ($export_type == 'server') {
295 * Gets the databases list - if it has not been built yet
297 if ($server > 0 && empty($dblist)) {
298 PMA_availableDatabases();
301 if (isset($db_select)) {
302 $tmp_select = implode($db_select, '|');
303 $tmp_select = '|' . $tmp_select . '|';
305 // Walk over databases
306 reset($dblist);
307 while (list(, $current_db) = each($dblist)) {
308 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
309 || !isset($tmp_select)) {
310 PMA_exportDBHeader($current_db);
311 PMA_exportDBCreate($current_db);
312 $tables = PMA_mysql_list_tables($current_db);
313 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
314 $i = 0;
315 while ($i < $num_tables) {
316 $table = PMA_mysql_tablename($tables, $i);
317 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
318 if (isset($GLOBALS[$what . '_structure'])) PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime);
319 if (isset($GLOBALS[$what . '_data'])) PMA_exportData($current_db, $table, $crlf, $err_url, $local_query);
320 $i++;
322 PMA_exportDBFooter($current_db);
325 } elseif ($export_type == 'database') {
326 PMA_exportDBHeader($db);
327 if (isset($table_select)) {
328 $tmp_select = implode($table_select, '|');
329 $tmp_select = '|' . $tmp_select . '|';
331 $i = 0;
332 while ($i < $num_tables) {
333 $table = PMA_mysql_tablename($tables, $i);
334 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
335 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
336 || !isset($tmp_select)) {
338 if (isset($GLOBALS[$what . '_structure'])) PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime);
339 if (isset($GLOBALS[$what . '_data'])) PMA_exportData($db, $table, $crlf, $err_url, $local_query);
341 $i++;
343 PMA_exportDBFooter($db);
344 } else {
345 PMA_exportDBHeader($db);
346 // We export just one table
348 if ($limit_to > 0 && $limit_from >= 0) {
349 $add_query = ' LIMIT '
350 . (($limit_from > 0) ? $limit_from . ', ' : '')
351 . $limit_to;
352 } else {
353 $add_query = '';
356 if (!empty($sql_query)) {
357 $local_query = $sql_query . $add_query;
358 PMA_mysql_select_db($db);
359 } else {
360 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
363 if (isset($GLOBALS[$what . '_structure'])) PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime);
364 if (isset($GLOBALS[$what . '_data'])) PMA_exportData($db, $table, $crlf, $err_url, $local_query);
365 PMA_exportDBFooter($db);
369 * Send the dump as a file...
371 if (!empty($asfile)) {
372 // Convert the charset if required.
373 if ($output_charset_conversion) {
374 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
377 // Do the compression
378 // 1. as a gzipped file
379 if (isset($compression) && $compression == 'zip') {
380 if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
381 if ($type == 'csv' ) {
382 $extbis = '.csv';
383 } else if ($type == 'xml') {
384 $extbis = '.xml';
385 } else {
386 $extbis = '.sql';
388 $zipfile = new zipfile();
389 $zipfile -> addFile($dump_buffer, $filename . $extbis);
390 $dump_buffer = $zipfile -> file();
393 // 2. as a bzipped file
394 else if (isset($compression) && $compression == 'bzip') {
395 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
396 $dump_buffer = bzcompress($dump_buffer);
397 // nijel: eval in next line is because otherwise === causes syntax error on php3
398 if (eval('return($dump_buffer === -8);')) {
399 include('./header.inc.php3');
400 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
401 include('./footer.inc.php3');
402 exit;
406 // 3. as a gzipped file
407 else if (isset($compression) && $compression == 'gzip') {
408 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
409 // without the optional parameter level because it bug
410 $dump_buffer = gzencode($dump_buffer);
414 /* If ve saved on server, we have to close file now */
415 if ($save_on_server) {
416 $write_result = @fwrite($file_handle, $dump_buffer);
417 fclose($file_handle);
418 if (strlen($dump_buffer) !=0 && (!$write_result || ($write_result != strlen($dump_buffer)))) {
419 $message = sprintf($strNoSpace, $save_filename);
420 } else {
421 $message = sprintf($strDumpSaved, $save_filename);
424 $js_to_run = 'functions.js';
425 include('./header.inc.php3');
426 if ($export_type == 'server') {
427 $active_page = 'server_export.php3';
428 include('./server_export.php3');
429 } elseif ($export_type == 'database') {
430 $active_page = 'db_details_export.php3';
431 include('./db_details_export.php3');
432 } else {
433 $active_page = 'tbl_properties_export.php3';
434 include('./tbl_properties_export.php3');
436 exit();
437 } else {
438 echo $dump_buffer;
442 * Displays the dump...
444 else {
446 * Close the html tags and add the footers in dump is displayed on screen
448 echo ' </pre>' . "\n";
449 echo '</div>' . "\n";
450 echo "\n";
451 include('./footer.inc.php3');
452 } // end if