Added faq 1.27 about GZIP output buffering problem.
[phpmyadmin/crack.git] / tbl_dump.php3
blobcb8a778f8e0174410542b6dcd7e2633048fa8047
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Formats the INSERT statements depending on the target (screen/file) of the
8 * sql dump
10 * @param string the insert statement
12 * @global string the buffer containing formatted strings
14 function PMA_myHandler($sql_insert)
16 global $tmp_buffer;
18 // Kanji encoding convert feature appended by Y.Kawada (2001/2/21)
19 if (function_exists('PMA_kanji_str_conv')) {
20 $sql_insert = PMA_kanji_str_conv($sql_insert, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
23 // Defines the end of line delimiter to use
24 $eol_dlm = (isset($GLOBALS['extended_ins']) && ($GLOBALS['current_row'] < $GLOBALS['rows_cnt']))
25 ? ','
26 : ';';
27 $tmp_buffer .= $sql_insert . $eol_dlm . $GLOBALS['crlf'];
28 } // end of the 'PMA_myHandler()' function
31 /**
32 * Formats the INSERT statements depending on the target (screen/file) of the
33 * cvs export
35 * Revisions: 2001-05-07, Lem9: added $add_character
36 * 2001-07-12, loic1: $crlf should be used only if there is no EOL
37 * character defined by the user
39 * @param string the insert statement
41 * @global string the character to add at the end of lines
42 * @global string the buffer containing formatted strings
44 function PMA_myCsvHandler($sql_insert)
46 global $add_character;
47 global $tmp_buffer;
49 // Kanji encoding convert feature appended by Y.Kawada (2001/2/21)
50 if (function_exists('PMA_kanji_str_conv')) {
51 $sql_insert = PMA_kanji_str_conv($sql_insert, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
53 $tmp_buffer .= $sql_insert . $add_character;
54 } // end of the 'PMA_myCsvHandler()' function
58 /**
59 * Get the variables sent or posted to this script and a core script
61 require('./libraries/grab_globals.lib.php3');
62 require('./libraries/common.lib.php3');
63 require('./libraries/build_dump.lib.php3');
64 require('./libraries/zip.lib.php3');
66 /**
67 * Defines the url to return to in case of error in a sql statement
69 $err_url = 'tbl_properties.php3?' . PMA_generate_common_url($db, isset($table) ? $table : '');
72 /**
73 * Increase time limit for script execution and initializes some variables
75 @set_time_limit($cfg['ExecTimeLimit']);
76 $dump_buffer = '';
77 // Defines the default <CR><LF> format
78 $crlf = PMA_whichCrlf();
81 /**
82 * Ensure zipped formats are associated with the download feature
84 if (empty($asfile)
85 && (!empty($zip) || !empty($gzip) || !empty($bzip))) {
86 $asfile = 1;
90 /**
91 * Send headers depending on whether the user chose to download a dump file
92 * or not
94 // No download
95 if (empty($asfile)) {
96 $backup_cfgServer = $cfg['Server'];
97 include('./header.inc.php3');
98 $cfg['Server'] = $backup_cfgServer;
99 unset($backup_cfgServer);
100 echo '<div align="' . $cell_align_left . '">' . "\n";
101 echo ' <pre>' . "\n";
102 } // end if
104 // Download
105 else {
106 // Defines filename and extension, and also mime types
107 if (!isset($table)) {
108 $filename = $db;
109 } else {
110 $filename = $table;
112 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
113 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
114 } else {
115 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
118 // Generate basic dump extension
119 if ($what == 'csv' || $what == 'excel') {
120 $ext = 'csv';
121 $mime_type = 'text/x-csv';
122 } else if ($what == 'xml') {
123 $ext = 'xml';
124 $mime_type = 'text/xml';
125 } else if ($what == 'latex') {
126 $ext = 'tex';
127 $mime_type = 'application/x-tex';
128 } else {
129 $ext = 'sql';
130 // loic1: 'application/octet-stream' is the registered IANA type but
131 // MSIE and Opera seems to prefer 'application/octetstream'
132 $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA')
133 ? 'application/octetstream'
134 : 'application/octet-stream';
137 // If dump is going to be copressed, set correct mime_type and add
138 // compression to extension
139 if (isset($bzip) && $bzip == 'bzip') {
140 $ext .= '.bz2';
141 $mime_type = 'application/x-bzip';
142 } else if (isset($gzip) && $gzip == 'gzip') {
143 $ext .= '.gz';
144 $mime_type = 'application/x-gzip';
145 } else if (isset($zip) && $zip == 'zip') {
146 $ext .= '.zip';
147 $mime_type = 'application/x-zip';
150 $now = gmdate('D, d M Y H:i:s') . ' GMT';
151 } // end download
155 * Builds the dump
157 // Gets the number of tables if a dump of a database has been required
158 if (!isset($table)) {
159 $tables = PMA_mysql_list_tables($db);
160 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
161 } else {
162 $num_tables = 1;
163 $single = TRUE;
166 // No table -> error message
167 if ($num_tables == 0) {
168 echo '# ' . $strNoTablesFound;
170 // At least one table -> do the work
171 else {
172 // No csv or xml or latex format -> add some comments at the top
174 if ($what != 'csv' && $what != 'excel' && $what != 'xml' && $what != 'latex') {
175 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
176 . '# version ' . PMA_VERSION . $crlf
177 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
178 . '#' . $crlf
179 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
180 if (!empty($cfg['Server']['port'])) {
181 $dump_buffer .= ':' . $cfg['Server']['port'];
183 $formatted_db_name = (isset($use_backquotes))
184 ? PMA_backquote($db)
185 : '\'' . $db . '\'';
186 $dump_buffer .= $crlf
187 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
188 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
189 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
190 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
192 $i = 0;
193 if (isset($table_select)) {
194 $tmp_select = implode($table_select, '|');
195 $tmp_select = '|' . $tmp_select . '|';
197 while ($i < $num_tables) {
198 if (!isset($single)) {
199 $table = PMA_mysql_tablename($tables, $i);
201 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
202 $i++;
203 } else {
204 $formatted_table_name = (isset($use_backquotes))
205 ? PMA_backquote($table)
206 : '\'' . $table . '\'';
207 // If only datas, no need to displays table name
208 if ($what != 'dataonly') {
209 $dump_buffer .= '# --------------------------------------------------------' . $crlf
210 . $crlf . '#' . $crlf
211 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
212 . '#' . $crlf . $crlf
213 . PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
215 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
216 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
218 // At least data
219 if (($what == 'data') || ($what == 'dataonly')) {
220 $tcmt = $crlf . '#' . $crlf
221 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
222 . '#' . $crlf .$crlf;
223 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
224 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
225 } else {
226 $dump_buffer .= $tcmt;
228 $tmp_buffer = '';
229 if (!isset($limit_from) || !isset($limit_to)) {
230 $limit_from = $limit_to = 0;
232 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url, (isset($sql_query)?urldecode($sql_query):''));
234 $dump_buffer .= $tmp_buffer;
235 } // end if
236 $i++;
237 } // end if-else
238 } // end while
240 // staybyte: don't remove, it makes easier to select & copy from
241 // browser
242 $dump_buffer .= $crlf;
243 } // end 'no csv or xml' case
245 // 'xml' case
246 else if ($GLOBALS['what'] == 'xml') {
247 // first add the xml tag
248 $dump_buffer .= '<?xml version="1.0" encoding="' . (empty($asfile) ? $charset : $charset_of_file) . '"?>' . $crlf . $crlf;
249 // some comments
250 $dump_buffer .= '<!--' . $crlf
251 . '-' . $crlf
252 . '- phpMyAdmin XML-Dump' . $crlf
253 . '- version ' . PMA_VERSION . $crlf
254 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
255 . '-' . $crlf
256 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
257 if (!empty($cfg['Server']['port'])) {
258 $dump_buffer .= ':' . $cfg['Server']['port'];
260 $dump_buffer .= $crlf
261 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
262 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
263 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
264 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
265 . '-' . $crlf
266 . '-->' . $crlf . $crlf;
267 // Now build the structure
268 // todo: Make db and table names XML compatible
269 $dump_buffer .= '<' . $db . '>' . $crlf;
270 if (isset($table_select)) {
271 $tmp_select = implode($table_select, '|');
272 $tmp_select = '|' . $tmp_select . '|';
274 $i = 0;
275 while ($i < $num_tables) {
276 if (!isset($single)) {
277 $table = PMA_mysql_tablename($tables, $i);
279 if (!isset($limit_from) || !isset($limit_to)) {
280 $limit_from = $limit_to = 0;
282 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
283 || (!isset($tmp_select) && !empty($table))) {
284 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url,
285 (isset($sql_query)?urldecode($sql_query):''));
287 $i++;
289 $dump_buffer .= '</' . $db . '>' . $crlf;
290 } // end 'xml' case
292 // latex case
293 else if ($GLOBALS['what'] == 'latex') {
296 $dump_buffer .= '% ' . $crlf
297 . '% phpMyAdmin LaTeX-Dump' . $crlf
298 . '% version ' . PMA_VERSION . $crlf
299 . '% http://www.phpmyadmin.net/ (download page)' . $crlf
300 . '%' . $crlf
301 . '% ' . $strHost . ': ' . $cfg['Server']['host'] . $crlf
302 . '%' . $crlf;
304 if (isset($table_select)) {
305 $tmp_select = implode($table_select, '|');
306 $tmp_select = '|' . $tmp_select . '|';
309 $i = 0;
310 while ($i < $num_tables) {
312 if (!isset($single)) {
313 $table = PMA_mysql_tablename($tables, $i);
315 if (!isset($limit_from) || !isset($limit_to)) {
316 $limit_from = $limit_to = 0;
318 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
319 || (!isset($tmp_select) && !empty($table))) {
321 // to do: add option for the formatting ( c, l, r, p)
322 $dump_buffer .= PMA_getTableLatex($db, $table, $environment, $limit_from, $limit_to, $crlf, $err_url,
323 (isset($sql_query)?urldecode($sql_query):''));
325 $i++;
328 } //end latex case
330 // 'csv' case
331 else {
332 // Handles the EOL character
333 if ($GLOBALS['what'] == 'excel') {
334 $add_character = "\015\012";
335 } else if (empty($add_character)) {
336 $add_character = $GLOBALS['crlf'];
337 } else {
338 if (get_magic_quotes_gpc()) {
339 $add_character = stripslashes($add_character);
341 $add_character = str_replace('\\r', "\015", $add_character);
342 $add_character = str_replace('\\n', "\012", $add_character);
343 $add_character = str_replace('\\t', "\011", $add_character);
344 } // end if
346 $tmp_buffer = '';
347 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url
348 , (isset($sql_query)?urldecode($sql_query):''));
349 $dump_buffer .= $tmp_buffer;
350 } // end 'csv case
351 } // end building the dump
355 * Send the dump as a file...
357 if (!empty($asfile)) {
358 // Convert the charset if required.
359 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
360 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
361 && (!empty($GLOBALS['asfile']))) {
362 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
365 // Do the compression
366 // 1. as a gzipped file
367 if (isset($zip) && $zip == 'zip') {
368 if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
369 if ($what == 'csv' || $what == 'excel') {
370 $extbis = '.csv';
371 } else if ($what == 'xml') {
372 $extbis = '.xml';
373 } else {
374 $extbis = '.sql';
376 $zipfile = new zipfile();
377 $zipfile -> addFile($dump_buffer, $filename . $extbis);
378 $dump_buffer = $zipfile -> file();
381 // 2. as a bzipped file
382 else if (isset($bzip) && $bzip == 'bzip') {
383 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
384 $dump_buffer = bzcompress($dump_buffer);
385 if ($dump_buffer === -8) {
386 include('./header.inc.php3');
387 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
388 include('./footer.inc.php3');
389 exit;
393 // 3. as a gzipped file
394 else if (isset($gzip) && $gzip == 'gzip') {
395 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
396 // without the optional parameter level because it bug
397 $dump_buffer = gzencode($dump_buffer);
401 // finally send the headers and the file
402 header('Content-Type: ' . $mime_type);
403 header('Expires: ' . $now);
404 // lem9 & loic1: IE need specific headers
405 if (PMA_USR_BROWSER_AGENT == 'IE') {
406 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
407 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
408 header('Pragma: public');
409 } else {
410 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
411 header('Pragma: no-cache');
413 echo $dump_buffer;
416 * Displays the dump...
418 else {
419 echo htmlspecialchars($dump_buffer);
421 * Close the html tags and add the footers in dump is displayed on screen
423 echo ' </pre>' . "\n";
424 echo '</div>' . "\n";
425 echo "\n";
426 include('./footer.inc.php3');
427 } // end if