sort order and tablename
[phpmyadmin/crack.git] / tbl_dump.php3
blobd504260acb8456d07a64417e502ca8158b33dbe1
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 = PMA_convert_string($convcharset, 'iso8859-1', $db);
150 } else {
151 $filename = PMA_convert_string($charset, 'iso8859-1', $table);
153 if (isset($bzip) && $bzip == 'bzip') {
154 $ext = 'bz2';
155 $mime_type = 'application/x-bzip';
156 } else if (isset($gzip) && $gzip == 'gzip') {
157 $ext = 'gz';
158 $mime_type = 'application/x-gzip';
159 } else if (isset($zip) && $zip == 'zip') {
160 $ext = 'zip';
161 $mime_type = 'application/x-zip';
162 } else if ($what == 'csv' || $what == 'excel') {
163 $ext = 'csv';
164 $mime_type = 'text/x-csv';
165 } else if ($what == 'xml') {
166 $ext = 'xml';
167 $mime_type = 'text/xml';
168 } else {
169 $ext = '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 $now = gmdate('D, d M Y H:i:s') . ' GMT';
179 // Send headers
180 header('Content-Type: ' . $mime_type);
181 header('Expires: ' . $now);
182 // lem9 & loic1: IE need specific headers
183 if (PMA_USR_BROWSER_AGENT == 'IE') {
184 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
185 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
186 header('Pragma: public');
187 } else {
188 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
189 header('Pragma: no-cache');
191 } // end download
195 * Builds the dump
197 // Gets the number of tables if a dump of a database has been required
198 if (!isset($table)) {
199 $tables = PMA_mysql_list_tables($db);
200 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
201 } else {
202 $num_tables = 1;
203 $single = TRUE;
206 // No table -> error message
207 if ($num_tables == 0) {
208 echo '# ' . $strNoTablesFound;
210 // At least one table -> do the work
211 else {
212 // No csv or xml format -> add some comments at the top
213 if ($what != 'csv' && $what != 'excel' && $what != 'xml') {
214 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
215 . '# version ' . PMA_VERSION . $crlf
216 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
217 . '#' . $crlf
218 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
219 if (!empty($cfg['Server']['port'])) {
220 $dump_buffer .= ':' . $cfg['Server']['port'];
222 $formatted_db_name = (isset($use_backquotes))
223 ? PMA_backquote($db)
224 : '\'' . $db . '\'';
225 $dump_buffer .= $crlf
226 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
227 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
228 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
229 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
231 $i = 0;
232 if (isset($table_select)) {
233 $tmp_select = implode($table_select, '|');
234 $tmp_select = '|' . $tmp_select . '|';
236 while ($i < $num_tables) {
237 if (!isset($single)) {
238 $table = PMA_mysql_tablename($tables, $i);
240 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
241 $i++;
242 } else {
243 $formatted_table_name = (isset($use_backquotes))
244 ? PMA_backquote($table)
245 : '\'' . $table . '\'';
246 // If only datas, no need to displays table name
247 if ($what != 'dataonly') {
248 $dump_buffer .= '# --------------------------------------------------------' . $crlf
249 . $crlf . '#' . $crlf
250 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
251 . '#' . $crlf . $crlf
252 . PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
254 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
255 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
257 // Convert the charset if required.
258 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
259 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
260 && (!empty($GLOBALS['asfile']))) {
261 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
263 // At least data
264 if (($what == 'data') || ($what == 'dataonly')) {
265 $tcmt = $crlf . '#' . $crlf
266 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
267 . '#' . $crlf .$crlf;
268 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
269 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
271 // Converts the charset if required.
272 else if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
273 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
274 && (!empty($GLOBALS['asfile']))) {
275 $dump_buffer .= PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $tcmt);
276 } else {
277 $dump_buffer .= $tcmt;
279 $tmp_buffer = '';
280 if (!isset($limit_from) || !isset($limit_to)) {
281 $limit_from = $limit_to = 0;
283 // loic1: display data if they aren't bufferized
284 if (!isset($zip) && !isset($bzip) && !isset($gzip)) {
285 echo $dump_buffer;
286 $dump_buffer = '';
288 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url);
290 $dump_buffer .= $tmp_buffer;
291 } // end if
292 $i++;
293 } // end if-else
294 } // end while
296 // staybyte: don't remove, it makes easier to select & copy from
297 // browser
298 $dump_buffer .= $crlf;
299 } // end 'no csv or xml' case
301 // 'xml' case
302 else if ($GLOBALS['what'] == 'xml') {
303 // first add the xml tag
304 $dump_buffer .= '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf . $crlf;
305 // some comments
306 $dump_buffer .= '<!--' . $crlf
307 . '-' . $crlf
308 . '- phpMyAdmin XML-Dump' . $crlf
309 . '- version ' . PMA_VERSION . $crlf
310 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
311 . '-' . $crlf
312 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
313 if (!empty($cfg['Server']['port'])) {
314 $dump_buffer .= ':' . $cfg['Server']['port'];
316 $dump_buffer .= $crlf
317 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
318 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
319 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
320 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
321 . '-' . $crlf
322 . '-->' . $crlf . $crlf;
323 // Now build the structure
324 // todo: Make db and table names XML compatible
325 $dump_buffer .= '<' . $db . '>' . $crlf;
326 if (isset($table_select)) {
327 $tmp_select = implode($table_select, '|');
328 $tmp_select = '|' . $tmp_select . '|';
330 $i = 0;
331 while ($i < $num_tables) {
332 if (!isset($single)) {
333 $table = PMA_mysql_tablename($tables, $i);
335 if (!isset($limit_from) || !isset($limit_to)) {
336 $limit_from = $limit_to = 0;
338 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
339 || (!isset($tmp_select) && !empty($table))) {
340 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url);
342 $i++;
344 $dump_buffer .= '</' . $db . '>' . $crlf;
345 } // end 'xml' case
347 // 'csv' case
348 else {
349 // Handles the EOL character
350 if ($GLOBALS['what'] == 'excel') {
351 $add_character = "\015\012";
352 } else if (empty($add_character)) {
353 $add_character = $GLOBALS['crlf'];
354 } else {
355 if (get_magic_quotes_gpc()) {
356 $add_character = stripslashes($add_character);
358 $add_character = str_replace('\\r', "\015", $add_character);
359 $add_character = str_replace('\\n', "\012", $add_character);
360 $add_character = str_replace('\\t', "\011", $add_character);
361 } // end if
363 $tmp_buffer = '';
364 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url);
365 $dump_buffer .= $tmp_buffer;
366 } // end 'csv case
367 } // end building the dump
371 * "Displays" the dump...
373 // 1. as a gzipped file
374 if (isset($zip) && $zip == 'zip') {
375 if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
376 if ($what == 'csv' || $what == 'excel') {
377 $extbis = '.csv';
378 } else if ($what == 'xml') {
379 $extbis = '.xml';
380 } else {
381 $extbis = '.sql';
383 $zipfile = new zipfile();
384 $zipfile -> addFile($dump_buffer, $filename . $extbis);
385 echo $zipfile -> file();
388 // 2. as a bzipped file
389 else if (isset($bzip) && $bzip == 'bzip') {
390 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
391 echo bzcompress($dump_buffer);
394 // 3. as a gzipped file
395 else if (isset($gzip) && $gzip == 'gzip') {
396 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
397 // without the optional parameter level because it bug
398 echo gzencode($dump_buffer);
401 // 4. as a text file
402 else if (!empty($asfile)) {
403 echo $dump_buffer;
405 // 5. on display
406 else {
407 echo htmlspecialchars($dump_buffer);
411 * Close the html tags and add the footers in dump is displayed on screen
413 if (empty($asfile)) {
414 echo ' </pre>' . "\n";
415 echo '</div>' . "\n";
416 echo "\n";
417 include('./footer.inc.php3');
418 } // end if