<?php
//
// Created on: 11 Sep 2001
// Usage: cfg_download.php?[language="eng|gb"]
//                        &bssid=0
//                        &version=iw05_01.048
//                        &tarfile=/usr/local/apache/htdocs/bss_db/bss0_20010914_0.tgz
// 
// Function: Transfer BSS software from OMC Server
//           to BSC harddisk based on bssdist.fls file
//
//
//
//
// Remarks:
//     - Use explode() as a faster split()
//     - Function like tar, mount, umount return 0 if there is no error
//
?>
<?php
include("nocache.inc");
include("header.inc");
include("ip2name.inc");
include("read_bssdist.inc");
include("cfg_download.inc");

//$debug=1;

if (!isset($language))
	$language="eng";
$parameter="${language}namearray";


if (!isset($version) || !isset($bssid))
{
	echo "<font color=red size=+1>{${$parameter}["err_msg"]}</font>";
	return;
}
if ($version=='')
{
	include("bssversion.inc");
}

/*****************************************
* Locate tar file if it is not given
*
*****************************************/
if (!isset($tarfile))
{
	echo "<HTML>\n";
?>

<SCRIPT language="javascript" type="text/javascript">
<!--
	function submit_check(myForm)
	{
		if (myForm.selectfile.value == "")
		{
			alert("Please select a backup file");
			return (false);
		}
		myForm.tarfile.value = "/usr/local/apache/htdocs/bss_db/" + myForm.selectfile.value;
		return (true);
	}
//-->
</SCRIPT>

<?php
	echo "<font color=blue size=+1>{${$parameter}["availfile"]}:</font>";
	echo "<HR>\n";
	echo "<PRE>\n";
	if (is_dir("/usr/local/apache/htdocs/bss_db"))
	{
		//$backuplist = Array();
		exec("ls -1t /usr/local/apache/htdocs/bss_db",$backuplist,$retval);
		$backupcount = count($backuplist);
		if ($backupcount <= 0)
			echo "{${$parameter}["no_backup"]}\n";
		else
			system("ls -1lht /usr/local/apache/htdocs/bss_db");
			
	}
	else
	{
		echo "{${$parameter}["no_backup"]}\n";
	}
	echo "</PRE>\n";



	echo "<BR>\n";
	echo "<FORM action=cfg_download.php method=post onSubmit='return submit_check(this);'>\n";
	echo "<font color=blue size=+1>{${$parameter}["selectfile"]}: </font>";
	echo "<SELECT name=selectfile size=1 style='width: 200'>\n";
	echo "<OPTION value=''></OPTION>\n";
	foreach ($backuplist as $value)
		echo "<OPTION value=\"$value\">$value</OPTION>\n";
	echo "</SELECT>\n";
	echo "<HR>\n";
	echo "<INPUT type=hidden name=language value=$language>\n";
	echo "<INPUT type=hidden name=bssid value=$bssid>\n";
	echo "<INPUT type=hidden name=version value=$version>\n";
	echo "<INPUT type=hidden name=tarfile>\n";

	echo "<INPUT type=submit value={${$parameter}["submit"]} style='width: 80'>\n";
	if (isset($bssid))
		echo "<INPUT type=button value='{${$parameter}[back]}' " .
		"onClick='javascript:parent.location.href=" .
		"\"modifyBssParam.php?language=$language&bssid=$bssid\"'>";
	echo "</FORM>\n";
	echo "</HTML>\n";
	return;
}


/******************************************
* Mount the BSC /hd0 to /tmp/$$ directory
*******************************************/
$tmpdir = sprintf("%.3f",time()/1000);
$iwbox = "bss${bssid}";
$ip = name2ip($iwbox);
$path_prefix = "tmp";

//---------------FTP BSS Config,EMS -> BSS-----------------------
$bscLoginName="www";
$bscLoginPassword="123456";
$remote_dir="/hd0/{$version}/compile/cfg";

flush();
echo "Please wait, the operation will take some time<br>";
ob_flush();
flush();

system("rm -rf /$path_prefix/$iwbox");
mkdir("/$path_prefix/$iwbox",0755);
system("cp $tarfile /$path_prefix/$iwbox");
chdir("/$path_prefix/$iwbox");
system("tar zxf *.tar.gz");
system("rm -rf *.tar.gz");


$conn_id=ftp_connect($ip);
if($conn_id == FALSE)
{
	echo "<b>FTP:connect to $ip <font color=red>failed</font>.</b><br>";
	back();
}
if(FALSE == ftp_login($conn_id,$bscLoginName,$bscLoginPassword))
{
	echo "<b>FTP:login to $ip <font color=red>failed</font>.</b><br>";
	back();
}


clearstatcache();
ftp_put_whole_dir("/$path_prefix/$iwbox",$remote_dir,$conn_id);

ftp_close($conn_id);

system("rm -rf /$path_prefix/$iwbox");

function ftp_put_whole_dir($local_dir,$remote_dir,$conn_id)
{
	if($handle = opendir($local_dir)) 
	{
		while(false !== ($file = readdir($handle))) 
		{
			if($file != "." && $file != "..") 
			{
				if(strlen(trim($file))<=0)
					continue;
				
				if(is_dir($file))
				{
					ftp_put_whole_dir($local_dir."/$file",$remote_dir."/$file",$conn_id);
				}
				else
				{
flush();
echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">"; 
ob_flush();
flush();
					
					ftp_put($conn_id,$remote_dir."/$file",$local_dir."/$file",FTP_BINARY);
					//echo "$local_dir $remote_dir $file<br>";
				}
			}
		}
		closedir($handle);
	}
}

