<?php
require("../../inc/header.inc");
require("../../configuration/sub_inc/sub_config.inc");
?>

<body onload="javascript:adjust();" onresize="javascript:adjust();" leftmargin="15" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">
<script language="javascript">
function select_bk_file(fname)
{
	document.getElementById('select_file_text').value=fname;
	document.getElementById('restore_bt').disabled=0;
}
function backup_opt()
{
	var res=confirm("Do you want to backup the configuration file from MSS");
	if(res == false)
		return;
	
	window.location.href="./mss_backup_restore.php?backup_file_flag=1";
}
function restore_opt(fname)
{
	var res=confirm("Do you want to restore configuration file to MSS, the operation will restart the MSS");
	if(res == false)
		return;
		
	window.location.href="./mss_backup_restore.php?restore_file_flag=1&backup_filename="+fname;
}
function download_file(fname)
{
	window.location.href="./output_file.php?directory=/usr/local/apache/htdocs/ftpFile&filename="+fname;
}
</script>

<?php
adjust_head();
adjust_title_head();
echo "<FORM NAME=\"myfprm\" action=\"./raidStatus.php\" method=\"post\">";
echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
	echo "<tr>";
		echo "<td width=\"50%\">";
		echo "Maintenance > Backup & Restore > Configuration";
		echo "</td>";
		echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
		$helpId="5.1.6";
		showHelp($helpId);
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td width=\"50%\">";
		echo "<br>";
		
		echo "&nbsp;&nbsp;&nbsp;&nbsp;";
		//echo "<input type=\"button\" class=\"button\" value=\"Restore\" id=\"restore_bt\" onclick=\"javascript:restore_opt();\" disabled title=\"Select a backup file to restore\">";
		//echo "From file";
		//echo "<input type=\"text\" style=\"vertical-align:top;background-color:#cecece;\" class=text readonly id=\"select_file_text\" size=30 readonly title=\"Select a backup file to restore\">";
		
		echo "</td>";
		echo "<td width=\"50%\" align=\"right\">";
		echo "<input type=\"button\" class=\"button\" value=\"Backup\" id=\"backup_bt\" onclick=\"javascript:backup_opt();\">";
		echo "&nbsp;";
		echo "</td>";
	echo "</tr>";
echo "</table>";
adjust_title_tail();
adjust_content_head();

$ftpinfo=getMSSFtpAccount();
$selSql = "SELECT ip FROM sysInfo where sysTypeNo=388 order by updateTime desc ";
$selRes = @mysqli_query($pubConn,$selSql);
$num=mysqli_num_rows($selRes);
$selRow = @mysqli_fetch_array($selRes);
$mss_ip=$selRow[ip];

