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