* @param $strSortType String containing either asc or desc [default to asc]
* @desc Naturally sorts an array using by the column $strSortBy
*/
+define('EXPORT_MAX_INSERT', 50000);
+
function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
{
// if the parameters are invalid
}
// update per company files
- $cnt = count($db_connections);
+ $cnt = max(1, count($db_connections));
for($i = 0; $i < $cnt; $i++)
{
$newexts = $extensions;
ini_set("max_execution_time", "180");
db_query("SET foreign_key_checks=0");
+ $check_line_len = false;
// uncrompress gziped backup files
if (strpos($filename, ".gz") || strpos($filename, ".GZ"))
- $lines = db_ungzip("lines", $filename);
- elseif (strpos($filename, ".zip") || strpos($filename, ".ZIP"))
+ { $lines = db_ungzip("lines", $filename);
+ $check_line_len = true;
+ } elseif (strpos($filename, ".zip") || strpos($filename, ".ZIP"))
$lines = db_unzip("lines", $filename);
else
$lines = file("". $filename);
$delimiter = ';';
foreach($lines as $line_no => $line)
{
+ $gzfile_bug = $check_line_len && (strlen($line) == 8190); // there is a bug in php (at least 4.1.1-5.5.9) gzfile which limits line length to 8190 bytes!
+
$line = trim($line);
-
$line = str_replace("0_", $connection["tbpref"], $line);
if ($query_table == '')
if($query_table != '') // inside allowed query
{
$table = $query_table;
-
- if (substr($line, -strlen($delimiter)) == $delimiter) // end of query found
+ if (!$gzfile_bug && substr($line, -strlen($delimiter)) == $delimiter) // end of query found
{
$line = substr($line, 0, strlen($line) - strlen($delimiter)); // strip delimiter
$query_table = '';
}
}
}
-
+
db_query("SET foreign_key_checks=1");
if ($delimiter != ';') db_query("delimiter ;"); // just for any case
// if ($auto_incr[$tablename])
// $out.=" AUTO_INCREMENT=".$auto_incr[$tablename];
$out.=" ;";
- $out.="\n\n\n";
+ $out.="\n\n";
// export data
if (!$error)
{
- $out.="### Data of table `".$tablename."` ###\n\n";
+ $out.="### Data of table `".$tablename."` ###\n";
// check if field types are NULL or NOT NULL
$res3 = db_query("SHOW COLUMNS FROM `" . $tablename . "`");
}
$res2 = db_query("SELECT * FROM `" . $tablename . "`");
+ $maxinsert = 0;
+ $insert = '';
for ($j = 0; $j < db_num_rows($res2); $j++)
{
- $out .= "INSERT INTO `" . $tablename . "` VALUES (";
$row2 = db_fetch_row($res2);
- // run through each field
+ $values = '(';
for ($k = 0; $k < $nf = db_num_fields($res2); $k++)
{
- $out .= db_escape($row2[$k], $field_null[$k]);
+ $values .= db_escape($row2[$k], $field_null[$k]);
if ($k < ($nf - 1))
- $out .= ", ";
+ $values .= ', ';
+ }
+ $values .= ')';
+ $len = strlen($values);
+ if ($maxinsert < $len+1)
+ {
+ $maxinsert = EXPORT_MAX_INSERT;
+ if ($insert)
+ {
+ $out .= $insert .';'; // flush insert query
+ $insert = '';
+ }
}
- $out .= ");\n";
+
+ if ($insert == '')
+ {
+ $insert = "\nINSERT INTO `" . $tablename . "` VALUES\n";
+ $maxinsert -= strlen($insert);
+ } else {
+ $insert .= ",\n";
+ }
+
+ $maxinsert -= $len;
+ $insert .= $values;
// if saving is successful, then empty $out, else set error flag
if (strlen($out) > $max_size && $zip != "zip")
$error = true;
}
}
-
+ if ($insert)
+ $out .= $insert. ';';
// an error occurred! Try to delete file and return error status
}
elseif ($error)