<?php
#VERSION=3
#CHANGES: use /tmp/images

include '/usr/fallback/beroConf.php';
$image_dir = "/tmp/images";
system("mkdir -p ".$image_dir);
$bc = new beroConf('root');

echo '
<html><head><title>beroWeb: Update tool</title></head>
<body>
<h2>beroWeb: Update tool</h2>
<hr>
<a href="../index.php">Back</a>';

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=update-tool.php>No</a></b> ";
	echo "<b><a href=update-tool.php?action=do_install&file=$file>Yes</a></b></p>";
	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/fallback/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 ( $file != "." && $file != "..") {
			$files++;
			if ($files == 1)
				echo '<table border="0" cellpadding="5">';
			$is_current = strcmp($file, $current) == 0;
			echo '<tr>';
			echo "<td><a href='update-tool.php?action=delete&file=$file'>delete</a></td>";
			if ($is_current)
				echo "<td><a href='update-tool.php?action=reinstall&file=$file'>reinstall</a></td>";
			else
				echo "<td><a href='update-tool.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="update-tool.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 '</body></html>';
?>
