'; if ($_GET['shared_key'] != $shared_key) { die(); } // BigDump ver. 0.34b from 2011-09-04 // Database configuration $db_server = '<%= escape_php db[:host] %>'; $db_user = '<%= escape_php db[:user] %>'; $db_password = '<%= escape_php db[:password] %>'; $db_name = '<%= escape_php db[:name] %>'; // Other settings (optional) $filename = ''; // Specify the dump filename to suppress the file selection dialog $ajax = true; // AJAX mode: import will be done without refreshing the website $linespersession = 3000; // Lines to be executed per one import session $delaypersession = 0; // You can specify a sleep time in milliseconds after each session // Works only if JavaScript is activated. Use to reduce server overrun // CSV related settings (only if you use a CSV dump) $csv_insert_table = ''; // Destination table for CSV files $csv_preempty_table = false; // true: delete all entries from table specified in $csv_insert_table before processing $csv_delimiter = ','; // Field delimiter in CSV file $csv_add_quotes = true; // If your CSV data already have quotes around each field set it to false $csv_add_slashes = true; // If your CSV data already have slashes in front of ' and " set it to false // Allowed comment markers: lines starting with these strings will be ignored by BigDump $comment[]='#'; // Standard comment lines are dropped by default $comment[]='-- '; $comment[]='DELIMITER'; // Ignore DELIMITER switch as it's not a valid SQL statement // $comment[]='---'; // Uncomment this line if using proprietary dump created by outdated mysqldump // $comment[]='CREATE DATABASE'; // Uncomment this line if your dump contains create database queries in order to ignore them $comment[]='/*!'; // Or add your own string to leave out other proprietary things // Pre-queries: SQL queries to be executed at the beginning of each import session // $pre_query[]='SET foreign_key_checks = 0'; // $pre_query[]='Add additional queries if you want here'; // Connection charset should be the same as the dump file charset (utf8, latin1, cp1251, koi8r etc.) // See http://dev.mysql.com/doc/refman/5.0/en/charset-charsets.html for the full list // Change this if you have problems with non-latin letters $db_connection_charset = '<%= db[:charset] || 'utf8' %>'; // Default query delimiter: this character at the line end tells Bigdump where a SQL statement ends // Can be changed by DELIMITER statement in the dump file (normally used when defining procedures/functions) $delimiter = ';'; // String quotes character $string_quotes = '\''; // Change to '"' if your dump file uses double qoutes for strings // ******************************************************************************************* // If not familiar with PHP please don't change anything below this line // ******************************************************************************************* if ($ajax) ob_start(); define ('VERSION','0.34b'); define ('DATA_CHUNK_LENGTH',16384); // How many chars are read per time define ('MAX_QUERY_LINES',300); // How many lines may be considered to be one query (except text lines) define ('TESTMODE',false); // Set to true to process the file without actually accessing the database header("Expires: Mon, 1 Dec 2003 01:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); @ini_set('auto_detect_line_endings', true); @set_time_limit(0); if (function_exists("date_default_timezone_set") && function_exists("date_default_timezone_get")) @date_default_timezone_set(@date_default_timezone_get()); // Clean and strip anything we don't want from user's input [0.27b] foreach ($_REQUEST as $key => $val) { $val = preg_replace("/[^_A-Za-z0-9-\.&= ;\$]/i",'', $val); $_REQUEST[$key] = $val; } ?> BigDump ver. <?php echo (VERSION); ?>
'); } function skin_close() { echo (''); } skin_open(); echo ('

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

'); skin_close(); $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('mysql_connect')) { echo ("

There is no mySQL extension available in your PHP installation. Sorry!

\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"]); // 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); 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(basename($_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) { $dbconnection = @mysql_connect($db_server,$db_user,$db_password); if ($dbconnection) $db = mysql_select_db($db_name); if (!$dbconnection || !$db) { echo ("

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

\n"); echo ("

Edit the database settings in ".$_SERVER["SCRIPT_FILENAME"]." or contact your database provider.

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

Error with pre-query.

\n"); echo ("

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

\n"); echo ("

MySQL: ".mysql_error()."

\n"); $error=true; break; } } } } else { $dbconnection = false; } // 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"); else echo ("\n \n"); } } } if ($dirhead) echo ("
FilenameSizeDate&TimeType  
$dirfile".filesize($dirfile)."".date ("Y-m-d H:i:s", filemtime($dirfile))."SQLGZipCSVMiscStart Import into $db_name at $db_serverDelete 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=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:

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"); } } // 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($curfilename,"r")) || ($gzipmode && !$file=@gzopen($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 (($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)) { // 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 && !mysql_query(trim($query), $dbconnection)) { echo ("

Error when deleting entries from $csv_insert_table.

\n"); echo ("

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

\n"); echo ("

MySQL: ".mysql_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)) && !$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 && !mysql_query($query, $dbconnection)) { echo ("

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

\n"); echo ("

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

\n"); echo ("

MySQL: ".mysql_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 ($dbconnection) mysql_close($dbconnection); if ($file && !$gzipmode) fclose($file); else if ($file && $gzipmode) gzclose($file); ?>

© 2003-2011 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; ?>