<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
       <title>DiGIR Resource Configuration Checker</title>
</head>
<body>
<h3>DiGIR Configuration Test</h3>
<?php
define('idDiGIR_CheckConfig','$Id: DiGIR_checkConfig.php,v 1.1 2003/04/25 15:44:34 vieglais Exp $');

function includeErrorHandler ($errno, $errmsg, $filename, $linenum, $vars)
{
    global $emsg;
    $emsg = '';
    $emsg .= "<pre>\nERROR (".$errno.")\n";
    $emsg .= $errmsg."\n\n</pre>";
}

function showHelp()
{
    echo '<p>You need to call this page with a url parameter "resource" equal to
    the name of the resource you want to check.</p>';
    $ares = getResourceList(DIGIR_CONFIG_DIR.DIGIR_RESOURCE_FILE);
    if (count($ares) > 0)
    {
        echo '<p>Resources available:</p>';
        echo '<table border="1">';
        foreach ($ares as $r=>$f)
        {
            echo '<tr><td><a href="DiGIR_checkConfig.php?resource='.$r.'">'.
                $r.'</a></td><td>'.$f.'</td></tr>';
        }
        echo '</table>';
    }
    else
    {
        echo '<p>No resources were found in your resources file ('
            .DIGIR_CONFIG_DIR.DIGIR_RESOURCE_FILE.').</p>';
        echo '<p>Check that the path to the file is correct.  If it is then
        check that it is formatted correctly.  A simple example follows.</p>';
        echo '<pre>
<resources>
  <resource name="example" configFile="dwc2config.xml" />
</resources>
</pre>';
    }
    echo "<hr width='66%' />";
    echo "<p><a href='DiGIR_checkConfig.php?resource=__phpInfo()'>Get PHP information.</a></p>\n";
    echo '</body></html>';
    die();
}

//return 0=ADOdb field name, 1 = status code
function checkMetaColumns(&$cn,$c)
{
    $ares = array('','Table not found - check case of table name');
    $flds = $cn->MetaColumns($c[1]);
    if (count($flds) > 0)
    {
        $ares[1] = 'Field ('.$c[2].') not found.';
        foreach ($flds as $f)
        {
            if ($f->name == $c[2])
            {
                $ares[0] = $f->name;
                $ares[1] = "OK";
                return $ares;
            }
            elseif (strcasecmp($f->name,$c[2]) == 0)
            {
                $ares[0] = $f->name;
                $ares[1] = "Field found but case doesn't match";
                return $ares;
            }
        }
    }
    return $ares;
}

//0 = adodb field, 1 = concept name, 2 = field, 3= status
function checkColumn($fld,$tbl,&$Config)
{
    $ares = array($fld,'','','Field in table but not listed as concept.');
    foreach ($Config->m_concepts as $c)
    {
        if ((strcasecmp($tbl,$c[1]) == 0) && (strcasecmp($fld,$c[2]) == 0))
        {
            $ares[1] = $c[3];
            $ares[2] = $c[2];
            $ares[3] = "Check case of field name for this concept.";
            if ($c[2] == $fld)
                $ares[3] = 'OK';
            return $ares;
        }
    }
    return $ares;
}

function getTableList(&$Config)
{
    $ares = array();
    foreach ($Config->m_concepts as $c)
    {
        if (array_search($c[1],$ares) === FALSE)
        {
            array_push($ares,$c[1]);
        }
    }
    return $ares;
}




/////////////////////////////////////////////////
//Start here
/////////////////////////////////////////////////
define('_DEBUG',FALSE);
$emsg = '';
if (substr(PHP_OS, 0, 3) == 'WIN')
{
    define('DIGIR_OS_WINDOWS', true);
    define('DIGIR_OS_UNIX',    false);
    define('DIGIR_OS',    'Windows');
    define('PATHSEP',';');
}
else
{
    define('DIGIR_OS_WINDOWS', false);
    define('DIGIR_OS_UNIX',    true);
    define('DIGIR_OS',    'Unix'); // blatant assumption
    define('PATHSEP',':');
}

