<?php
			
	OnlyShowIf(CheckAdmin());
	
	header("Location: ".$siteroot."/diagram/1/2");
	
	ob_start();
	
	echo 'Attempting Restore!';
	
	if ($siteroot == "")
	{
		$siteroot = "/s/".$site;
		echo 'New Root: '.$siteroot;
	}
	
	echo 'Shutting Down Drivers...';
	if ((PHP_OS == "Linux") || (PHP_OS == "Unix"))
	{
		//Run the process to install the driver...
		$cmd = "sudo /opt/ardi/drivers/ardidrivers localhost".$siteroot." remove";
		ob_start();
		system($cmd);
		ob_get_clean();
	}
	
	//Shutting Down Profiles

	
	$pth = $sitefolder.'/restore';
	if (!file_exists($pth))
	{
		mkdir($pth);
	}
	
	if (file_exists($pth.'/restore.zip'))
	{
		unlink($pth.'/restore.zip');
	}	
	
	if (!isset($_FILES['restorefile']))
	{
		if (!isset($_REQUEST['local']))
		{
			echo 'File Not Set';
			exit();
		}
	}
	
	if (!isset($_REQUEST['local']))
	{	
		if ($_FILES['restorefile']['error'] != 0)
		{
			if ($_FILES['restorefile']['error'] == 1) echo 'Backup File Too Large';
			exit();
		}
	}
	
	$rlog = "";
	global $rlog;
	$dbfile = $pth.'/';
	global $dbfile;
	
	if (file_exists($dbfile))
	{
		unlink($dbfile);
	}
	
	$rlog = 'Restoring Backup File...';
	//move_uploaded_file($_FILES['restorefile']['tmp_name'],$pth.'/restore.zip');
	
	function Unzip($filename, $outpath, $destiny="")
	{
		global $rlog;
		global $dbfile;
		global $sitefolder;
		$outpath .= DIRECTORY_SEPARATOR;
		$path_file = $filename;
		
		$finalpath = str_replace("\\","/",$path_file);
		
		$dir = $outpath;
		
		$ln = strlen($outpath);
		
		$zip = new ZipArchive();
		if ($zip->open($finalpath,ZipArchive::CHECKCONS) !== TRUE)
		{
			echo 'Failed to Open ZIP File';
			return;
		}
		
		for ($i = 0; $i < $zip->numFiles; $i++) {
			 $name = $zip->getNameIndex($i);
			 if ($name === FALSE) continue;
			 
		     $path_file = str_replace('/',DIRECTORY_SEPARATOR,$name);			 
			 
			 $info = pathinfo($path_file);			
			 $fname = $info['basename'];
			 $new_dir = $info['dirname'];
			
			if ($path_file == "db.sql")
			{
				global $dbfile;
				
				//echo "Hello there - I have me a database file!";
				//if (!isset($_REQUEST['db'])) continue;
				$rlog .= "Saving SQL Database Backup To ".$dbfile;
				
				$zip->extractTo($dbfile,$name);
				
				//Extract this to an alternate folder...
				$fp = fopen($dbfile, "w");
				fwrite($fp, $buf);
				fflush($fp);
				fclose($fp);
				echo 'Extracting Database To '.$dbfile;
				continue;
			}
			
			//echo 'Directory: '.$new_dir.'<br/>';

			$bits = explode(DIRECTORY_SEPARATOR,$new_dir);
			
			$dest = $sitefolder.'/';
			
			//echo 'Copying To: '.$dest.'<br/>';
			
			if ($dest == "") continue;
			$new_dir = $dest;
			
			
			echo 'Extracting '.$name.' to '.$dest.'<br/>';
			$zip->extractTo($dest,$name);
				
			
 		}
		
		$zip->close();		
		
	}
	
	if (isset($_REQUEST['local']))
	{
		$fn = $_SERVER['DOCUMENT_ROOT'].'/cache/backup.zip';
	}	
	else
		$fn = $_FILES['restorefile']['tmp_name'];
				
	Unzip($fn,$_SERVER['DOCUMENT_ROOT']);
	
	//exit();
		
	$dbbackupcmd = "";
	if ($dbtype == "mysql")
	{
		$dbbackupcmd = "mysql -h %HOST% -u %USER% -f --password=\"%PASSWORD%\" %DBNAME% < %FILENAME%";
	}
	if ($dbtype == "pgsql")
	{
		$dbbackupcmd = "pg_restore --column-inserts -h %HOST% -U %USER% -w -d %DBNAME% %FILENAME%";
	}
						
	$dbfile .= 'db.sql';
	
	if (file_exists($dbfile))
	{	
		$cmd = str_replace("%HOST%","localhost",$dbbackupcmd);
		$cmd = str_replace("%USER%",$dbuser,$cmd);
		$cmd = str_replace("%PASSWORD%",$dbpass,$cmd);
		$cmd = str_replace("%DBNAME%",$dbname,$cmd);
		$cmd = str_replace("%FILENAME%",$dbfile,$cmd);
			
		echo $dbbackupcmd;
		
		if ($dbname != "") {
			echo 'Found Database File. Importing with '.$cmd."\r\n";
			$rlog .= 'Call: '.$cmd;
			ob_start();
			system($cmd);
			$result = ob_get_clean();
			$rlog .= $result;
		}
		
		//echo $result;
	}
	
	//echo $dbbackupcmd;
	
	include($installfolder.'/model/sites.php');
	Sites::AutoClean();
	
	echo 'Restoring Drivers...';
	
	if ((PHP_OS == "Linux") || (PHP_OS == "Unix"))
	{
		//Run the process to install the driver...
		$cmd = "sudo /opt/ardi/drivers/ardidrivers localhost".$siteroot." install";
		ob_start();
		system($cmd);
		ob_get_clean();
	}
	
	//Restoring Profiles
	
	$rlog2 = ob_get_clean();
	
	file_put_contents($sitefolder.'/cache/restore.log',$rlog);
	file_put_contents($sitefolder.'/cache/restore.log2',$rlog2);
	
	if (file_exists($dbfile))
	{
		unlink($dbfile);
	}
		
	$background = true;
	include($installfolder.'/admin/tools/cacherebuild.php');
	
	exit;
	
?>