--- backup.module	Mon Oct 02 10:05:34 2006
+++ backup.module	Sat Oct 21 14:44:14 2006
@@ -17,6 +17,16 @@
 */
 
 /**
+* addition to make it work with Windows
+* values:
+* "Unix" - Unix/Linux
+* "Win32" - Windows
+* @todo Make these constants?
+*/
+global $os;
+
+
+/**
  * Implementation of hook_help().
  */
 function backup_help($section) {
@@ -69,6 +79,7 @@
 */
 function backup_page() {
 
+	_set_os();
 	$content = "";
 
 	//
@@ -152,6 +163,7 @@
 */
 function _backup_backup() {
 
+	_set_os();
 	$files = backup_get_files();
 	$programs = backup_get_programs();
 	backup_get_programs_list($programs, $error);
@@ -172,8 +184,9 @@
 	* @todo Create a function that returns the string '/tmp' and another that
 	*	calls tempname() and returns that string, since we currently have
 	*	the string '/tmp/ referenced in multiple places.
+	** partially done..
 	*/
-	$tmp_dir = '/tmp';
+	$tmp_dir = _get_temp_dir();
 	$tmpfile = tempnam($tmp_dir, 'drupal_backup_');
 	//
 	// $tmpfile2 is our destination file for the tarball.
@@ -191,8 +204,9 @@
 	// to prevent any anonymos user from exploiting a race condition
 	// and potentially downloading the database.
 	//
+	// addition: replaced ':' with '-' for Win32 compatibility
 	$db_file = $tmp_dir . '/' . 'drupal_backup_database-' 
-		. strftime('%Y-%m-%d-%H:%M:%S') 
+		. strftime('%Y-%m-%d-%H-%M-%S') 
 		. '-' . rand(0, 1000000) 
 		. '.gz';
 
@@ -323,6 +337,8 @@
 
 	// Init
 	$retval = array();
+	
+	$tmpdir = _get_temp_dir();
 
 	$files = _backup_get_files();
 
@@ -345,7 +361,12 @@
 		if ($line == '.' || $line == '..') {
 			continue;
 		}
-
+		// slight hack to exclude drupal temp folder from inclusion in the tar. this will fail if the temp folder is within the website but not in the webroot, however. (i.e. /files/temp rather than /temp)
+		// actually, with the temp-folder-within-the-web detected and removed, tar still fails. gonna leave this line in for now tho.
+		// need to put in warning that temp folder should be outside web-root
+		if ($line == $tmpdir) {
+			continue;
+		}
 		if ($files[$line]) {
 			$retval[$line] = $files[$line];
 		} else {
@@ -394,15 +415,24 @@
 *
 * @return mixed The full pathname to the file, or null on failure.
 */
+// hacked for Win32 compatibility..
 function backup_search_path($file) {
-
+global $os;
 	$retval = '';
 
 	$path = getenv('PATH');
-	$path = explode(':', $path);
+	
+	if ($os == 'Win32') {
+		$path = explode(';', $path);
+		$ext = '.exe';
+	} else {
+		// unix/original default
+		$path = explode(':', $path);
+		$ext = '';
+	}
 
 	foreach ($path as $key => $value) {
-		$file2 = $value . '/' . $file;
+		$file2 = $value . '/' . $file . $ext;
 		if (is_file($file2)) {
 			$retval = $file2;
 			break;
@@ -520,7 +550,9 @@
 *
 * @return string Any content to be written.
 */
+// hacked for Win32 compatibility
 function backup_tar(&$files, $tmpfile, &$error) {
+global $os;
 
 	// Init
 	$retval = '';
@@ -529,18 +561,36 @@
 	// Create our tar command for the backup
 	//
 	$file_list = '';
-	foreach ($files as $key => $value) {
-		$file_list .= $key . ' ';
+	// Win32 hack.. added quotes around all filenames to circumvent spaces-in-filenames prob.
+	if ($os == 'Win32') {
+		foreach ($files as $key => $value) {
+			$file_list .= '"' . $key . '" ';
+		}
+	} else {
+		// unix/original default
+		foreach ($files as $key => $value) {
+			$file_list .= $key . ' ';
+		}
 	}
-
+	
 	$verbose = '';
 	//$verbose = 'v';
 
-	$cmd = 'tar cf' . $verbose . 'z ' 
-		. $tmpfile 
-		. ' ' . $file_list 
-		. ' 2>&1';
-
+	if ($os == 'Win32') {
+		// can't get 'z' option to work on win32. don't know why. means the archive is uncompressed
+		// also added quotes around $tmpfile and put -f option last otherwise tempfile is not set correctly
+		$cmd = 'tar -c' . $verbose . 'f "'  
+			. $tmpfile . '"'
+			. ' ' . $file_list 
+			. ' 2>&1';
+		
+	} else {
+		// unix/original default
+		$cmd = 'tar cf' . $verbose . 'z ' 
+			. $tmpfile 
+			. ' ' . $file_list 
+			. ' 2>&1';
+	}
 	//echo $cmd; exit(); // Debugging
 
 	//
@@ -570,6 +620,11 @@
 				continue;
 			}
 
+			// added for Win32 compatibility. Assuming unix will never output this so won't cause prob and don't need a Win32 check here.
+			if (stristr($line, 'tar: Removing drive spec')) {
+				continue;
+			}
+			
 			if (empty($beenhere)) {
 				$retval .= '<pre>';
 				$beenhere = 1;
@@ -674,7 +729,7 @@
 */
 function backup_cron() {
 
-	$tmp_dir = '/tmp';
+	$tmp_dir = _get_temp_dir();
 
 	$fp = @opendir($tmp_dir);
 
@@ -737,6 +792,48 @@
 
 } // End of backup_show_path()
 
+
+/*
+** addition for Win32
+* Return path to tmp dir we will use
+* on Unix, leave this set as is /tmp (as in the original module)
+* on Windows, return the Drupal tmp path for now
+** this is not ideal since the drupal path may be within the webroot, causing tar recursion problems
+*/
+
+function _get_temp_dir()
+{
+global $os;
+	if ($os == 'Win32') {
+		$tmp_dir = file_directory_temp();
+		// don't use \ even for win32 - causes problems and / works just as well at os level.
+		$tmp_dir = str_replace('\\', '/', $tmp_dir);
+	} else {
+		$tmp_dir = '/tmp';
+	}
+	return $tmp_dir;
+}
+
+
+/*
+* sets the global os string.
+* a better way to do this would be to have
+* get_os, or maybe check_os($os) so that we don't have a global that needs to be initialised..
+* but for now...
+*/
+
+function _set_os()
+{
+global $os;
+	$os = getenv('OS');
+	// getenv('OS') returns a value on Win32 systems. I don't know if it does on any other system, so relying on Win32 return value to set $os
+	if (stristr($os, 'Windows')) {
+		$os = 'Win32';
+	} else {
+		$os = 'Unix';
+	}
+
+}
 
 
 ?>
