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 // Defines the end of line delimiter to use
18 $eol_dlm = (isset($GLOBALS['extended_ins']) && ($GLOBALS['current_row'] < $GLOBALS['rows_cnt']))
21 // Result has to be displayed on screen
22 if (empty($GLOBALS['asfile'])) {
23 echo htmlspecialchars($sql_insert . $eol_dlm . $GLOBALS['crlf']);
25 // Result has to be saved in a text file
26 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
27 echo $sql_insert . $eol_dlm . $GLOBALS['crlf'];
29 // Result will be saved in a *zipped file
31 $tmp_buffer .= $sql_insert . $eol_dlm . $GLOBALS['crlf'];
33 } // end of the 'PMA_myHandler()' function
37 * Formats the INSERT statements depending on the target (screen/file) of the
40 * Revisions: 2001-05-07, Lem9: added $add_character
41 * 2001-07-12, loic1: $crlf should be used only if there is no EOL
42 * character defined by the user
44 * @param string the insert statement
46 * @global string the character to add at the end of lines
47 * @global string the buffer containing formatted strings
49 function PMA_myCsvHandler($sql_insert)
51 global $add_character;
54 // Result has to be displayed on screen
55 if (empty($GLOBALS['asfile'])) {
56 echo htmlspecialchars($sql_insert) . $add_character;
58 // Result has to be saved in a text file
59 else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
60 echo $sql_insert . $add_character;
62 // Result will be saved in a *zipped file
64 $tmp_buffer .= $sql_insert . $add_character;
66 } // end of the 'PMA_myCsvHandler()' function
71 * Get the variables sent or posted to this script and a core script
73 require('./libraries/grab_globals.lib.php3');
74 require('./libraries/common.lib.php3');
75 require('./libraries/build_dump.lib.php3');
76 require('./libraries/zip.lib.php3');
80 * Defines the url to return to in case of error in a sql statement
82 $err_url = 'tbl_properties.php3'
84 . '&server=' . $server
85 . '&db=' . urlencode($db)
86 . (isset($table) ?
'&table=' . urlencode($table) : '');
90 * Increase time limit for script execution and initializes some variables
92 @set_time_limit
($cfgExecTimeLimit);
94 // Defines the default <CR><LF> format
95 $crlf = PMA_whichCrlf();
99 * Ensure zipped formats are associated with the download feature
102 && (!empty($zip) ||
!empty($gzip) ||
!empty($bzip))) {
108 * Send headers depending on whether the user choosen to download a dump file
112 if (empty($asfile)) {
113 $cfgServer_backup = $cfgServer;
114 include('./header.inc.php3');
115 $cfgServer = $cfgServer_backup;
116 unset($cfgServer_backup);
117 echo '<div align="' . $cell_align_left . '">' . "\n";
118 echo ' <pre>' . "\n";
123 // Defines filename and extension, and also mime types
124 if (!isset($table)) {
129 if (isset($bzip) && $bzip == 'bzip') {
131 $mime_type = 'application/x-bzip';
132 } else if (isset($gzip) && $gzip == 'gzip') {
134 $mime_type = 'application/x-gzip';
135 } else if (isset($zip) && $zip == 'zip') {
137 $mime_type = 'application/x-zip';
138 } else if ($what == 'csv' ||
$what == 'excel') {
140 $mime_type = 'text/x-csv';
143 // loic1: 'application/octet-stream' is the registered IANA type but
144 // MSIE and Opera seems to prefer 'application/octetstream'
145 $mime_type = (PMA_USR_BROWSER_AGENT
== 'IE' || PMA_USR_BROWSER_AGENT
== 'OPERA')
146 ?
'application/octetstream'
147 : 'application/octet-stream';
151 header('Content-Type: ' . $mime_type);
152 // lem9 & loic1: IE need specific headers
153 if (PMA_USR_BROWSER_AGENT
== 'IE') {
154 header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
155 header('Expires: 0');
156 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
157 header('Pragma: public');
159 header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
160 header('Expires: 0');
161 header('Pragma: no-cache');
169 // Gets the number of tables if a dump of a database has been required
170 if (!isset($table)) {
171 $tables = mysql_list_tables($db);
172 $num_tables = @mysql_numrows
($tables);
178 // No table -> error message
179 if ($num_tables == 0) {
180 echo '# ' . $strNoTablesFound;
182 // At least on table -> do the work
184 // No csv format -> add some comments at the top
185 if ($what != 'csv' && $what != 'excel') {
186 $dump_buffer .= '# phpMyAdmin MySQL-Dump' . $crlf
187 . '# version ' . PMA_VERSION
. $crlf
188 . '# http://phpwizard.net/phpMyAdmin/' . $crlf
189 . '# http://phpmyadmin.sourceforge.net/ (download page)' . $crlf
191 . '# ' . $strHost . ': ' . $cfgServer['host'];
192 if (!empty($cfgServer['port'])) {
193 $dump_buffer .= ':' . $cfgServer['port'];
195 $formatted_db_name = (isset($use_backquotes))
198 $dump_buffer .= $crlf
199 . '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
200 . '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION
, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION
, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION
, 3) . $crlf
201 . '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
202 . '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
205 if (isset($table_select)) {
206 $tmp_select = implode($table_select, '|');
207 $tmp_select = '|' . $tmp_select . '|';
209 while ($i < $num_tables) {
210 if (!isset($single)) {
211 $table = mysql_tablename($tables, $i);
213 if (isset($tmp_select) && is_int(strpos($tmp_select, '|' . $table . '|')) == FALSE) {
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 ($what != 'dataonly') {
221 $dump_buffer.= '# --------------------------------------------------------' . $crlf
222 . $crlf . '#' . $crlf
223 . '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
224 . '#' . $crlf . $crlf
225 . PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
228 if (($what == 'data') ||
($what == 'dataonly')) {
229 $dump_buffer .= $crlf . '#' . $crlf
230 . '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
231 . '#' . $crlf .$crlf;
233 if (!isset($limit_from) ||
!isset($limit_to)) {
234 $limit_from = $limit_to = 0;
236 // loic1: display data if they aren't bufferized
237 if (!isset($zip) && !isset($bzip) && !isset($gzip)) {
241 PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url);
242 $dump_buffer .= $tmp_buffer;
248 // staybyte: don't remove, it makes easier to select & copy from
250 $dump_buffer .= $crlf;
251 } // end 'no csv' case
255 // Handles the EOL character
256 if ($GLOBALS['what'] == 'excel') {
257 $add_character = "\015\012";
258 } else if (empty($add_character)) {
259 $add_character = $GLOBALS['crlf'];
261 if (get_magic_quotes_gpc()) {
262 $add_character = stripslashes($add_character);
264 $add_character = str_replace('\\r', "\015", $add_character);
265 $add_character = str_replace('\\n', "\012", $add_character);
266 $add_character = str_replace('\\t', "\011", $add_character);
270 PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url);
271 $dump_buffer .= $tmp_buffer;
273 } // end building the dump
277 * "Displays" the dump...
279 // 1. as a gzipped file
280 if (isset($zip) && $zip == 'zip') {
281 if (PMA_PHP_INT_VERSION
>= 40000 && @function_exists
('gzcompress')) {
282 if ($what == 'csv' ||
$what == 'excel') {
287 $zipfile = new zipfile();
288 $zipfile -> addFile($dump_buffer, $filename . $extbis);
289 echo $zipfile -> file();
292 // 2. as a bzipped file
293 else if (isset($bzip) && $bzip == 'bzip') {
294 if (PMA_PHP_INT_VERSION
>= 40004 && @function_exists
('bzcompress')) {
295 echo bzcompress($dump_buffer);
298 // 3. as a gzipped file
299 else if (isset($gzip) && $gzip == 'gzip') {
300 if (PMA_PHP_INT_VERSION
>= 40004 && @function_exists
('gzencode')) {
301 // without the optional parameter level because it bug
302 echo gzencode($dump_buffer);
305 // 4. on screen or as a text file
312 * Close the html tags and add the footers in dump is displayed on screen
314 if (empty($asfile)) {
315 echo ' </pre>' . "\n";
316 echo '</div>' . "\n";
318 include('./footer.inc.php3');