$val) { $val = preg_replace("/[^_A-Za-z0-9-\.&= ;\$]/i",'', $val); $_REQUEST[$key] = $val; } // Plugin handling is still EXPERIMENTAL and DISABLED // Register plugins by including plugin_name.php from each ./plugins/plugin_name /* if (is_dir(PLUGIN_DIR)) { if ($dh = opendir(PLUGIN_DIR)) { while (($file = readdir($dh)) !== false) { if (is_dir(PLUGIN_DIR.$file) && $file!='.' && $file!='..' && file_exists(PLUGIN_DIR.$file.'/'.$file.'.php')) include (PLUGIN_DIR.$file.'/'.$file.'.php'); } closedir($dh); } } */ do_action('header'); ?> BigDump ver. <?php echo (VERSION); ?>
'); } function skin_close() { echo (''); } skin_open(); echo ('

BigDump: Staggered MySQL Dump Importer v'.VERSION.'

'); skin_close(); do_action('after_headline'); $error = false; $file = false; // Check PHP version if (!$error && !function_exists('version_compare')) { echo ("

PHP version 4.1.0 is required for BigDump to proceed. You have PHP ".phpversion()." installed. Sorry!

\n"); $error=true; } // Check if mysql extension is available if (!$error && !function_exists('mysqli_connect')) { echo ("

There is no mySQLi extension found in your PHP installation. You can use an older Bigdump version if your PHP supports mySQL extension.

\n"); $error=true; } // Calculate PHP max upload size (handle settings like 10M or 100K) if (!$error) { $upload_max_filesize=ini_get("upload_max_filesize"); if (preg_match("/([0-9]+)K/i",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024; if (preg_match("/([0-9]+)M/i",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024; if (preg_match("/([0-9]+)G/i",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024*1024; } // Get the current directory /* if (isset($_SERVER["CGIA"])) $upload_dir=dirname($_SERVER["CGIA"]); else if (isset($_SERVER["ORIG_PATH_TRANSLATED"])) $upload_dir=dirname($_SERVER["ORIG_PATH_TRANSLATED"]); else if (isset($_SERVER["ORIG_SCRIPT_FILENAME"])) $upload_dir=dirname($_SERVER["ORIG_SCRIPT_FILENAME"]); else if (isset($_SERVER["PATH_TRANSLATED"])) $upload_dir=dirname($_SERVER["PATH_TRANSLATED"]); else $upload_dir=dirname($_SERVER["SCRIPT_FILENAME"]); */ do_action ('script_runs'); // Handle file upload if (!$error && isset($_REQUEST["uploadbutton"])) { if (is_uploaded_file($_FILES["dumpfile"]["tmp_name"]) && ($_FILES["dumpfile"]["error"])==0) { $uploaded_filename=str_replace(" ","_",$_FILES["dumpfile"]["name"]); $uploaded_filename=preg_replace("/[^_A-Za-z0-9-\.]/i",'',$uploaded_filename); $uploaded_filepath=str_replace("\\","/",$upload_dir."/".$uploaded_filename); do_action('file_uploaded'); if (file_exists($uploaded_filename)) { echo ("

File $uploaded_filename already exist! Delete and upload again!

\n"); } else if (!preg_match("/(\.(sql|gz|csv))$/i",$uploaded_filename)) { echo ("

You may only upload .sql .gz or .csv files.

\n"); } else if (!@move_uploaded_file($_FILES["dumpfile"]["tmp_name"],$uploaded_filepath)) { echo ("

Error moving uploaded file ".$_FILES["dumpfile"]["tmp_name"]." to the $uploaded_filepath

\n"); echo ("

Check the directory permissions for $upload_dir (must be 777)!

\n"); } else { echo ("

Uploaded file saved as $uploaded_filename

\n"); } } else { echo ("

Error uploading file ".$_FILES["dumpfile"]["name"]."

\n"); } } // Handle file deletion (delete only in the current directory for security reasons) if (!$error && isset($_REQUEST["delete"]) && $_REQUEST["delete"]!=basename($_SERVER["SCRIPT_FILENAME"])) { if (preg_match("/(\.(sql|gz|csv))$/i",$_REQUEST["delete"]) && @unlink($upload_dir.'/'.$_REQUEST["delete"])) echo ("

".$_REQUEST["delete"]." was removed successfully

\n"); else echo ("

