update
[phpmyadmin/crack.git] / tbl_dump.php3
blobd2052d991e14875a9cf9d2371f4340ebdf352395
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 // Convert the charset if required.
24 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
25 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
26 && (!empty($GLOBALS['asfile']))) {
27 $sql_insert = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $sql_insert);
30 // Defines the end of line delimiter to use
31 $eol_dlm = (isset($GLOBALS['extended_ins']) && ($GLOBALS['current_row'] < $GLOBALS['rows_cnt']))
32 ? ','
33 : ';';
34 // Result has to be displayed on screen
35 if (empty($GLOBALS['asfile'])) {
36 echo htmlspecialchars($sql_insert . $eol_dlm . $GLOBALS['crlf']);
38 // Result has to be saved in a text file
39 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
40 echo $sql_insert . $eol_dlm . $GLOBALS['crlf'];
42 // Result will be saved in a *zipped file
43 else {
44 $tmp_buffer .= $sql_insert . $eol_dlm . $GLOBALS['crlf'];
46 } // end of the 'PMA_myHandler()' function
49 /**
50 * Formats the INSERT statements depending on the target (screen/file) of the
51 * cvs export
53 * Revisions: 2001-05-07, Lem9: added $add_character
54 * 2001-07-12, loic1: $crlf should be used only if there is no EOL
55 * character defined by the user
57 * @param string the insert statement
59 * @global string the character to add at the end of lines
60 * @global string the buffer containing formatted strings
62 function PMA_myCsvHandler($sql_insert)
64 global $add_character;
65 global $tmp_buffer;
67 // Kanji encoding convert feature appended by Y.Kawada (2001/2/21)
68 if (function_exists('PMA_kanji_str_conv')) {
69 $sql_insert = PMA_kanji_str_conv($sql_insert, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
71 // Convert the charset if required.
72 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
73 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
74 && (!empty($GLOBALS['asfile']))) {
75 $sql_insert = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $sql_insert);
77 // Result has to be displayed on screen
78 if (empty($GLOBALS['asfile'])) {
79 echo htmlspecialchars($sql_insert) . $add_character;
81 // Result has to be saved in a text file
82 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
83 echo $sql_insert . $add_character;
85 // Result will be saved in a *zipped file
86 else {
87 $tmp_buffer .= $sql_insert . $add_character;
89 } // end of the 'PMA_myCsvHandler()' function
93 /**
94 * Get the variables sent or posted to this script and a core script
96 require('./libraries/grab_globals.lib.php3');
97 require('./libraries/common.lib.php3');
98 require('./libraries/build_dump.lib.php3');
99 require('./libraries/zip.lib.php3');
103 * Defines the url to return to in case of error in a sql statement
105 $err_url = 'tbl_properties.php3'
106 . '?lang=' . $lang
107 . '&amp;convcharset=' . $convcharset
108 . '&amp;server=' . $server
109 . '&amp;db=' . urlencode($db)
110 . (isset($table) ? '&amp;table=' . urlencode($table) : '');
114 * Increase time limit for script execution and initializes some variables
116 @set_time_limit($cfg['ExecTimeLimit']);
117 $dump_buffer = '';
118 // Defines the default <CR><LF> format
119 $crlf = PMA_whichCrlf();
123 * Ensure zipped formats are associated with the download feature
125 if (empty($asfile)
126 && (!empty($zip) || !empty($gzip) || !empty($bzip))) {
127 $asfile = 1;
132 * Send headers depending on whether the user choosen to download a dump file
133 * or not
135 // No download
136 if (empty($asfile)) {
137 $backup_cfgServer = $cfg['Server'];
138 include('./header.inc.php3');
139 $cfg['Server'] = $backup_cfgServer;
140 unset($backup_cfgServer);
141 echo '<div align="' . $cell_align_left . '">' . "\n";
142 echo ' <pre>' . "\n";
143 } // end if
145 // Download
146 else {
147 // Defines filename and extension, and also mime types
148 if (!isset($table)) {
149 $filename = $db;
150 } else {
151 $filename = $table;
153 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
154 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
155 } else {
156 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
158 if (isset($bzip) && $bzip == 'bzip') {
159 $ext = 'bz2';
160 $mime_type = 'application/x-bzip';
161 } else if (isset($gzip) && $gzip == 'gzip') {
162 $ext = 'gz';
163 $mime_type = 'application/x-gzip';
164 } else if (isset($zip) && $zip == 'zip') {
165 $ext = 'zip';
166 $mime_type = 'application/x-zip';
167 } else if ($what == 'csv' || $what == 'excel') {
168 $ext = 'csv';
169 $mime_type = 'text/x-csv';
170 } else if ($what == 'xml') {
171 $ext = 'xml';
172 $mime_type = 'text/xml';
173 } else {
174 $ext = 'sql';
175 // loic1: 'application/octet-stream' is the registered IANA type but
176 // MSIE and Opera seems to prefer 'application/octetstream'
177 $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA')
178 ? 'application/octetstream'
179 : 'application/octet-stream';
182 $now = gmdate('D, d M Y H:i:s') . ' GMT';
184 // Send headers
185 header('Content-Type: ' . $mime_type);
186 header('Expires: ' . $now);
187 // lem9 & loic1: IE need specific headers
188 if (PMA_USR_BROWSER_AGENT == 'IE') {
189 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
190 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
191 header('Pragma: public');
192 } else {
193 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
194 header('Pragma: no-cache');
196 } // end download
200 * Builds the dump
202 // Gets the number of tables if a dump of a database has been required
203 if (!isset($table)) {
204 $tables = PMA_mysql_list_tables($db);
205 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
206 } else {
207 $num_tables = 1;
208 $single = TRUE;
211 // No table -> error message
212 if ($num_tables == 0) {
213 echo '# ' . $strNoTablesFound;
215 // At least one table -> do the work
216 else {
217 // No csv or xml format -> add some comments at the top
218 if ($what != 'csv' && $what != 'excel' && $what != 'xml') {
219 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
220 . '# version ' . PMA_VERSION . $crlf
221 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
222 . '#' . $crlf
223 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
224 if (!empty($cfg['Server']['port'])) {
225 $dump_buffer .= ':' . $cfg['Server']['port'];
227 $formatted_db_name = (isset($use_backquotes))
228 ? PMA_backquote($db)
229 : '\'' . $db . '\'';
230 $dump_buffer .= $crlf
231 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
232 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
233 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
234 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
236 $i = 0;
237 if (isset($table_select)) {
238 $tmp_select = implode($table_select, '|');
239 $tmp_select = '|' . $tmp_select . '|';
241 while ($i < $num_tables) {
242 if (!isset($single)) {
243 $table = PMA_mysql_tablename($tables, $i);
245 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
246 $i++;
247 } else {
248 $formatted_table_name = (isset($use_backquotes))
249 ? PMA_backquote($table)
250 : '\'' . $table . '\'';
251 // If only datas, no need to displays table name
252 if ($what != 'dataonly') {
253 $dump_buffer .= '# --------------------------------------------------------' . $crlf
254 . $crlf . '#' . $crlf
255 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
256 . '#' . $crlf . $crlf
257 . PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
259 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
260 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
262 // Convert the charset if required.
263 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
264 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
265 && (!empty($GLOBALS['asfile']))) {
266 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
268 // At least data
269 if (($what == 'data') || ($what == 'dataonly')) {
270 $tcmt = $crlf . '#' . $crlf
271 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
272 . '#' . $crlf .$crlf;
273 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
274 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
276 // Converts the charset if required.
277 else if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
278 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
279 && (!empty($GLOBALS['asfile']))) {
280 $dump_buffer .= PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $tcmt);
281 } else {
282 $dump_buffer .= $tcmt;
284 $tmp_buffer = '';
285 if (!isset($limit_from) || !isset($limit_to)) {
286 $limit_from = $limit_to = 0;
288 // loic1: display data if they aren't bufferized
289 if (!isset($zip) && !isset($bzip) && !isset($gzip)) {
290 echo $dump_buffer;
291 $dump_buffer = '';
293 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url);
295 $dump_buffer .= $tmp_buffer;
296 } // end if
297 $i++;
298 } // end if-else
299 } // end while
301 // staybyte: don't remove, it makes easier to select & copy from
302 // browser
303 $dump_buffer .= $crlf;
304 } // end 'no csv or xml' case
306 // 'xml' case
307 else if ($GLOBALS['what'] == 'xml') {
308 // first add the xml tag
309 $dump_buffer .= '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf . $crlf;
310 // some comments
311 $dump_buffer .= '<!--' . $crlf
312 . '-' . $crlf
313 . '- phpMyAdmin XML-Dump' . $crlf
314 . '- version ' . PMA_VERSION . $crlf
315 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
316 . '-' . $crlf
317 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
318 if (!empty($cfg['Server']['port'])) {
319 $dump_buffer .= ':' . $cfg['Server']['port'];
321 $dump_buffer .= $crlf
322 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
323 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
324 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
325 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
326 . '-' . $crlf
327 . '-->' . $crlf . $crlf;
328 // Now build the structure
329 // todo: Make db and table names XML compatible
330 $dump_buffer .= '<' . $db . '>' . $crlf;
331 if (isset($table_select)) {
332 $tmp_select = implode($table_select, '|');
333 $tmp_select = '|' . $tmp_select . '|';
335 $i = 0;
336 while ($i < $num_tables) {
337 if (!isset($single)) {
338 $table = PMA_mysql_tablename($tables, $i);
340 if (!isset($limit_from) || !isset($limit_to)) {
341 $limit_from = $limit_to = 0;
343 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
344 || (!isset($tmp_select) && !empty($table))) {
345 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url);
347 $i++;
349 $dump_buffer .= '</' . $db . '>' . $crlf;
350 } // end 'xml' case
352 // 'csv' case
353 else {
354 // Handles the EOL character
355 if ($GLOBALS['what'] == 'excel') {
356 $add_character = "\015\012";
357 } else if (empty($add_character)) {
358 $add_character = $GLOBALS['crlf'];
359 } else {
360 if (get_magic_quotes_gpc()) {
361 $add_character = stripslashes($add_character);
363 $add_character = str_replace('\\r', "\015", $add_character);
364 $add_character = str_replace('\\n', "\012", $add_character);
365 $add_character = str_replace('\\t', "\011", $add_character);
366 } // end if
368 $tmp_buffer = '';
369 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url);
370 $dump_buffer .= $tmp_buffer;
371 } // end 'csv case
372 } // end building the dump
376 * "Displays" the dump...
378 // 1. as a gzipped file
379 if (isset($zip) && $zip == 'zip') {
380 if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
381 if ($what == 'csv' || $what == 'excel') {
382 $extbis = '.csv';
383 } else if ($what == 'xml') {
384 $extbis = '.xml';
385 } else {
386 $extbis = '.sql';
388 $zipfile = new zipfile();
389 $zipfile -> addFile($dump_buffer, $filename . $extbis);
390 echo $zipfile -> file();
393 // 2. as a bzipped file
394 else if (isset($bzip) && $bzip == 'bzip') {
395 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
396 echo bzcompress($dump_buffer);
399 // 3. as a gzipped file
400 else if (isset($gzip) && $gzip == 'gzip') {
401 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
402 // without the optional parameter level because it bug
403 echo gzencode($dump_buffer);
406 // 4. as a text file
407 else if (!empty($asfile)) {
408 echo $dump_buffer;
410 // 5. on display
411 else {
412 echo htmlspecialchars($dump_buffer);
416 * Close the html tags and add the footers in dump is displayed on screen
418 if (empty($asfile)) {
419 echo ' </pre>' . "\n";
420 echo '</div>' . "\n";
421 echo "\n";
422 include('./footer.inc.php3');
423 } // end if