<?php
/************************************************************************/
/* log.php                                                              */
/*----------------------------------------------------------------------*/
/* Defines EventLog singleton.                                          */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/

// In case we need to log to syslog
define_syslog_variables();

// Singleton accessor
function GetEventLog()
{
  static $log;
  if( !$log )
  {
    $log = new EventLog();
  }
  return $log;
}

// open syslog, include the process ID and also send
// the log to standard error, and use a user defined
// logging mechanism
class EventLog
{
  // Severity levels
  const ACCESS = 0;
  const INFO = 1;
  const WARNING = 2;
  const ERROR = 3;

  private $handle;
  private $browser;
  private $ipaddr;
  private $syslog;
  function __construct()
  {
    $syslog = 0;
    $config = GetConfiguration();
    if( $config->LogToFile )
    {
      if( !file_exists( $config->LogToFile ) )
      {
        $this->handle  = fopen( $config->LogToFile, 'w' );
      }
      else
      {
        $this->handle  = fopen( $config->LogToFile, 'a' );
      }
    }
    if( $config->LogToSyslog )
    {
      $this->syslog = openlog("VMProLog", LOG_PID | LOG_ODELAY, LOG_USER);
    }
  }
  function __destruct()
  {
    if( $this->handle )
    {
      fclose( $this->handle );
    }
    if( $this->syslog )
    {
      closelog();
    }
  }

  private function Write( $typeid, $msg )
  {
    $type = gettext('Error');
    $syslogtype = LOG_ERR;

    switch( $typeid )
    {
      case EventLog::ACCESS:
       $type = gettext('Access');
       $syslogtype = LOG_INFO;
       break;

      case EventLog::INFO:
       $type = gettext('Info');
       $syslogtype = LOG_INFO;
       break;

      case EventLog::WARNING:
       $type = gettext('Warning');
       $syslogtype = LOG_WARNING;
       break;

      default:
      // It's an error if $typeid isn't valid!
      case EventLog::ERROR:
       $type = gettext('Error');
       $syslogtype = LOG_ERR;
       break;

    }

    $date = date("Y/m/d H:i:s");
    $sessid = session_id();
    if( $sessid )
    {
      $sessid = '{' . $sessid . '} ';
    }
    else
    {
      $sessid = '(' . $_SERVER['HTTP_USER_AGENT'] . '@' . $_SERVER['REMOTE_ADDR'] . ') ';
    }
    $errmsg = $type;
    $errmsg .= ': ' . $msg;

    if( $this->handle )
    {
      fwrite( $this->handle, $date. ' ' . $sessid . $errmsg . "\n" );
    }
    if( $this->syslog )
    {
      syslog( $syslogtype, $date. ' ' . $sessid . $errmsg );
    }
  }

  // Function to output an error message to log.
  public function WriteError( $msg )
  {
    $this->Write( EventLog::ERROR, $msg );
  }

  // Function to output a warning message to log.
    public function WriteWarning( $msg )
  {
    $this->Write( EventLog::WARNING, $msg );
  }

  // Function to output an info message to log.
  public function WriteInfo( $msg )
  {
    $this->Write( EventLog::INFO, $msg );
  }

  // Function to introduce a new php script request
  public function LogAccess()
  {
    $this->Write( EventLog::ACCESS, $_SERVER['REQUEST_URI'] . ' (' . (VERSION) . ')' , 0 );
  }

  // Function to record linkage between requesting IP addr and session ID
  public function LogSessionStart()
  {
    $date = date("Y/m/d H:i:s");
    $msg = $date. ' (' . $_SERVER['HTTP_USER_AGENT'] . '@' . $_SERVER['REMOTE_ADDR'] . ') Started session: {' . session_id() . '}';
    if( $this->handle )
    {
      fwrite( $this->handle, $msg . "\n" );
    }
    if( $this->syslog )
    {
      syslog( LOG_INFO, $msg );
    }
  }
}


?>