Mr. A. Flaherty

current_app/sql.log * $DB_LOG_SQLFILE_NAME may be redefined by the user */ if($_SERVER["REMOTE_ADDR"] == "143.117.17.119" /*Alan Collins PC*/|| $_SERVER["REMOTE_ADDR"] == "143.117.14.72" /*Alan Collins on terminal server*/ || $_SERVER["REMOTE_ADDR"] == "143.117.17.69" /*GWM PC */){ $DB_LOG_SQL = true; $DB_LOG_SQLFILE_NAME = "/usr/users/iserve/sqllogs/".strtok($_SERVER["SCRIPT_NAME"],"/").".log"; } //include_once(INCLUDES."common.inc"); //include_once(INCLUDES."qubCommon.inc"); class dbConnection { /* ***** CLASS VARIABLE DECLARATIONS ***** */ var $connection = 0; // The Connection to the database var $results = 0; // Resultset from the database. var $current_row = 0; // A link to the current row in the result set var $database_name = ""; // The name of the database to use e.g. viking::sislive / warehouse etc. var $database_type = ""; // The type of the database to be connected e.g. ingres / mysql / sqlserver var $database_host = ""; // The hostname of the machine where the database resides e.g. 143.117.143.26 or 143.117.14.52 var $database_user = ""; // The username of the database var $database_pass = ""; // The password of the database var $database_encoding = ""; // The encoding ... var $error_message = ""; // A holder for the global error_message var $db_error = false; var $rows_returned = false; var $halt_on_error = true; var $author_address = ""; var $database_types = array("ingres" => 0, "mysql" => 0, "mssql" => 0, "file" => 0); /** * CONSTRUCTOR */ function dbConnection($database, $halt_on_error=true) { global $author; if (!is_array($database)) { $this->database_name = $database; $this->database_type = "ingres"; } else { $this->database_name = $database['name']; $this->database_type = $database['type']; //$this->database_host = $database['host']?$database['host']:"localhost"; $this->database_user = $database['user']; $this->database_pass = $database['pass']; //$this->database_encoding = $database['encoding']; $this->database_host = $database['host']; } $this->log("dbConnection($this->database_name,$this->database_type)"); if (!isset($this->database_types[$this->database_type])) { $this->error_check(false,"\"constructor: invalid type of \" "); } $this->halt_on_error = $halt_on_error; if (isset($author)) { if (is_array($author)) { list($name,$this->author_address) = each($author); } else { $this->author_address = $author; } } $this->error_check($this->database_type,"setup"); } function error_check($variable, $type) { if (!$variable) { switch ($this->database_type) { case "ingres": $this->error_message = ereg_replace("
\nWarning: Ingres II: ","",$this->error_message); $this->error_message = ereg_replace("\n","",$this->error_message); $this->error_message = strip_tags(trim($this->error_message)); break; case "mysql": $this->error_message = mysql_error(); break; case "mssql": $this->error_message = ereg_replace("
\nWarning: Sybase error: ","",$this->error_message); $this->error_message = ereg_replace("\n","",$this->error_message); $this->error_message = strip_tags(trim($this->error_message)); break; case "file": $this->error_message = "error with file"; } $this->db_error = true; //@$this->disconnect(); $body = "DB:\t$this->database_name\n"; $body .= "TYPE:\t$this->database_type\n"; $body .= "HOST:\t$this->database_host\n"; $body .= "IP:\t".$_SERVER['REMOTE_ADDR']."\n"; $body .= "ERROR:\t$this->error_message\n"; $body .= "SCRIPT:\t".$_SERVER['SCRIPT_NAME']."\n"; if ($this->query_string) $body .= "QUERY:\t$this->query_string"; // Mail the owner of the application ... //if (isset($this->author_address)) { //mail($this->author_address, $_SERVER['SERVER_NAME'], $body); //} $this->log($body); if ($this->halt_on_error) { printf("
There has been an error, cannot $type database, please try again later.

