<?php
/**
 * Project:     astat - simple site engine
 * File:        /inc/logger.class.php
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.astat.org SVN: $URL: http://svn.astat.org/astat/trunk/inc/logger.class.php $
 * @copyright 2009 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package astat core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: logger.class.php 91 2009-10-22 20:47:08Z genuineparts $
*/

$module["logger"]["name"]="Logging Class";
$module["logger"]["ver"]="0.2.1";
/*
Already defined by PHP. I'll leave it here for Info
define("LOG_EMERG", 0);
define("LOG_ALERT", 1);
define("LOG_CRIT", 2);
define("LOG_ERR", 3);
define("LOG_WARNING", 4);
define("LOG_INFO", 6);
define("LOG_DEBUG", 7);
*/

class logger {
	var $type="file";
	var $logfile;
	var $level=LOG_ERR;
	var $filelink;
 
	function __construct($type="file", $logfile='/logs/logfile.log', $level=LOG_INFO) {
		global $core, $config, $db;
		$this->type=$type;
		$this->logfile=$logfile;
		$this->level=$level;
		switch($this->type){
			case "syslog":
				openlog("astatlog", LOG_ODELAY , LOG_USER);
			break;
			case "SQL":
				if(!is_object($db)){
					$this->type="file";
					$this->filelink=$this->open_file($this->logfile);
				}
			break;
			case "file":
			default:
				if($this->logfile==""){
					$this->logfile='/logs/logfile.log';
				}
				$this->filelink=$this->open_file($this->logfile);
			break;
		}		
		$this->write("File Logging instanziert.", LOG_DEBUG);
	}

	function __destruct() {
		$this->close($this->filelink);
	}

	function open_file($file){
		global $core, $config, $db;
                $filelink = fopen($file, "a");
		return $filelink;
    	}


	function close(){
		if($this->type=="file")
                	fclose($this->filelink);
   	}

	function write($text,$level=LOG_INFO,$line="",$file=""){
		global $db, $config;
		if($level<=$this->level){
			$timestamp = date("d.m.Y, H:i:s",time());
			$date = date("d.m.Y",time());
			$time = date("H:i:s",time());
		        $ip = $_SERVER["REMOTE_ADDR"];
			switch ($this->type) {
				case "syslog":
					syslog($level, '<' . $timestamp . '> '.$text.' IP: ' . $ip .' FILE: '.  $file .  ' LINE:' . $line);
				break;
				case "SQL":
					$db->query("INSERT INTO ".$config["prefix"] . "logs (date,time,ip,file,line,text) VALUES ('".$date."','".$time."','".$ip."','".$file."','".$line."','".$text."')");
				break;
				case "file":
				default:
		      			$log = "<" . $timestamp . ">;" . $ip .  ";" . $file .  ";" . $line .  ";" . $text . "\r\n";
					fwrite($this->filelink, $log);
				break;
			}
		}

    	}


}
?>
