updates
[phpmyadmin/crack.git] / tbl_dump.php3
blob3b9cc3070d918587da61e5ea73e2800666776a9f
1 <?php
2 /* $Id$ */
5 /**
6 * Formats the INSERT statements depending on the target (screen/file) of the
7 * sql dump
9 * @param string the insert statement
11 * @global string the buffer containing formatted strings
13 function PMA_myHandler($sql_insert)
15 global $tmp_buffer;
17 // Kanji encoding convert feature appended by Y.Kawada (2001/2/21)
18 if (function_exists('PMA_kanji_str_conv')) {
19 $sql_insert = PMA_kanji_str_conv($sql_insert, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
22 // Convert the charset if required.
23 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
24 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
25 && (!empty($GLOBALS['asfile']))) {
26 $sql_insert = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $sql_insert);
29 // Defines the end of line delimiter to use
30 $eol_dlm = (isset($GLOBALS['extended_ins']) && ($GLOBALS['current_row'] < $GLOBALS['rows_cnt']))
31 ? ','
32 : ';';
33 // Result has to be displayed on screen
34 if (empty($GLOBALS['asfile'])) {
35 echo htmlspecialchars($sql_insert . $eol_dlm . $GLOBALS['crlf']);
37 // Result has to be saved in a text file
38 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
39 echo $sql_insert . $eol_dlm . $GLOBALS['crlf'];
41 // Result will be saved in a *zipped file
42 else {
43 $tmp_buffer .= $sql_insert . $eol_dlm . $GLOBALS['crlf'];
45 } // end of the 'PMA_myHandler()' function
48 /**
49 * Formats the INSERT statements depending on the target (screen/file) of the
50 * cvs export
52 * Revisions: 2001-05-07, Lem9: added $add_character
53 * 2001-07-12, loic1: $crlf should be used only if there is no EOL
54 * character defined by the user
56 * @param string the insert statement
58 * @global string the character to add at the end of lines
59 * @global string the buffer containing formatted strings
61 function PMA_myCsvHandler($sql_insert)
63 global $add_character;
64 global $tmp_buffer;
66 // Kanji encoding convert feature appended by Y.Kawada (2001/2/21)
67 if (function_exists('PMA_kanji_str_conv')) {
68 $sql_insert = PMA_kanji_str_conv($sql_insert, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
70 // Convert the charset if required.
71 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
72 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
73 && (!empty($GLOBALS['asfile']))) {
74 $sql_insert = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $sql_insert);
76 // Result has to be displayed on screen
77 if (empty($GLOBALS['asfile'])) {
78 echo htmlspecialchars($sql_insert) . $add_character;
80 // Result has to be saved in a text file
81 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
82 echo $sql_insert . $add_character;
84 // Result will be saved in a *zipped file
85 else {
86 $tmp_buffer .= $sql_insert . $add_character;
88 } // end of the 'PMA_myCsvHandler()' function
92 /**
93 * Get the variables sent or posted to this script and a core script
95 require('./libraries/grab_globals.lib.php3');
96 require('./libraries/common.lib.php3');
97 require('./libraries/build_dump.lib.php3');
98 require('./libraries/zip.lib.php3');
102 * Defines the url to return to in case of error in a sql statement
104 $err_url = 'tbl_properties.php3'
105 . '?lang=' . $lang
106 . '&amp;convcharset=' . $convcharset
107 . '&amp;server=' . $server
108 . '&amp;db=' . urlencode($db)
109 . (isset($table) ? '&amp;table=' . urlencode($table) : '');
113 * Increase time limit for script execution and initializes some variables
115 @set_time_limit($cfg['ExecTimeLimit']);
116 $dump_buffer = '';
117 // Defines the default <CR><LF> format
118 $crlf = PMA_whichCrlf();
122 * Ensure zipped formats are associated with the download feature
124 if (empty($asfile)
125 && (!empty($zip) || !empty($gzip) || !empty($bzip))) {
126 $asfile = 1;
131 * Send headers depending on whether the user choosen to download a dump file
132 * or not
134 // No download
135 if (empty($asfile)) {
136 $backup_cfgServer = $cfg['Server'];
137 include('./header.inc.php3');
138 $cfg['Server'] = $backup_cfgServer;
139 unset($backup_cfgServer);
140 echo '<div align="' . $cell_align_left . '">' . "\n";
141 echo ' <pre>' . "\n";
142 } // end if
144 // Download
145 else {
146 // Defines filename and extension, and also mime types
147 if (!isset($table)) {
148 $filename = PMA_convert_string($convcharset, 'iso8859-1', $db);
149 } else {
150 $filename = PMA_convert_string($charset, 'iso8859-1', $table);
152 if (isset($bzip) && $bzip == 'bzip') {
153 $ext = 'bz2';
154 $mime_type = 'application/x-bzip';
155 } else if (isset($gzip) && $gzip == 'gzip') {
156 $ext = 'gz';
157 $mime_type = 'application/x-gzip';
158 } else if (isset($zip) && $zip == 'zip') {
159 $ext = 'zip';
160 $mime_type = 'application/x-zip';
161 } else if ($what == 'csv' || $what == 'excel') {
162 $ext = 'csv';
163 $mime_type = 'text/x-csv';
164 } else if ($what == 'xml') {
165 $ext = 'xml';
166 $mime_type = 'text/xml';
167 } else {
168 $ext = 'sql';
169 // loic1: 'application/octet-stream' is the registered IANA type but
170 // MSIE and Opera seems to prefer 'application/octetstream'
171 $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA')
172 ? 'application/octetstream'
173 : 'application/octet-stream';
176 // Send headers
177 header('Content-Type: ' . $mime_type);
178 // lem9 & loic1: IE need specific headers
179 if (PMA_USR_BROWSER_AGENT == 'IE') {
180 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
181 header('Expires: 0');
182 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
183 header('Pragma: public');
184 } else {
185 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
186 header('Expires: 0');
187 header('Pragma: no-cache');
189 } // end download
193 * Builds the dump
195 // Gets the number of tables if a dump of a database has been required
196 if (!isset($table)) {
197 $tables = PMA_mysql_list_tables($db);
198 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
199 } else {
200 $num_tables = 1;
201 $single = TRUE;
204 // No table -> error message
205 if ($num_tables == 0) {
206 echo '# ' . $strNoTablesFound;
208 // At least one table -> do the work
209 else {
210 // No csv or xml format -> add some comments at the top
211 if ($what != 'csv' && $what != 'excel' && $what != 'xml') {
212 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
213 . '# version ' . PMA_VERSION . $crlf
214 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
215 . '#' . $crlf
216 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
217 if (!empty($cfg['Server']['port'])) {
218 $dump_buffer .= ':' . $cfg['Server']['port'];
220 $formatted_db_name = (isset($use_backquotes))
221 ? PMA_backquote($db)
222 : '\'' . $db . '\'';
223 $dump_buffer .= $crlf
224 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
225 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
226 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
227 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
229 $i = 0;
230 if (isset($table_select)) {
231 $tmp_select = implode($table_select, '|');
232 $tmp_select = '|' . $tmp_select . '|';
234 while ($i < $num_tables) {
235 if (!isset($single)) {
236 $table = PMA_mysql_tablename($tables, $i);
238 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
239 $i++;
240 } else {
241 $formatted_table_name = (isset($use_backquotes))
242 ? PMA_backquote($table)
243 : '\'' . $table . '\'';
244 // If only datas, no need to displays table name
245 if ($what != 'dataonly') {
246 $dump_buffer .= '# --------------------------------------------------------' . $crlf
247 . $crlf . '#' . $crlf
248 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
249 . '#' . $crlf . $crlf
250 . PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
252 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
253 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
255 // Convert the charset if required.
256 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
257 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
258 && (!empty($GLOBALS['asfile']))) {
259 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
261 // At least data
262 if (($what == 'data') || ($what == 'dataonly')) {
263 $tcmt = $crlf . '#' . $crlf
264 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
265 . '#' . $crlf .$crlf;
266 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
267 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
269 // Converts the charset if required.
270 else if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
271 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
272 && (!empty($GLOBALS['asfile']))) {
273 $dump_buffer .= PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $tcmt);
274 } else {
275 $dump_buffer .= $tcmt;
277 $tmp_buffer = '';
278 if (!isset($limit_from) || !isset($limit_to)) {
279 $limit_from = $limit_to = 0;
281 // loic1: display data if they aren't bufferized
282 if (!isset($zip) && !isset($bzip) && !isset($gzip)) {
283 echo $dump_buffer;
284 $dump_buffer = '';
286 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url);
288 $dump_buffer .= $tmp_buffer;
289 } // end if
290 $i++;
291 } // end if-else
292 } // end while
294 // staybyte: don't remove, it makes easier to select & copy from
295 // browser
296 $dump_buffer .= $crlf;
297 } // end 'no csv or xml' case
299 // 'xml' case
300 else if ($GLOBALS['what'] == 'xml') {
301 // first add the xml tag
302 $dump_buffer .= '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf . $crlf;
303 // some comments
304 $dump_buffer .= '<!--' . $crlf
305 . '-' . $crlf
306 . '- phpMyAdmin XML-Dump' . $crlf
307 . '- version ' . PMA_VERSION . $crlf
308 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
309 . '-' . $crlf
310 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
311 if (!empty($cfg['Server']['port'])) {
312 $dump_buffer .= ':' . $cfg['Server']['port'];
314 $dump_buffer .= $crlf
315 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
316 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
317 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
318 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
319 . '-' . $crlf
320 . '-->' . $crlf . $crlf;
321 // Now build the structure
322 // todo: Make db and table names XML compatible
323 $dump_buffer .= '<' . $db . '>' . $crlf;
324 if (isset($table_select)) {
325 $tmp_select = implode($table_select, '|');
326 $tmp_select = '|' . $tmp_select . '|';
328 $i = 0;
329 while ($i < $num_tables) {
330 if (!isset($single)) {
331 $table = PMA_mysql_tablename($tables, $i);
333 if (!isset($limit_from) || !isset($limit_to)) {
334 $limit_from = $limit_to = 0;
336 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
337 || (!isset($tmp_select) && !empty($table))) {
338 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url);
340 $i++;
342 $dump_buffer .= '</' . $db . '>' . $crlf;
343 } // end 'xml' case
345 // 'csv' case
346 else {
347 // Handles the EOL character
348 if ($GLOBALS['what'] == 'excel') {
349 $add_character = "\015\012";
350 } else if (empty($add_character)) {
351 $add_character = $GLOBALS['crlf'];
352 } else {
353 if (get_magic_quotes_gpc()) {
354 $add_character = stripslashes($add_character);
356 $add_character = str_replace('\\r', "\015", $add_character);
357 $add_character = str_replace('\\n', "\012", $add_character);
358 $add_character = str_replace('\\t', "\011", $add_character);
359 } // end if
361 $tmp_buffer = '';
362 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url);
363 $dump_buffer .= $tmp_buffer;
364 } // end 'csv case
365 } // end building the dump
369 * "Displays" the dump...
371 // 1. as a gzipped file
372 if (isset($zip) && $zip == 'zip') {
373 if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
374 if ($what == 'csv' || $what == 'excel') {
375 $extbis = '.csv';
376 } else if ($what == 'xml') {
377 $extbis = '.xml';
378 } else {
379 $extbis = '.sql';
381 $zipfile = new zipfile();
382 $zipfile -> addFile($dump_buffer, $filename . $extbis);
383 echo $zipfile -> file();
386 // 2. as a bzipped file
387 else if (isset($bzip) && $bzip == 'bzip') {
388 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
389 echo bzcompress($dump_buffer);
392 // 3. as a gzipped file
393 else if (isset($gzip) && $gzip == 'gzip') {
394 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
395 // without the optional parameter level because it bug
396 echo gzencode($dump_buffer);
399 // 4. as a text file
400 else if (!empty($asfile)) {
401 echo $dump_buffer;
403 // 5. on display
404 else {
405 echo htmlspecialchars($dump_buffer);
409 * Close the html tags and add the footers in dump is displayed on screen
411 if (empty($asfile)) {
412 echo ' </pre>' . "\n";
413 echo '</div>' . "\n";
414 echo "\n";
415 include('./footer.inc.php3');
416 } // end if