image/png: inline
[phpmyadmin/crack.git] / tbl_dump.php3
blobf3db609d5cd7e86212590ef9af06c2ae5d723eb6
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 (isset($use_comments) && $use_comments) {
175 require('./libraries/relation.lib.php3');
176 $cfgRelation = PMA_getRelationsParam();
177 $use_comments_work = true;
178 } else {
179 $use_comments_work = false;
182 if ($what != 'csv' && $what != 'excel' && $what != 'xml' && $what != 'latex') {
183 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
184 . '# version ' . PMA_VERSION . $crlf
185 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
186 . '#' . $crlf
187 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
188 if (!empty($cfg['Server']['port'])) {
189 $dump_buffer .= ':' . $cfg['Server']['port'];
191 $formatted_db_name = (isset($use_backquotes))
192 ? PMA_backquote($db)
193 : '\'' . $db . '\'';
194 $dump_buffer .= $crlf
195 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
196 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
197 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
198 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
200 $i = 0;
201 if (isset($table_select)) {
202 $tmp_select = implode($table_select, '|');
203 $tmp_select = '|' . $tmp_select . '|';
205 while ($i < $num_tables) {
206 if (!isset($single)) {
207 $table = PMA_mysql_tablename($tables, $i);
209 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
210 $i++;
211 } else {
212 $formatted_table_name = (isset($use_backquotes))
213 ? PMA_backquote($table)
214 : '\'' . $table . '\'';
215 // If only datas, no need to displays table name
216 if ($what != 'dataonly') {
217 $dump_buffer .= '# --------------------------------------------------------' . $crlf
218 . $crlf . '#' . $crlf
219 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
220 . '#' . $crlf . $crlf
221 . PMA_getTableDef($db, $table, $crlf, $err_url, $use_comments_work) . ';' . $crlf;
223 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
224 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
226 // At least data
227 if (($what == 'data') || ($what == 'dataonly')) {
228 $tcmt = $crlf . '#' . $crlf
229 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
230 . '#' . $crlf .$crlf;
231 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
232 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
233 } else {
234 $dump_buffer .= $tcmt;
236 $tmp_buffer = '';
237 if (!isset($limit_from) || !isset($limit_to)) {
238 $limit_from = $limit_to = 0;
240 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url, (isset($sql_query)?urldecode($sql_query):''));
242 $dump_buffer .= $tmp_buffer;
243 } // end if
244 $i++;
245 } // end if-else
246 } // end while
248 // staybyte: don't remove, it makes easier to select & copy from
249 // browser
250 $dump_buffer .= $crlf;
251 } // end 'no csv or xml' case
253 // 'xml' case
254 else if ($GLOBALS['what'] == 'xml') {
255 // first add the xml tag
256 $dump_buffer .= '<?xml version="1.0" encoding="' . (empty($asfile) ? $charset : (isset($charset_of_file) ? $charset_of_file : $charset)) . '"?>' . $crlf . $crlf;
257 // some comments
258 $dump_buffer .= '<!--' . $crlf
259 . '-' . $crlf
260 . '- phpMyAdmin XML-Dump' . $crlf
261 . '- version ' . PMA_VERSION . $crlf
262 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
263 . '-' . $crlf
264 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
265 if (!empty($cfg['Server']['port'])) {
266 $dump_buffer .= ':' . $cfg['Server']['port'];
268 $dump_buffer .= $crlf
269 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
270 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
271 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
272 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
273 . '-' . $crlf
274 . '-->' . $crlf . $crlf;
275 // Now build the structure
276 // todo: Make db and table names XML compatible
277 $dump_buffer .= '<' . $db . '>' . $crlf;
278 if (isset($table_select)) {
279 $tmp_select = implode($table_select, '|');
280 $tmp_select = '|' . $tmp_select . '|';
282 $i = 0;
283 while ($i < $num_tables) {
284 if (!isset($single)) {
285 $table = PMA_mysql_tablename($tables, $i);
287 if (!isset($limit_from) || !isset($limit_to)) {
288 $limit_from = $limit_to = 0;
290 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
291 || (!isset($tmp_select) && !empty($table))) {
292 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url,
293 (isset($sql_query)?urldecode($sql_query):''));
295 $i++;
297 $dump_buffer .= '</' . $db . '>' . $crlf;
298 } // end 'xml' case
300 // latex case
301 else if ($GLOBALS['what'] == 'latex') {
304 $dump_buffer .= '% ' . $crlf
305 . '% phpMyAdmin LaTeX-Dump' . $crlf
306 . '% version ' . PMA_VERSION . $crlf
307 . '% http://www.phpmyadmin.net/ (download page)' . $crlf
308 . '%' . $crlf
309 . '% ' . $strHost . ': ' . $cfg['Server']['host'] . $crlf
310 . '%' . $crlf;
312 if (isset($table_select)) {
313 $tmp_select = implode($table_select, '|');
314 $tmp_select = '|' . $tmp_select . '|';
317 $i = 0;
318 while ($i < $num_tables) {
320 if (!isset($single)) {
321 $table = PMA_mysql_tablename($tables, $i);
323 if (!isset($limit_from) || !isset($limit_to)) {
324 $limit_from = $limit_to = 0;
326 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
327 || (!isset($tmp_select) && !empty($table))) {
329 // to do: add option for the formatting ( c, l, r, p)
330 $dump_buffer .= PMA_getTableLatex($db, $table, $environment, $limit_from, $limit_to, $crlf, $err_url,
331 (isset($sql_query)?urldecode($sql_query):''));
333 $i++;
336 } //end latex case
338 // 'csv' case
339 else {
340 // Handles the EOL character
341 if ($GLOBALS['what'] == 'excel') {
342 $add_character = "\015\012";
343 } else if (empty($add_character)) {
344 $add_character = $GLOBALS['crlf'];
345 } else {
346 $add_character = str_replace('\\r', "\015", $add_character);
347 $add_character = str_replace('\\n', "\012", $add_character);
348 $add_character = str_replace('\\t', "\011", $add_character);
349 } // end if
351 $tmp_buffer = '';
352 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url
353 , (isset($sql_query)?urldecode($sql_query):''));
354 $dump_buffer .= $tmp_buffer;
355 } // end 'csv case
356 } // end building the dump
360 * Send the dump as a file...
362 if (!empty($asfile)) {
363 // Convert the charset if required.
364 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
365 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
366 && (!empty($GLOBALS['asfile']))) {
367 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
370 // Do the compression
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 $dump_buffer = $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 $dump_buffer = bzcompress($dump_buffer);
390 if ($dump_buffer === -8) {
391 include('./header.inc.php3');
392 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
393 include('./footer.inc.php3');
394 exit;
398 // 3. as a gzipped file
399 else if (isset($gzip) && $gzip == 'gzip') {
400 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
401 // without the optional parameter level because it bug
402 $dump_buffer = gzencode($dump_buffer);
406 // finally send the headers and the file
407 header('Content-Type: ' . $mime_type);
408 header('Expires: ' . $now);
409 // lem9 & loic1: IE need specific headers
410 if (PMA_USR_BROWSER_AGENT == 'IE') {
411 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
412 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
413 header('Pragma: public');
414 } else {
415 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
416 header('Pragma: no-cache');
418 echo $dump_buffer;
421 * Displays the dump...
423 else {
424 echo htmlspecialchars($dump_buffer);
426 * Close the html tags and add the footers in dump is displayed on screen
428 echo ' </pre>' . "\n";
429 echo '</div>' . "\n";
430 echo "\n";
431 include('./footer.inc.php3');
432 } // end if