<?
	#---------------------------------------------------------------------#
	# This source code is (c)2000 White Pajamas, Inc                      #
	# see http://www.stonekeep.com/license.html for licensing information #
	#---------------------------------------------------------------------#

	#---------------------------------------------------------------------#
	# $Id: schecker.php3,v 1.10 2000/12/14 19:14:00 shevett Exp $
	#---------------------------------------------------------------------#

$scversion = 0.90;
	include "./conf/keystone.conf.default";
	if (file_exists("./conf/keystone.conf")) {
		include "./conf/keystone.conf";
	}

Function Title($titletext) {
	Global $scversion;
	?>
	<title>Keystone Structure Checker v<? echo ${scversion} ?> </title>
	<center>
	<form method="post" action="schecker.php3">
	<table border=0 cellspacing=0>
		<tr bgcolor=#888888>
			<td>
				<b><? echo $titletext ?></b>
			</td>
			<td align=right>
				SChecker v <? echo $scversion?>
			</td>
		</tr>
	<?
}
	
Function Hello() {
	Global $sys_dbname;

	Title("Describe your environment");
	?>
		<input type=hidden name=seq value=2>
		<tr valign=top>
			<td>
				<b>Server Host</b><br>
				This is the machine that has the<br>
				Keystone database on it.  'localhost'<br>
				generally means the same machine as<br>
				the one running the webserver.
			</td>
			<td>
				<!-- FIXME: needs maxsize based on database query -->
				<input name=c_host value="localhost">
			</td>
		</tr>
		
		<tr><td colspan=2><hr></td></tr>

		<tr valign=top>
			<td>
				<b>Login Name</b><br>
				Some database servers require a login name<br>
				and a password before they can be accessed.<br>
				Enter them here, or leave blank for none.<br>
			</td>
			<td>
				<!-- FIXME: needs maxsize based on database query -->
				User:<br><input name=c_user value=""><br>
				<!-- FIXME: needs maxsize based on database query -->
				Pass:<br><input type=password name=c_pass value=""><br>
			</td>
		</tr>

		<tr><td colspan=2><hr></td></tr>

		<tr valign=top>
			<td>
				<b>Database Name</b><br>
				Generally, Keystone is installed as <br>
				the database <i>Keystone</i>, but your <br>
				site might be using a different name. <br>
			</td>
			<td>
				<!-- FIXME: needs maxsize based on database query -->
				<input name=c_db value="<? echo $sys_dbname; ?>">
			</td>
		</tr>

		<tr><td colspan=2><hr></td></tr>

		<tr valign=top bgcolor=#888888>
			<td colspan=2 align=center>
				<input type=submit name=buttonval value="Verify Connectivity">
			</td>
		</tr>

	</table>
	</form>
	<? 
}

Function Verify() {
	Global $c_host, $c_user, $c_pass, $c_db, $sys_version;
	Title("Confirm database connecivity");
	echo "<input type=hidden name=c_host value=\"$c_host\">";
	echo "<input type=hidden name=c_user value=\"$c_user\">";
	echo "<input type=hidden name=c_pass value=\"$c_pass\">";
	echo "<input type=hidden name=c_db value=\"$c_db\">";
	echo "<tr valign=top><td>";
	echo "<b>Open connection to server</b><br>";
	echo "This step will attempt to connect to the MySQL<br>";
	echo "server defined in the first screen.  No other<br>";
	echo "actions except a simple socket connect are<br>";
	echo "attempted at this time.";
	echo "</td>";
	echo "<td>";
	if (! mysql_connect($c_host,$c_user,$c_pass)) {
		echo "Connect <b>Failed</b><br>";
		echo mysql_errno() . "<br>";
		echo mysql_error() . "<br>";
	} else {
		echo "<b>Succeeded.</b>";
		echo "</td></tr>\n";
		echo "<tr><td colspan=2><hr></td></tr>";
		echo "<tr valign=top><td><b>Checking for database</b><br>";
		echo "This step checks for the existence of the actual<br>";
		echo "database specified on the previous screen.";
		echo "</td>";
		echo "<td>";
		$dblist = mysql_listdbs();
		$counter = mysql_numrows($dblist);
		$foundit=0;  
		while ($counter-- > 0) { 
			$dbname = mysql_dbname($dblist,$counter);
			if ("$dbname" == "$c_db") {
				echo "<b>Succeeded.</b>";
				$foundit=1;
			}
		}
		if (! $foundit) {
			echo "<b>Failed!</b><br>";
			echo "This indicates the database<br>";
			echo "you specified in the first form does<br>";
			echo "does not exist on this server.  You will<br>";
			echo "need to issue the command 'mysqladmin create <i>dbname</i>'<br>";
			echo "to create the database<br>";
		}
	}	
	echo "</td>";
	echo "</tr>";

	echo "<tr><td colspan=2><hr></td></tr>";

	echo "<tr valign=top>";
	echo "<td align=left>";
	echo "<b>Database Structure URL</b><br>";
	echo "This specifies which URL to use to fetch<br>";
	echo "structure information about your Keystone<br>";
	echo "installation.  Generally you don't want to <br>";
	echo "change this.";
	echo "</td>";
	echo "<td>";
	# FIXME: needs maxsize based on database query
	echo "<input name=posturl size=25 value=\"http://www.stonekeep.com/structserver.php3\">";
	echo "</td>";
	echo "</tr>";

	echo "<tr><td colspan=2><hr></td></tr>";

	echo "<tr valign=top>";
	echo "<td align=left>";
	echo "<b>Keystone Version</b><br>";
	echo "In order to figure out what table structure<br>";
	echo "to use, SChecker needs to know what version of<br>";
	echo "Keystone you are running.  This number -must- be<br>";
	echo "correct to get an accurate report.";
	echo "</td>";
	echo "<td>";
	# FIXME: needs maxsize based on database query
	echo "<input name=version size=25 value=\"$sys_version\">";
	echo "</td>";
	echo "</tr>";

	echo "<td align=left>";
	echo "<tr valign=top bgcolor=#888888>";
	echo "<td align=left>";
	echo "<input type=submit name=buttonval value=\"Back\">";
	echo "</td>";
	echo "<td align=right>";
	echo "<input type=submit name=buttonval value=\"Continue\">";
	echo "</td>";
	echo "</table>";
}
		
