<?php
/**
 * ****************************************************************
 * Project:      *
 * Copyright:    Copyright (c) 2007
 * Company:      Thomas Niemann
 * ****************************************************************
 * Workfile:     ErrorLogLoader.php
 * ****************************************************************
 * Description:
 *
 * ***************************************************************/
 class ErrorLogLoader extends BasicLoader{
 	private $smartyInstance;
 	private $configInstance;
 	private $errorText;
 	
 	private static $instance;	
	/****************************************************************
	 * singleton, provides only one instance
	 ****************************************************************/
	public static function getInstance()
	{
		if (!isset (self :: $instance))
		{
			$class = __CLASS__;
			self :: $instance = new $class ();
		}
		return self :: $instance;
	}
	private function __construct() {
	    $this->smartyInstance =   SmartyHelper::getInstance();
	    $this->configInstance =  ConfigLoader::getInstance();
    }
 	public function logError ($_error,$_class = null,$_sendEmail = false, $_logFile = 'pageError') {
		$filename = '_errorLogs/'.$_logFile.'.log';
		$messages = null;
		if (!empty ($_error)) {
			if(is_array($_error)){
				foreach ($_error as $message){
					$_errorPlace = empty($_class)? '' : ' in class '.$_class;
					$this->errorText = "[".date('d-m-Y H:i:s', time())."] -> ".strip_tags($message).$_errorPlace."\r\n";
				}
			}else{
				$_errorPlace = empty($_class)? '' : ' in class '.$_class;
				$this->errorText = "[".date('d-m-Y H:i:s', time())."] -> ".strip_tags($_error).$_errorPlace."\r\n";
			}
		}else{
			$this->errorText = "Keine Fehlerbeschreibung vorhanden!";
		}
		$this->writeInFile($filename, $this->errorText);
		unset ($_SESSION['messages']);
		if($this->configInstance->getPHPSettings('display_errors') == 'off' && $_sendEmail){
       //send E-Mail to an admin if error was logged
		   $subj = 'Fehler auf: '.$_SERVER['HTTP_REFERER'];
		   $this->smartyInstance->assign('error_msg',$_error);
		   $content = $this->smartyInstance->fetch('email/errorMail.tpl');
		   $to = $this->configInstance->getEmail('webmaster');
		   $email = new EmailHelper();
       $email->sendEmail($subj, $content, $to);
		}
	}

	public function logInvalidEmail ($_email) {
		$filename = '_errorLogs/invalidEmail.log';
		$_error = "[".date('d-m-Y H:i:s', time())."] -> ".$_email."\n";
		$this->writeInFile($filename, $_error);
	}

	private function writeInFile($filename, $_error){
		if (is_writable($filename)) {
			if (!$handle = fopen($filename, "a")) {
			// Wir öffnen $filename im "Anhänge" - Modus.
		  		// Der Dateizeiger befindet sich am Ende der Datei, und
		 		 // dort wird $error später mit fwrite() geschrieben.
		        print "Kann die Datei $filename nicht öffnen";
		        exit;
		  	}
			// Schreibe $error in die geöffnete Datei.
		   if (!fwrite($handle, (string)$_error)) {
		       print "Kann in die Datei $filename nicht schreiben";
		       exit;
		   }
		   fclose($handle);
		}
		else{
		  print "Die Datei ".$filename." ist nicht schreibbar";
		}
	}
 }
?>