#!/usr/bin/php
<?php   
/*----------------------------------------------------------------------------*/
// Copyright (C) 2009  Paul Yasi (citrusdb.org)
// This script will write out the activity log from the day before to a log file
//
// This script should be put into the cron to be run each morning before logwatch
//
// log format should be similar to syslog:
// Nov 11 15:54:14 citrusdb citrusdb/activity: admin 127.0.0.1 10005 delete service 50 success
//
/*----------------------------------------------------------------------------*/

// set the path and name of the log file
$logfile = "/var/log/citrusdb.log";

// set the hostname to be included in the log
$hostname = "citrusdb";

// open the log file to append to it
$filehandle = fopen($logfile, 'a') or die("can't open $logfile file");

// Includes
include('./include/config.inc.php');
include('./include/database.inc.php');
include("$lang");
include('./include/billing.inc.php');
include('./include/citrus_base.php');
include('./include/support.inc.php');

$yesterday  = date("Y-m-d", mktime(0, 0, 0, date("m")  , date("d")-1, date("Y")));

// Select records that paid today and requested a receipt from the database
$query = "SELECT date(datetime) AS date, time(datetime) AS time, user, ".
  "ip_address, account_number, activity_type, record_type, record_id, result ".
  "FROM activity_log WHERE date(datetime) = '$yesterday'";

$DB->SetFetchMode(ADODB_FETCH_ASSOC);
$activitylog = $DB->Execute($query) or die ("activity log select failed");

// go through each receipt recipient
while ($myactivitylog = $activitylog->FetchRow()) {

  // get each item from the activity log
  $date = $myactivitylog['date'];
  $time = $myactivitylog['time'];
  $user = $myactivitylog['user'];
  $ip_address = $myactivitylog['ip_address'];
  $account_number = $myactivitylog['account_number'];
  $activity_type = $myactivitylog['activity_type'];
  $record_type = $myactivitylog['record_type'];
  $record_id = $myactivitylog['record_id'];
  $result = $myactivitylog['result'];

  /*-------------------------------------------------------------------------*/
  // append items to the citrusdb.log
  /*-------------------------------------------------------------------------*/

  // split the iso date into parts
  list($myyear, $mymonth, $myday) = split('-', $date);

  // assign the month it's written name
  switch($mymonth) {
  case "01":
    $mymonth = "Jan";
    break;
  case "02":
    $mymonth = "Feb";
    break;
  case "03":
    $mymonth = "Mar";
    break;
  case "04":
    $mymonth = "Apr";
    break;
  case "05":
    $mymonth = "May";
    break;
  case "06":
    $mymonth = "Jun";
    break;
  case "07":
    $mymonth = "Jul";
    break;
  case "08":
    $mymonth = "Aug";
    break;
  case "09":
    $mymonth = "Sep";
    break;
  case "10":
    $mymonth = "Oct";
    break;
  case "11":
    $mymonth = "Nov";
    break;
  case "12":
    $mymonth = "Dec";
    break;    
  }

  // replace the zero with a space for single digit days
  if ($myday < 10) {
    $myday = str_replace( "0" , " " , $myday );
  }

  // Nov 11 15:54:14 citrusdb citrusdb: admin 127.0.0.1 10005 delete service 50 success
  $logtext = "$mymonth $myday $time $hostname citrusdb: $user $ip_address $account_number $activity_type $record_type $record_id $result\n";

  // write to the log file
  fwrite($filehandle, $logtext);
  
 } // end while activity log

// close the log file
fclose($filehandle);

?>
