<?php
/* - - - - - - - - - - - - - - - - - -
* Based on http://www.phpclasses.org/browse/file/1431.html: Vinicius Gallafrio BR (24/01/2002)
* Modified by martinfasani@gmail.com
* Date : 13/04/2005
E.g:
* include "ibclass.php";
* $ib = New ib('/path/to/database.gdb','sysdba','masterkey',0);
* $ib->query("SELECT * FROM table"); ^-- change to 1 for debug mode
*
*/
/* <INTERBASE PUBLIC FUNCTIONS >*/
class ib {
/* public: connection parameters */
var $debug;
var $database;
var $user;
var $password;
/* private: connection parameters */
var $conn;
var $result;
//in spanish "Dom","Lun","Mar","Mie","Jue","Vie","Sab"
var $dayweek=array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
/** interbase::interbase()
* Constructor this class - define public connection parameters and call the connect method
*
* @param $database
* @param $user
* @param $password
* @param $debug
*/
function ib($database,$user,$password,$debug=0) {
$this->debug = $debug;
//if ($this->debug) echo "\n\nDebug On <br>\n";
$this->user = $user;
$this->password = $password;
$this->database = $database;
// open connection
$this->connect();
}
/**
* interbase::connect()
* Open connection with the server
* @return conn id
*/
function connect () {
$this->conn = @ibase_connect($this->database,$this->user,$this->password);
if ( ibase_errmsg() ) {
$report ="PAGE: $_SERVER[REQUEST_URI] \r\n ERRORMSG: ".ibase_errmsg();
if ($this->debug) {
die("<font color=red>$report</font><br><br>Connecting to ".$this->database);
} else {
$connlogfile="ibconnlog.txt";
//Open the file in append mode and write log
$fp = fopen($connlogfile,a);
// Write $log to our opened file.
$log=$_SERVER["REMOTE_ADDR"]." | ".date ("l dS of F Y h:i:s A")." | ". ibase_errmsg()." \n";
if (!fwrite($fp, $log)) {
print "Cannot write to file ($connlogfile)";
exit;
}
fclose($fp);
die("<font color=red><br>La connectividad con base de datos ha fallado. Se ha creado un registro del error y se ha enviado al equipo técnico. Por favor inténtelo mas tarde.</font>");
}
}
return $this->conn;
}
function disconnect() {
return ibase_close($this->conn);
}
// ibquery with error reporting to table STABUG
function query ($query="") {
if (empty($query)) return false;
$this->result = @ibase_query($query,$this->conn);
if ( ibase_errmsg() ) {
$report ="PAGE: $_SERVER[REQUEST_URI] \r\n ERRORMSG: ".ibase_errmsg();
if ($this->debug) { /* Debug: Dies with DB error & query */
die("<font color=red>$report</font><br><br>".$query);
} else { /* No debug: Writes a row in STABUG and shows message */
$bugquery = $this->ibvalstring($query);
$ibase_errmsg= ibase_errmsg();
$buglog = "INSERT INTO BUGLOG (BUGURI,BUGMSG,BUGQRY,BUGSTA) VALUES ('$_SERVER[REQUEST_URI]','ibase_errormsg: $ibase_errmsg','$bugquery',current_timestamp)";
$result = @ibase_query($buglog) or die ($buglog);
die("<font color=red><br>La consulta a la base de datos ha fallado. Se ha creado un registro del error y se ha enviado al equipo técnico. Por favor inténtelo mas tarde.</font>");
}
}
return $this->result;
}
function free_result () {
return ibase_free_result($this->result);
}
// Replaces [\r\n] and single quotes for ibase
function ibvalstring($query) {
$search = array ("'([\r\n])[\s]+'");
$replace = array ("");
$query = preg_replace($search, $replace, $query);
$query = str_replace("'", "''", $query);
return $query;
}
// DATE functions .STORED IN IB numeric YYYYMMDD
function ibtonorm($dt) { // IN: YYYYMMDD OUT: DD/MM/YYYY
$yr=strval(substr($dt,0,4 ));
$mo=strval(substr($dt,4,2 ));
$da=strval(substr($dt,6,2 ));
return $da."/".$mo."/".$yr;
}
function normtoib($dt) { // IN: DD/MM/YYYY OUT: YYYYMMDD
$da=strval(substr($dt,0,2 ));
$mo=strval(substr($dt,3,2 ));
$yr=strval(substr($dt,6,4 ));
return "$yr$mo$da";
}
function ibtostamp($dt) { // IN: MM/DD/YYYY HH:MM:SS OUT: YYYYMMDDHHMMSS
$mo=strval(substr($dt,0,2 ));
$da=strval(substr($dt,3,2 ));
$yr=strval(substr($dt,6,4 ));
$hh=strval(substr($dt,11,2 ));
$mm=strval(substr($dt,14,2 ));
$ss=strval(substr($dt,17,2 ));
return "$yr$mo$da$hh$mm$ss";
}
function ibgetday($dt) { // IN:YYYYMMDD OUT: Lun /Mar
$wn=date ("w", strtotime($dt) );
return $this->dayweek[$wn];
}
}
/* </INTERBASE PUBLIC FUNCTIONS> */
?>
|