Can't remove ".$_REQUEST["delete"]."

\n"); } // Connect to the database, set charset and execute pre-queries if (!$error && !TESTMODE) { $mysqli = new mysqli($db_server, $db_username, $db_password, $db_name); if (mysqli_connect_error()) { echo ("

Database connection failed due to ".mysqli_connect_error()."

\n"); echo ("

Edit the database settings in BigDump configuration, or contact your database provider.

\n"); $error=true; } if (!$error && $db_connection_charset!=='') $mysqli->query("SET NAMES $db_connection_charset"); if (!$error && isset ($pre_query) && sizeof ($pre_query)>0) { reset($pre_query); foreach ($pre_query as $pre_query_value) { if (!$mysqli->query($pre_query_value)) { echo ("

Error with pre-query.

\n"); echo ("

Query: ".trim(nl2br(htmlentities($pre_query_value)))."

\n"); echo ("

MySQL: ".$mysqli->error."

\n"); $error=true; break; } } } } else { $dbconnection = false; } do_action('database_connected'); // DIAGNOSTIC // echo("

Checkpoint!

"); // List uploaded files in multifile mode if (!$error && !isset($_REQUEST["fn"]) && $filename=="") { if ($dirhandle = opendir($upload_dir)) { $files=array(); while (false !== ($files[] = readdir($dirhandle))); closedir($dirhandle); $dirhead=false; if (sizeof($files)>0) { sort($files); foreach ($files as $dirfile) { if ($dirfile != "." && $dirfile != ".." && $dirfile!=basename($_SERVER["SCRIPT_FILENAME"]) && preg_match("/\.(sql|gz|csv)$/i",$dirfile)) { if (!$dirhead) { echo ("\n"); echo ("\n"); $dirhead=true; } echo (""); if (preg_match("/\.sql$/i",$dirfile)) echo (""); elseif (preg_match("/\.gz$/i",$dirfile)) echo (""); elseif (preg_match("/\.csv$/i",$dirfile)) echo (""); else echo (""); if ((preg_match("/\.gz$/i",$dirfile) && function_exists("gzopen")) || preg_match("/\.sql$/i",$dirfile) || preg_match("/\.csv$/i",$dirfile)) echo ("\n \n"); // TODO: echo ("\n \n"); else echo ("\n \n"); } } } if ($dirhead) echo ("
FilenameSizeDate&TimeType  
$dirfile".filesize($upload_dir.'/'.$dirfile)."".date ("Y-m-d H:i:s", filemtime($upload_dir.'/'.$dirfile))."SQLGZipCSVMiscStart Import into $db_name at $db_serverDelete file
Start ImportDelete file
  
\n"); else echo ("

No uploaded SQL, GZ or CSV files found in the working directory

\n"); } else { echo ("

Error listing directory $upload_dir

\n"); $error=true; } } // Single file mode if (!$error && !isset ($_REQUEST["fn"]) && $filename!="") { echo ("

Start Import from $filename into $db_name at $db_server

\n"); } // File Upload Form if (!$error && !isset($_REQUEST["fn"]) && $filename=="") { // Test permissions on working directory do { $tempfilename=$upload_dir.'/'.time().".tmp"; } while (file_exists($tempfilename)); if (!($tempfile=@fopen($tempfilename,"w"))) { echo ("

Upload form disabled. Permissions for the working directory $upload_dir must be set writable for the webserver in order "); echo ("to upload files here. Alternatively you can upload your dump files via FTP.

\n"); } else { fclose($tempfile); unlink ($tempfilename); echo ("

You can now upload your dump file up to $upload_max_filesize bytes (".round ($upload_max_filesize/1024/1024)." Mbytes) "); echo ("directly from your browser to the server. Alternatively you can upload your dump files of any size via FTP.

\n"); ?>
" enctype="multipart/form-data">

Dump file:

