xhtml typo
[phpmyadmin/crack.git] / tbl_dump.php3
blobd10c5f2ba51ba7ac6d3234d6246ee226b6529106
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 && (isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == '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($compression) && $compression == 'bzip') {
140 $ext .= '.bz2';
141 $mime_type = 'application/x-bzip';
142 } else if (isset($compression) && $compression == 'gzip') {
143 $ext .= '.gz';
144 $mime_type = 'application/x-gzip';
145 } else if (isset($compression) && $compression == '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 . '|';
206 while ($i < $num_tables) {
207 if (!isset($single)) {
208 $table = PMA_mysql_tablename($tables, $i);
210 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
211 $i++;
212 } else {
213 $formatted_table_name = (isset($use_backquotes))
214 ? PMA_backquote($table)
215 : '\'' . $table . '\'';
216 // If only datas, no need to displays table name
217 if ((isset($sql_structure) && $sql_structure == 'structure') || ($what != 'sql' && $what != 'dataonly')) {
218 $dump_buffer .= '# --------------------------------------------------------' . $crlf
219 . $crlf . '#' . $crlf
220 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
221 . '#' . $crlf
222 . PMA_getTableDef($db, $table, $crlf, $err_url, $use_comments_work) . ';' . $crlf;
225 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
226 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
228 // At least data
229 if ((isset($sql_data) && $sql_data == 'data') || (!isset($sql_data) && ($what == 'data' || $what == 'dataonly'))) {
230 $tcmt = $crlf . '#' . $crlf
231 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
232 . '#' . $crlf .$crlf;
233 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
234 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
235 } else {
236 $dump_buffer .= $tcmt;
238 $tmp_buffer = '';
239 if (!isset($limit_from) || !isset($limit_to)) {
240 $limit_from = $limit_to = 0;
242 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url, (isset($sql_query)?urldecode($sql_query):''));
244 $dump_buffer .= $tmp_buffer;
245 } // end if
246 $i++;
247 } // end if-else
248 } // end while
250 // staybyte: don't remove, it makes easier to select & copy from
251 // browser
252 $dump_buffer .= $crlf;
253 } // end 'no csv or xml' case
255 // 'xml' case
256 else if ($GLOBALS['what'] == 'xml') {
257 // first add the xml tag
258 $dump_buffer .= '<?xml version="1.0" encoding="' . (empty($asfile) ? $charset : (isset($charset_of_file) ? $charset_of_file : $charset)) . '"?>' . $crlf . $crlf;
259 // some comments
260 $dump_buffer .= '<!--' . $crlf
261 . '-' . $crlf
262 . '- phpMyAdmin XML-Dump' . $crlf
263 . '- version ' . PMA_VERSION . $crlf
264 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
265 . '-' . $crlf
266 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
267 if (!empty($cfg['Server']['port'])) {
268 $dump_buffer .= ':' . $cfg['Server']['port'];
270 $dump_buffer .= $crlf
271 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
272 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
273 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
274 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
275 . '-' . $crlf
276 . '-->' . $crlf . $crlf;
277 // Now build the structure
278 // todo: Make db and table names XML compatible
279 $dump_buffer .= '<' . $db . '>' . $crlf;
280 if (isset($table_select)) {
281 $tmp_select = implode($table_select, '|');
282 $tmp_select = '|' . $tmp_select . '|';
284 $i = 0;
285 while ($i < $num_tables) {
286 if (!isset($single)) {
287 $table = PMA_mysql_tablename($tables, $i);
289 if (!isset($limit_from) || !isset($limit_to)) {
290 $limit_from = $limit_to = 0;
292 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
293 || (!isset($tmp_select) && !empty($table))) {
294 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url,
295 (isset($sql_query)?urldecode($sql_query):''));
297 $i++;
299 $dump_buffer .= '</' . $db . '>' . $crlf;
300 } // end 'xml' case
302 // latex case
303 else if ($GLOBALS['what'] == 'latex') {
306 $dump_buffer .= '% ' . $crlf
307 . '% phpMyAdmin LaTeX-Dump' . $crlf
308 . '% version ' . PMA_VERSION . $crlf
309 . '% http://www.phpmyadmin.net/ (download page)' . $crlf
310 . '%' . $crlf
311 . '% ' . $strHost . ': ' . $cfg['Server']['host'] . $crlf
312 . '%' . $crlf;
314 if (isset($table_select)) {
315 $tmp_select = implode($table_select, '|');
316 $tmp_select = '|' . $tmp_select . '|';
319 $i = 0;
320 while ($i < $num_tables) {
322 if (!isset($single)) {
323 $table = PMA_mysql_tablename($tables, $i);
325 if (!isset($limit_from) || !isset($limit_to)) {
326 $limit_from = $limit_to = 0;
328 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
329 || (!isset($tmp_select) && !empty($table))) {
331 // to do: add option for the formatting ( c, l, r, p)
332 $dump_buffer .= PMA_getTableLatex($db, $table, $environment, $limit_from, $limit_to, $crlf, $err_url,
333 (isset($sql_query)?urldecode($sql_query):''));
335 $i++;
338 } //end latex case
340 // 'csv' case
341 else {
342 // Handles the EOL character
343 if ($GLOBALS['what'] == 'excel') {
344 $add_character = "\015\012";
345 } else if (empty($add_character)) {
346 $add_character = $GLOBALS['crlf'];
347 } else {
348 $add_character = str_replace('\\r', "\015", $add_character);
349 $add_character = str_replace('\\n', "\012", $add_character);
350 $add_character = str_replace('\\t', "\011", $add_character);
351 } // end if
353 $tmp_buffer = '';
354 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url
355 , (isset($sql_query)?urldecode($sql_query):''));
356 $dump_buffer .= $tmp_buffer;
357 } // end 'csv case
358 } // end building the dump
362 * Send the dump as a file...
364 if (!empty($asfile)) {
365 // Convert the charset if required.
366 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
367 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
368 && (!empty($GLOBALS['asfile']))) {
369 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
372 // Do the compression
373 // 1. as a gzipped file
374 if (isset($compression) && $compression == '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 $dump_buffer = $zipfile -> file();
388 // 2. as a bzipped file
389 else if (isset($compression) && $compression == 'bzip') {
390 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
391 $dump_buffer = bzcompress($dump_buffer);
392 if ($dump_buffer === -8) {
393 include('./header.inc.php3');
394 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
395 include('./footer.inc.php3');
396 exit;
400 // 3. as a gzipped file
401 else if (isset($compression) && $compression == 'gzip') {
402 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
403 // without the optional parameter level because it bug
404 $dump_buffer = gzencode($dump_buffer);
408 // finally send the headers and the file
409 header('Content-Type: ' . $mime_type);
410 header('Expires: ' . $now);
411 // lem9 & loic1: IE need specific headers
412 if (PMA_USR_BROWSER_AGENT == 'IE') {
413 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
414 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
415 header('Pragma: public');
416 } else {
417 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
418 header('Pragma: no-cache');
420 echo $dump_buffer;
423 * Displays the dump...
425 else {
426 echo htmlspecialchars($dump_buffer);
428 * Close the html tags and add the footers in dump is displayed on screen
430 echo ' </pre>' . "\n";
431 echo '</div>' . "\n";
432 echo "\n";
433 include('./footer.inc.php3');
434 } // end if