Function Inventory() {
	Global $c_host, $c_user, $c_pass, $c_db, $posturl,$version,$CMD;
	Title("Perform System Inventory");
	echo "</form>";
	echo "<center>";
	echo "<form method=post action=\"$posturl\">\n";
	echo "<input type=hidden name=version value=\"$version\">\n";
	echo "<input type=hidden name=command value=\"$CMD\">\n";
	echo "<tr><th align=left>Local table name</th><th align=right>Column Count</th></tr>";
	mysql_connect($c_host,$c_user,$c_pass);
	$tl = mysql_list_tables($c_db);
	$i = 0;
	while ($i < mysql_num_rows($tl) ) { #>
		$tb_names[$i] = mysql_tablename ($tl, $i);
		$i++;
	}
	sort($tb_names);
	$counter=0;
	while ($i > $counter) {
		echo "<tr valign=top>\n";
		# Okay, first hte table name T01 = followups  (with the 01 incrementing, of course)
		$id = sprintf("%02d",$counter);
		echo "<input type=hidden name=T$id value=$tb_names[$counter]>\n";
		echo "<td align=left>$tb_names[$counter]</td>\n";
		$finfo = mysql_listfields($c_db,$tb_names[$counter]);
		$fc = mysql_numfields($finfo) ;
		$fcounter = 0;
		echo "<td align=right>";
		while ($fcounter < $fc) {  #>
			$cid=sprintf("%02d",$fcounter);
			$ft = mysql_field_type($finfo,$fcounter);
			echo "<input type=hidden name=T${id}C${cid}type value=$ft>\n";
			$fn = mysql_field_name($finfo,$fcounter);
			echo "<input type=hidden name=T${id}C${cid}name value=$fn>\n";
			$fl = mysql_field_len($finfo,$fcounter);
			echo "<input type=hidden name=T${id}C${cid}len value=$fl>\n";
			$fa = mysql_field_flags($finfo,$fcounter);
			echo "<input type=hidden name=T${id}C${cid}flags value=\"$fa\">\n";
			$fcounter++;
		}
		echo "$fcounter</td></tr>\n";	
		$counter++;
	}
	echo "<tr bgcolor=#888888>";
	echo "<tr>";
	echo "<td align=left>Additional flags</td>";
	# FIXME: needs maxsize based on database query
	echo "<td align=right><input name=command></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td align=center colspan=2>";
	echo "<input name=buttonval type=submit value=\"Perform Check\">";
	echo "</td></tr>";
	echo "</table>";
	echo "</form>";
}

switch ($buttonval) {
	case "Verify Connectivity" : {
		Verify();
		break;;
	}
	case "Back" : {
		Hello();
		break;;
	}
	case "Continue" : {
		Inventory();
		break;;
	}
	case "" : {
		Hello();
		break;;
	}
}
?>
		