query("SHOW VARIABLES LIKE 'character_set_connection';"); if ($result) { $row = $result->fetch_assoc(); if ($row) { $charset = $row['Value']; echo ("

Note: The current mySQL connection charset is $charset. Your dump file must be encoded in $charset in order to avoid problems with non-latin characters. You can change the connection charset using the \$db_connection_charset variable in bigdump.php

\n"); } $result->free(); } } // Open the file if (!$error && isset($_REQUEST["start"])) { // Set current filename ($filename overrides $_REQUEST["fn"] if set) if ($filename!="") $curfilename=$filename; else if (isset($_REQUEST["fn"])) $curfilename=urldecode($_REQUEST["fn"]); else $curfilename=""; // Recognize GZip filename if (preg_match("/\.gz$/i",$curfilename)) $gzipmode=true; else $gzipmode=false; if ((!$gzipmode && !$file=@fopen($upload_dir.'/'.$curfilename,"r")) || ($gzipmode && !$file=@gzopen($upload_dir.'/'.$curfilename,"r"))) { echo ("

Can't open ".$curfilename." for import

\n"); echo ("

Please, check that your dump file name contains only alphanumerical characters, and rename it accordingly, for example: $curfilename.". "
Or, specify \$filename in bigdump.php with the full filename. ". "
Or, you have to upload the $curfilename to the server first.

\n"); $error=true; } // Get the file size (can't do it fast on gzipped files, no idea how) else if ((!$gzipmode && @fseek($file, 0, SEEK_END)==0) || ($gzipmode && @gzseek($file, 0)==0)) { if (!$gzipmode) $filesize = ftell($file); else $filesize = gztell($file); // Always zero, ignore } else { echo ("

I can't seek into $curfilename

\n"); $error=true; } // Stop if csv file is used, but $csv_insert_table is not set if (!$error && ($csv_insert_table == "") && (preg_match("/(\.csv)$/i",$curfilename))) { echo ("

You have to specify \$csv_insert_table when using a CSV file.

\n"); $error=true; } } // ******************************************************************************************* // START IMPORT SESSION HERE // ******************************************************************************************* if (!$error && isset($_REQUEST["start"]) && isset($_REQUEST["foffset"]) && preg_match("/(\.(sql|gz|csv))$/i",$curfilename)) { do_action('session_start'); // Check start and foffset are numeric values if (!is_numeric($_REQUEST["start"]) || !is_numeric($_REQUEST["foffset"])) { echo ("

UNEXPECTED: Non-numeric values for start and foffset

\n"); $error=true; } else { $_REQUEST["start"] = floor($_REQUEST["start"]); $_REQUEST["foffset"] = floor($_REQUEST["foffset"]); } // Set the current delimiter if defined if (isset($_REQUEST["delimiter"])) $delimiter = $_REQUEST["delimiter"]; // Empty CSV table if requested if (!$error && $_REQUEST["start"]==1 && $csv_insert_table != "" && $csv_preempty_table) { $query = "DELETE FROM `$csv_insert_table`"; if (!TESTMODE && !$mysqli->query(trim($query))) { echo ("

Error when deleting entries from $csv_insert_table.

\n"); echo ("

Query: ".trim(nl2br(htmlentities($query)))."

\n"); echo ("

MySQL: ".$mysqli->error."

\n"); $error=true; } } // Print start message if (!$error) { skin_open(); if (TESTMODE) echo ("

TEST MODE ENABLED

\n"); echo ("

Processing file: ".$curfilename."

\n"); echo ("

Starting from line: ".$_REQUEST["start"]."

\n"); skin_close(); } // Check $_REQUEST["foffset"] upon $filesize (can't do it on gzipped files) if (!$error && !$gzipmode && $_REQUEST["foffset"]>$filesize) { echo ("

UNEXPECTED: Can't set file pointer behind the end of file

\n"); $error=true; } // Set file pointer to $_REQUEST["foffset"] if (!$error && ((!$gzipmode && fseek($file, $_REQUEST["foffset"])!=0) || ($gzipmode && gzseek($file, $_REQUEST["foffset"])!=0))) { echo ("

UNEXPECTED: Can't set file pointer to offset: ".$_REQUEST["foffset"]."

\n"); $error=true; } // Start processing queries from $file if (!$error) { $query=""; $queries=0; $totalqueries=$_REQUEST["totalqueries"]; $linenumber=$_REQUEST["start"]; $querylines=0; $inparents=false; // Stay processing as long as the $linespersession is not reached or the query is still incomplete while ($linenumber<$_REQUEST["start"]+$linespersession || $query!="") { // Read the whole next line $dumpline = ""; while (!feof($file) && substr ($dumpline, -1) != "\n" && substr ($dumpline, -1) != "\r") { if (!$gzipmode) $dumpline .= fgets($file, DATA_CHUNK_LENGTH); else $dumpline .= gzgets($file, DATA_CHUNK_LENGTH); } if ($dumpline==="") break; // Remove UTF8 Byte Order Mark at the file beginning if any if ($_REQUEST["foffset"]==0) $dumpline=preg_replace('|^\xEF\xBB\xBF|','',$dumpline); // Create an SQL query from CSV line if (($csv_insert_table != "") && (preg_match("/(\.csv)$/i",$curfilename))) { if ($csv_add_slashes) $dumpline = addslashes($dumpline); $dumpline = explode($csv_delimiter,$dumpline); if ($csv_add_quotes) $dumpline = "'".implode("','",$dumpline)."'"; else $dumpline = implode(",",$dumpline); $dumpline = 'INSERT INTO '.$csv_insert_table.' VALUES ('.$dumpline.');'; } // Handle DOS and Mac encoded linebreaks (I don't know if it really works on Win32 or Mac Servers) $dumpline=str_replace("\r\n", "\n", $dumpline); $dumpline=str_replace("\r", "\n", $dumpline); // DIAGNOSTIC // echo ("

Line $linenumber: $dumpline

\n"); // Recognize delimiter statement if (!$inparents && strpos ($dumpline, "DELIMITER ") === 0) $delimiter = str_replace ("DELIMITER ","",trim($dumpline)); // Skip comments and blank lines only if NOT in parents if (!$inparents) { $skipline=false; reset($comment); foreach ($comment as $comment_value) { // DIAGNOSTIC // echo ($comment_value); if (trim($dumpline)=="" || strpos (trim($dumpline), $comment_value) === 0) { $skipline=true; break; } } if ($skipline) { $linenumber++; // DIAGNOSTIC // echo ("

Comment line skipped

\n"); continue; } } // Remove double back-slashes from the dumpline prior to count the quotes ('\\' can only be within strings) $dumpline_deslashed = str_replace ("\\\\","",$dumpline); // Count ' and \' (or " and \") in the dumpline to avoid query break within a text field ending by $delimiter $parents=substr_count ($dumpline_deslashed, $string_quotes)-substr_count ($dumpline_deslashed, "\\$string_quotes"); if ($parents % 2 != 0) $inparents=!$inparents; // Add the line to query $query .= $dumpline; // Don't count the line if in parents (text fields may include unlimited linebreaks) if (!$inparents) $querylines++; // Stop if query contains more lines as defined by $max_query_lines if ($querylines>$max_query_lines) { echo ("

Stopped at the line $linenumber.

"); echo ("

At this place the current query includes more than ".$max_query_lines." dump lines. That can happen if your dump file was "); echo ("created by some tool which doesn't place a semicolon followed by a linebreak at the end of each query, or if your dump contains "); echo ("extended inserts or very long procedure definitions. Please read the BigDump usage notes "); echo ("for more infos. Ask for our support services "); echo ("in order to handle dump files containing extended inserts.

\n"); $error=true; break; } // Execute query if end of query detected ($delimiter as last character) AND NOT in parents // DIAGNOSTIC // echo ("

Regex: ".'/'.preg_quote($delimiter).'$/'."

\n"); // echo ("

In Parents: ".($inparents?"true":"false")."

\n"); // echo ("

Line: $dumpline

\n"); if ((preg_match('/'.preg_quote($delimiter,'/').'$/',trim($dumpline)) || $delimiter=='') && !$inparents) { // Cut off delimiter of the end of the query $query = substr(trim($query),0,-1*strlen($delimiter)); // DIAGNOSTIC // echo ("

Query: ".trim(nl2br(htmlentities($query)))."

\n"); if (!TESTMODE && !$mysqli->query($query)) { echo ("

Error at the line $linenumber: ". trim($dumpline)."

\n"); echo ("

Query: ".trim(nl2br(htmlentities($query)))."

\n"); echo ("

MySQL: ".$mysqli->error."

\n"); $error=true; break; } $totalqueries++; $queries++; $query=""; $querylines=0; } $linenumber++; } } // Get the current file position if (!$error) { if (!$gzipmode) $foffset = ftell($file); else $foffset = gztell($file); if (!$foffset) { echo ("

UNEXPECTED: Can't read the file pointer offset

\n"); $error=true; } } // Print statistics skin_open(); // echo ("

Statistics

\n"); if (!$error) { $lines_this = $linenumber-$_REQUEST["start"]; $lines_done = $linenumber-1; $lines_togo = ' ? '; $lines_tota = ' ? '; $queries_this = $queries; $queries_done = $totalqueries; $queries_togo = ' ? '; $queries_tota = ' ? '; $bytes_this = $foffset-$_REQUEST["foffset"]; $bytes_done = $foffset; $kbytes_this = round($bytes_this/1024,2); $kbytes_done = round($bytes_done/1024,2); $mbytes_this = round($kbytes_this/1024,2); $mbytes_done = round($kbytes_done/1024,2); if (!$gzipmode) { $bytes_togo = $filesize-$foffset; $bytes_tota = $filesize; $kbytes_togo = round($bytes_togo/1024,2); $kbytes_tota = round($bytes_tota/1024,2); $mbytes_togo = round($kbytes_togo/1024,2); $mbytes_tota = round($kbytes_tota/1024,2); $pct_this = ceil($bytes_this/$filesize*100); $pct_done = ceil($foffset/$filesize*100); $pct_togo = 100 - $pct_done; $pct_tota = 100; if ($bytes_togo==0) { $lines_togo = '0'; $lines_tota = $linenumber-1; $queries_togo = '0'; $queries_tota = $totalqueries; } $pct_bar = "
"; } else { $bytes_togo = ' ? '; $bytes_tota = ' ? '; $kbytes_togo = ' ? '; $kbytes_tota = ' ? '; $mbytes_togo = ' ? '; $mbytes_tota = ' ? '; $pct_this = ' ? '; $pct_done = ' ? '; $pct_togo = ' ? '; $pct_tota = 100; $pct_bar = str_replace(' ',' ','[ Not available for gzipped files ]'); } echo ("
SessionDoneTo goTotal
Lines$lines_this$lines_done$lines_togo$lines_tota
Queries$queries_this$queries_done$queries_togo$queries_tota
Bytes$bytes_this$bytes_done$bytes_togo$bytes_tota
KB$kbytes_this$kbytes_done$kbytes_togo$kbytes_tota
MB$mbytes_this$mbytes_done$mbytes_togo$mbytes_tota
%$pct_this$pct_done$pct_togo$pct_tota
% bar$pct_bar
\n"); // Finish message and restart the script if ($linenumber<$_REQUEST["start"]+$linespersession) { echo ("

Congratulations: End of file reached, assuming OK

\n"); echo ("

IMPORTANT: REMOVE YOUR DUMP FILE and BIGDUMP SCRIPT FROM SERVER NOW!

\n"); echo ("

Thank you for using this tool! Please rate Bigdump at Hotscripts.com

\n"); echo ("

You can send me some bucks or euros as appreciation via PayPal. Thank you!

\n"); ?>
Now I'm waiting $delaypersession milliseconds before starting next session...

\n"); if (!$ajax) echo ("\n"); echo ("\n"); echo ("

Press STOP to abort the import OR WAIT!

\n"); } } else echo ("

Stopped on error

\n"); skin_close(); } if ($error) echo ("

Start from the beginning (DROP the old tables before restarting)

\n"); if ($mysqli) $mysqli->close(); if ($file && !$gzipmode) fclose($file); else if ($file && $gzipmode) gzclose($file); ?>

© 2003-2015 Alexey Ozerov

'; echo ""; // data - for calculations echo "$linenumber"; echo "$foffset"; echo "$curfilename"; echo "$totalqueries"; echo "$delimiter"; // results - for page update echo "$lines_this"; echo "$lines_done"; echo "$lines_togo"; echo "$lines_tota"; echo "$queries_this"; echo "$queries_done"; echo "$queries_togo"; echo "$queries_tota"; echo "$bytes_this"; echo "$bytes_done"; echo "$bytes_togo"; echo "$bytes_tota"; echo "$kbytes_this"; echo "$kbytes_done"; echo "$kbytes_togo"; echo "$kbytes_tota"; echo "$mbytes_this"; echo "$mbytes_done"; echo "$mbytes_togo"; echo "$mbytes_tota"; echo "$pct_this"; echo "$pct_done"; echo "$pct_togo"; echo "$pct_tota"; echo "".htmlentities($pct_bar).""; echo ""; } function create_ajax_script() { global $linenumber, $foffset, $totalqueries, $delaypersession, $curfilename, $delimiter; ?>