update
[phpmyadmin/crack.git] / tbl_dump.php3
blobc317c13b1c56a4c4f42256509e9aef30eecec08f
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'] : '');
21 // Defines the end of line delimiter to use
22 $eol_dlm = (isset($GLOBALS['extended_ins']) && ($GLOBALS['current_row'] < $GLOBALS['rows_cnt']))
23 ? ','
24 : ';';
25 // Result has to be displayed on screen
26 if (empty($GLOBALS['asfile'])) {
27 echo htmlspecialchars($sql_insert . $eol_dlm . $GLOBALS['crlf']);
29 // Result has to be saved in a text file
30 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
31 echo $sql_insert . $eol_dlm . $GLOBALS['crlf'];
33 // Result will be saved in a *zipped file
34 else {
35 $tmp_buffer .= $sql_insert . $eol_dlm . $GLOBALS['crlf'];
37 } // end of the 'PMA_myHandler()' function
40 /**
41 * Formats the INSERT statements depending on the target (screen/file) of the
42 * cvs export
44 * Revisions: 2001-05-07, Lem9: added $add_character
45 * 2001-07-12, loic1: $crlf should be used only if there is no EOL
46 * character defined by the user
48 * @param string the insert statement
50 * @global string the character to add at the end of lines
51 * @global string the buffer containing formatted strings
53 function PMA_myCsvHandler($sql_insert)
55 global $add_character;
56 global $tmp_buffer;
58 // Kanji encoding convert feature appended by Y.Kawada (2001/2/21)
59 if (function_exists('PMA_kanji_str_conv')) {
60 $sql_insert = PMA_kanji_str_conv($sql_insert, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
62 // Result has to be displayed on screen
63 if (empty($GLOBALS['asfile'])) {
64 echo htmlspecialchars($sql_insert) . $add_character;
66 // Result has to be saved in a text file
67 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
68 echo $sql_insert . $add_character;
70 // Result will be saved in a *zipped file
71 else {
72 $tmp_buffer .= $sql_insert . $add_character;
74 } // end of the 'PMA_myCsvHandler()' function
78 /**
79 * Get the variables sent or posted to this script and a core script
81 require('./libraries/grab_globals.lib.php3');
82 require('./libraries/common.lib.php3');
83 require('./libraries/build_dump.lib.php3');
84 require('./libraries/zip.lib.php3');
87 /**
88 * Defines the url to return to in case of error in a sql statement
90 $err_url = 'tbl_properties.php3'
91 . '?lang=' . $lang
92 . '&amp;convcharset=' . $convcharset
93 . '&amp;server=' . $server
94 . '&amp;db=' . urlencode($db)
95 . (isset($table) ? '&amp;table=' . urlencode($table) : '');
98 /**
99 * Increase time limit for script execution and initializes some variables
101 @set_time_limit($cfg['ExecTimeLimit']);
102 $dump_buffer = '';
103 // Defines the default <CR><LF> format
104 $crlf = PMA_whichCrlf();
108 * Ensure zipped formats are associated with the download feature
110 if (empty($asfile)
111 && (!empty($zip) || !empty($gzip) || !empty($bzip))) {
112 $asfile = 1;
117 * Send headers depending on whether the user choosen to download a dump file
118 * or not
120 // No download
121 if (empty($asfile)) {
122 $backup_cfgServer = $cfg['Server'];
123 include('./header.inc.php3');
124 $cfg['Server'] = $backup_cfgServer;
125 unset($backup_cfgServer);
126 echo '<div align="' . $cell_align_left . '">' . "\n";
127 echo ' <pre>' . "\n";
128 } // end if
130 // Download
131 else {
132 // Defines filename and extension, and also mime types
133 if (!isset($table)) {
134 $filename = $db;
135 } else {
136 $filename = $table;
138 if (isset($bzip) && $bzip == 'bzip') {
139 $ext = 'bz2';
140 $mime_type = 'application/x-bzip';
141 } else if (isset($gzip) && $gzip == 'gzip') {
142 $ext = 'gz';
143 $mime_type = 'application/x-gzip';
144 } else if (isset($zip) && $zip == 'zip') {
145 $ext = 'zip';
146 $mime_type = 'application/x-zip';
147 } else if ($what == 'csv' || $what == 'excel') {
148 $ext = 'csv';
149 $mime_type = 'text/x-csv';
150 } else if ($what == 'xml') {
151 $ext = 'xml';
152 $mime_type = 'text/xml';
153 } else {
154 $ext = 'sql';
155 // loic1: 'application/octet-stream' is the registered IANA type but
156 // MSIE and Opera seems to prefer 'application/octetstream'
157 $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA')
158 ? 'application/octetstream'
159 : 'application/octet-stream';
162 // Send headers
163 header('Content-Type: ' . $mime_type);
164 // lem9 & loic1: IE need specific headers
165 if (PMA_USR_BROWSER_AGENT == 'IE') {
166 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
167 header('Expires: 0');
168 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
169 header('Pragma: public');
170 } else {
171 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
172 header('Expires: 0');
173 header('Pragma: no-cache');
175 } // end download
179 * Builds the dump
181 // Gets the number of tables if a dump of a database has been required
182 if (!isset($table)) {
183 $tables = PMA_mysql_list_tables($db);
184 $num_tables = ($tables) ? @mysql_numrows($tables) : 0;
185 } else {
186 $num_tables = 1;
187 $single = TRUE;
190 // No table -> error message
191 if ($num_tables == 0) {
192 echo '# ' . $strNoTablesFound;
194 // At least one table -> do the work
195 else {
196 // No csv or xml format -> add some comments at the top
197 if ($what != 'csv' && $what != 'excel' && $what != 'xml') {
198 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
199 . '# version ' . PMA_VERSION . $crlf
200 . '# http://phpwizard.net/phpMyAdmin/' . $crlf
201 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
202 . '#' . $crlf
203 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
204 if (!empty($cfg['Server']['port'])) {
205 $dump_buffer .= ':' . $cfg['Server']['port'];
207 $formatted_db_name = (isset($use_backquotes))
208 ? PMA_backquote($db)
209 : '\'' . $db . '\'';
210 $dump_buffer .= $crlf
211 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
212 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
213 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
214 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
216 $i = 0;
217 if (isset($table_select)) {
218 $tmp_select = implode($table_select, '|');
219 $tmp_select = '|' . $tmp_select . '|';
221 while ($i < $num_tables) {
222 if (!isset($single)) {
223 $table = PMA_mysql_tablename($tables, $i);
225 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
226 $i++;
227 } else {
228 $formatted_table_name = (isset($use_backquotes))
229 ? PMA_backquote($table)
230 : '\'' . $table . '\'';
231 // If only datas, no need to displays table name
232 if ($what != 'dataonly') {
233 $dump_buffer .= '# --------------------------------------------------------' . $crlf
234 . $crlf . '#' . $crlf
235 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
236 . '#' . $crlf . $crlf
237 . PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
239 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
240 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ? $xkana : '');
242 // At least data
243 if (($what == 'data') || ($what == 'dataonly')) {
244 $tcmt = $crlf . '#' . $crlf
245 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
246 . '#' . $crlf .$crlf;
247 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
248 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ? $xkana : '');
249 } else {
250 $dump_buffer .= $tcmt;
252 $tmp_buffer = '';
253 if (!isset($limit_from) || !isset($limit_to)) {
254 $limit_from = $limit_to = 0;
256 // loic1: display data if they aren't bufferized
257 if (!isset($zip) && !isset($bzip) && !isset($gzip)) {
258 echo $dump_buffer;
259 $dump_buffer = '';
261 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url);
263 $dump_buffer .= $tmp_buffer;
264 } // end if
265 $i++;
266 } // end if-else
267 } // end while
269 // staybyte: don't remove, it makes easier to select & copy from
270 // browser
271 $dump_buffer .= $crlf;
272 } // end 'no csv or xml' case
274 // 'xml' case
275 else if ($GLOBALS['what'] == 'xml') {
276 // first add the xml tag
277 $dump_buffer .= '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf . $crlf;
278 // some comments
279 $dump_buffer .= '<!--' . $crlf
280 . '-' . $crlf
281 . '- phpMyAdmin XML-Dump' . $crlf
282 . '- version ' . PMA_VERSION . $crlf
283 . '- http://phpwizard.net/phpMyAdmin/' . $crlf
284 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
285 . '-' . $crlf
286 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
287 if (!empty($cfg['Server']['port'])) {
288 $dump_buffer .= ':' . $cfg['Server']['port'];
290 $dump_buffer .= $crlf
291 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
292 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
293 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
294 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
295 . '-' . $crlf
296 . '-->' . $crlf . $crlf;
297 // Now build the structure
298 // todo: Make db and table names XML compatible
299 $dump_buffer .= '<' . $db . '>' . $crlf;
300 if (isset($table_select)) {
301 $tmp_select = implode($table_select, '|');
302 $tmp_select = '|' . $tmp_select . '|';
304 $i = 0;
305 while ($i < $num_tables) {
306 if (!isset($single)) {
307 $table = PMA_mysql_tablename($tables, $i);
309 if (!isset($limit_from) || !isset($limit_to)) {
310 $limit_from = $limit_to = 0;
312 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
313 || (!isset($tmp_select) && !empty($table))) {
314 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url);
316 $i++;
318 $dump_buffer .= '</' . $db . '>' . $crlf;
319 } // end 'xml' case
321 // 'csv' case
322 else {
323 // Handles the EOL character
324 if ($GLOBALS['what'] == 'excel') {
325 $add_character = "\015\012";
326 } else if (empty($add_character)) {
327 $add_character = $GLOBALS['crlf'];
328 } else {
329 if (get_magic_quotes_gpc()) {
330 $add_character = stripslashes($add_character);
332 $add_character = str_replace('\\r', "\015", $add_character);
333 $add_character = str_replace('\\n', "\012", $add_character);
334 $add_character = str_replace('\\t', "\011", $add_character);
335 } // end if
337 $tmp_buffer = '';
338 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url);
339 $dump_buffer .= $tmp_buffer;
340 } // end 'csv case
341 } // end building the dump
345 * "Displays" the dump...
347 // 1. as a gzipped file
348 if (isset($zip) && $zip == 'zip') {
349 if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
350 if ($what == 'csv' || $what == 'excel') {
351 $extbis = '.csv';
352 } else if ($what == 'xml') {
353 $extbis = '.xml';
354 } else {
355 $extbis = '.sql';
357 $zipfile = new zipfile();
358 $zipfile -> addFile($dump_buffer, $filename . $extbis);
359 echo $zipfile -> file();
362 // 2. as a bzipped file
363 else if (isset($bzip) && $bzip == 'bzip') {
364 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
365 echo bzcompress($dump_buffer);
368 // 3. as a gzipped file
369 else if (isset($gzip) && $gzip == 'gzip') {
370 if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
371 // without the optional parameter level because it bug
372 echo gzencode($dump_buffer);
375 // 4. as a text file
376 else if (!empty($asfile)) {
377 echo $dump_buffer;
379 // 5. on display
380 else {
381 echo htmlspecialchars($dump_buffer);
385 * Close the html tags and add the footers in dump is displayed on screen
387 if (empty($asfile)) {
388 echo ' </pre>' . "\n";
389 echo '</div>' . "\n";
390 echo "\n";
391 include('./footer.inc.php3');
392 } // end if