Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: export.php,v 2.3.2.1 2004/02/02 17:03:52 lem9 Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00008 require_once('./libraries/grab_globals.lib.php'); 00009 require_once('./libraries/common.lib.php'); 00010 require_once('./libraries/zip.lib.php'); 00011 00012 PMA_checkParameters(array('what')); 00013 00014 // What type of export are we doing? 00015 if ($what == 'excel') { 00016 $type = 'csv'; 00017 } else { 00018 $type = $what; 00019 } 00020 00024 require('./libraries/export/' . preg_replace('@\.\.*@','.',$type) . '.php'); 00025 00026 // Generate error url 00027 if ($export_type == 'server') { 00028 $err_url = 'server_export.php?' . PMA_generate_common_url(); 00029 } elseif ($export_type == 'database') { 00030 $err_url = 'db_details_export.php?' . PMA_generate_common_url($db); 00031 } else { 00032 $err_url = 'tbl_properties_export.php?' . PMA_generate_common_url($db, $table); 00033 } 00034 00038 @set_time_limit($cfg['ExecTimeLimit']); 00039 00040 // Start with empty buffer 00041 $dump_buffer = ''; 00042 $dump_buffer_len = 0; 00043 00044 // We send fake headers to avoid browser timeout when buffering 00045 $time_start = time(); 00046 00047 00056 function PMA_exportOutputHandler($line) 00057 { 00058 global $time_start, $dump_buffer, $dump_buffer_len; 00059 00060 // Kanji encoding convert feature 00061 if (function_exists('PMA_kanji_str_conv')) { 00062 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''); 00063 } 00064 // If we have to buffer data, we will perform everything at once at the end 00065 if ($GLOBALS['buffer_needed']) { 00066 00067 $dump_buffer .= $line; 00068 if ($GLOBALS['onfly_compression']) { 00069 00070 $dump_buffer_len += strlen($dump_buffer); 00071 00072 if ($dump_buffer_len > $GLOBALS['memory_limit']) { 00073 // as bzipped 00074 if ($GLOBALS['output_charset_conversion']) { 00075 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer); 00076 } 00077 if ($GLOBALS['compression'] == 'bzip' && @function_exists('bzcompress')) { 00078 $dump_buffer = bzcompress($dump_buffer); 00079 } 00080 // as a gzipped file 00081 else if ($GLOBALS['compression'] == 'gzip' && @function_exists('gzencode')) { 00082 // without the optional parameter level because it bug 00083 $dump_buffer = gzencode($dump_buffer); 00084 } 00085 if ($GLOBALS['save_on_server']) { 00086 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer); 00087 if (!$write_result || ($write_result != strlen($line))) { 00088 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename)); 00089 return FALSE; 00090 } 00091 } else { 00092 echo $dump_buffer; 00093 } 00094 $dump_buffer = ''; 00095 $dump_buffer_len = 0; 00096 } 00097 } else { 00098 $time_now = time(); 00099 if ($time_start >= $time_now + 30) { 00100 $time_start = $time_now; 00101 header('X-pmaPing: Pong'); 00102 } // end if 00103 } 00104 } else { 00105 if ($GLOBALS['asfile']) { 00106 if ($GLOBALS['save_on_server']) { 00107 $write_result = @fwrite($GLOBALS['file_handle'], $line); 00108 if (!$write_result || ($write_result != strlen($line))) { 00109 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename)); 00110 return FALSE; 00111 } 00112 $time_now = time(); 00113 if ($time_start >= $time_now + 30) { 00114 $time_start = $time_now; 00115 header('X-pmaPing: Pong'); 00116 } // end if 00117 } else { 00118 // We export as file - output normally 00119 if ($GLOBALS['output_charset_conversion']) { 00120 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line); 00121 } 00122 echo $line; 00123 } 00124 } else { 00125 // We export as html - replace special chars 00126 echo htmlspecialchars($line); 00127 } 00128 } 00129 return TRUE; 00130 } // end of the 'PMA_exportOutputHandler()' function 00131 00132 // Will we save dump on server? 00133 $save_on_server = isset($cfg['SaveDir']) && !empty($cfg['SaveDir']) && !empty($onserver); 00134 00135 // Ensure compressed formats are associated with the download feature 00136 if (empty($asfile)) { 00137 if ($save_on_server) { 00138 $asfile = TRUE; 00139 } elseif (isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip')) { 00140 $asfile = TRUE; 00141 } else { 00142 $asfile = FALSE; 00143 } 00144 } else { 00145 $asfile = TRUE; 00146 } 00147 00148 // Defines the default <CR><LF> format 00149 $crlf = PMA_whichCrlf(); 00150 00151 // Do we need to convert charset? 00152 $output_charset_conversion = $asfile && 00153 $cfg['AllowAnywhereRecoding'] && $allow_recoding 00154 && isset($charset_of_file) && $charset_of_file != $charset; 00155 00156 // Set whether we will need buffering 00157 $buffer_needed = isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip'); 00158 00159 // Use on fly compression? 00160 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && isset($compression) && ($compression == 'gzip' | $compression == 'bzip'); 00161 if ($onfly_compression) { 00162 $memory_limit = trim(@ini_get('memory_limit')); 00163 // 2 MB as default 00164 if (empty($memory_limit)) $memory_limit = 2 * 1024 * 1024; 00165 00166 if (strtolower(substr($memory_limit, -1)) == 'm') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024; 00167 elseif (strtolower(substr($memory_limit, -1)) == 'k') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024; 00168 elseif (strtolower(substr($memory_limit, -1)) == 'g') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024; 00169 else $memory_limit = (int)$memory_limit; 00170 00171 // Some of memory is needed for other thins and as treshold. 00172 // Nijel: During export I had allocated (see memory_get_usage function) 00173 // approx 1.2MB so this comes from that. 00174 if ($memory_limit > 1500000) $memory_limit -= 1500000; 00175 00176 // Some memory is needed for compression, assume 1/3 00177 $memory_limit *= 2/3; 00178 } 00179 00180 // Generate filename and mime type if needed 00181 if ($asfile) { 00182 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']); 00183 if ($export_type == 'server') { 00184 if (isset($remember_template)) { 00185 setcookie('pma_server_filename_template', $filename_template , 0, 00186 substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')), 00187 '', ($pma_uri_parts['scheme'] == 'https')); 00188 } 00189 $filename = str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)); 00190 } elseif ($export_type == 'database') { 00191 if (isset($remember_template)) { 00192 setcookie('pma_db_filename_template', $filename_template , 0, 00193 substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')), 00194 '', ($pma_uri_parts['scheme'] == 'https')); 00195 } 00196 $filename = str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template))); 00197 } else { 00198 if (isset($remember_template)) { 00199 setcookie('pma_table_filename_template', $filename_template , 0, 00200 substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')), 00201 '', ($pma_uri_parts['scheme'] == 'https')); 00202 } 00203 $filename = str_replace('__TABLE__', $table, str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)))); 00204 } 00205 00206 // convert filename to iso-8859-1, it is safer 00207 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) { 00208 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename); 00209 } else { 00210 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename); 00211 } 00212 00213 // Generate basic dump extension 00214 if ($type == 'csv') { 00215 $filename .= '.csv'; 00216 $mime_type = 'text/x-csv'; 00217 } else if ($type == 'xml') { 00218 $filename .= '.xml'; 00219 $mime_type = 'text/xml'; 00220 } else if ($type == 'latex') { 00221 $filename .= '.tex'; 00222 $mime_type = 'application/x-tex'; 00223 } else { 00224 $filename .= '.sql'; 00225 // loic1: 'application/octet-stream' is the registered IANA type but 00226 // MSIE and Opera seems to prefer 'application/octetstream' 00227 $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA') 00228 ? 'application/octetstream' 00229 : 'application/octet-stream'; 00230 } 00231 00232 // If dump is going to be compressed, set correct mime_type and add 00233 // compression to extension 00234 if (isset($compression) && $compression == 'bzip') { 00235 $filename .= '.bz2'; 00236 $mime_type = 'application/x-bzip'; 00237 } else if (isset($compression) && $compression == 'gzip') { 00238 $filename .= '.gz'; 00239 $mime_type = 'application/x-gzip'; 00240 } else if (isset($compression) && $compression == 'zip') { 00241 $filename .= '.zip'; 00242 $mime_type = 'application/x-zip'; 00243 } 00244 } 00245 00246 // Open file on server if needed 00247 if ($save_on_server) { 00248 if (substr($cfg['SaveDir'], -1) != '/') { 00249 $cfg['SaveDir'] .= '/'; 00250 } 00251 $save_filename = $cfg['SaveDir'] . preg_replace('@[/\\\\]@','_',$filename); 00252 unset($message); 00253 if (file_exists($save_filename) && empty($onserverover)) { 00254 $message = sprintf($strFileAlreadyExists, htmlspecialchars($save_filename)); 00255 } else { 00256 if (is_file($save_filename) && !is_writable($save_filename)) { 00257 $message = sprintf($strNoPermission, htmlspecialchars($save_filename)); 00258 } else { 00259 if (!$file_handle = @fopen($save_filename, 'w')) { 00260 $message = sprintf($strNoPermission, htmlspecialchars($save_filename)); 00261 } 00262 } 00263 } 00264 if (isset($message)) { 00265 $js_to_run = 'functions.js'; 00266 require_once('./header.inc.php'); 00267 if ($export_type == 'server') { 00268 $active_page = 'server_export.php'; 00269 require('./server_export.php'); 00270 } elseif ($export_type == 'database') { 00271 $active_page = 'db_details_export.php'; 00272 require('./db_details_export.php'); 00273 } else { 00274 $active_page = 'tbl_properties_export.php'; 00275 require('./tbl_properties_export.php'); 00276 } 00277 exit(); 00278 } 00279 } 00280 00285 if (!$save_on_server) { 00286 if ($asfile ) { 00287 // Download 00288 header('Content-Type: ' . $mime_type); 00289 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 00290 // lem9 & loic1: IE need specific headers 00291 if (PMA_USR_BROWSER_AGENT == 'IE') { 00292 header('Content-Disposition: inline; filename="' . $filename . '"'); 00293 header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 00294 header('Pragma: public'); 00295 } else { 00296 header('Content-Disposition: attachment; filename="' . $filename . '"'); 00297 header('Pragma: no-cache'); 00298 } 00299 } else { 00300 // HTML 00301 $backup_cfgServer = $cfg['Server']; 00302 require_once('./header.inc.php'); 00303 $cfg['Server'] = $backup_cfgServer; 00304 unset($backup_cfgServer); 00305 echo '<div align="' . $cell_align_left . '">' . "\n"; 00306 echo ' <pre>' . "\n"; 00307 } // end download 00308 } 00309 00310 // Check if we have something to export 00311 if ($export_type == 'database') { 00312 $tables = PMA_mysql_list_tables($db); 00313 $num_tables = ($tables) ? @mysql_numrows($tables) : 0; 00314 if ($num_tables == 0) { 00315 $message = $strNoTablesFound; 00316 $js_to_run = 'functions.js'; 00317 require_once('./header.inc.php'); 00318 if ($export_type == 'server') { 00319 $active_page = 'server_export.php'; 00320 require('./server_export.php'); 00321 } elseif ($export_type == 'database') { 00322 $active_page = 'db_details_export.php'; 00323 require('./db_details_export.php'); 00324 } else { 00325 $active_page = 'tbl_properties_export.php'; 00326 require('./tbl_properties_export.php'); 00327 } 00328 exit(); 00329 } 00330 } 00331 00332 // Add possibly some comments to export 00333 PMA_exportHeader(); 00334 00335 // Will we need relation & co. setup? 00336 $do_relation = isset($GLOBALS[$what . '_relation']); 00337 $do_comments = isset($GLOBALS[$what . '_comments']); 00338 $do_mime = isset($GLOBALS[$what . '_mime']); 00339 if ($do_relation || $do_comments || $do_mime) { 00340 require_once('./libraries/relation.lib.php'); 00341 $cfgRelation = PMA_getRelationsParam(); 00342 } 00343 if ($do_mime) { 00344 require_once('./libraries/transformations.lib.php'); 00345 } 00346 00347 // Include dates in export? 00348 $do_dates = isset($GLOBALS[$what . '_dates']); 00349 00353 // Gets the number of tables if a dump of a database has been required 00354 if ($export_type == 'server') { 00358 if ($server > 0 && empty($dblist)) { 00359 PMA_availableDatabases(); 00360 } 00361 00362 if (isset($db_select)) { 00363 $tmp_select = implode($db_select, '|'); 00364 $tmp_select = '|' . $tmp_select . '|'; 00365 } 00366 // Walk over databases 00367 foreach($dblist AS $current_db) { 00368 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|')) 00369 || !isset($tmp_select)) { 00370 PMA_exportDBHeader($current_db); 00371 PMA_exportDBCreate($current_db); 00372 $tables = PMA_mysql_list_tables($current_db); 00373 $num_tables = ($tables) ? @mysql_numrows($tables) : 0; 00374 $i = 0; 00375 while ($i < $num_tables) { 00376 $table = PMA_mysql_tablename($tables, $i); 00377 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table); 00378 if (isset($GLOBALS[$what . '_structure'])) PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates); 00379 if (isset($GLOBALS[$what . '_data'])) PMA_exportData($current_db, $table, $crlf, $err_url, $local_query); 00380 $i++; 00381 } 00382 PMA_exportDBFooter($current_db); 00383 } 00384 } 00385 } elseif ($export_type == 'database') { 00386 PMA_exportDBHeader($db); 00387 if (isset($table_select)) { 00388 $tmp_select = implode($table_select, '|'); 00389 $tmp_select = '|' . $tmp_select . '|'; 00390 } 00391 $i = 0; 00392 while ($i < $num_tables) { 00393 $table = PMA_mysql_tablename($tables, $i); 00394 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table); 00395 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|')) 00396 || !isset($tmp_select)) { 00397 00398 if (isset($GLOBALS[$what . '_structure'])) PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates); 00399 if (isset($GLOBALS[$what . '_data'])) PMA_exportData($db, $table, $crlf, $err_url, $local_query); 00400 } 00401 $i++; 00402 } 00403 PMA_exportDBFooter($db); 00404 } else { 00405 PMA_exportDBHeader($db); 00406 // We export just one table 00407 00408 if ($limit_to > 0 && $limit_from >= 0) { 00409 $add_query = ' LIMIT ' 00410 . (($limit_from > 0) ? $limit_from . ', ' : '') 00411 . $limit_to; 00412 } else { 00413 $add_query = ''; 00414 } 00415 00416 if (!empty($sql_query)) { 00417 $local_query = $sql_query . $add_query; 00418 PMA_mysql_select_db($db); 00419 } else { 00420 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query; 00421 } 00422 00423 if (isset($GLOBALS[$what . '_structure'])) PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates); 00424 if (isset($GLOBALS[$what . '_data'])) PMA_exportData($db, $table, $crlf, $err_url, $local_query); 00425 PMA_exportDBFooter($db); 00426 } 00427 00431 if (!empty($asfile)) { 00432 // Convert the charset if required. 00433 if ($output_charset_conversion) { 00434 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer); 00435 } 00436 00437 // Do the compression 00438 // 1. as a gzipped file 00439 if (isset($compression) && $compression == 'zip') { 00440 if (@function_exists('gzcompress')) { 00441 if ($type == 'csv' ) { 00442 $extbis = '.csv'; 00443 } else if ($type == 'xml') { 00444 $extbis = '.xml'; 00445 } else { 00446 $extbis = '.sql'; 00447 } 00448 $zipfile = new zipfile(); 00449 $zipfile -> addFile($dump_buffer, $filename . $extbis); 00450 $dump_buffer = $zipfile -> file(); 00451 } 00452 } 00453 // 2. as a bzipped file 00454 else if (isset($compression) && $compression == 'bzip') { 00455 if (@function_exists('bzcompress')) { 00456 $dump_buffer = bzcompress($dump_buffer); 00457 if ($dump_buffer === -8) { 00458 require_once('./header.inc.php'); 00459 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>'); 00460 require_once('./footer.inc.php'); 00461 } 00462 } 00463 } 00464 // 3. as a gzipped file 00465 else if (isset($compression) && $compression == 'gzip') { 00466 if (@function_exists('gzencode')) { 00467 // without the optional parameter level because it bug 00468 $dump_buffer = gzencode($dump_buffer); 00469 } 00470 } 00471 00472 /* If ve saved on server, we have to close file now */ 00473 if ($save_on_server) { 00474 $write_result = @fwrite($file_handle, $dump_buffer); 00475 fclose($file_handle); 00476 if (strlen($dump_buffer) !=0 && (!$write_result || ($write_result != strlen($dump_buffer)))) { 00477 $message = sprintf($strNoSpace, htmlspecialchars($save_filename)); 00478 } else { 00479 $message = sprintf($strDumpSaved, htmlspecialchars($save_filename)); 00480 } 00481 00482 $js_to_run = 'functions.js'; 00483 require_once('./header.inc.php'); 00484 if ($export_type == 'server') { 00485 $active_page = 'server_export.php'; 00486 require_once('./server_export.php'); 00487 } elseif ($export_type == 'database') { 00488 $active_page = 'db_details_export.php'; 00489 require_once('./db_details_export.php'); 00490 } else { 00491 $active_page = 'tbl_properties_export.php'; 00492 require_once('./tbl_properties_export.php'); 00493 } 00494 exit(); 00495 } else { 00496 echo $dump_buffer; 00497 } 00498 } 00502 else { 00506 echo ' </pre>' . "\n"; 00507 echo '</div>' . "\n"; 00508 echo "\n"; 00509 require_once('./footer.inc.php'); 00510 } // end if 00511 ?>