<?php
if (file_exists("/usr/local/www/index.php")){
	$host = $_SERVER['HTTP_HOST'];
	header('Location:http://' . $host . '/app/berogui/index.php');
} else {
$image_dir = "/tmp/images";
$show=true;

echo '
<html><head><title>beroWeb: Update tool</title>
<link rel="stylesheet" type="text/css" href="cgi-bin/beroWeb_css.css" />
<link rel="icon" href="cgi-bin/images/favicon.ico" type="image/x-icon">
</head>
<body>
<br><table align="center" bgcolor="#f0f0ec" border="0" cellpadding="0" cellspacing="0" width="900">
<tbody><tr>
<td colspan="2"><img src="cgi-bin/images/bg_topindex.png"></td>
</tr>
<tr>
<td background="cgi-bin/images/bg_left.png" width="30"><img src="cgi-bin/images/trans.gif" height="10" width="39"></td>
<td><br>
<h2>beroWeb Recovery GUI</h2>
<hr>
<b><h3>Update Tool</h3></b>';

echo '<table class="contenttoc" width="55%">';

switch ($_GET['action']) {

case 'upload':
	if (is_dir($image_dir) && ($dh = opendir($image_dir))) {
		while (($file = readdir($dh)) != false) {
			if (strlen($file) > 7 && substr_compare($file, ".tar.gz", -7, 7) == 0) {
				$file_ = $image_dir . '/' . $file;
				if (strlen($file_) != 0 && file_exists($file_)) unlink($file_);
			}
			if ($file=="content.txt"){
				$file_ = $image_dir . '/' . $file;
				if (strlen($file_) != 0 && file_exists($file_)) unlink($file_);
			}
		}
	}
	$split_ext=explode(".",$_FILES['userfile']['name']);

	if ($split_ext[count($split_ext)-1]=="gz" && $split_ext[count($split_ext)-2]=="tar" && count($split_ext)>=3){
		$upload_file = $image_dir . '/' . basename($_FILES['userfile']['name']);
		if ((filesize($_FILES['userfile']['tmp_name'])/1024)/1024>7.9){
			echo '<tr><td><font color="red">File size limit exceeded!</font></td></tr>';
		}else{
			if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_file)){
				$file = basename($_FILES['userfile']['name']);
				echo '<tr><td style="width:500px"><p><h5>You are about to install a firmware image.
						<br>This will stop all running services and restart them after the installation
						<br>has finished.</h5></p>';
				echo '<p><h3>Are you sure you want to install <font color="red">' . $file . '</font>?</h3></p>';
				echo '<p><h5>(This may take several minutes, please only click once).</h5></p>';
				echo "<p><b><a href=index.php>No</a></b> ";
				echo "<b><a href=index.php?m=up_tool&action=do_install&file=$file>Yes</a></b></p></td></tr></table>";
				$show=false;
			}else{
				echo '<tr><td>File upload failed!</td></tr>';
			}
		}
	}else{
		echo '<tr><td><font color="red">Wrong file extension!<br/>File upload failed!</font></td></tr>';
	}
	break;
case 'do_install':
	system("ps ax|grep -v grep|grep install.sh > /dev/null", $retval);
	if ($retval == 0)
		break;
	$file = $_GET['file'];
	if (strlen($file) != 0 && file_exists($image_dir . '/' . $file)) {
		echo '<p><b>Install log:</b></p>';
		echo '<pre>';
		system("/usr/fallback/install.sh $file", $retval);
		echo '</pre>';
	}
	break;
}


switch ($show) {
case true:
	if (!is_dir($image_dir) || !($dh = opendir($image_dir))) {
		echo "<h3>Error:</h3><b>Failed to read $image_dir!</b><br>";
		break;
	}


	echo '<tr><td><font size="3"><b>Upload new image file:</b></font></td></tr>';
	echo '<form class="network" enctype="multipart/form-data" action="index.php?action=upload" method="POST">';
	echo '<tr><td>Image file: <input name="userfile" type="file">';
	echo '<input type="submit" value="Upload file"></td></tr>';
	echo '</form></table>';
	break;
}


echo '<hr size= \"1\" noshade>
<br><a href="http://www.beronet.com" target="_blank" onfocus="if (this.blur()) { this.blur(); }"><img src="cgi-bin/images/beroLogo.gif" align="right" border="0"></a>
</td></tr>
<tr>
<td colspan="2" style="background-repeat:no-repeat" background="cgi-bin/images/bg_bottomindex.png"><img src="cgi-bin/images/trans.gif" border="0" height="39" width="10"><div style="color: white;" align="right">&nbsp;&nbsp;&nbsp;</div>
</td></tr>
</tbody>
</table>';
echo '</body></html>';
}
?>
