<?php
require("../../inc/header.inc");
$helpId = isset($helpId)?$helpId:'5.1.6';
$ThisPage="$strMaintenance&nbsp;>&nbsp;$strBackupRestore&nbsp;>&nbsp;"."Download&Upload";
$DEBUG = 0;
$pubDb=$omc_db;
?>

<body leftmargin=12 topmargin=5>
<?php
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "$ThisPage";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
$serverip =  getenv("HTTP_HOST"); 
$download=$_REQUEST['download'];
if( $serverip == "" ){ 
	$serverip = getenv("SERVER_ADDR"); 
}
$downLinkStr = '';
$tgzName = "allBackup.tgz";
$downurl = "http://$serverip/db_backup/archive/tgzFile/$tgzName";
$downFileName = "/usr/local/apache/htdocs/db_backup/archive/tgzFile/$tgzName";
if (file_exists($downFileName))
{
    $lastModifiedTime = date("Y-m-d  H:i:s", filemtime($downFileName));
	$downLinkStr = "<a href=\"$downurl\"><b>Download tgz File</b></a><font color=\"blue\"> [$lastModifiedTime]</font>";
}

?>
<?php
	if(!isset($download) && !isset($upload))
	{
		$download=1;
	}
	if($download==1)
	{
		$downBackColorStr="background:yellow";
		$uploadBackColorStr="";
	}
	if($upload==1)
	{
		$downBackColorStr="";
		$uploadBackColorStr="background:yellow";
	}
	
	$downId = "DownLoadAll";
	$upId 	= "UploadAll";
	echo "<table border=\"0\" width=\"20%\">";
	echo "<tr>";
	echo "<td width=\"10%\">";
	echo "<span id=\"download\" style=\"cursor:pointer;$downBackColorStr\" onclick=\"javascript:showItem(this,'$downId','$upId')\"><font color=\"blue\"><b>Download</b></font></span>";
	echo "</td>";
	echo "<td width=\"10%\">";
	echo "<span id=\"upload\" style=\"cursor:pointer;$uploadBackColorStr\" onclick=\"javascript:showItem(this,'$downId','$upId')\"><font color=\"blue\"><b>Upload</b></font></span>";
	echo "</td>";
	echo "</tr>";
?>
<?php

$selSql = " select * from OMC_PUB.elementExistConf where existFlag=1 and elementTypeNo!=0 order by elementTypeNo";
$exitRes = mysqli_query($pubConn,$selSql);
while($exitRows = mysqli_fetch_array($exitRes))
{
	$typeNo = $exitRows[elementTypeNo];
	if($typeNo==320)
	{
		$typeNo=322;
		$allSysArr[] = array('typeNo' => 325,	'name' => 'VLR');
	}
	$name = $exitRows[elementName];
	$allSysArr[] = array('typeNo' => $typeNo,	'name' => $name);
}

for($i=0; $i<sizeof($allSysArr); $i++)
{
	$tmpSysTypeNo = $allSysArr[$i][typeNo];

	$selSql = "select sysConf.sysTypeName as name,sysInfo.ip as ip 
				from sysConf,sysInfo 
				where (sysConf.sysTypeNo=$tmpSysTypeNo and sysConf.sysTypeNo=sysInfo.sysTypeNo )";
	$Result = mysqli_query($pubConn,$selSql);
	$Rows = mysqli_fetch_array($Result);
	if($DEBUG)echo "<br>$pubDb,$selSql";
//	$allSysArr[$i][name] = $Rows[name];
	$allSysArr[$i][ip] = $Rows[ip];
}
	
if($download==1)
{
	echo "<table  id=\"$downId\" style=\"display:\" rules=rows border=\"1\" width=\"60%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";	
	echo "<th colspan=\"2\">System Summary</th>";
	echo "<tr>";
	echo "<td width=\"20%\"><b>System Name</b></td>";
	echo "<td width=\"40%\"><b>Ip Address</b></td>";
	echo "</tr>";
	for($i=0; $i<sizeof($allSysArr); $i++)
	{
		$ip = $allSysArr[$i][ip];
		if(strlen($ip)<7) continue;
		$tmpSysTypeNo = $allSysArr[$i][typeNo];
		echo "<tr>";
		echo "<td>{$allSysArr[$i][name]}</td>";
		echo "<td>$ip</td>";
		echo "</tr>";
	}
	echo "<tr>";
	echo "<td align=\"left\" title=\"update all database and create an new .tag file\" id=\"$id\" style=\"cursor:pointer\" onclick=\"javascript:doDownLoad();\"><font color=\"blue\"><b>Create Tgz File</b></font>";
	echo "<td id=\"downLink\">$downLinkStr</td>";
	echo "</tr>";
	echo "</table>";
}//end download

if($upload==1)
{
?>
<table></table>
<form id="<?php echo $upId;?>" method ="POST" action="<?php echo"uploadFile.php";?>"  enctype="multipart/form-data">

File to upload:
<input type="file" name="inputFile">
<input type="hidden" name="MAX_FILE_SIZE" value="20971520"><!-- MAX_FILE_SIZE = 20M -->
<input type="submit" value="Upload">
</form> 


<?php
}//end upload show

	echo "<br><span id=\"statusPos\"></span>";

?>

<script language="JavaScript">
	function showItem(item,downId,upId)
	{
		var downDisp,upDisp;
		if(item.id=='download')
		{
			document.getElementById("download").style.background="yellow";
			document.getElementById("upload").style.background="";
			href = "<?php echo $PHP_SELF;?>"+"?download=1";
		}
		else
		{
			document.getElementById("download").style.background="";
			document.getElementById("upload").style.background="yellow";
			href = "<?php echo $PHP_SELF;?>"+"?upload=1";
		}
		window.location = href;
	}
	function doDownLoad(flg,curSys)
	{
		window.location="downloadFile.php";
	}
</script>
