Location: PHPKode > scripts > sLog > sLog-0.8/http/insertLog.php
<?php
/** sLog v0.8
 *  copyright (c) 2008 Kjell-Inge Gustafsson kigkonsult
 *  www.kigkonsult.se/slog/index.php
 *  hide@address.com
 *
 *  Description:
 *  This file is a simple PHP HTTP sLog database server
 *
 *  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
 */
define( 'VERSION', 'insertLog_V1.0_sLog_V0.8' );
            /** keep alive respond, interface + sLog versions */
if( isset( $_REQUEST['ping'] )) errExit();
            /** check input ***********************************/
// $o = '1 '.VERSION.' '.date('Y-m-d H:i:s');foreach($_REQUEST as $k=>$v) $o.=" $k=>$v";error_log( $o );// test ###
foreach( $_REQUEST as $k => $v )
  $_REQUEST[$k] = urldecode( $v );
if( !isset( $_REQUEST['time'] ))
  $_REQUEST['time']     = '';
if( !isset( $_REQUEST['date'] ))
  $_REQUEST['date']     = '';
if( !isset( $_REQUEST['subject'] ))
  $_REQUEST['subject']  = '';
if( !isset( $_REQUEST['body'] ))
  $_REQUEST['body'] = null;
if( !isset( $_REQUEST['application'] )) 
  errExit( 'missing input - application' );
if( !isset( $_REQUEST['service'] ))
  $_REQUEST['service']  = '';
if( !isset( $_REQUEST['operation'] ))
  $_REQUEST['operation'] = '';
if( !isset( $_REQUEST['objno'] ))             // default 1
  $_REQUEST['objno']    = 1;
if( !isset( $_REQUEST['client'] ))
  errExit( 'missing input - client' );
if( !isset( $_REQUEST['client_spec'] ))       // null allowed
  $_REQUEST['client_spec'] = null;
if( !isset( $_REQUEST['organisation'] ))
  errExit( 'missing input - organisation' );
if( !isset( $_REQUEST['organisation_spec'] )) // null allowed
  $_REQUEST['organisation_spec'] = null;
if( !isset( $_REQUEST['user1'] ))
  errExit( 'missing input - user' );
if( !isset( $_REQUEST['user2'] ))             // null allowed
  $_REQUEST['user2']    = null;

// $o = '2 '.VERSION.' '.date('Y-m-d H:i:s');foreach($_REQUEST as $k=>$v) $o.=" $k=>$v";error_log( $o );// test ###
            /** required : database connection parameters *****/
$dbserver = 'localhost';
$database = 'slog';
$dbuserid = 'slogcaller';
$dbpasswd = 'slogcaller';
            /** create a database connection ******************/
$cnt      = 0; 
while( TRUE ) {
  $link   = mysql_connect( $dbserver, $dbuserid, $dbpasswd );
  if( $link )
    break;
           /* if to many simultaneous database connections, try 50 times during 25 seconds */
  elseif(( 1040 == mysql_errno()) && ( 50 > $cnt )) {
    usleep(500000); // wait 0.5 sec
    $cnt++;
  }
  else // quit.. .
     errExit( 'Could not connect to db server (mysql errno '.mysql_errno().':'.mysql_error().')' );
}
mysql_select_db( $database, $link ) or 
     errExit( 'Could not select database (mysql errno '.mysql_errno().':'.mysql_error().')' );
            /** create the sql-string *************************/
$sql      = "SELECT $database.fslog('";
$sql     .=       $_REQUEST['date'];
$sql     .= "','".$_REQUEST['time'];
$sql     .= "','".$_REQUEST['application'];
$sql     .= "','".$_REQUEST['service'];
$sql     .= "','".$_REQUEST['operation'];
$sql     .= "','".$_REQUEST['objno'];
$sql     .= "','".$_REQUEST['client'];
$sql     .= "','".$_REQUEST['client_spec'];
$sql     .= "','".$_REQUEST['organisation'];
$sql     .= "','".$_REQUEST['organisation_spec'];
$sql     .= "','".$_REQUEST['user1'];
$sql     .= "','".$_REQUEST['user2'];
$sql     .= "','".$_REQUEST['subject'];
$sql     .= "','".$_REQUEST['body'];
$sql     .= "')";
            /** execute insert ********************************/
$result   = mysql_query( $sql, $link ) or 
     errExit( "execution failed in database, sql=$sql (mysql errno ".mysql_errno().':'.mysql_error().')' );
            /** checking result and exit **********************/
if(( $row  = mysql_fetch_array( $result, MYSQL_NUM )) && ( '0' == $row[0] ))
  die( $row[0] );   // answer 0
else
//  die( $row[0] );   // answer the stored function error code (from '-10' to '-80')
  errExit( "no or error result from database $database (".$row[0].') (mysql errno '.mysql_errno().':'.mysql_error().')' ); // test ###
function errExit( $message=null ) {
  $output = VERSION.' '.date('Y-m-d H:i:s')." $message";
  error_log( $output );
  exit( $output );
}
?> 
Return current item: sLog