//force PHP to use only local includes - should all be part of DiGIR as of
//version 1.3 (20 Sep 2002)
ini_set('include_path','.'.PATHSEP.'pear');

echo "<h3>System</h3>\n";
echo "<table border='1'>\n";
echo "<tr><td>PHP Version</td><td>".phpversion()."</td></tr>\n";
echo "<tr><td>Operating System</td><td>".php_uname()."</td></tr>\n";
echo "<tr><td>Web Server</td><td>".$_SERVER['SERVER_SOFTWARE']."</td></tr>\n";
echo "<tr><td>Interface Version</td><td>".$_SERVER['GATEWAY_INTERFACE']."</td></tr>\n";
echo "<tr><td>Protocol</td><td>".$_SERVER['SERVER_PROTOCOL']."</td></tr>\n";
echo "<tr><td>PHP Include Path</td><td>".ini_get('include_path')."</td></tr>\n";
echo "</table>\n";
/////////////////////////////////////////////////
//try to find the necessary libraries
@include_once("localconfig.php");
require_once('DiGIR_globals.php');
require_once('DiGIR_utils.php');
require_once('DiGIR_errors.php');
require_once('DiGIR_config.php');
require_once('xpath/XPath.class.php');
require_once('adodb/adodb.inc.php');
require_once('Cache/HTTP_Request.php');
require_once('Cache/Function.php');

$includes = array('XPath'=>'xpath/XPath.class.php',
                  'ADOdb'=>'adodb/adodb.inc.php',
                  'httpCache'=>'Cache/HTTP_Request.php',
                  'functionCache'=>'Cache/Function.php');

/*echo "<h3>Checking installed libraries...</h3>\n";
$olderrorRep = error_reporting(0);
$olderrofunc = set_error_handler('includeErrorHandler');

foreach ($includes as $k=>$v)
{
    echo $k.'...';
    $emsg = "OK.<br />\n";
    include_once($v);
    echo $emsg;
    flush();
}
restore_error_handler($olderrorfunc);
error_reporting($olderrorRep);
*/
/////////////////////////////////////////////////
//Start checking resource configuration
echo "<h3>Checking Resource Installation...</h3>\n";
flush();

$resName = getVar('resource','');
if ($resName == '')
{
    showHelp();
}
if ($resName == '__phpInfo()')
{
    phpinfo();
    die();
}

/////////////////////////////////////////////////
//ok process a resource file.
//first extract the file name from the resources.xml file
echo '<p>Processing resource name: '.$resName.'</p>';
$resFile = getResourceConfigFile($resName,DIGIR_CONFIG_DIR.DIGIR_RESOURCE_FILE);
if (!$resFile)
{
    echo '<p>ERROR: No configuration file is associated with resource name = ';
    echo $resName.'</p>';
    showHelp();
}
$resFile = DIGIR_CONFIG_DIR.$resFile;

/////////////////////////////////////////////////
//Now load the Config information from the file...
echo '<p>Processing file: '.$resFile."</p>";
flush();
$cfgBuilder = new ConfigBuilder($resFile);
$Config = $cfgBuilder->Config;
echo "<h3>Your configuration file contents:</h3>";
$Config->Dump(TRUE);
flush();

echo "<hr /><h3>Checking Configuration Settings</h3>\n";

/////////////////////////////////////////////////
//Try connecting to the database using ADOdb
echo "<p>Testing connection to database...<br />\n";
flush();
$cn = &ADONewConnection($Config->connectionType);
if (!is_object($cn))
{
    echo " ERROR: could not create database connection object of type: "
        .$Config->connectionType."<br />";
    die('can not contine tests.');
}
echo "&nbsp;&nbsp;Created connection object OK.<br />\n";
flush();
$res = $cn->PConnect($Config->connectionString,
                     $Config->connectionUID,
                     $Config->connectionPWD,
                     $Config->connectionDB);
if (!$res)
{
    echo " ERROR: Could not open database connection.<br />\n";
    echo "ADODB:".$cn->errorMsg()."<br />\n";
    die('can not contine tests.');
}
echo "&nbsp;&nbsp;Connected to database OK.<br />\n";
flush();