if($backup_file_flag == 1)
{
	echo "<script language=\"javascript\">\n";
	echo "javascript:adjust();\n";
	echo "</script>\n";
	
	if($num == 0)
	{
		echo "Can not get IP of MSS<br>";
	}
	else
	{
		//oid 1.3.6.1.4.1.1373.2.3.3.99.2.1.1, value=6
		$sql="replace into OBJ_388.param_99(subSysNo,paramType,level_1,level_2,level_3,instanceNo,sortNo,valueType,operType,length,initValue)values(99,0,2,1,1,0,0,4,0,1,'06') ";
		mysqli_query($pubConn,$sql);
		
		$commIdStr=oidSysCommSend("388_99_99",3,"2.1.1",-1,0);
		//checkState($commIdStr,"./mss_backup_restore.php");
		flush();
		
		for($t=0;$t<4;$t++)
		{
			flush();
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">"; 
			ob_flush();
			flush();
			sleep(1);
		}
		/* close ftp
		$conn_id=ftp_connect($mss_ip);
		if($conn_id == FALSE)
		{
			echo "<b>FTP:connect to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
		*/
		
		$connection=sftp_connect($mss_ip);
		if(!$connection)
		{
			echo "<b>SFTP:connect to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
		
		/* close ftp
		if(FALSE == ftp_login($conn_id,$ftpinfo[UserName],$ftpinfo[Password]))
		{
			echo "<b>FTP:login to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
		*/
		
		$key=sftp_login($connection,$ftpinfo[UserName],$ftpinfo[Password]);
		if(!$key)
		{
			echo "<b>SFTP:login to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
		
		$local_file="/usr/local/apache/htdocs/ftpFile/mssConf".date("YmdHis").".tar";
		$remote_file="/usr/local/subsManage/conf.bk.tar";
		/* close ftp
		if(FALSE == ftp_get($conn_id,$local_file,$remote_file,FTP_BINARY))
		{
			echo "<b>FTP:Get file $remote_file <font color=red>failed</font>.</b><br>";
			exit();
		}
		ftp_close($conn_id);
		*/
		
		//if(!sftp_get($key,$local_file,$remote_file,SFTP_ASCII))
		if(!sftp_get($connection,$local_file,$remote_file,SFTP_ASCII))
		{
			echo "<b>SFTP:Get file $remote_file <font color=red>failed</font>.</b><br>";
			exit();
		}
		
		insertLog("Backup mss configuration file conf.bk.tar");
	}
}
else if($restore_file_flag == 1)
{
	echo "<script language=\"javascript\">\n";
	echo "javascript:adjust();\n";
	echo "</script>\n";
	if($num == 0)
	{
		echo "Can not get IP of MSS<br>";
	}
	else
	{
		$original_file="/usr/local/apache/htdocs/ftpFile/$backup_filename";
		$ftp_file="/usr/local/apache/htdocs/ftpFile/conf.bk.tar";
		system("cp -f $original_file $ftp_file");
		system("chmod 777 $ftp_file");
		
		/* close ftp
		$conn_id=ftp_connect($mss_ip);
		if($conn_id == FALSE)
		{
			echo "<b>FTP:connect to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
		*/
		
		$connection=sftp_connect($mss_ip);
		if(!$connection)
		{
			echo "<b>SFTP:connect to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
	
		/* close ftp
		if(FALSE == ftp_login($conn_id,$ftpinfo[UserName],$ftpinfo[Password]))
		{
			echo "<b>FTP:login to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
		*/
		
		$key=sftp_login($connection,$ftpinfo[UserName],$ftpinfo[Password]);
		if(!$key)
		{
			echo "<b>SFTP:login to $mss_ip <font color=red>failed</font>.</b><br>";
			exit();
		}
		
		$local_file=$ftp_file;
		$remote_file="/usr/local/subsManage/conf.bk.tar";
		
		/* close ftp
		if(FALSE == ftp_put($conn_id,$remote_file,$local_file,FTP_BINARY))
		{
			echo "<b>FTP:Put file $original_file <font color=red>failed</font>.</b><br>";
			exit();
		}
		ftp_close($conn_id);
		*/
		
		if(!sftp_put($key,$remote_file,$local_file,SFTP_ASCII))
		{
			echo "<b>FTP:Put file $original_file <font color=red>failed</font>.</b><br>";
			exit();
		}
		
		unlink($ftp_file);
		
		//oid 1.3.6.1.4.1.1373.2.3.3.99.2.1.1, value=7
		$sql="replace into OBJ_388.param_99(subSysNo,paramType,level_1,level_2,level_3,instanceNo,sortNo,valueType,operType,length,initValue)values(99,0,2,1,1,0,0,4,0,1,'07') ";
		mysqli_query($pubConn,$sql);
		
		$commIdStr=oidSysCommSend("388_99_99",3,"2.1.1",-1,0);
		//checkState($commIdStr,"./mss_backup_restore.php");
		
		insertLog("Restore mss configuration file from $local_file");
	}
}
else if($download_file_flag == 1)
{
	header("Content-type: application/csv");
	header("Content-Disposition: attachment; filename=\"{$backup_filename}\"");
		
		//use for download csv in https
	header('Expires: 0'); 
	header('Pragma: public'); 
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
	header('Cache-Control: public'); 

	readfile("/usr/local/apache/htdocs/ftpFile/{$backup_filename}");
}

//Get the backup file list
$dir="/usr/local/apache/htdocs/ftpFile/";
$handle=opendir($dir);
if($handle == FALSE)
{
	echo "Can not open dir $dir<br>";
	exit();
}

$filecount=0;
while (($file = readdir($handle)) !== false) 
{
	if(strstr($file,"mssConf"))
	{
		$fileList[FullName][$filecount]=$dir.$file;
		$fileList[FileName][$filecount]=$file;
		$fileList[FileSize][$filecount]=filesize($dir.$file);
		$fileList[FileMTime][$filecount]=date("Y-m-d H:i:s",filemtime($dir.$file));
		$filecount++;
	}
}

if($filecount > 0)
	arsort($fileList[FileMTime]);

if(0)
{
echo "<pre>";
print_r($fileList);
echo "</pre>";
}

closedir($handle);



echo "<br><br>";
echo "<table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#ffffff\">";
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td width=\"35%\">MSS Configuration Backup File</td>";
echo "<td width=\"15%\" align=\"center\">File Size</td>";
echo "<td width=\"20%\" align=\"center\">Backup Time</td>";
echo "<td width=\"15%\" align=\"center\">Download</td>";
echo "<td width=\"15%\" align=\"center\">Upload</td>";
echo "</tr>";
echo "</table>";
echo "<table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
echo "<tr>";
echo "<td width=\"35%\"><img src=\"../../images/line.gif\"></td>";
echo "<td width=\"15%\">&nbsp;</td>";
echo "<td width=\"20%\">&nbsp;</td>";
echo "<td width=\"15%\">&nbsp;</td>";
echo "<td width=\"15%\">&nbsp;</td>";
echo "</tr>";

echo "<tr>";
echo "<td width=\"35%\"><img src=\"../../images/midminus.gif\"><img src=\"../../images/open.gif\"></td>";
echo "<td width=\"15%\">&nbsp;</td>";
echo "<td width=\"20%\">&nbsp;</td>";
echo "<td width=\"15%\">&nbsp;</td>";
echo "<td width=\"15%\">&nbsp;</td>";
echo "</tr>";


if($filecount > 0)
{	
	$i=0;
	foreach($fileList[FileMTime] as $key => $value)
	{
		echo "<tr>";
		echo "<td width=\"35%\"><img src=\"../../images/blank.gif\">";
		if($i == $filecount - 1)
			echo "<img src=\"../../images/lastblk.gif\">";
		else
			echo "<img src=\"../../images/midblk.gif\">";
		echo "<a href=#>";
		echo "<img border=0 src=\"../../images/zip_file.gif\">{$fileList[FileName][$key]}&nbsp;";
		echo "</a>";
		echo "</td>";
		echo "<td width=\"15%\" align=\"center\">{$fileList[FileSize][$key]}&nbsp;</td>";
		echo "<td width=\"20%\" align=\"center\">{$fileList[FileMTime][$key]}&nbsp;</td>";
		echo "<td width=\"15%\" align=\"center\"><a href=# onclick=\"javascript:download_file('{$fileList[FileName][$key]}');\">Download</a></td>";
		echo "<td width=\"15%\" align=\"center\"><a href=# onclick=\"javascript:restore_opt('{$fileList[FileName][$key]}');\">Upload</a></td>";
		echo "</tr>";
		$i++;
	}
}

echo "</table>";


adjust_content_tail();

?>
