new font
[phpmyadmin/crack.git] / tbl_dump.php3
blob7466500200807b5ea0155f377a60cbed67dc0f37
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 {
107 // Defines filename and extension, and also mime types
108 if (!isset($table)) {
109 if (isset($remember_template)) setcookie('pma_db_filename_template', $filename_template , 0, $GLOBALS['cookie_path'], '' , $GLOBALS['is_https']);
110 $filename = ereg_replace('__DB__', $db, strftime($filename_template));
111 } else {
112 if (isset($remember_template)) setcookie('pma_table_filename_template', $filename_template , 0, $GLOBALS['cookie_path'], '' , $GLOBALS['is_https']);
113 $filename = ereg_replace('__TABLE__', $table, ereg_replace('__DB__', $db, strftime($filename_template)));
115 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
116 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
117 } else {
118 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
121 // Generate basic dump extension
122 if ($what == 'csv' || $what == 'excel') {
123 $ext = 'csv';
124 $mime_type = 'text/x-csv';
125 } else if ($what == 'xml') {
126 $ext = 'xml';
127 $mime_type = 'text/xml';
128 } else if ($what == 'latex') {
129 $ext = 'tex';
130 $mime_type = 'application/x-tex';
131 } else {
132 $ext = 'sql';
133 // loic1: 'application/octet-stream' is the registered IANA type but
134 // MSIE and Opera seems to prefer 'application/octetstream'
135 $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA')
136 ? 'application/octetstream'
137 : 'application/octet-stream';
140 // If dump is going to be copressed, set correct mime_type and add
141 // compression to extension
142 if (isset($compression) && $compression == 'bzip') {
143 $ext .= '.bz2';
144 $mime_type = 'application/x-bzip';
145 } else if (isset($compression) && $compression == 'gzip') {
146 $ext .= '.gz';
147 $mime_type = 'application/x-gzip';
148 } else if (isset($compression) && $compression == 'zip') {
149 $ext .= '.zip';
150 $mime_type = 'application/x-zip';
153 $now = gmdate('D, d M Y H:i:s') . ' GMT';
154 } // end download
158 * Builds the dump
160 // Gets the number of tables if a dump of a database has been required
161 if (!isset($table)) {
162 $tables = PMA_mysql_list_tables($db);
163 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
164 } else {
165 $num_tables = 1;
166 $single = TRUE;
169 // No table -> error message
170 if ($num_tables == 0) {
171 echo '# ' . $strNoTablesFound;
173 // At least one table -> do the work
174 else {
175 // No csv or xml or latex format -> add some comments at the top
177 if (isset($use_comments) && $use_comments) {
178 require('./libraries/relation.lib.php3');
179 $cfgRelation = PMA_getRelationsParam();
180 $use_comments_work = true;
181 } else {
182 $use_comments_work = false;
185 if ($what != 'csv' && $what != 'excel' && $what != 'xml' && $what != 'latex') {
186 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
187 . '# version ' . PMA_VERSION . $crlf
188 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
189 . '#' . $crlf
190 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
191 if (!empty($cfg['Server']['port'])) {
192 $dump_buffer .= ':' . $cfg['Server']['port'];
194 $formatted_db_name = (isset($use_backquotes))
195 ? PMA_backquote($db)
196 : '\'' . $db . '\'';
197 $dump_buffer .= $crlf
198 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
199 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
200 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
201 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
203 $i = 0;
204 if (isset($table_select)) {
205 $tmp_select = implode($table_select, '|');
206 $tmp_select = '|' . $tmp_select . '|';
209 while ($i < $num_tables) {
210 if (!isset($single)) {
211 $table = PMA_mysql_tablename($tables, $i);
213 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
214 $i++;
215 } else {
216 $formatted_table_name = (isset($use_backquotes))
217 ? PMA_backquote($table)
218 : '\'' . $table . '\'';
219 // If only datas, no need to displays table name
220 if ((isset($sql_structure) && $sql_structure == 'structure') || ($what != 'sql' && $what != 'dataonly')) {
221 $dump_buffer .= '# --------------------------------------------------------' . $crlf
222 . $crlf . '#' . $crlf
223 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
224 . '#' . $crlf
225 . PMA_getTableDef($db, $table, $crlf, $err_url, $use_comments_work) . ';' . $crlf;
228 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
229 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
231 // At least data
232 if ((isset($sql_data) && $sql_data == 'data') || (!isset($sql_data) && ($what == 'data' || $what == 'dataonly'))) {
233 $tcmt = $crlf . '#' . $crlf
234 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
235 . '#' . $crlf .$crlf;
236 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
237 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
238 } else {
239 $dump_buffer .= $tcmt;
241 $tmp_buffer = '';
242 if (!isset($limit_from) || !isset($limit_to)) {
243 $limit_from = $limit_to = 0;
245 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url, (isset($sql_query)?urldecode($sql_query):''));
247 $dump_buffer .= $tmp_buffer;
248 } // end if
249 $i++;
250 } // end if-else
251 } // end while
253 // staybyte: don't remove, it makes easier to select & copy from
254 // browser
255 $dump_buffer .= $crlf;
256 } // end 'no csv or xml' case
258 // 'xml' case
259 else if ($GLOBALS['what'] == 'xml') {
260 // first add the xml tag
261 $dump_buffer .= '<?xml version="1.0" encoding="' . (empty($asfile) ? $charset : (isset($charset_of_file) ? $charset_of_file : $charset)) . '"?>' . $crlf . $crlf;
262 // some comments
263 $dump_buffer .= '<!--' . $crlf
264 . '-' . $crlf
265 . '- phpMyAdmin XML-Dump' . $crlf
266 . '- version ' . PMA_VERSION . $crlf
267 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
268 . '-' . $crlf
269 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
270 if (!empty($cfg['Server']['port'])) {
271 $dump_buffer .= ':' . $cfg['Server']['port'];
273 $dump_buffer .= $crlf
274 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
275 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
276 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
277 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
278 . '-' . $crlf
279 . '-->' . $crlf . $crlf;
280 // Now build the structure
281 // todo: Make db and table names XML compatible
282 $dump_buffer .= '<' . $db . '>' . $crlf;
283 if (isset($table_select)) {
284 $tmp_select = implode($table_select, '|');
285 $tmp_select = '|' . $tmp_select . '|';
287 $i = 0;
288 while ($i < $num_tables) {
289 if (!isset($single)) {
290 $table = PMA_mysql_tablename($tables, $i);
292 if (!isset($limit_from) || !isset($limit_to)) {
293 $limit_from = $limit_to = 0;
295 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
296 || (!isset($tmp_select) && !empty($table))) {
297 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url,
298 (isset($sql_query)?urldecode($sql_query):''));
300 $i++;
302 $dump_buffer .= '</' . $db . '>' . $crlf;
303 } // end 'xml' case
305 // latex case
306 else if ($GLOBALS['what'] == 'latex') {
309 $dump_buffer .= '% ' . $crlf
310 . '% phpMyAdmin LaTeX-Dump' . $crlf
311 . '% version ' . PMA_VERSION . $crlf
312 . '% http://www.phpmyadmin.net/ (download page)' . $crlf
313 . '%' . $crlf
314 . '% ' . $strHost . ': ' . $cfg['Server']['host'] . $crlf
315 . '%' . $crlf;
317 if (isset($table_select)) {
318 $tmp_select = implode($table_select, '|');
319 $tmp_select = '|' . $tmp_select . '|';
322 $i = 0;
323 while ($i < $num_tables) {
325 if (!isset($single)) {
326 $table = PMA_mysql_tablename($tables, $i);
328 if (!isset($limit_from) || !isset($limit_to)) {
329 $limit_from = $limit_to = 0;
331 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
332 || (!isset($tmp_select) && !empty($table))) {
334 // to do: add option for the formatting ( c, l, r, p)
335 $dump_buffer .= PMA_getTableLatex($db, $table, $environment, $limit_from, $limit_to, $crlf, $err_url,
336 (isset($sql_query)?urldecode($sql_query):''));
338 $i++;
341 } //end latex case
343 // 'csv' case
344 else {
345 // Handles the EOL character
346 if ($GLOBALS['what'] == 'excel') {
347 $add_character = "\015\012";
348 } else if (empty($add_character)) {
349 $add_character = $GLOBALS['crlf'];
350 } else {
351 $add_character = str_replace('\\r', "\015", $add_character);
352 $add_character = str_replace('\\n', "\012", $add_character);
353 $add_character = str_replace('\\t', "\011", $add_character);
354 } // end if
356 $tmp_buffer = '';
357 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url
358 , (isset($sql_query)?urldecode($sql_query):''));
359 $dump_buffer .= $tmp_buffer;
360 } // end 'csv case
361 } // end building the dump
365 * Send the dump as a file...
367 if (!empty($asfile)) {
368 // Convert the charset if required.
369 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
370 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
371 && (!empty($GLOBALS['asfile']))) {
372 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
375 // Do the compression
376 // 1. as a gzipped file
377 if (isset($compression) && $compression == 'zip') {
378 if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
379 if ($what == 'csv' || $what == 'excel') {
380 $extbis = '.csv';
381 } else if ($what == 'xml') {
382 $extbis = '.xml';
383 } else {
384 $extbis = '.sql';
386 $zipfile = new zipfile();
387 $zipfile -> addFile($dump_buffer, $filename . $extbis);
388 $dump_buffer = $zipfile -> file();
391 // 2. as a bzipped file
392 else if (isset($compression) && $compression == 'bzip') {
393 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
394 $dump_buffer = bzcompress($dump_buffer);
395 if ($dump_buffer === -8) {
396 include('./header.inc.php3');
397 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
398 include('./footer.inc.php3');
399 exit;
403 // 3. as a gzipped file
404 else if (isset($compression) && $compression == 'gzip') {
405 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
406 // without the optional parameter level because it bug
407 $dump_buffer = gzencode($dump_buffer);
411 // finally send the headers and the file
412 header('Content-Type: ' . $mime_type);
413 header('Expires: ' . $now);
414 // lem9 & loic1: IE need specific headers
415 if (PMA_USR_BROWSER_AGENT == 'IE') {
416 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
417 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
418 header('Pragma: public');
419 } else {
420 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
421 header('Pragma: no-cache');
423 echo $dump_buffer;
426 * Displays the dump...
428 else {
429 echo htmlspecialchars($dump_buffer);
431 * Close the html tags and add the footers in dump is displayed on screen
433 echo ' </pre>' . "\n";
434 echo '</div>' . "\n";
435 echo "\n";
436 include('./footer.inc.php3');
437 } // end if