<?
/*
* DLOG simple logging class
* $Header: d:\\cvs/classistd/dlog/dlog.php,v 1.6 2003/04/29 19:28:57 darvin Exp $
*
* Copyright (C) 2003 Andrioli Darvin <hide@address.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* parameter from the configuration file
*/
require_once("dlog_cfg.inc");
/**
* class dlog
*
*
*
*/
class dlog {
/**
* I've opened the db? (TRUE/FALSE)
*
*
*
*/
var $DBOpen;
/**
* Opened resource to database connection
*
*
*
* @var $DbConn resource
*/
var $DbConn;
/**
* I've opened the flat file? (TRUE/FALSE)
*
*
*
* @var $FlatOpen bool
*/
var $FlatOpen;
/**
* Opened flat file pointer
*
*
*
* @var $FpFlat integer
*/
var $FpFlat;
/**
* constructor
*
*
*
*/
function dlog()
{
$this->DBOpen=FALSE;
$this->FlatOpen=FALSE;
}
/**
* Set the function Log as new error handler
*
*
*
* @return string Return the old error handler or FALSE on failure
*/
function register_error_handler()
{
return(set_error_handler(array(&$this,'Log')));
}
/**
* Write a new messagge via browser, log, mail, according to the configuration
*
* Parameter order as an PHP error handler
*
* @access public
* @parameter int $errno error number
* @parameter char $errmsg error description
* @parameter char $filename
* @parameter int $linenum
* @parameter char $vars
* @return none
*/
function Log($errno, $errmsg, $filename, $linenum, $vars=0) {
if (!isset($GLOBALS['dlog_ErrorMgr'])) {
// I lost my configuration parameter!
die($this->InternalError('Missing declaration of $dlog_ErrorMgr!Have you included dlog_cfg.inc?',__FILE__,__LINE__));
}
$cfg=$GLOBALS['dlog_ErrorMgr'];
if(isset($cfg[$errno]['display'])) { $this->Log2Display($errno, $errmsg, $filename, $linenum);}
if(isset($cfg[$errno]['mail'])) { $this->Log2Mail($errno, $errmsg, $filename, $linenum);}
if(isset($cfg[$errno]['flat'])) { $this->Log2flat($errno, $errmsg, $filename, $linenum);}
if(isset($cfg[$errno]['db'])) { $this->Log2db($errno, $errmsg, $filename, $linenum);}
if(isset($cfg[$errno]['die'])) {
if($cfg[$errno]['die']==1) exit();
}
}
/**
* Write the message on the screen. Draw a simply msg box
*
*
*
* @access private
* @parameter int errno error number
* @parameter char errmsg error description
* @parameter char filename
* @parameter int linenum
*/
function Log2Display($errno, $errmsg, $filename, $linenum) {
if($GLOBALS['dlog_ErrorMgr'][$errno]['display']==1) {
$ErrType=($errno)?'Error':'Info';
echo "<table align=center border=2><tr><td><p>
".$ErrType.": [".$errno."] - ".$errmsg."<br>
File: ".$filename." at line".$linenum."<br>
Rem.Addr: ".$_SERVER['REMOTE_ADDR']." Browser:".$_SERVER['HTTP_USER_AGENT'];
$ExtraP=$this->ExtraParam();
while(list($key,$value)=each($ExtraP)) {
echo '<br>'.$key.': '.$value;
}
echo "</p></td></tr></table>";
}
}
/**
* Send the error message by mail.
*
* Send the log by e-mail
* Require the mail section in php.ini configured
*
* @access private
* @parameter int errno error number
* @parameter char errmsg error description
* @parameter char filename
* @parameter int linenum
*/
function Log2Mail($errno, $errmsg, $filename, $linenum) {
if($GLOBALS['dlog_ErrorMgr'][$errno]['mail']!=1) return;
$ErrType=($errno)?'Error':'Info';
$Message=$ErrType.": [".$errno."] - ".$errmsg."\n File: ".$filename." at line".$linenum."\n"
."Rem.Addr: ".$_SERVER['REMOTE_ADDR']." Browser:".$_SERVER['HTTP_USER_AGENT'];
$ExtraP=$this->ExtraParam();
while(list($key,$value)=each($ExtraP)) {
$Message.= "\n".$key.': '.$value;
}
$Obj=(array_key_exists('dlog_MailObj',$GLOBALS))?$GLOBALS['dlog_MailObj']:'Error message';
$Sender=(array_key_exists('dlog_MailSender',$GLOBALS))?$GLOBALS['dlog_MailSender']:'DLogClass@'.$_SERVER['SERVER_NAME'];
$Header="From: ".$Sender." \r\n"
."X-Mailer: PHP/" . phpversion();
// for each destination send an e-mail
$NTo=count($GLOBALS['dlog_MailTo']);
for($i=0;$i<$NTo;$i++)
mail($GLOBALS['dlog_MailTo'][$i],$Obj,$Message,$Header);
}
/**
* Write out the error into a flat file.
*
*
*
* @access private
* @parameter int errno error number
* @parameter char errmsg error description
* @parameter char filename
* @parameter int linenum
*/
function Log2Flat($errno, $errmsg, $filename, $linenum) {
if($GLOBALS['dlog_ErrorMgr'][$errno]['flat']!=1) return;
$this->OpenFlat();
$ErrType=($errno)?'Error':'Info';
$Message=date('r')." ".$ErrType.": [".$errno."] - ".$errmsg." File: ".$filename." at line".$linenum."
Rem.Addr: ".$_SERVER['REMOTE_ADDR']." Browser:".$_SERVER['HTTP_USER_AGENT'];
$ExtraP=$this->ExtraParam();
while(list($key,$value)=each($ExtraP)) {
$Message.= " ".$key.': '.$value;
}
$Message.="\n";
if(!fwrite($this->FpFlat,$Message))
die($this->InternalError('Error writing file:'.$GLOBALS['dlog_FlatFileName'].' Description:'.$php_errormsg,__FILE__,__LINE__));
}
/**
* Open the specified flat file
*
*
*
* @access private
*/
function OpenFlat() {
if($this->FlatOpen)
return;
$this->FpFlat=@fopen($GLOBALS['dlog_FlatFileName'],'at')
or die($this->InternalError('Error during open file:'.$GLOBALS['dlog_FlatFileName'].' Description:'.$php_errormsg,__FILE__,__LINE__));
$this->FlatOpen=TRUE;
}
/**
* Write out the error into a db
*
*
*
* @access private
* @parameter int errno error number
* @parameter char errmsg error description
* @parameter char filename
* @parameter int linenum
*/
function Log2Db($errno, $errmsg, $filename, $linenum) {
if($GLOBALS['dlog_ErrorMgr'][$errno]['db']!=1) return;
$Param=array(
'DEvent' => date('YmdHis'),
'Errno' => $errno,
'Text' => $errmsg,
'FileName' => $filename,
'Line' => $linenum,
'RemAddr' => $_SERVER['REMOTE_ADDR'],
'Browser' => $_SERVER['HTTP_USER_AGENT']
);
$ExtraP=$this->ExtraParam();
$field=array_merge($Param,$ExtraP);
$this->WriteDb($field);
}
/**
* Convert the array into sql string and execute it
*
*
*
* @access private
* @parameter array field
*/
function WriteDb($field) {
$this->OpenDb();
$sql='insert into ' . $GLOBALS['dlog_LogTable'].' ';
$i=0;
$comma=' ';
$fieldName='';
$value='';
while (list ($key, $val) = each ($field)) {
if($i) $comma=',';
$fieldName.=$comma.$key;
$value.=$comma.'\''.addslashes($val).'\'';
$i++;
}
$sql.=' ('.$fieldName.') values ('.$value.')';
$this->DbExec($sql);
}
/**
* Handle the error risen inside the class
*
*
*
* @access private
* @parameter char Message
* @parameter char File source File
* @parameter char Line line number
*/
function InternalError($Message,$File,$Line) {
echo "<table align=center border=2><tr><td><p>
Error: [internal] - ".$Message."<br>
File: ".$File." at line".$Line."
</p></td></tr></table>";
}
/**
* Add extra information to the standard message.
* You should extends this class and then insert
* your addtional informational information in the new one
*
* @access private
* @return array with the new parameters. The index should be:
* array('ParamName' => 'ParamValue',...)
*/
function ExtraParam()
{
// In the base class I don't define any extra parameter.
$Param=array();
return($Param);
}
/**
* Group of function to perform database operations
*/
/**
* Open the database connection
*
* Mysql version
*
* @access private
*/
function OpenDb() {
if($this->DBOpen) return;
$this->DbConn=mysql_connect($GLOBALS['dlog_DbSystem'],$GLOBALS['dlog_DbUser'],$GLOBALS['dlog_DbPassword'])
or die($this->InternalError('Error during connection to database server',__FILE__,__LINE__));
mysql_select_db($GLOBALS['dlog_Dbname'],$this->DbConn)
or die($this->InternalError('Error during selection of database '.$GLOBALS['dlog_Dbname'],__FILE__,__LINE__));
$this->DBOpen=TRUE;
}
/**
* Execute a generic sql statement on the open conection
*
* Mysql version
*
* @access private
* @parameter char sql
*/
function DbExec($sql) {
$this->OpenDb();
mysql_query($sql,$this->DbConn)
or die($this->InternalError('Sql error '.mysql_errno().','.mysql_error().' sql:' .$sql,__FILE__,__LINE__));
}
} // that's all folk!
?>