Index: C:/Documents and Settings/odw/My Documents/Webs/www/wordpress/wp-content/plugins/wp-db-backup/wp-db-backup.php
===================================================================
--- C:/Documents and Settings/odw/My Documents/Webs/www/wordpress/wp-content/plugins/wp-db-backup/wp-db-backup.php	(revision 2631)
+++ C:/Documents and Settings/odw/My Documents/Webs/www/wordpress/wp-content/plugins/wp-db-backup/wp-db-backup.php	(working copy)
@@ -42,7 +42,8 @@
 		if ('smtp' == $_POST['deliver']) {
 			wp_deliver_backup ($backup_file, $_POST['deliver'], $_POST['backup_recipient']);
 		} elseif ('http' == $_POST['deliver']) {
-			header('Refresh: 3; ' . get_settings('siteurl') . "/wp-admin/edit.php?page=wp-db-backup.php&backup=$backup_file");
+			$this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__);
+			header('Refresh: 3; ' . get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}&backup=$backup_file");
 		}
 		// we do this to say we're done.
 		$_POST['do_backup'] = 'DONE';
@@ -97,7 +98,7 @@
 } // function backquote($a_name, $do_it = TRUE)
 
 /////////////////////////////
-function backup_table($table) {
+function backup_table($fp, $table) {
 global $wp_backup_error, $wpdb;
 
 /*
@@ -109,54 +110,46 @@
 to use the WordPress $wpdb object
 */
 
-$sql_statements  = '';
-	
 //
 // Add SQL statement to drop existing table
-$sql_statements .= "\n";
-$sql_statements .= "\n";
-$sql_statements .= "#\n";
-$sql_statements .= "# Delete any existing table " . backquote($table) . "\n";
-$sql_statements .= "#\n";
-$sql_statements .= "\n";
-$sql_statements .= "DROP TABLE IF EXISTS " . backquote($table) . ";\n";
+fwrite($fp, "\n");
+fwrite($fp, "\n");
+fwrite($fp, "#\n");
+fwrite($fp, "# Delete any existing table " . backquote($table) . "\n");
+fwrite($fp, "#\n");
+fwrite($fp, "\n");
+fwrite($fp, "DROP TABLE IF EXISTS " . backquote($table) . ";\n");
 
 // 
 //Table structure
 // Comment in SQL-file
-$sql_statements .= "\n";
-$sql_statements .= "\n";
-$sql_statements .= "#\n";
-$sql_statements .= "# Table structure of table " . backquote($table) . "\n";
-$sql_statements .= "#\n";
-$sql_statements .= "\n";
+fwrite($fp, "\n");
+fwrite($fp, "\n");
+fwrite($fp, "#\n");
+fwrite($fp, "# Table structure of table " . backquote($table) . "\n");
+fwrite($fp, "#\n");
+fwrite($fp, "\n");
 
 $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N);
 if (FALSE === $create_table) {
 	$wp_backup_error .= "Error with SHOW CREATE TABLE for $table.\r\n";
-	return "#\n# Error with SHOW CREATE TABLE for $table!\n#\n";
+	fwrite($fp, "#\n# Error with SHOW CREATE TABLE for $table!\n#\n");
 }
-$sql_statements .= $create_table[0][1] . ' ;';
+fwrite($fp, $create_table[0][1] . ' ;');
 
 $table_structure = $wpdb->get_results("DESCRIBE $table");
 if (FALSE === $table_structure) {
 	$wp_backup_error .= "Error getting table structure of $table\r\n";
-	return "#\n# Error getting table structure of $table!\n#\n";
+	fwrite($fp, "#\n# Error getting table structure of $table!\n#\n");
 }
 
-$table_data = $wpdb->get_results("SELECT * FROM $table", ARRAY_A);
-if (FALSE === $table_data) {
-	$wp_backup_error .= "Error getting table contents from $table\r\n";
-	return "#\n# Error getting table contents fom $table!\n#\n";
-}
-
 //
 // Comment in SQL-file
-$sql_statements .= "\n";
-$sql_statements .= "\n";
-$sql_statements .= "#\n";
-$sql_statements .= '# Data contents of table ' . backquote($table) . "\n";
-$sql_statements .= "#\n";
+fwrite($fp, "\n");
+fwrite($fp, "\n");
+fwrite($fp, "#\n");
+fwrite($fp, '# Data contents of table ' . backquote($table) . "\n");
+fwrite($fp, "#\n");
 
 $ints = array();
 foreach ($table_structure as $struct) {
@@ -170,30 +163,48 @@
 	}
 }
 