\n"); //printFooter(); //error("dbConnection: $type error: db=$this->database_name: $this->error_message: $this->query_string ","LOGFILE","HALT"); } else { error("dbConnection: $type error: db=$this->database_name: $this->error_message","LOGFILE","CONTINUE"); return false; } } else { return true; } } function connect() { // If Necessary, connect to the database if (0 == $this->connection) { switch ($this->database_type) { case "ingres": $this->connection = @ingres_connect($this->database_name); $this->log("connection = @ingres_connect($this->database_name)"); break; case "mysql": $this->connection = @mysql_connect($this->database_host, $this->database_user, $this->database_pass); if ($this->connection) @mysql_select_db($this->database_name); $this->log("connection = @mysql_pconnect($this->database_host)"); break; case "mssql": $this->connection = @mssql_connect($this->database_host, $this->database_user, $this->database_pass); if ($this->connection) @mssql_select_db($this->database_name); $this->log("connection = @mssql_pconnect($this->database_host)"); break; case "file": $this->connection = @fopen($this->database_name,"r"); $this->log("connection = @fopen($this->database_name,\"r\")"); } // Check for error in connection if (!$this->error_check($this->connection,"db connect")) return false; } } function log($sql) { // // if $DB_LOG_SQL == true, then query($sql) and insert(sql) etc. will be logged to sql.log on // file $DB_LOG_SQLFILE_NAME, default = $DOCUMENT_ROOT/$user->current_app/sql.log // // $DB_LOG_SQLFILE_NAME may be re-assigned vy the user // global $DOCUMENT_ROOT,$REMOTE_ADDR,$SCRIPT_NAME,$user,$DB_LOG_SQL,$DB_LOG_SQLFILE_NAME,$_SERVER; if ($DB_LOG_SQL /* && (($_SERVER['SERVER_NAME'] == "trident.qub.ac.uk") || $_SERVER['SERVER_NAME'] == "")*/) { if(!isset($DB_LOG_SQLFILE_NAME)){ $DB_LOG_SQLFILE_NAME = "/usr/users/iserve/sqllogs/$user->current_app".".log"; } // //remove indents from lines // $s = ""; $new_line = false; $i = 0; while ($iid,$SCRIPT_NAME,$s); } else { $logstring = sprintf("[%-15s %s] [$timestring] [%s]\n",$REMOTE_ADDR,$user->id,$SCRIPT_NAME); } fputs($file,$logstring); } } } function query($query_string) { // Check that there are no INSERT/UPDATE/DELETES in the query $this->log($query_string); $uppercase = strtoupper($query_string); if (ereg("^(INSERT|UPDATE|DELETE|DROP)( )",$uppercase) || (ereg("( |;)(INSERT|UPDATE|DELETE|DROP)( )",$uppercase))) { if ($this->halt_on_error) { printf("
There has been an error, cannot query database, please try again later.