echo "<br>Download completely<br>";
echo "</PRE>\n";
echo "<INPUT type=button value={${$parameter}["rebootbsc"]} " .
     "onClick='javascript:window.location.href=\"modify.php?bssid=$bssid&url=bssfunc.php&language=$language&nmicommand=action 7:2&objectid=Bsc.0\"'>";


exit();
//-------------------------------------------------------

/***************************************************************************************
* PHP state machine
*
* Rename original build and create directory
* Format: it is a 2D array
*	Array(
*         Array('function','description','next state if success','next state if fail'
*       )
****************************************************************************************/
$cmdlist = Array(
	's00' => Array ('system("/bin/ping -q -c 1 $ip >> /dev/null", $retval);return($retval);',
		"{${$parameter}["ping"]} $iwbox ($ip) ...",
		"kickout","s01"),

	's01' => Array ('return(is_dir("$path_prefix/$iwbox"));',
		"{${$parameter}["checkexist"]} $path_prefix/$iwbox",
		"s03","s02"),

	's02' => Array ('return(mkdir("$path_prefix/$iwbox",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox",
		"s03","halt"),

	's03' => Array ('system("umount $path_prefix/$iwbox", $retval);return($retval);',
		"",
		//"{${$parameter}["umounting"]} $path_prefix/$iwbox",
		"s04","s03"),

	's04' => Array ('system("mount $path_prefix/$iwbox", $retval);return($retval);',
		"{${$parameter}["mounting"]} $path_prefix/$iwbox",
		"halt","s40"),

	's40' => Array ('return(is_dir(sprintf("$path_prefix/$iwbox/%s/compile/cfg",strtoupper($version))));',
		"{${$parameter}["checkexist"]} $path_prefix/$iwbox/$version/compile/cfg",
		"s41","halt"),

	's41' => Array ('return(chdir (sprintf("$path_prefix/$iwbox/%s/compile/cfg",strtoupper($version))));',
		"{${$parameter}["chdir"]} $path_prefix/$iwbox/$version/compile/cfg",
		"s60","halt"),

	's60' => Array ('return(is_file("$tarfile"));',
		"{${$parameter}["checkexist"]} $tarfile",
		"s61","halt"),

	's61' => Array ('system("tar zxf $tarfile", $retval);return($retval);',
		"{${$parameter}["untar"]} $tarfile ...",
		"halt","cleanup"),

	"cleanup" => Array('return(is_dir("$path_prefix/$iwbox"));',
		"{${$parameter}["checkexist"]} $path_prefix/$iwbox",
		"chroot","getout"),

	'chroot' => Array ('return(chdir("/"));',
		"{${$parameter}["chdir"]} $path_prefix/$iwbox/$version/compile/cfg",
		"umount","getout"),

	"umount" => Array('system("umount $path_prefix/$iwbox", $retval);return($retval);',
		"{${$parameter}["umounting"]} $path_prefix/$iwbox",
		"umount","rmdir"),

	"rmdir" => Array('return(rmdir("$path_prefix/$iwbox"));',
		"{${$parameter}["rmdir"]} $path_prefix/$iwbox",
		"getout","getout")
);


if (isset($debug))
{
	foreach ($cmdlist as $key => $value)
	{
		echo "$key,$value[0],$value[1],$value[2],$value[3]<BR>";
	}
}


/*********************************************
 * Execute the state machine here
 *********************************************/
echo "<font color=red size=+1>{${$parameter}["heading"]}<BR></font>";
echo "<PRE>";
for ($state="s00" ; ; )
{
	if (isset($debug))
		echo "{$cmdlist[$state][0]}\n$state: ";

	echo "{${cmdlist}[$state][1]}";
	flush();
	if (@eval(${cmdlist}["$state"][0]))
		$state = $cmdlist[$state][2];
	else
		$state = $cmdlist[$state][3];


	if ($state == "getout")
	{
		echo " ... {${$parameter}["ok"]}\n";
		break;
	}
	else if ($state == "kickout")
	{
		echo " ... {${$parameter}["not_ok"]}\n";
		echo "</PRE><font color=red size=+1>... {${$parameter}["failed"]}<BR></font>";
		echo "<PRE>";
		break;
	}
	else if ($state == "halt")
	{
		echo " ... {${$parameter}["not_ok"]}\n";
		echo "</PRE><font color=red size=+1>... {${$parameter}["failed"]}<BR></font>";
		echo "<PRE>";
		$state = "cleanup";
	}
	else
	{
		if (${cmdlist}[$state][1] != "")
			echo " ... {${$parameter}["ok"]}\n";
	}
}

echo "</PRE>\n";
echo "<font color=blue>... {${$parameter}["done"]}<BR></font>";
echo "<INPUT type=button value={${$parameter}["rebootbsc"]} " .
     "onClick='javascript:window.location.href=\"modify.php?bssid=$bssid&url=bssfunc.php&language=$language&nmicommand=action 7:2&objectid=Bsc.0\"'>";


?>