-$entries = 'INSERT INTO ' . backquote($table) . ' VALUES (';	
-//    \x08\\x09, not required
-$search = array("\x00", "\x0a", "\x0d", "\x1a");
-$replace = array('\0', '\n', '\r', '\Z');
-foreach ($table_data as $row) {
-	$values = array();
-	foreach ($row as $key => $value) {
-		if ($ints[strtolower($key)]) {
-			$values[] = $value;
-		} else {
-			$values[] = "'" . str_replace($search, $replace, sql_addslashes($value)) . "'";
+
+// Batch by $row_inc
+
+$row_start = 0;
+$row_inc = 10;
+
+do {	
+	$table_data = $wpdb->get_results("SELECT * FROM $table LIMIT {$row_start}, {$row_inc}", ARRAY_A);
+	
+	/*
+	if (FALSE === $table_data) {
+		$wp_backup_error .= "Error getting table contents from $table\r\n";
+		fwrite($fp, "#\n# Error getting table contents fom $table!\n#\n");
+	}
+	*/
+		
+	$entries = 'INSERT INTO ' . backquote($table) . ' VALUES (';	
+	//    \x08\\x09, not required
+	$search = array("\x00", "\x0a", "\x0d", "\x1a");
+	$replace = array('\0', '\n', '\r', '\Z');
+	if($table_data) {
+		foreach ($table_data as $row) {
+			$values = array();
+			foreach ($row as $key => $value) {
+				if ($ints[strtolower($key)]) {
+					$values[] = $value;
+				} else {
+					$values[] = "'" . str_replace($search, $replace, sql_addslashes($value)) . "'";
+				}
+			}
+			fwrite($fp, " \n" . $entries . implode(', ', $values) . ') ;');
 		}
+		$row_start += $row_inc;
 	}
-	$sql_statements .= " \n" . $entries . implode(', ', $values) . ') ;';
-}
+} while(count($table_data) > 0);
 // Create footer/closing comment in SQL-file
-$sql_statements .= "\n";
-$sql_statements .= "#\n";
-$sql_statements .= "# End of data contents of table " . backquote($table) . "\n";
-$sql_statements .= "# --------------------------------------------------------\n";
-$sql_statements .= "\n";
+fwrite($fp, "\n");
+fwrite($fp, "#\n");
+fwrite($fp, "# End of data contents of table " . backquote($table) . "\n");
+fwrite($fp, "# --------------------------------------------------------\n");
+fwrite($fp, "\n");
 
-return $sql_statements;
-
 } // end backup_table()
 
 ////////////////////////////
@@ -204,18 +215,29 @@
 $done = array();
 
 $datum = date("Ymd_B");
+$wp_backup_temp = DB_NAME . "_$table_prefix$datum.tmp";
 $wp_backup_filename = DB_NAME . "_$table_prefix$datum.sql";
 if ($gzip) {
-	$wp_backup_filename .= '.gz';
+	$wp_backup_filename .= '.zip';
 }
 
+if (is_writable(ABSPATH . $wp_backup_dir)) {
+	if ($gzip) {
+		$fp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_temp, 'w');
+	} else {
+		$fp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_filename, 'w');
+	}
+} else {
+	return false;
+}
+
 //Begin new backup of MySql
-$sql  = "# WordPress MySQL database backup\n";
-$sql .= "#\n";
-$sql .= "# Generated: " . date("l j. F Y H:i T") . "\n";
-$sql .= "# Hostname: " . DB_HOST . "\n";
-$sql .= "# Database: " . backquote(DB_NAME) . "\n";
-$sql .= "# --------------------------------------------------------\n";
+fwrite($fp, "# WordPress MySQL database backup\n");
+fwrite($fp, "#\n");
+fwrite($fp, "# Generated: " . date("l j. F Y H:i T") . "\n");
+fwrite($fp, "# Hostname: " . DB_HOST . "\n");
+fwrite($fp, "# Database: " . backquote(DB_NAME) . "\n");
+fwrite($fp, "# --------------------------------------------------------\n");
 
 foreach ($core_tables as $table) {
 	if (in_array($table, $done)) { continue; }
@@ -223,37 +245,60 @@
 	if ( !ini_get('safe_mode')) @set_time_limit(15*60);
 	//ini_set('memory_limit', '16M');
 	// Create the SQL statements
-	$tbl = "# --------------------------------------------------------\n";
-	$tbl .= "# Table: " . backquote($table) . "\n";
-	$tbl .= "# --------------------------------------------------------\n";
-	$tbl .= backup_table($table);
-	$sql .= $tbl;
+	fwrite($fp, "# --------------------------------------------------------\n");
+	fwrite($fp, "# Table: " . backquote($table) . "\n");
+	fwrite($fp, "# --------------------------------------------------------\n");
+	backup_table($fp, $table);
 	$done[] = $table;
 }
 
 if (count($other_tables) > 0) {
 	foreach ($other_tables as $other_table) {
+		echo "$table<br/>";
 		if (in_array($other_table, $done)) { continue; }
 		// Increase script execution time-limit to 15 min for every table.
 		if ( !ini_get('safe_mode')) @set_time_limit(15*60);
 		//ini_set('memory_limit', '16M');
 		// Create the SQL statements
-		$tbl = "# --------------------------------------------------------\n";
-		$tbl .= "# Table: " . backquote($other_table) . "\n";
-		$tbl .= "# --------------------------------------------------------\n";
-		$tbl .= backup_table($other_table);
-		$sql .= $tbl;
+		fwrite($fp, "# --------------------------------------------------------\n");
+		fwrite($fp, "# Table: " . backquote($other_table) . "\n");
+		fwrite($fp, "# --------------------------------------------------------\n");
+		backup_table($other_table, $fp);
 		$done[] = $other_table;
 	} // foreach
 } // if other_tables...
 