/////////////////////////////////////////////////
//check list of tables - match names and case of names
echo "<h4>Checking for correct table names...</h4>\n";
$tables = $cn->MetaTables();
echo "<table border='1'>\n";
echo "<tr><th>Config Table Name</th><th>ADODB Table Name</th>
      <th>Message</th></tr>";
$warnCase = FALSE;
foreach ($Config->m_tables as $k=>$t)
{
    echo "<tr><td>".$t->m_name."</td>";
    $msg = '<td></td><td>Not found in database!</td>';
    foreach ($tables as $atab)
    {
        if (strcasecmp($t->m_name,$atab) === 0)
        {
            echo "<td>".$atab."</td>";
            $msg = '<td>Check case of name in config file</td>';
            if (strcmp($t->m_name,$atab) == 0)
            {
                $msg = '<td>OK</td>';
            }
            elseif (!$warnCase)
                $warnCase = TRUE;

        }
    }
    echo $msg."</tr>\n";
}
echo "</table>\n";
if ($warnCase)
{
    echo '<p>The case of the table names in one or more &lt;table&gt; elements
    of your configuration file does
    not match the case of the names reported by the ADOdb library.  For some
    platform + database combinations this is not important, but to minimize the
    chances of unexpected errors it is always a good idea to ensure that the
    case of the table names in the configuration file match those that are
    reported by the ADOdb library.  Note that the case of the names reported
    by ADOdb may not match the case of the names as they appear in your
    database management application!  The simplest solution is generally to
    update your configuration file and leave the database alone.</p>';
}
flush();

/////////////////////////////////////////////////
//check relationships between tables.
echo "<h4>Checking relationships between tables</h4>\n";

echo "<pre>";

print_r($Config->m_tables);

echo "</pre>";


/////////////////////////////////////////////////
//check concept field names and their data types
echo "<h4>Checking field names as known by database library...</h4>\n";

//open database connection
//Concepts = array:
    //[0] = type
    //[1] = table
    //[2] = field
    //[3] = name
    //[4] = zid
    //[5] = namespace
    //[6] = boolean searchable?
    //[7] = boolean returnable?
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$tableList = getTableList($Config);

foreach ($tableList as $t)
{
    echo "<h5>Table: ".$t."</h5>\n";
    $sql = "SELECT * FROM ".$t;
    $rs = $cn->SelectLimit($sql,1);
    if (!is_object($rs))
    {
        echo "<p>$sql produced no result set.<br />";
        echo "ADOdb reports: ".htmlEntities($cn->ErrorMsg())."<br />";
    }
    else
    {
        $arow = $rs->FetchRow();
        echo "<table border='1'>\n";
        echo "<tr><th>ADOdb Field</th><th>Name</th><th>Field</th><th>Status</th></tr>\n";
        foreach ($arow as $k=>$v)
        {
            $ares = checkColumn($k,$t,$Config);
            echo "<tr>";
            echo "<td>".$ares[0]."</td>";
            echo "<td>".$ares[1]."</td>";
            echo "<td>".$ares[2]."</td>";
            echo "<td>".$ares[3]."</td>";
            echo "</tr>\n";
        }
        echo "</table>";
        $rs->Close();
    }
}

/*
echo "<table border='1'>\n";
echo "<tr><th>Name</th><th>Table</th><th>Field</th><th>ADOdb Field</th><th>Status</th></tr>\n";
foreach ($Config->m_concepts as $k=>$c)
{
    echo "<tr>";
    echo "<td>".$c[3]."</td>";
    echo "<td>".$c[1]."</td>";
    echo "<td>".$c[2]."</td>";
    $ares = checkMetaColumns($cn,$c);
    echo "<td>".$ares[0]."</td>";
    echo "<td>".$ares[1]."</td>";
    echo "</tr>\n";
}
echo"</table>\n";

/////////////////////////////////////////////////
//check metatype of data
echo "<h4>Checking column metatypes...</h4>\n";

/////////////////////////////////////////////////
//check concept names by comparing with conceptual schema
*/

$cn->Close();

echo "<p>Completed.</p>"
?>
</body>
</html>