\n"); printFooter(); $this->disconnect(); error("dbConnection: Non SELECT activity in query function : db=$this->database_name : $query_string","LOGFILE","HALT"); } else { error("dbConnection: Non SELECT activity in query function : db=$this->database_name : $query_string","LOGFILE","CONTINUE"); } } $this->query_string = $query_string; $this->connect(); switch ($this->database_type) { case "ingres": // ***** INGRES QUERY ***** ob_start(); $this->results = ingres_query($query_string,$this->connection); $this->error_message = ob_get_contents(); ob_end_clean(); if (!$this->error_check($this->results,"db query")) return false; $this->current_row = @ingres_fetch_row($this->connection); $field_count = sizeof($this->current_row); // index the field names .... $i=1; do { $field_name = @ingres_field_name($i, $this->connection); if ($field_name != "") { $this->$field_name = $i; } $i++; } while ($i <= $field_count); if ($this->current_row) { $this->rows_returned = 1; return true; } else { $this->rows_returned = 0; return false; } break; case "mysql": // ***** MYSQL QUERY ***** $this->results = @mysql_query($query_string,$this->connection); if (!$this->error_check($this->results,"query")) return false; $this->current_row = @mysql_fetch_object($this->results); if ($this->current_row) { $this->rows_returned = @mysql_num_rows($this->results); return true; } else { $this->rows_returned = 0; return false; } break; case "mssql": // ***** MSSQL QUERY ***** ob_start(); $this->results = mssql_query($query_string,$this->connection); $this->error_message = ob_get_contents(); ob_end_clean(); if (!$this->error_check($this->results,"query")) return false; $this->current_row = @mssql_fetch_object($this->results); if ($this->current_row) { $this->rows_returned = @mssql_num_rows($this->results); return true; } else { $this->rows_returned = 0; return false; } break; case "file": // ***** FILE QUERY ***** // Read the list of field names from the file (these will be held, tab seperated in the first line of the file) $field_names = fgets($this->connection); $this->current_row = explode("\t",trim($field_names)); if (!$this->error_check($this->current_row[0],"query")) return false; // Get the first row into the array $row = fgets($this->connection); $fields = explode("\t",$row); foreach($fields as $field_id => $value) { $this->current_row[$this->current_row[$field_id]] = $value; } $this->rows_returned = 1; return true; } unset($this->query_string); } function insert($query_string) { $this->log($query_string); $this->query_string = $query_string; $this->connect(); switch ($this->database_type) { case "ingres": // ***** INGRES INSERT ***** $this->results = @ingres_query($query_string,$this->connection); if (!$this->error_check($this->results,"db write")) return false; $this->rows_returned = @ingres_num_rows($this->connection); break; case "mysql": // ***** MYSQL INSERT ***** $this->results = @mysql_query($query_string,$this->connection); if (!$this->error_check($this->results,"db write : syntax")) return false; $this->rows_returned = @mysql_affected_rows($this->connection); if (!$this->error_check($this->results,"db write : 0 rows")) return false; break; case "mssql": // ***** MSSQL INSERT ***** ob_start(); $this->results = mssql_query($query_string, $this->connection); $this->error_message = ob_get_contents(); ob_end_clean(); if (!$this->error_check($this->results,"db write")) return false; $result = mssql_query("SELECT @@ROWCOUNT"); list($affected) = mssql_fetch_row($result); $this->rows_returned = $affected; break; } if ($this->rows_returned > 0) { return true; } else { return false; } } function get_next_record() { switch ($this->database_type) { case "ingres": // ***** INGRES QUERY ***** $this->current_row = @ingres_fetch_row($this->connection); break; case "mysql": // ***** MYSQL QUERY ***** $this->current_row = @mysql_fetch_object($this->results); break; case "mssql": // ***** MSSQL QUERY ***** $this->current_row = @mssql_fetch_object($this->results); break; case "file": // ***** FILE QUERY ***** if (!feof($this->connection)) { $row = fgets($this->connection); $fields = explode("\t",$row); if ($fields[0] == "") { $this->current_row = false; } else { foreach($fields as $field_id => $value) { $this->current_row[$this->current_row[$field_id]] = $value; } } } else { $this->current_row = false; } break; } if ($this->current_row) return true; else return false; } function get_field($field, $no_trim=0) { switch ($this->database_type) { case "ingres": // ***** INGRES QUERY ***** $value = $this->current_row[$this->$field]; break; case "mysql": // ***** MYSQL QUERY ***** $value = $this->current_row->$field; if ($this->database_encoding == "utf-8") { $value = utf8_encode($value); } break; case "mssql": // ***** MSSQL QUERY ***** $value = $this->current_row->$field; break; case "file": // ***** FILE QUERY ***** $value = $this->current_row[$field]; break; } if (!$no_trim) $value = trim($value); return $value; } /** * Puts the contents of the current resultset into an HTML table */ function get_into_table() { $array = $this->get_into_array(); $column_names = array(); if (count($array) > 0) { // Get the column names printf("

\n"); foreach ($array[0] AS $key=>$value) { if (!ereg("^[0-9]*$",$key)) { array_push($column_names,$key); printf("\n"); } } printf("\n"); $parity=1; for($i=0; $i<=count($array); $i++) { printf("\n",++$parity%2==0?"#ffffff":"#dddddd"); foreach($column_names AS $key) { printf("\n",$array[$i]->$key); } printf("\n"); } printf("
$key
%s
\n"); } } /** * Will put the contents of the current result set into * a two dimensional array */ function get_into_array() { $data = array(); if ($this->rows_returned) { switch ($this->database_type) { case "ingres": $max_fields = @ingres_num_fields($this->connection); break; case "mysql": $max_fields = @mysql_num_fields($this->connection); break; case "mssql": $max_fields = @mssql_num_fields($this->connection); break; } $row_num = 0; do { $data[$row_num] = $this->current_row; $row_num++; } while ($this->get_next_record()); } return $data; } function commit() { switch ($this->database_type) { case "ingres": @ingres_commit($this->connection); $this->log(";commit"); case "mysql": break; case "mssql": @mssql_query("COMMIT TRANSACTION",$this->connection); $this->log(";commit"); break; } } function rollback() { switch ($this->database_type) { case "ingres": @ingres_rollback($this->connection); $this->log(";rollback"); case "mysql": break; case "mssql": @mssql_query("ROLLBACK TRANSACTION",$this->connection); $this->log(";rollback"); break; } } function disconnect() { if ($this->connection != 0) { switch ($this->database_type) { case "ingres": @ingres_close($this->connection); break; case "mysql": @mysql_close($this->connection); break; case "mssql": @mssql_close($this->connection); break; case "file": @fclose($this->connection); break; } $this->log("disconnect"); } } function autocommit() { if ($this->connection != 0) { @ingres_autocommit($this->connection); $this->log(";autocommit"); } } } ?>