<?php
/**
* This file is PHP PEAR SOAP sLog database server class
*
* PHP version 5
*
* LICENSE:
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
* @author Kjell-Inge Gustafsson <hide@address.com> Original Author
* @copyright 2008 Kjell-Inge Gustafsson kigkonsult
* @license GNU General Public License
* @link http://www.kigkonsult.se/slog
*/
/**
* sLog SOAP Server Class
*
* This class is the soap interface for making sLog inserts.
*
* basic usage:<code>
* $slogserver = new slogserver( array dbparams | file dbparams [ , file debugFile | bool debug] );
* // Initialize the SOAP server
* $server = new SOAP_Server( $dbparams );
* // set the slogserver class as default responder, define urn
* $server->addObjectMap( $slogserver, 'urn:slogserver' );
* // bind WSDL resource
* $server->bindWSDL( 'http://localhost/.../slogserver.wsdl' );
* // start server
* $server->service( $HTTP_RAW_POST_DATA );
* </code>
*
* @access public
* @author Kjell-Inge Gustafsson <hide@address.com> Original Author
* @since 0.8 - 2008-03-29
* @version 0.8
*/
class slogserver {
/**
* slogserver version
*
* @access private
* @var string
*/
private $version;
/**
* database parameters
*
* @access private
* @var array
*/
private $dbparams;
/**
* if debug is set, db connection result, insert sql et.al. is logged
* if set to TRUE, function error_log is used, or else use a valid log file name
* any db connection errors are always sent to to the server error log
*
* @access private
* @var array
*/
private $debug;
/**
* starttime
*
* @access private
* @var float
*/
private $starttime;
/**
*
* Constructor
*
* @access public
* @param mixed $dbparams
* @param bool $debug, opt, default FALSE
* @since 0.8 - 2008-03-29
*/
public function __construct( $dbparams, $debug=FALSE ) {
$this->starttime = microtime( TRUE );
$this->version = 'slogserver_V0.8_sLog_V0.8 ';
$this->debug = $debug;
if( $this->debug )
$this->_log( str_pad( 'setLog started ', 50, '#'));
if( !is_array( $dbparams ) && is_file( $dbparams ) && is_readable( $dbparams )) {
$lines = file( $dbparams );
$dbparams = array();
foreach( $lines as $line ) {
$pair = split( ":", $line, 2);
$dbparams[$pair[0]] = trim( $pair[1] );
}
}
$this->dbparams = array();
foreach( $dbparams as $key => $value ) {
switch( strtolower( $key )) {
case 'dbserver' : $this->dbparams['dbserver'] = $value; break;
case 'database' : $this->dbparams['database'] = $value; break;
case 'dbuserid' : $this->dbparams['dbuserid'] = $value; break;
case 'dbpasswd' : $this->dbparams['dbpasswd'] = $value; break;
}
if( $this->debug )
$this->_log( "dbparams: $key => $value" );
}
}
/**
*
* simple 'ping' service, returns only versions for slogserver class and slog database plus date+time
*
* @access public
* @param string
* @return string
* @since 0.8 - 2008-03-29
*/
public function ping ( $dummy ) {
if( $this->debug )
$this->_log( 'ping exec.time:'.number_format(( microtime( TRUE ) - $this->starttime ), 4 ));
return $this->versionDt();
}
/**
*
* log entry database insert
*
* @access public
* @param string $date
* @param string $time
* @param string $application
* @param string $service
* @param string $operation
* @param string $objno
* @param string $client
* @param string $client_spec
* @param string $organisation
* @param string $organisation_spec
* @param string $user1
* @param string $user2
* @param string $subject
* @param string $body
* @return string
* @since 0.8 - 2008-03-29
*/
public function setLog( $date
, $time
, $application
, $service
, $operation
, $objno
, $client
, $client_spec
, $organisation
, $organisation_spec
, $user1
, $user2
, $subject
, $body ) {
/** create a database connection ******************/
$link = mysql_connect( $this->dbparams['dbserver'], $this->dbparams['dbuserid'], $this->dbparams['dbpasswd'] );
if( !$link ) {
$message = ' DB '.$this->dbparams['dbserver'].' connection error (mysql errno '.mysql_errno().':'.mysql_error().')';
if( $this->debug )
$this->_log( $message );
error_log( $this->versionDt().$message );
return $this->versionDt().$message;
}
$result = mysql_select_db( $this->dbparams['database'], $link );
if( !$result ) {
$message = ' DB '.$this->dbparams['dbserver'].':'.$this->dbparams['database'].' select database error (mysql errno '.mysql_errno().':'.mysql_error().')';
if( $this->debug )
$this->_log( $message );
error_log( $this->versionDt().$message );
return $this->versionDt().$message;
}
if( $this->debug )
$this->_log( 'setLog db connection OK' );
/** create the sql-string *************************/
$sql = 'SELECT '.$this->dbparams['database'].".fslog('";
$sql .= $date;
$sql .= "','".$time;
$sql .= "','".$application;
$sql .= "','".$service;
$sql .= "','".$operation;
$sql .= "','".$objno;
$sql .= "','".$client;
$sql .= "','".$client_spec;
$sql .= "','".$organisation;
$sql .= "','".$organisation_spec;
$sql .= "','".$user1;
$sql .= "','".$user2;
$sql .= "','".$subject;
$sql .= "','".$body;
$sql .= "')";
if( $this->debug )
$this->_log( 'slogserver->setLog sql='.$sql);
/** execute insert ********************************/
$result = mysql_query( $sql, $link );
if( !$result ) {
$message = " DB $database execution error, sql=$sql (mysql errno ".mysql_errno().':'.mysql_error().')';
if( $this->debug )
$this->_log( $message );
error_log( $this->versionDt().$message );
return $this->versionDt().$message;
}
if( $this->debug )
$this->_log( 'setLog mysql execution OK');
/** checking result and exit **********************/
if(( $row = mysql_fetch_array( $result, MYSQL_NUM )) && ( '0' == $row[0] )) {
$result = '0'; // answer 0
}
else {
$message = " DB $database insert error (".$row[0].') (mysql errno '.mysql_errno().':'.mysql_error().')';
if( $this->debug )
$this->_log( $message );
error_log( $this->versionDt().$message );
$result = $row[0]; // answer the stored function error code (from '-10' to '-80')
}
if( $this->debug )
$this->_log( 'slogserver->setLog finished! res='.$result.' exec.time:'.number_format(( microtime( TRUE ) - $this->starttime ), 4 ) );
return $result;
}
/** ******************************************************************************************
*
* returns slog class version, formatted date+time+mseconds
*
* @access private
* @return string
* @since 0.8 - 2008-03-29
*/
private function versionDt() {
list($usec, $sec) = explode(" ",microtime());
return $this->version.' '.date('Y-m-d H:i:s', $sec ).':'.substr($usec, 2, 4);
}
/** ******************************************************************************************
*
* log to function error_log or to a valid log file
*
* @access private
* @param string $message
* @return void
* @since 0.8 - 2008-03-29
*/
private function _log( $message ) {
if( is_file( $this->debug ) && is_writable( $this->debug ) && ( FALSE !== ( $fp = @fopen ( $this->debug, "a" )))) {
@fwrite( $fp, $this->versionDt()." $message\n" );
@fclose( $fp );
}
elseif( TRUE === $this->debug )
error_log( $this->versionDt().' '.$message );
}
}
?>