<?php

// VERSION=6

function get_app_master () {

	$src_dir = "/home/admin/apps";

	if (is_dir($dh) && ($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);
}

include((file_exists("/usr/local/php/beroConf.php") ? "/usr/local/php/beroConf.php" : "/usr/fallback/beroConf.php"));

$host = $_SERVER['HTTP_HOST'];

$userapp_master_dir = get_app_master();
$userapp_master_www = file_exists('/home/admin/apps/' . $userapp_master_dir . '/www/index.php');

if (($root_db = new beroConf("root")) && ($root_db->get("root", "userapp-enabled") == 1) && $userapp_master_www) {
	header('Location:http://' . $host . '/userapp/' . $userapp_master_dir . '/index.php');
}
elseif (file_exists("/usr/local/www/index.php")) {
	header('Location:http://' . $host . '/app/index.php');
} else {
	echo '
<html>
<head>
<title>beroWeb</title>
</head>
<body>
<h2>beroWeb</h2>
<hr>
<h3>Diagnostic:</h3>
<ul>
<li><a href="cgi-bin/info.sh">Info</a></li>
<li><a href="cgi-bin/phpinfo.php">PHPinfo</a></li>
</ul>
<h3>Configuration:</h3>
<ul>
<li><a href="cgi-bin/confdb.php">Configuration database</a></li>
<li><a href="cgi-bin/network-tool.php">Network tool</a></li>
</ul>
<h3>Firmware:</h3>
<ul>
<li><a href="cgi-bin/update-tool.php">Update tool</a></li>
</ul>
<h3>System:</h3>
<ul>
<li><a href="cgi-bin/reboot.php">Reboot</a></li>
</ul>
</body>
</html>';
}

?>
