<?
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Class		:	connection
* Version :	1.0
* Date    :	05 April 2004
* Author  :	Darwish A. Khalil
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* This class is used to:
* 	1- Open a secure connection to the server
*		2- Select a certain MySQL database on this server
*   3- Generate error reports when connection failed either to server or database
*   4- Generate log file of the queries executed in the project
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Notes:
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* All the data needed to connect to both Server and MySQL database are stored
* in a seperate file to be included in this class for security reasons.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

// Class Definition
class connection {

	/*-*-*-*-* Private Class Variables *-*-*-*-*-*-*/
	// Connection Variables
	var $hostName; 			// Name of server
	var $dbLink; 				// Link to database
	var $dbName; 				// Name of databse
	var $dbUser; 				// Database user name
	var $dbPassword; 		// Database password
	var $dbPwd; 				// Password used or not?

	// Query Result Variables
	var $dbQryResult; 	// Result of last query
	var $dbResultLine; 	// One Result line of last query

	// Query Log Variables
	var $keepLog; 			// Should a log of all queries be kept
	var $logPath; 			// Path where log should be saved
	var $fullErr; 			// Display full or partial error
	var $rollBackSup; 	// Are you using tables with transactional support (ie. InnoDB)

	// Constructor of the class
	// Creates object and makes connection to database.
  function connection() {
		// Read config info from cfg file and save them in class variables
    require("connection.cfg.php");
		$this->hostName = $host;
		$this->dbUser = $user;
		$this->dbPassword = $pwd;
		$this->dbName = $database;
		$this->keepLog = $keepLog;
		$this->logPath = $logPath;
		$this->fullErr = $fullErr;
		$this->rollBackSup = $rollBackSup;

		if (strlen($pwd) > 0) { $this->dbPwd = "Yes"; } else { $this->dbPwd = "No"; }
		// Creates connection to server and selects appropriate database
		$dbLink = mysql_connect($this->hostName, $this->dbUser, $pwd) or $this->goError($qry, mysql_error());
       mysql_select_db($this->dbName) or $this->goError($qry, mysql_error());
	}

	// Close connection to the database
  function dbclose(){
    mysql_close();
	}

	// Executes a query ($qry).
  function make_query($qry) {
		$this->dbQryResult = mysql_query($qry) or $this->goError($qry, mysql_error());

		// If required, write query to a log file
    if ($this->keepLog == 1) {
			$fp = fopen($this->logPath."connection.class.log", "a+");
      fputs($fp, date("r", time())." - ".$_SERVER['PHP_SELF']."<BR>");
      fputs($fp, $qry."<BR>");
      fclose($fp);
		}
    return $this->dbQryResult;
	}

	// Returns the resultset, one row at a time in an array
  function get_data() {
		$this->dbResultLine = mysql_fetch_object($this->dbQryResult);
    return $this->dbResultLine;
	}

	// Number of rows in resultset
  function row_count() {
		$qtyLines = mysql_num_rows($this->dbQryResult);
    return $qtyLines;
	}

	// Array with field names of resultset
  function get_fields() {
		$fldList = Array();
    for ($fldCntr = 0; $fldCntr < mysql_num_fields($this->dbQryResult); $fldCntr++) {
			$fldList[$fldCntr] = mysql_field_name($this->dbQryResult, $fldCntr);
		}
    return $fldList;
	}

	// If something has gone wrong, displays an error
  function goError($qry, $err) {
		// Rolls back transaction if required
    if ($this->rollBackSup == 1) {
      print "<font color=\"red\"><b>Transaction rolled back!</b></font><br><Br>\n";
      mysql_query("rollback;") or $this->rollError(mysql_error());
		}
    print "<font color=\"red\" face=\"Helvitica\" size=\"+2\"><b><u>Error!</u></b></font><br><br>";
    print "<font face=\"Helvitica\">";
    if ($this->fullErr == 1) {
      print "<b>Query:</b> <i>$qry</i><br>";
		}
		$this->endError($err);
	}

	// Displays the end of the error message
  function endError($err) {
		// Displays error
    print "<font color=\"red\" face=\"Helvitica\">";
    print "<b>".$err."</b>";
    print "</font><br><br>";

		// Displays conection details as well as the page using this class if set
    if ($this->fullErr == 1) {
      print "<b><u>System Variables</u></b><br>";
      print "<b>Host:</b> ".$this->hostName."<br>";
      print "<b>Database:</b> ".$this->dbName."<br>";
      print "<b>User:</b> ".$this->dbUser."<br>";
      print "<b>Password Used:</b> ".$this->dbPwd."<br>";
      print "<b>Refering Page:</b> ".$_SERVER['PHP_SELF']."<br>";
		}
    print "</font>";
    print "<br>Please contact your system administrator for assistance!<br><br>";

		// Make sure script dies
		die(date(r, time()));
	}

	// Displays error if a rollback could not be done.
  function rollError($err) {
    print "<font color=\"red\" face=\"Helvitica\" size=\"+2\"><b>ERROR:</b></font><br>";
    print "<font face=\"Helvitica\">";
    if ($this->fullErr == 1) {
      print "<b>Query:</b> <i>$qry</i><br>";
		}
    print "<font color=\"red\"><b>Could not roll back transaction!!</b></font><br><br>\n";
		$this->endError($err);
	}

}
?>