<?php



function pg_vfs_date($pg_date) {

    list($date, $time, $zone) = split(' ', $pg_date );

    list($year, $month, $day) = split('-', $date);
    list($hour, $min, $sec) = split(':', $time);

    $vfs_date = date("G:i:s - D j M Y", mktime ($hour, $min, $sec, $month, $day, $year));

    return $vfs_date;
}





if($dbObjDefined != 1)
{

  $dbObjDefined = 1;

  //Wrapper class for database calls
  class dbObj
  {

    //Connection handle to database
    var $conn;
    
    //Default connection parameters
    var $host = "localhost";
    var $user = "vfs";
    var $password = "vfs.passy";
    var $port = "5432";
    var $dbname = "vfs";

    //Open initial connection. $params is an associative array holding 
    //parameters to the pg_Connect function.
    function init($parame)
    {

      if(isset($parame[host]))
        $host = $parame[host];
      else
        $host = $this->host;

      if(isset($parame[user]))
        $user = $parame[user];
      else
        $user = $this->user;

      if(isset($parame[password]))
        $password = $parame[password];
      else
        $password = $this->password;

      if(isset($parame[port]))
        $port = $parame[port];
      else
        $port = $this->port;

      if(isset($parame[dbname]))
        $dbname = $parame[dbname];
      else
        $dbname = $this->dbname;

      $this->conn = pg_Connect (" host=$host user=$user password=$password port=$port dbname=$dbname ");  
    }

    //Send SQL to database connection.  
    //Return recordset object on success.
    //Return 0 on failure.
    function execute($SQL)
    {
      $resultset = pg_Exec($this->conn, $SQL);

      if ($resultset) {
        $recset = new recordset;

        $recset->init($resultset);

        return $recset;
      }
      else {
        return 0;
      }
    }    

    //Close connection to database
    function free()
    {
      pg_close($this->conn);
    }
  };

  /*
    This is a simple recordset class which can be 
    traversed using next(), prev(), and current() methods.
    It is initialized from a resultset returned from the 
    function "pg_Exec" or can be generated by a call to the
    exec method from the dbObj.
  */
  class recordset
  {
    var $resultset;
    var $index;
    var $numFields;
    var $numTuples;

    function init($newResultset)
    {
      $this->resultset = $newResultset;
      $this->index = 0;
      $this->numFields = pg_NumFields($this->resultset);
      $this->numTuples = pg_NumRows($this->resultset);
    }

    //Get a value by row name and either column name or column number
    function getVal($row, $col)
    {
      return pg_Result($this->resultset, $row, $col);
    }

    //Return an array of field names
    function getFields()
    {
      for($i=0; $i<$this->numFields; $i++)
        $retArray[] = pg_FieldName($this->resultset, $i);

      return $retArray;
    }

    //Get number of columns in resultset
    function getNumFields()
    {
      return $this->numFields;
    }

    //Get a tuple (associative array of column values) by row number
    function getTupleDirect($row)
    {
      for($i=0; $i<$this->numFields; $i++)
        $retArray[pg_FieldName($this->resultset, $i)] = 
          pg_Result($this->resultset, $row, $i);

      return $retArray;
    }

    //Get tuple pointed to by the current index
    function getTuple()
    {
      if($this->index>=0 && $this->index < $this->numTuples)
        return $this->getTupleDirect($this->index);
      else
        return 0;
    }

    //Get an array filled with all values in a column
    //(using either column name or column number)
    function getColumn($col)
    {
      for($i=0; $i<$this->numTuples; $i++)
        $retArray[] = pg_Result($this->resultset, $i, $col);

      return $retArray;
    }

    //Return the number of records in the recordset
    function getNumTuples()
    {
      return $this->numTuples;
    }

    //Return 1 if index is within bounds of the recordset
    function current()
    {
      if($this->index>=0 && $this->index < $this->numTuples)
        return 1;
      else
        return 0;
    }

    //Incriment index
    function next()
    {
      if($this->index<$this->numTuples)
      {
        $this->index++;
        return 1;
      }
      else
      {
        return 0;
      }
    }

    //Decriment index
    function prev()
    {
      if($this->index >= 0)
      {
        $this->index--;
        return 1;
      }
      else
      {
        return 0;
      }
    }

    //Reset index to 0
    function reset()
    {
      $this->index = 0;
    }

    //Free memory allocated to recordset.
    function free()
    {
      pg_Freeresult($this->resultset);
    }
  };



	$db = new dbObj;
	$db->init("");

	if(! $db) {

?>
<html>
<body bgcolor="#ffffff">
<h1>Database Error<h1>
<br>
There was an un-recoverable error when trying to access the database server - Please try again later
<br>
</body>
</html>
<?php
		exit; // if(! db)
	}

}

?>
