<?php
// Xinfeng Chen, 2002.11.26
// require_once('class_forum_base.php');

define('_LOG_SIZE',32000);
define('_LOG_GROUPSIZE',20);

class CLog {

 function _getCurrentFilePath(){
  $gfname=CForumBase::getFilePath('LOGGROUP');
  $lines=@file($gfname); $logid=explode(';',$lines[0]);
  if($logid[0]){
   $fname=CForumBase::getFilePath('LOG',$logid[0]);
   if(filesize($fname) < _LOG_SIZE){return $fname;}
  }
  // Generate new log file
  $fp=fopen($gfname,'a+');
  flock($fp,2); //LOCK
  $line=fgets($fp,20*_LOG_GROUPSIZE); $logid=explode(';',$line);
  while(count($logid) >= _LOG_GROUPSIZE){
   if($oldid=array_pop($logid)){unlink(CForumBase::getFilePath('LOG',$oldid));}
  }
  $newid=strftime("%y%m%d%H%M%S",time());
  array_unshift($logid,$newid);
  ftruncate($fp,0); fseek($fp,0);
  fputs($fp,implode(';',$logid));
  flock($fp,3); //RELEASE
  fclose($fp);
  return CForumBase::getFilePath('LOG',$newid);
 }

 function writeLogEntry($a_entry){
  global $HTTP_SESSION_VARS;

  $ip=getenv('REMOTE_ADDR'); $ip1 = getenv('HTTP_X_FORWARDED_FOR');
  if (($ip1!='')&&($ip1!='unknown')){$ip=$ip1;}

  $stime=strftime("%Y-%m-%d %H:%M:%S",time());

  $fname=CLOG::_getCurrentFilePath();
  $fp=fopen($fname,'a');
  fwrite($fp,"$ip|$stime|$HTTP_SESSION_VARS[sv_user]|".trim($a_entry)."\n");
  fclose($fp);
 }

 function getLogFileGroup(){
  $gfname=CForumBase::getFilePath('LOGGROUP');
  $lines=@file($gfname);
  return explode(';',$lines[0]);
 }

 function getLogFilePath($a_logFileId){
  return CForumBase::getFilePath('LOG',$a_logFileId);
 }
}

?>