<?php
chdir("..");

include "./includes/variables.php";
include "./misc/database.php";
$ba= new beroAri();
$res=$ba->select("select show_app from set_app");
if (sqlite_fetch_single($res)==1){
include '/usr/local/php/beroConf.php';
$image_dir = "/mnt/images";
$bc = new beroConf('root');

echo '
<html><head><title>beroWeb: Update tool</title>
<link rel="stylesheet" type="text/css" href="beroWeb_css.css" />
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
</head>
<body>
<br><br>
<h2>beroWeb: Update tool</h2>
<hr>';

switch ($_GET['action']) {
case 'delete':
	$file = $image_dir . '/' . $_GET['file'];
	if (strlen($file) != 0 && file_exists($file) && unlink($file))
		echo '<p>File was successfully deleted.</p>';
	else
		echo '<p>Failed to delete file!</p>';
	break;
case 'upload':
	$upload_file = $image_dir . '/' . basename($_FILES['userfile']['name']);
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_file))
		echo '<p>File was successfully uploaded.</p>';
	else
		echo '<p>File upload failed!</p>';
	break;
case 'install':
case 'reinstall':
	$file = $_GET['file'];
	echo '<p><h5>You are about to ' . $_GET['action'] . ' a firmware image.<br>';
	echo 'This will stop all running services and restart them after the installation<br>';
	echo 'has finished.</h5></p>';
	echo '<p><h3>Are you sure you want to ' . $_GET['action'] . ' <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=application.php>No</a></b> ";
	echo "<b><a href=application.php?action=do_install&file=$file>Yes</a></b></p>";
	echo '<hr size= \"1\" noshade>
		<br><a href="http://www.beronet.com" target="_blank" onfocus="if (this.blur()) { this.blur(); }"><img src="images/beroLogo.gif" align="right" border="0"></a>
		</td></tr>
		<tr>
		<td colspan="2" style="background-repeat:no-repeat" background="images/bg_bottomindex.png"><img src="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>';
	exit(0);
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/local/sbin/install.sh $file", $retval);
		echo '</pre>';
	}
	break;
}

$files = 0;

switch (1) {
default:
	if (!$bc->valid()) {
		echo "<h3>Error:</h3><b>Failed to connect to config database!</b><br>";
		break;
	}
	if (!is_dir($image_dir) || !($dh = opendir($image_dir))) {
		echo "<h3>Error:</h3><b>Failed to read $image_dir!</b><br>";
		break;
	}
	
	if (strcmp("running", $bc->get("root", "app-status")) == 0)
		$current = $bc->get("root", "app-image");

	echo '<h3>Available image files:</h3>';
	while (($file = readdir($dh)) != false) {
		if (strlen($file) > 7 && substr_compare($file, ".tar.gz", -7, 7) == 0) {
			$files++;
			if ($files == 1)
				echo '<table border="0" cellpadding="5">';
			$is_current = strcmp($file, $current) == 0;
			echo '<tr>';
			echo "<td><a href='application.php?action=delete&file=$file'>delete</a></td>";
			if ($is_current)
				echo "<td><a href='application.php?action=reinstall&file=$file'>reinstall</a></td>";
			else
				echo "<td><a href='application.php?action=install&file=$file'>install</a></td>";
			echo "<td><b>$file</b></td>";
			if ($is_current)
				echo "<td><font color='red'>(active)</font></td>";
			else
				echo "<td></td>";
			echo '</tr>';
		}
	}
	if ($files > 0)
		echo '</table>';
	if ($files == 0)
		echo "<p>none.</p>";
	
	echo '<h3>Upload new image file:</h3>';
	if ($files > 4) {
		echo "<p><b>Image file capacity exceeded!</b><br>Please delete one of the above image files before uploading a new one.</p>";
		break;
	} else {
		echo '<form enctype="multipart/form-data" action="application.php?action=upload" method="POST">';
		echo '<input type="hidden" name="MAX_FILE_SIZE" value="52428800" />';
		echo 'Image file: <input name="userfile" type="file" />';
		echo '<input type="submit" value="Upload file" />';
		echo '</form>';
	}
}
echo "<br/><h2>Backup and restore:</h2><hr/><table>
            <tr>
	            <th nowrap>&nbsp;Download configuration's databases&nbsp;</th>
            	<th><a href='files_download.php')\">download</a></th>
            </tr>
            </table>
			<br/><br/>
			<form name='upload' action='upload.php' method='POST' ENCTYPE=\"multipart/form-data\">
			<table class='contenttoc'>
			<tr>
				<th>&nbsp;Restore&nbsp;configuration's&nbsp;databases (beroGui.tar.gz)&nbsp;</th>
				<th><input type='file' size='28' name='uploadfile' ></th>
			</tr>
			<tr>
				<td colspan='2'>
					<input type='submit' value='Upload'	onclick=\"if(confirm('With this action the whole configuration will be changed. Do you want to proceed?') == false){return false;}\">&nbsp;&nbsp;
					<input type='reset' value='Reset'>
				</td>
			</tr>
			</table>
			</form>
			<br/>";
			
if ($_POST['rck']=='Restart Applications'){
		exec("/usr/local/init/rcK",$ausgabe);
		exec("/usr/local/init/rcS",$ausgabe);
		echo"<br/><br/><form name='restart' method='POST'>";
		echo "<input type='submit' name='rck' value=\"Restart Applications\"></form>";
		foreach ($ausgabe as $key=>$value){
			echo $value."<br/>";
		}
	}else{
		echo "<br/><br/><form name='restart' method='post'>";
		echo "<input type='submit' name='rck' value=\"Restart Applications\"></form>";
	}
	
	echo "<br/><br/>";
	(strcmp("do_reboot", $_GET['v']) == "No" ? print( '<meta http-equiv="Refresh" content="10; url=./misc/application.php">') : print(''));
		//Die Tabelle Infos
		if ($_POST['v']=="Yes"){
			echo '<h5>Rebooting...</h5>';
			exec ('/sbin/reboot');
		}
		echo "Are you sure you want to reboot now?<br/>
			(This may take several minutes)<br/><br/><br/>
			<form class='beroari_form' action='' method='POST' name='reboot'>
			<input type='submit' name='v' value='Yes'>&nbsp;<input type='submit' value='No'>
			</form>";
		
echo '</body></html>';
}else{
	echo "<h2>Access Denied</h2>";
}
?>
