<?php

function get_app_master () {

	$src_dir = "/apps";

	if ($dh = opendir($src_dir)) {

		while (($dir = readdir($dh)) !== false) {
			if ($dir != "." && $dir != "..") {
				$file = $src_dir. "/" . $dir . "/VERSION";

				if ($fh = fopen($file, "r")) {

					$fconts = fread($fh, filesize($file));
					fclose($fh);

					if (strstr($fconts, "CLASS=master")) {
						closedir($dh);
						return($dir);
					}

				}
			}

		}

		closedir($dh);
	}

	return(NULL);
}

function get_app_slots ($appname) {

	$ret		= 0;
	$src_dir	= "/apps";

	if (!empty($appname)) {
		$file = $src_dir . "/" . $appname . "/VERSION";

		if ($fh = fopen($file, "r")) {

			$fconts = fread($fh, filesize($file));
			fclose($fh);

			preg_match("/SLOTS=([1-8])/", $fconts, $res);
			$ret = $res[1];
		}
	}

	return($ret);
}

function get_app_desc ($appname) {

	$ret		= 0;
	$src_dir	= "/apps";

	if (!empty($appname)) {
		$file = $src_dir . "/" . $appname . "/VERSION";

		if ($fh = fopen($file, "r")) {

			$fconts = fread($fh, filesize($file));
			fclose($fh);

			preg_match("/DESCRIPTION=\"(.*)\"\n/", $fconts, $res);
			$ret = $res[1];
		}
	}

	return($ret);
}

function get_app_vers ($appname) {

	$ret		= 0;
	$src_dir	= "/apps";

	if (!empty($appname)) {
		$file = $src_dir . "/" . $appname . "/VERSION";

		if ($fh = fopen($file, "r")) {

			$fconts = fread($fh, filesize($file));
			fclose($fh);

			preg_match("/VERSION=([0-9]*)/", $fconts, $res);
			$ret = $res[1];
		}
	}

	return($ret);
}

function print_form_uins ($name) {

	$ret =	"<form action=\"index.php\" name=\"app_uins\" method=\"POST\">\n" .
		"<input type=\"hidden\" name=\"app_name\" value=\"" . $name . "\" />\n" .
		"<input type=\"submit\" name=\"uninstall\" value=\"uninstall\" />\n" .
		"</form>\n";

	return($ret);
}

function get_installed_apps () {

	$apps = array();

	if ($dh = opendir("/apps")) {
		while (($file = readdir($dh)) !== false) {
			if ($file != ".." && $file != ".") {
				$tmp[name] = $file;
				$tmp[slot] = get_app_slots($file);
				$tmp[desc] = get_app_desc($file);
				$tmp[vers] = get_app_vers($file);
				$apps[] = $tmp;
			}
		}

		closedir($dh);
	}

	$ret =	"<table class=\"contenttoc\" width=\"95%\">\n" .
		"<tr style=\"background-color: rgb(235,223,223);\">\n" .
		"<th colspan=\"5\">Installed Apps</th>\n" .
		"<tr>\n" .
		"<tr style=\"background-color: rgb(240,240,236);\">\n" .
		"<td width=\"4\"><b>Slot</b></td>" .
		"<td width=\"8\"><b>Name</b></td>" .
		"<td width=\"2\"><b>Version</b></td>" .
		"<td><b>Description</td>" .
		"<td width=\"8\"><b>Action</b></td>\n" .
		"<tr>\n";

	for ($i = 0; $i < 8; $i++) {

		$name = strlen($apps[$i][name]) != 0 ? $apps[$i][name] : "free";
		$link = ($name == "free") ? "free" : "<a href=\"" . $name . "\">" . $name . "</a>";
		$uins = ($name == "free") ? "" : print_form_uins($name);

		$ret .= "<tr>\n" .
			"<td>" . ($i + 1) . "</td>" .
			"<td rowspan=\"" . $apps[$i][slot] . "\">". $link . "</td>" .
			"<td rowspan=\"" . $apps[$i][slot] . "\">". $apps[$i][vers] . "</td>" .
			"<td rowspan=\"" . $apps[$i][slot] . "\">". $apps[$i][desc] . "</td>" .
			"<td rowspan=\"" . $apps[$i][slot] . "\">" . $uins . "</td>" .
			"</tr>\n";

		if ($apps[$i][slot] > 1) {
			for ($j = 1; $j < $apps[$i][slot]; $j++) {
				$ret .= "<tr>\n" .
					"<td>" . ($i + $j + 1) . "</td>" .
					"</tr>\n";
			}

			$i += $j - 1;
		}
	}

	$ret .= "</table>\n";

	return($ret);
}

@session_start();
if (!isset($_SESSION['beroari_time'])) {
	header("Location:/app/berogui/includes/login.php");
} elseif ((isset($_SESSION['beroari_time'])) && (($_SESSION['beroari_time'] + 1200) < time())) {
	session_unset();
	session_destroy();
	header("Location:/app/berogui/includes/login.php");
}

$_SESSION['beroari_time'] = time();

if (isset($_POST['uninstall'])) {
	exec("/usr/local/sbin/userapp_uninstall.sh " . $_POST['app_name']);
}

echo	"<xml version=\"1.0\" encoding=\"UTF-8\">\n" .
	"\t<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" .
	"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n" .
	"\t<head>\n" .
	"\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" .
	"\t\t<link type=\"text/css\" href=\"css/beroApp.css\" rel=\"Stylesheet\" />\n" .
	"\t\t<link rel=\"icon\" href=\"/app/berogui/includes/images/favicon.ico\" type=\"image/x-icon\" />\n" .
	"\t\t<title>bero*fix UserApp Management</title>\n" .
	"\t</head>\n" .
	"\t<body>\n" .
	"\t\t<div class=\"main\">\n" .
	"\t\t\t<div class=\"top\"><img src=\"/app/berogui/includes/images/bg_top.png\" /></div>\n" .
	"\t\t\t<div class=\"left\">\n" .
	"\t\t\t\t<div>Go to <a href=\"/app/berogui\">berogui</a></div>\n" .
	"\t\t\t\t<h1>bero*fix UserApp Management</h1>\n" .
	get_installed_apps() .
	"\t\t\t</div>\n" .
	"\t\t\t<div class=\"bottom\"><img src=\"/app/berogui/includes/images/bg_bottom.png\" /></div>\n" .
	"\t\t</div>\n" .
	"\t</body>\n" .
	"</html>";
?>
