<?php  

$DIR = "library/";

function get_file_list($dir)
{
	$dir = preg_replace("/\/$/","",$dir);
	$res = opendir($dir);

	if ($res == FALSE) return FALSE;

	while($file = readdir($res)) {
		if ($file[0] != '.' && !is_dir("$dir/$file")) {
			$files[] = $dir."/".$file;
		}
	}

	closedir($res);

	return $files;
}

function read_sensor_config($file)
{
	$res = file($file);

	while (list($index,$value) = each($res)) {
		if ($value[0] == '#') {
			continue;
		}

		list($name,$val) = split(' *= *',$value);

		$val = str_replace("\"","",$val);
		if ($name && $val) $values[$name]=$val;
	}

	return $values;
}

function load_sensors($dir)
{
	$dirlist = get_file_list($dir);

	if ($dirlist == FALSE) return FALSE;

	while (list($index,$file) = each($dirlist)) {
		$sensors[$file] = read_sensor_config($file);
	}

	return $sensors;
}

#$action = $_GET["action"];
#$file = $_GET["file"];

if (empty($action)) {
	$sensor_list = load_sensors($DIR);

	if ($sensor_list == FALSE) {
		print "Error getting sensor information.";
		exit;
	}

	while(list($index,$sensor) = each($sensor_list)) {
		print "<A href=$PHP_SELF?action=show&file=$index>".$sensor["name"]."</a><BR>\n";
	}
} else if($action == "show") {
	if ($file[0] == '/' || strstr($file,"..")) {
		print "Can't show file\n";
		exit;
	}

	$sensor = read_sensor_config($file);

	$keys = split(", *",$sensor["keys"]);
	$values = split(", *",$sensor["values"]);

	print "<center>";
	print $sensor["manufacturer"].": ".$sensor["name"]."<BR><p>\n";
	print "<table border = 1 cellpadding = 5><tr><th>".$sensor["keyunits"]."</th><th>".$sensor["valunits"]."</th></tr>\n";

	while (list($index,$key) = each ($keys)) {
		print "<tr><td align=center>$key</td><td align=center>$values[$index]</td></tr>\n";
	}

	print "</table>";
}

?>
