Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: read_dump.php,v 2.35 2005/08/09 12:09:46 lem9 Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00008 require_once('./libraries/read_dump.lib.php'); 00009 require_once('./libraries/grab_globals.lib.php'); 00010 require_once('./libraries/common.lib.php'); 00011 00012 if (!isset($db)) { 00013 $db = ''; 00014 } 00015 00019 @set_time_limit($cfg['ExecTimeLimit']); 00020 00021 00025 if (!isset($goto) || !preg_match('@^(db_details|tbl_properties)(_[a-z]*)?\.php$@i', $goto)) { 00026 $goto = 'db_details.php'; 00027 } 00028 $err_url = $goto 00029 . '?' . PMA_generate_common_url($db) 00030 . (preg_match('@^tbl_properties(_[a-z]*)?\.php$@', $goto) ? '&table=' . urlencode($table) : ''); 00031 00032 00036 $view_bookmark = 0; 00037 $sql_bookmark = isset($sql_bookmark) ? $sql_bookmark : ''; 00038 $sql_query = isset($sql_query) ? $sql_query : ''; 00039 00040 if (!empty($sql_localfile) && !empty($cfg['UploadDir'])) { 00041 00042 // sanitize $sql_localfile as it comes from a POST 00043 $sql_localfile = PMA_securePath($sql_localfile); 00044 00045 if (substr($cfg['UploadDir'], -1) != '/') { 00046 $cfg['UploadDir'] .= '/'; 00047 } 00048 $sql_file = $cfg['UploadDir'] . $sql_localfile; 00049 } else if (empty($sql_file)) { 00050 $sql_file = 'none'; 00051 } 00052 00056 if (!empty($id_bookmark)) { 00057 require_once('./libraries/bookmark.lib.php'); 00058 switch ($action_bookmark) { 00059 case 0: // bookmarked query that have to be run 00060 $sql_query = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark,'id', (isset($action_bookmark_all) ? TRUE : FALSE)); 00061 if (isset($bookmark_variable) && !empty($bookmark_variable)) { 00062 $sql_query = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $sql_query); 00063 } 00064 break; 00065 case 1: // bookmarked query that have to be displayed 00066 $sql_query = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark); 00067 $view_bookmark = 1; 00068 break; 00069 case 2: // bookmarked query that have to be deleted 00070 $sql_query = PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark); 00071 break; 00072 } 00073 } // end if 00074 00075 00079 // Gets the query from a file if required 00080 if ($sql_file != 'none') { 00081 // file_exists() returns false if open_basedir is set 00082 00083 if ((is_uploaded_file($sql_file)) 00084 ||(isset($sql_localfile) && $sql_file == $cfg['UploadDir'] . $sql_localfile) && file_exists($sql_file)) { 00085 00086 $open_basedir = @ini_get('open_basedir'); 00087 00088 if (!isset($sql_file_compression)) $sql_file_compression = ''; 00089 00090 // If we are on a server with open_basedir, we must move the file 00091 // before opening it. The doc explains how to create the "./tmp" 00092 // directory 00093 00094 if (!empty($open_basedir)) { 00095 00096 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/'); 00097 00098 // function is_writeable() is valid on PHP3 and 4 00099 if (!is_writeable($tmp_subdir)) { 00100 $sql_query = PMA_readFile($sql_file, $sql_file_compression); 00101 if ($sql_query == FALSE) { 00102 $message = $strFileCouldNotBeRead . ' (1)'; 00103 } 00104 } else { 00105 $sql_file_new = $tmp_subdir . basename($sql_file); 00106 if (move_uploaded_file($sql_file, $sql_file_new)) { 00107 $sql_query = PMA_readFile($sql_file_new, $sql_file_compression); 00108 if ($sql_query == FALSE) { 00109 $message = $strFileCouldNotBeRead . ' (2)'; 00110 } 00111 unlink($sql_file_new); 00112 } else { 00113 // Moving uploaded file failed. Falling back to try reading it immediately. 00114 $sql_query = PMA_readFile($sql_file, $sql_file_compression); 00115 if ($sql_query == FALSE) { 00116 $message = $strFileCouldNotBeRead . ' (3)'; 00117 } 00118 } 00119 } 00120 } else { 00121 // read from the normal upload dir 00122 $sql_query = PMA_readFile($sql_file, $sql_file_compression); 00123 if ($sql_query == FALSE) { 00124 $message = $strFileCouldNotBeRead . ' (4)'; 00125 } 00126 } 00127 00128 // Convert the file's charset if necessary 00129 if (PMA_MYSQL_INT_VERSION < 40100 00130 && $cfg['AllowAnywhereRecoding'] && $allow_recoding 00131 && isset($charset_of_file) && $charset_of_file != $charset) { 00132 $sql_query = PMA_convert_string($charset_of_file, $charset, $sql_query); 00133 } else if (PMA_MYSQL_INT_VERSION >= 40100 00134 && isset($charset_of_file) && $charset_of_file != 'utf8') { 00135 $sql_query = 'SET NAMES \'' . $charset_of_file . "';\n" 00136 . $sql_query . "\n" 00137 . "SET CHARACTER SET utf8;\n" 00138 . "SET SESSION collation_connection ='" . $collation_connection . "';"; 00139 } 00140 } // end uploaded file stuff 00141 } 00142 00143 // Kanji convert SQL textfile 2002/1/4 by Y.Kawada 00144 if (@function_exists('PMA_kanji_str_conv')) { 00145 // do not trim here: see bug #1030644 00146 //$sql_tmp = trim($sql_query); 00147 $sql_tmp = $sql_query; 00148 PMA_change_enc_order(); 00149 $sql_query = PMA_kanji_str_conv($sql_tmp, $knjenc, isset($xkana) ? $xkana : ''); 00150 PMA_change_enc_order(); 00151 } //else { 00152 // do not trim here: see bug #1030644 00153 //$sql_query = trim($sql_query); 00154 //} 00155 00156 // $sql_query come from the query textarea, if it's a reposted query gets its 00157 // 'true' value 00158 if (!empty($prev_sql_query)) { 00159 $prev_sql_query = urldecode($prev_sql_query); 00160 if ($sql_query == trim(htmlspecialchars($prev_sql_query))) { 00161 $sql_query = $prev_sql_query; 00162 } 00163 } 00164 00165 // Drop database is not allowed -> ensure the query can be run 00166 if (!$cfg['AllowUserDropDatabase'] 00167 && preg_match('@DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $sql_query)) { 00168 // Checks if the user is a Superuser 00169 // TODO: set a global variable with this information 00170 // loic1: optimized query 00171 if (!($result = PMA_DBI_select_db('mysql'))) { 00172 require_once('./header.inc.php'); 00173 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url); 00174 } 00175 } 00176 define('PMA_CHK_DROP', 1); 00177 00181 if (isset($SQLbookmark) && $sql_query != '') { 00182 require_once('./libraries/bookmark.lib.php'); 00183 $bfields = array( 00184 'dbase' => $db, 00185 'user' => $cfg['Bookmark']['user'], 00186 'query' => urlencode($sql_query), 00187 'label' => $bkm_label 00188 ); 00189 00190 PMA_addBookmarks($bfields, $cfg['Bookmark'], (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false)); 00191 } 00192 00196 if ($sql_query != '') { 00197 $pieces = array(); 00198 PMA_splitSqlFile($pieces, $sql_query, PMA_MYSQL_INT_VERSION); 00199 $pieces_count = count($pieces); 00200 00201 // Copy of the cleaned sql statement for display purpose only (see near the 00202 // beginning of "db_details.php" & "tbl_properties.php") 00203 00204 // You can either 00205 // * specify the amount of maximum pieces per query (having max_*_length set to 0!) or 00206 // * specify the amount of maximum chars per query (having max_*_pieces set to 0!) 00207 // - max_nofile_* is used for any queries submitted via copy&paste in the textarea 00208 // - max_file_* is used for any file-submitted query 00209 if (!$cfg['VerboseMultiSubmit']) { 00210 // Here be the values if the Verbose-Mode (see config.inc.php) is NOT activated 00211 $max_nofile_length = 500; 00212 $max_nofile_pieces = 0; 00213 // Nijel: Here must be some limit, as extended inserts can be really 00214 // huge and parsing them eats megabytes of memory 00215 $max_file_length = 10000; 00216 $max_file_pieces = 10; 00217 } else { 00218 // Values for verbose-mode 00219 $max_nofile_length = 0; 00220 $max_nofile_pieces = 50; 00221 // Nijel: Here must be some limit, as extended inserts can be really 00222 // huge and parsing them eats megabytes of memory 00223 $max_file_length = 50000; 00224 $max_file_pieces = 50; 00225 } 00226 00227 if ($sql_file != 'none' && 00228 (($max_file_pieces != 0 && ($pieces_count > $max_file_pieces)) 00229 || 00230 ($max_file_length != 0 && (strlen($sql_query) > $max_file_length)))) { 00231 // Be nice with bandwidth... 00232 $sql_query_cpy = $sql_query = ''; 00233 $save_bandwidth = TRUE; 00234 $save_bandwidth_length = $max_file_length; 00235 $save_bandwidth_pieces = $max_file_pieces; 00236 } else { 00237 00238 $sql_query_cpy = $sql_query; 00239 // Be nice with bandwidth... for now, an arbitrary limit of 500, 00240 // could be made configurable but probably not necessary 00241 if (($max_nofile_length != 0 && (strlen($sql_query_cpy) > $max_nofile_length)) 00242 || ($max_nofile_pieces != 0 && $pieces_count > $max_nofile_pieces)) { 00243 $sql_query_cpy = $sql_query = ''; 00244 $save_bandwidth = TRUE; 00245 $save_bandwidth_length = $max_nofile_length; 00246 $save_bandwidth_pieces = $max_nofile_pieces; 00247 } 00248 } 00249 // really run the query? 00250 if ($view_bookmark == 0) { 00251 // Only one query to run 00252 if ($pieces_count == 1 && !empty($pieces[0]['query'])) { 00253 $sql_query = $pieces[0]['query']; 00254 // .*? below is non greedy expansion, just in case somebody wants to understand it... 00255 if (preg_match('@^((-- |#)[^\n]*\n|/\*.*?\*/)*(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $sql_query)) { 00256 $reload = 1; 00257 } 00258 require('./sql.php'); 00259 } 00260 00261 // Runs multiple queries 00262 // (Possibly to create a db, so no db was selected in the 00263 // left frame and $db is empty) 00264 else if (empty($db) || PMA_DBI_select_db($db)) { 00265 $mult = TRUE; 00266 $info_msg = ''; 00267 $info_count = 0; 00268 00269 // just skip last empty query (can contain just comments at the end) 00270 $count = $pieces_count; 00271 if ($pieces[$count - 1]['empty']) $count--; 00272 00273 for ($i = 0; $i < $count; $i++) { 00274 $a_sql_query = $pieces[$i]['query']; 00275 00276 // .*? below is non greedy expansion, just in case somebody wants to understand it... 00277 // looks ok here without using PCRE_MULTILINE 00278 if ($i == $count - 1 && preg_match('@^((-- |#)[^\n]*\n|[\s]*\n|/\*.*?\*/)*(SELECT|SHOW)@i', $a_sql_query)) { 00279 $complete_query = $sql_query; 00280 $display_query = $sql_query; 00281 $sql_query = $a_sql_query; 00282 require('./sql.php'); 00283 } 00284 $result = PMA_DBI_try_query($a_sql_query); 00285 if ($result == FALSE) { // readdump failed 00286 if (isset($my_die) && $cfg['IgnoreMultiSubmitErrors']) { 00287 $my_die[] = "\n\n" . $a_sql_query; 00288 } elseif ($cfg['IgnoreMultiSubmitErrors']) { 00289 $my_die = array(); 00290 $my_die[] = $a_sql_query; 00291 } else { 00292 $my_die = $a_sql_query; 00293 } 00294 00295 if ($cfg['VerboseMultiSubmit']) { 00296 $info_msg .= $a_sql_query . '; # ' . $strError . "\n"; 00297 $info_count++; 00298 } 00299 00300 if (!$cfg['IgnoreMultiSubmitErrors']) { 00301 break; 00302 } 00303 } else if ($cfg['VerboseMultiSubmit']) { 00304 $a_num_rows = (int)@PMA_DBI_num_rows($result); 00305 $a_aff_rows = (int)@PMA_DBI_affected_rows(); 00306 if ($a_num_rows > 0) { 00307 $a_rows = $a_num_rows; 00308 $a_switch = $strRows . ': '; 00309 } elseif ($a_aff_rows > 0) { 00310 $a_rows = $a_aff_rows; 00311 $a_switch = $strAffectedRows;; 00312 } else { 00313 $a_rows = ''; 00314 $a_switch = $strEmptyResultSet; 00315 } 00316 00317 $info_msg .= $a_sql_query . "; # " . $a_switch . $a_rows . "\n"; 00318 $info_count++; 00319 } 00320 00321 // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one 00322 // .*? below is non greedy expansion, just in case somebody wants to understand it... 00323 if ($result != FALSE && preg_match('@^((-- |#)^[\n]*|/\*.*?\*/)*USE[[:space:]]*([\S]+)@i', $a_sql_query, $match)) { 00324 $db = trim($match[3]); 00325 $reload = 1; 00326 } 00327 00328 // .*? below is non greedy expansion, just in case somebody wants to understand it... 00329 // must check $a_sql_query and use PCRE_MULTILINE 00330 00331 if (!isset($reload) && preg_match('@^((-- |#)[^\n]*\n|/\*.*?\*/)*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $a_sql_query)) { 00332 $reload = 1; 00333 } 00334 } // end for 00335 00336 if ($cfg['VerboseMultiSubmit'] && strlen($info_msg) > 0 && 00337 ((!isset($save_bandwidth) || $save_bandwidth == FALSE) || 00338 ($save_bandwidth_pieces == 0 && strlen($info_msg) < $save_bandwidth_length) || 00339 ($save_bandwidth_length == 0 && $info_count < $save_bandwidth_pieces))) { 00340 $sql_query = $info_msg; 00341 } 00342 00343 } // end else if 00344 } // end if (really run the query) 00345 unset($pieces); 00346 } // end if 00347 00348 00349 00353 if (isset($my_die)) { 00354 $js_to_run = 'functions.js'; 00355 require_once('./header.inc.php'); 00356 if (is_array($my_die)) { 00357 foreach ($my_die AS $key => $die_string) { 00358 PMA_mysqlDie('', $die_string, '', $err_url, FALSE); 00359 echo '<hr />'; 00360 } 00361 } else { 00362 PMA_mysqlDie('', $my_die, '', $err_url, TRUE); 00363 } 00364 } 00365 00366 00370 // Checks for a valid target script 00371 if (isset($table) && $table == '') { 00372 unset($table); 00373 } 00374 if (isset($db) && $db == '') { 00375 unset($db); 00376 } 00377 00378 $is_db = $is_table = FALSE; 00379 if ($goto == 'tbl_properties.php') { 00380 if (!isset($table)) { 00381 $goto = 'db_details.php'; 00382 } else { 00383 PMA_DBI_select_db($db); 00384 $is_table = PMA_DBI_try_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'', NULL, PMA_DBI_QUERY_STORE); 00385 if (!($is_table && @PMA_DBI_num_rows($is_table))) { 00386 $goto = 'db_details.php'; 00387 unset($table); 00388 } 00389 } // end if... else... 00390 } 00391 if ($goto == 'db_details.php') { 00392 if (isset($table)) { 00393 unset($table); 00394 } 00395 if (!isset($db)) { 00396 $goto = 'main.php'; 00397 } else { 00398 $is_db = @PMA_DBI_select_db($db); 00399 if (!$is_db) { 00400 $goto = 'main.php'; 00401 unset($db); 00402 } 00403 } // end if... else... 00404 } 00405 // Defines the message to be displayed 00406 if (!empty($id_bookmark) && $action_bookmark == 2) { 00407 $message = $strBookmarkDeleted; 00408 } else if (!isset($sql_query_cpy)) { 00409 if (empty($message)) { 00410 $message = $strNoQuery; 00411 } 00412 } else if ($sql_query_cpy == '') { 00413 $message = "$strSuccess:[br]$strTheContent (" 00414 . (isset($sql_file_name) ? $sql_file_name . ': ' : '') 00415 . "$pieces_count $strInstructions) "; 00416 } else { 00417 $message = $strSuccess; 00418 } 00419 // Loads to target script 00420 if ($goto == 'db_details.php' || $goto == 'tbl_properties.php' || $goto == 'tbl_properties_structure.php') { 00421 00422 // maybe we should do this instead: 00423 //if (strpos(' ' . $goto, 'db_details') == 1 || strpos(' ' . $goto, 'tbl_properties') == 1) { 00424 // but I'm not sure 00425 00426 $js_to_run = 'functions.js'; 00427 } 00428 if ($goto != 'main.php') { 00429 require_once('./header.inc.php'); 00430 } 00431 $active_page = $goto; 00432 require('./' . $goto); 00433 ?>