<?php
/**
 * Legt Logs an und verwaltet Diese.
 * 
 * @license myHP, the construct for every webpage you want.
    Copyright (C) 2010  Stefan 'Eichi' Eichhorst <eichi@blubbbel.de>
    
    This program 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 3 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 Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @package EoT-Clan BF3 Leaderboard
 * @copyright Eichi (Stefan Eichhorst)
 * @author Eichi - eichi@blubbbel.eu
 */

class logging {
	
	protected $sFile = "";
	protected $bAuto = true;
	const MAX_LOGS   = 100;
	const MAX_LINES  = 1024;
	const SUFFIX     = '.log';
	
	/**
	 * Bestimmt welches Log bedient werden soll.
	 * 
	 * @param String $sFile
	 */
	public function __construct($sFile){
		$this->setFile($sFile);
	}
	
	/**
	 * Setzt den Speicherort des zu verwendenden Logs
	 * 
	 * @param String $sFile
	 */
	public function setFile($sFile){
		$this->bAuto = false;
		$sPath = LOG_DIR . date("d_m_Y");
		if ($sFile == ""){
			// NOT implemented! (modified by Eichi)
			exit("Error, this feature is not supported in this version, please contact Eichi (eichi@blubbbel.eu) for more informations.");
		}
		$this->sFile = $sPath . S . $sFile;
	}
	
	/**
	 * Gibt die angegebenen Zeilen aus einem Log zurück.
	 * 
	 * @param String $sFile
	 * @param Integer $iFrom
	 * @param Integer $iLimit
	 */
	public function getLines($sFile = "", $iFrom = 0, $iLimit = 1, $bArray = false){
		if ($iLimit <= 0){
			exit("Error: the numbers of lines must be over 1 in " . __FILE__ . ' at Line ' . __LINE__);
			return false;
		}
		$aFile = file($sFile);
		$mLine = "";
	
		for ($i = $iFrom; $i <= ($iFrom + $iLimit); ++$i){
			if ($bArray === true){
				if (is_string($mLine)) $mLine = array();
				$mLine[] = $aFile[$i];
			} else {
				$mLine .= "\r\n" . $aFile[$i];
			}
		}
		
		return $mLine;
	}
	
	/**
	 * Erweitert bis zum maximalen Wert eine Log-Datei
	 * Kann ein Array an Zeilen übergeben werden oder der String selber.
	 * 
	 * @param Mixed $mValue
	 * @param String $sFile
	 * @param Boolean $bBottom
	 * 
	 * @return Boolean
	 */
	public function appendLog($mEntry, $sFile = "", $bBottom = true){
		if (is_array($mEntry)){
			$sEntry = implode("\r\n", $mEntry);
		} elseif (is_string($mEntry)){
			$sEntry = $mEntry;
		} else {
			exit("Error, wrong parameter (array/string) in " . __FILE__ . ' at Line ' . __LINE__);
			return false;
		}
		if ($sFile == "") 
			$sFile = $this->sFile;
		if (!file_exists($sFile)) 
			return $this->saveLog($mEntry);
		if ($bBottom === true){
			$hFile = fopen($sFile, 'a+');
		} elseif ($bBottom === false){
			$hFile = fopen($sFile, 'r+');
		} else {
			exit("Error, wrong parameter (boolean) in " . __FILE__ . ' at Line ' . __LINE__);
			return false;
		}
		
		if ($bBottom === true){
			fwrite($hFile, $sEntry);
		} else {
			$sFileContent = file_get_contents($sFile);
			fwrite($hFile, $sEntry . $sFileContent);
		}
		fclose($hFile);
		
		return true;
	}
	
	/**
	 * Speichert das Logfile
	 * Kann ein Array an Zeilen übergeben werden oder der String selber.
	 * 
	 * @param String $mEntry
	 * @param String $sFile
	 * 
	 * @return Boolean
	 */
	public function saveLog($mEntry, $sFile = ""){
		$bError = true;
		if (is_array($mEntry)){
			$sEntry = implode("\r\n", $mEntry);
		} elseif (is_string($mEntry)){
			$sEntry = $mEntry;
		} else {
			exit("Error, wrong parameter (array/string) in " . __FILE__ . ' at Line ' . __LINE__);
			return false;
		}
		if ($sFile == "") 
			$sFile = $this->sFile;
		if (!file_exists(dirname($sFile))) 
			mkdir(dirname($sFile));
		
		$hFile = fopen($sFile, 'w+') or $bError = false;
		
		if ($bError === false){
			exit("Error, could not load log-file ($sFile) in " . __FILE__ . ' at Line ' . __LINE__);
		} else {
			fwrite($hFile, $sEntry);
			fclose($hFile);
		}
		
		return $bError;
	}
	
}