-if (is_writable(ABSPATH . $wp_backup_dir)) {
-	if ($gzip) {
-		$sql = gzencode($sql);
+fclose($fp);
+
+if ($gzip) {
+	$mem_limit = ini_get('memory_limit');
+	function return_bytes($val) {
+	   $val = trim($val);
+	   $last = strtolower($val{strlen($val)-1});
+	   switch($last) {
+	       // The 'G' modifier is available since PHP 5.1.0
+	       case 'g':
+	           $val *= 1024;
+	       case 'm':
+	           $val *= 1024;
+	       case 'k':
+	           $val *= 1024;
+	   }
+	
+	   return $val;
 	}
-	$cachefp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_filename, "w");
-	fwrite($cachefp, $sql);
-	fclose($cachefp);
+	$mem_limit = return_bytes($mem_limit) - 2000000;  // Approx. 2M for running script?
+	if(filesize(ABSPATH . $wp_backup_dir . $wp_backup_temp) > $mem_limit) {
+		// Uh, oh.
+		$wp_backup_error = __('The script to backup your database is too large for PHP to load entirely into memory, and so cannot be compressed within PHP.  <strong>Please turn off the gzip compression option.</strong>');
+	} else {
+		$data = implode("", file(ABSPATH . $wp_backup_dir . $wp_backup_temp));
+		$gzdata = gzencode($data, 9);
+		$fp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_filename, "w");
+		fwrite($fp, $gzdata);
+		fclose($fp);
+	}
+	unlink (ABSPATH . $wp_backup_dir . $wp_backup_temp);
 }
 
 if ('' == $wp_backup_error) {
@@ -403,15 +448,15 @@
 }
 echo '</tr></table></fieldset>';
 echo '<fieldset class="options"><legend>' . __('Backup Options', 'wp_backup') . '</legend><table width="100%" align="center" cellpadding="5" cellspacing="5">';
-echo '<tr><td align="center">';
+echo '<tr><td align="left">';
 echo __('Deliver backup file by', 'wp_backup') . ":<br />";
-echo '<input type="radio" name="deliver" value="none" /> ' . __('None', 'wp_backup') . '&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />';
-echo '<input type="radio" name="deliver" value="smtp" /> ' . __('Email', 'wp_backup') . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />';
-echo '<input type="radio" name="deliver" value="http" /> ' . __('Download', 'wp_backup');
+echo '<label style="display:block;"><input type="radio" name="deliver" value="none" /> ' . __('None', 'wp_backup') . '</label>';
+echo '<label style="display:block;"><input type="radio" name="deliver" value="smtp" /> ' . __('Email', 'wp_backup') . '</label>';
+echo '<label style="display:block;"><input type="radio" name="deliver" value="http" /> ' . __('Download', 'wp_backup') . '</label>';
 echo '</td><td align="left">' . __('Email backup to', 'wp_backup') . ':<br /> <input type="text" name="backup_recipient" size="20" value="' . get_settings('admin_email') . '" /></td></tr>';
-echo '<tr class="alternate"><td colspan="2" align="center">';
+echo '<tr class="alternate"><td colspan="2" align="center"><label style="display:block;">';
 if (! $WHOOPS) {
-	echo __('Use gzip compression', 'wp_backup') . '? <input type="checkbox" checked="checked" name="gzip" value="gzip" /><br />';
+	echo __('Use gzip compression', 'wp_backup') . '? <input type="checkbox" checked="checked" name="gzip" value="gzip" /></label>';
 	echo '<input type="hidden" name="do_backup" value="backup" />';
 	echo '<input type="submit" name="submit" value="' . __('Backup', 'wp_backup') . '!" / >';
 } else {

