<?php
#This page gathers the name so that we can write an NDA on the next page.
require("includes/.htconfig.php");

#########################################################################################
#These next ones are dangerous utilities for looking at the database from investigate.php

function show_fields($table)		#Might want to echo <PRE> around it.
{
  $result = mysql_query("SHOW COLUMNS FROM $table");
	if (!$result)
	{
	    return 'Could not run query to show columns of the $table table: ' . mysql_error();
	}
	$a="";
	if (mysql_num_rows($result) > 0)
	{
		while ($row = mysql_fetch_assoc($result))
		{
			$a.=print_r($row,TRUE);
		}
	}
	return $a;
}

function spill_query_results($query,$title,$border=1,$limit=60)
{
	$res=mysql_query($query);
	if (!$res)
	{
		return reporterror("showing query results",$query);
	}
	$a="<H4>$title</H4><TABLE border=$border><TR>";
	$nf=mysql_num_fields($res);
	for ($i=0;$i<$nf;$i++) $a.="<TH>".mysql_field_name($res,$i)."</TH>";
	echo "</TR>";
	while ($row=mysql_fetch_array($res))
	{
		$a.="<TR>";
		for ($i=0;$i<$nf;$i++) $a.="<TD>".$row[$i]."</TD>";
		$a.="</TR>";
	}
	$a.="</TABLE>";
	return $a;
}

function spill_table($table,$border=1,$limit=4)
{
	$query="select * from $table limit $limit";
	return spill_query_results($query,"$table table",$border,$limit);
}
$menues="<FORM action=$script method=post>";
$menues.="\n<INPUT type=submit name=command value=debuggery>";
$menues.="\n<INPUT type=submit name=command value='clear debuggery'>";
$menues.="\n<INPUT type=submit name=command value='show log'>";
$menues.="\n<INPUT type=submit name=command value='kill log'>";
$menues.="\n<INPUT type=submit name=command value=session>";
$menues.="\n<INPUT type=submit name=command value='kill session'>";
$menues.="\n<INPUT type=submit name=command value=info>";
$menues.="\n<INPUT name=query>";
$menues.="\n<INPUT type=submit name=command value='run query'>";
$menues.="\n</FORM>";
echo $menues;
echo "<HR>";
if (array_key_exists("command",$_REQUEST))
switch ($_REQUEST["command"])
{
  case "run query":
    $query=fromRequest("query");
    if (is_null($query))
      echo "<BR>You gave us no query to run.";
    else
      echo spill_query_results($query,"Results for:<BR>$query");
    break;
  case "clear debuggery":
    shell_exec("rm ".DEBUGGERY);
  case "debuggery":
    echo "<PRE>";
    readfile(DEBUGGERY);
    echo "</PRE>";
    break;
  case "kill log":
    shell_exec("rm ".LOGFILENAME);
  case "show log":
    echo "<PRE>";
    readfile(LOGFILENAME);
    echo "</PRE>";
    break;
  case "kill session":
    $_SESSION = array();
    setcookie ("PHPSESSID", "", time() - 3600);
    session_destroy();
  case "session":
    if (DEBUGMU)      #Don't show the file if .htconfig is no longer writing it.
    {
      echo "<H2>The SESSION variables are:</H2>";
      echo "<PRE>";
      print_r($_SESSION);
      echo "</PRE><HR>";
    }
    foreach($_SESSION as $_key=>$_value)
    {
      echo "<BR>$_key=>".print_r($_value,TRUE);
    }
    break;
  case "info":
    phpinfo();
    break;
  default:
}
echo $menues;
?>