6 * Formats the INSERT statements depending on the target (screen/file) of the
9 * @param string the insert statement
11 * @global string the buffer containing formatted strings
13 function PMA_myHandler($sql_insert)
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'] : '');
22 // Convert the charset if required.
23 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
24 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
25 && (!empty($GLOBALS['asfile']))) {
26 $sql_insert = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $sql_insert);
29 // Defines the end of line delimiter to use
30 $eol_dlm = (isset($GLOBALS['extended_ins']) && ($GLOBALS['current_row'] < $GLOBALS['rows_cnt']))
33 // Result has to be displayed on screen
34 if (empty($GLOBALS['asfile'])) {
35 echo htmlspecialchars($sql_insert . $eol_dlm . $GLOBALS['crlf']);
37 // Result has to be saved in a text file
38 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
39 echo $sql_insert . $eol_dlm . $GLOBALS['crlf'];
41 // Result will be saved in a *zipped file
43 $tmp_buffer .= $sql_insert . $eol_dlm . $GLOBALS['crlf'];
45 } // end of the 'PMA_myHandler()' function
49 * Formats the INSERT statements depending on the target (screen/file) of the
52 * Revisions: 2001-05-07, Lem9: added $add_character
53 * 2001-07-12, loic1: $crlf should be used only if there is no EOL
54 * character defined by the user
56 * @param string the insert statement
58 * @global string the character to add at the end of lines
59 * @global string the buffer containing formatted strings
61 function PMA_myCsvHandler($sql_insert)
63 global $add_character;
66 // Kanji encoding convert feature appended by Y.Kawada (2001/2/21)
67 if (function_exists('PMA_kanji_str_conv')) {
68 $sql_insert = PMA_kanji_str_conv($sql_insert, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ?
$GLOBALS['xkana'] : '');
70 // Convert the charset if required.
71 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
72 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
73 && (!empty($GLOBALS['asfile']))) {
74 $sql_insert = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $sql_insert);
76 // Result has to be displayed on screen
77 if (empty($GLOBALS['asfile'])) {
78 echo htmlspecialchars($sql_insert) . $add_character;
80 // Result has to be saved in a text file
81 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
82 echo $sql_insert . $add_character;
84 // Result will be saved in a *zipped file
86 $tmp_buffer .= $sql_insert . $add_character;
88 } // end of the 'PMA_myCsvHandler()' function
93 * Get the variables sent or posted to this script and a core script
95 require('./libraries/grab_globals.lib.php3');
96 require('./libraries/common.lib.php3');
97 require('./libraries/build_dump.lib.php3');
98 require('./libraries/zip.lib.php3');
102 * Defines the url to return to in case of error in a sql statement
104 $err_url = 'tbl_properties.php3'
106 . '&convcharset=' . $convcharset
107 . '&server=' . $server
108 . '&db=' . urlencode($db)
109 . (isset($table) ?
'&table=' . urlencode($table) : '');
113 * Increase time limit for script execution and initializes some variables
115 @set_time_limit
($cfg['ExecTimeLimit']);
117 // Defines the default <CR><LF> format
118 $crlf = PMA_whichCrlf();
122 * Ensure zipped formats are associated with the download feature
125 && (!empty($zip) ||
!empty($gzip) ||
!empty($bzip))) {
131 * Send headers depending on whether the user choosen to download a dump file
135 if (empty($asfile)) {
136 $backup_cfgServer = $cfg['Server'];
137 include('./header.inc.php3');
138 $cfg['Server'] = $backup_cfgServer;
139 unset($backup_cfgServer);
140 echo '<div align="' . $cell_align_left . '">' . "\n";
141 echo ' <pre>' . "\n";
146 // Defines filename and extension, and also mime types
147 if (!isset($table)) {
148 $filename = PMA_convert_string($convcharset, 'iso8859-1', $db);
150 $filename = PMA_convert_string($charset, 'iso8859-1', $table);
152 if (isset($bzip) && $bzip == 'bzip') {
154 $mime_type = 'application/x-bzip';
155 } else if (isset($gzip) && $gzip == 'gzip') {
157 $mime_type = 'application/x-gzip';
158 } else if (isset($zip) && $zip == 'zip') {
160 $mime_type = 'application/x-zip';
161 } else if ($what == 'csv' ||
$what == 'excel') {
163 $mime_type = 'text/x-csv';
164 } else if ($what == 'xml') {
166 $mime_type = 'text/xml';
169 // loic1: 'application/octet-stream' is the registered IANA type but
170 // MSIE and Opera seems to prefer 'application/octetstream'
171 $mime_type = (PMA_USR_BROWSER_AGENT
== 'IE' || PMA_USR_BROWSER_AGENT
== 'OPERA')
172 ?
'application/octetstream'
173 : 'application/octet-stream';
177 header('Content-Type: ' . $mime_type);
178 // lem9 & loic1: IE need specific headers
179 if (PMA_USR_BROWSER_AGENT
== 'IE') {
180 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
181 header('Expires: 0');
182 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
183 header('Pragma: public');
185 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
186 header('Expires: 0');
187 header('Pragma: no-cache');
195 // Gets the number of tables if a dump of a database has been required
196 if (!isset($table)) {
197 $tables = PMA_mysql_list_tables($db);
198 $num_tables = ($tables) ? @mysql_numrows
($tables) : 0;
204 // No table -> error message
205 if ($num_tables == 0) {
206 echo '# ' . $strNoTablesFound;
208 // At least one table -> do the work
210 // No csv or xml format -> add some comments at the top
211 if ($what != 'csv' && $what != 'excel' && $what != 'xml') {
212 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
213 . '# version ' . PMA_VERSION
. $crlf
214 . '# http://www.phpmyadmin.net/ (download page)' . $crlf
216 . '# ' . $strHost . ': ' . $cfg['Server']['host'];
217 if (!empty($cfg['Server']['port'])) {
218 $dump_buffer .= ':' . $cfg['Server']['port'];
220 $formatted_db_name = (isset($use_backquotes))
223 $dump_buffer .= $crlf
224 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
225 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION
, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION
, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION
, 3) . $crlf
226 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
227 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
230 if (isset($table_select)) {
231 $tmp_select = implode($table_select, '|');
232 $tmp_select = '|' . $tmp_select . '|';
234 while ($i < $num_tables) {
235 if (!isset($single)) {
236 $table = PMA_mysql_tablename($tables, $i);
238 if (isset($tmp_select) && !strpos(' ' . $tmp_select, '|' . $table . '|')) {
241 $formatted_table_name = (isset($use_backquotes))
242 ?
PMA_backquote($table)
243 : '\'' . $table . '\'';
244 // If only datas, no need to displays table name
245 if ($what != 'dataonly') {
246 $dump_buffer .= '# --------------------------------------------------------' . $crlf
247 . $crlf . '#' . $crlf
248 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
249 . '#' . $crlf . $crlf
250 . PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
252 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
253 $dump_buffer = PMA_kanji_str_conv($dump_buffer, $knjenc, isset($xkana) ?
$xkana : '');
255 // Convert the charset if required.
256 if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
257 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
258 && (!empty($GLOBALS['asfile']))) {
259 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
262 if (($what == 'data') ||
($what == 'dataonly')) {
263 $tcmt = $crlf . '#' . $crlf
264 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
265 . '#' . $crlf .$crlf;
266 if (function_exists('PMA_kanji_str_conv')) { // Y.Kawada
267 $dump_buffer .= PMA_kanji_str_conv($tcmt, $knjenc, isset($xkana) ?
$xkana : '');
269 // Converts the charset if required.
270 else if ($GLOBALS['cfg']['AllowAnywhereRecoding'] && $GLOBALS['allow_recoding']
271 && isset($GLOBALS['charset_of_file']) && $GLOBALS['charset_of_file'] != $GLOBALS['charset']
272 && (!empty($GLOBALS['asfile']))) {
273 $dump_buffer .= PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $tcmt);
275 $dump_buffer .= $tcmt;
278 if (!isset($limit_from) ||
!isset($limit_to)) {
279 $limit_from = $limit_to = 0;
281 // loic1: display data if they aren't bufferized
282 if (!isset($zip) && !isset($bzip) && !isset($gzip)) {
286 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url);
288 $dump_buffer .= $tmp_buffer;
294 // staybyte: don't remove, it makes easier to select & copy from
296 $dump_buffer .= $crlf;
297 } // end 'no csv or xml' case
300 else if ($GLOBALS['what'] == 'xml') {
301 // first add the xml tag
302 $dump_buffer .= '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf . $crlf;
304 $dump_buffer .= '<!--' . $crlf
306 . '- phpMyAdmin XML-Dump' . $crlf
307 . '- version ' . PMA_VERSION
. $crlf
308 . '- http://www.phpmyadmin.net/ (download page)' . $crlf
310 . '- ' . $strHost . ': ' . $cfg['Server']['host'];
311 if (!empty($cfg['Server']['port'])) {
312 $dump_buffer .= ':' . $cfg['Server']['port'];
314 $dump_buffer .= $crlf
315 . '- ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
316 . '- ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION
, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION
, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION
, 3) . $crlf
317 . '- ' . $strPHPVersion . ': ' . phpversion() . $crlf
318 . '- ' . $strDatabase . ': \'' . $db . '\'' . $crlf
320 . '-->' . $crlf . $crlf;
321 // Now build the structure
322 // todo: Make db and table names XML compatible
323 $dump_buffer .= '<' . $db . '>' . $crlf;
324 if (isset($table_select)) {
325 $tmp_select = implode($table_select, '|');
326 $tmp_select = '|' . $tmp_select . '|';
329 while ($i < $num_tables) {
330 if (!isset($single)) {
331 $table = PMA_mysql_tablename($tables, $i);
333 if (!isset($limit_from) ||
!isset($limit_to)) {
334 $limit_from = $limit_to = 0;
336 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
337 ||
(!isset($tmp_select) && !empty($table))) {
338 $dump_buffer .= PMA_getTableXML($db, $table, $limit_from, $limit_to, $crlf, $err_url);
342 $dump_buffer .= '</' . $db . '>' . $crlf;
347 // Handles the EOL character
348 if ($GLOBALS['what'] == 'excel') {
349 $add_character = "\015\012";
350 } else if (empty($add_character)) {
351 $add_character = $GLOBALS['crlf'];
353 if (get_magic_quotes_gpc()) {
354 $add_character = stripslashes($add_character);
356 $add_character = str_replace('\\r', "\015", $add_character);
357 $add_character = str_replace('\\n', "\012", $add_character);
358 $add_character = str_replace('\\t', "\011", $add_character);
362 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url);
363 $dump_buffer .= $tmp_buffer;
365 } // end building the dump
369 * "Displays" the dump...
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') {
376 } else if ($what == 'xml') {
381 $zipfile = new zipfile();
382 $zipfile -> addFile($dump_buffer, $filename . $extbis);
383 echo $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 echo bzcompress($dump_buffer);
392 // 3. as a gzipped file
393 else if (isset($gzip) && $gzip == 'gzip') {
394 if (PMA_PHP_INT_VERSION
>= 40004 && @function_exists
('gzencode')) {
395 // without the optional parameter level because it bug
396 echo gzencode($dump_buffer);
400 else if (!empty($asfile)) {
405 echo htmlspecialchars($dump_buffer);
409 * Close the html tags and add the footers in dump is displayed on screen
411 if (empty($asfile)) {
412 echo ' </pre>' . "\n";
413 echo '</div>' . "\n";
415 include('./footer.inc.php3');