<?php
/* 
 * Log frameworka
 */

class MFLog {

    private static $logLevel = 0;
    private static $logLevels = array('0'=>'DEBUG','1'=>'INFO','2'=>'WARN','3'=>'ERROR', '4'=>'FATAL');
    public static $fileLog = false;
    private static $consoleLog = false;
    private static $consolePrivateIp = null;
    private static $sqliteLog = false;

    public function __construct() {

    }
	/*
	 * UStawianie poziomu logowania
	 */
    public static function SetLogLevel($level) {
	self::$logLevel = $level;
    }
	/*
	 * Metoda Debug
	 */
    public static function Debug($data) {
    //self::DataLog($data, 0);
    }
	/*
	 * Metoda Info
	 */
    public static function Info($data) {
	self::DataLog($data, 1);
    }
	/*
	 * Metoda Warn
	 */
    public static function Warn($data) {
	self::DataLog($data, 2);
    }
	/*
	 * Metoda Error
	 */
    public static function Error($data) {
	self::DataLog($data, 3);
    }
	/*
	 * Metoda Fatal
	 */
    public static function Fatal($data) {
	self::DataLog($data, 4);
    }
	/*
	 * Logowanie do pliku
	 */
    private static function FileLog($data, $type) {
	$logger = LoggerManager::getLogger(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : __FILE__);
	switch ($type) {
	    case 0:
		$logger->warn($data);
		break;
	    case 1:
		$logger->info($data);
		break;
	    case 2:
		$logger->warn($data);
		break;
	    case 3:
		$logger->error($data);
		break;
	    case 4:
		$logger->fatal($data);
		break;
	}

    }
	/*
	 * Logowanie an konsole
	 */
    private static function ConsoleLog($data, $type) {
	if(self::$consolePrivateIp==null || (self::$consolePrivateIp!=null && $_SERVER['REMOTE_ADDR'] == self::$consolePrivateIp)) {

	    if($type<4) {
		Core::LoadPlugin('fb');
		fb($data, eval('return FirePHP::'.str_replace('DEBUG', 'LOG', self::$logLevels[$type]).';'));
	    }
	}

    }
	/*
	 * Metoda proxy logowania
	 */
    private static function DataLog($data, $type) {
	if(self::$consoleLog == true) {
	    self::ConsoleLog($data, $type);
	}
	if(self::$fileLog == true) {
	    self::FileLog($data, $type);
	}
	if(self::$sqliteLog == true) {
	    self::SqlliteLog($data, $type);
	}
    }
	/*
	 * Ustawia logowanie do pliku
	 */
    public static function SetFileLog($set) {
	self::$fileLog = $set;
    }
	/*
	 * Ustawia logowanie na konsole
	 */
    public static function SetConsoleLog($set) {
	self::$consoleLog = $set;
    }

    public static function DbLog($message, $server = null, $type = 0) {
	$log = new MfDbLog();
	$log->SetMessage($message);
	$log->SetServer($server);
	$log->SetType($type);
	MfDbLogDAL::Save($log);
    }

    public static function Pack() {
	//Core::RequireOnce('../core/lib/phpmailer/class.phpmailer.php');
	$fileArray = array();
	$zipfile = date("Ymd").".zip";
	$zip = new ZipArchive;
	$return = '';
	//if ($zip->open('../logs/'.$zipfile, ZipArchive::CREATE) === TRUE) {

	    if ($handle = opendir('../logs')) {
		while (false !== ($file = readdir($handle))) {
		    if ($file != "." && $file != ".." && $file != ".svn" && date("Ymd", filemtime('../logs/'.$file))==date("Ymd")) {
			//echo "$file was last modified: " . date("Ymd", filemtime('../logs/'.$file))."<br>";
		    
//			$zip->addFile('../logs/'.$file, $file);
//			$fileArray[] = '../logs/'.$file;
$return .= file_get_contents('../logs/'.$file);
		    }
		}
	    
		closedir($handle);
	    }
	    //$zip->close();
	    
	//}

	return $return;


//	if(is_file('../logs/'.$zipfile)) {
//			$mail = new PHPMailer();
//			$mail->AddAddress('pawy@formix.pl');
//
//
//				$mail->Username = Config::Get('MAIL_USERNAME');
//				$mail->Password = Config::Get('MAIL_PASSWORD');
//				$mail->Host		= Config::Get('MAIL_HOST');
//				$mail->SMTPAuth = true;
//				$mail->Mailer	= 'smtp';
//
//			$mail->From = Config::Get('SERVER_MAIL_FROM');
//			$mail->FromName = 'Turystyka.wp.pl';
//			$mail->WordWrap = 50;                              // set word wrap
//			$mail->IsHTML("type_html");                               // send as HTML
//			$mail->CharSet = "iso-8859-2";
//
//
//
//			$mail->Subject  =  'Logi '.date("Ymd");
//			$mail->AddAttachment('../logs/'.$zipfile, $zipfile);
//
//			$mailingState = $mail->Send();
//
//			unlink('../logs/'.$zipfile);
//	}
//
//	if(!$mail->ErrorInfo) {
//	    foreach($fileArray as $file) {
//		unlink($file);
//	    }
//	} else {
//	    self::Error('Cant email logs!');
//	}

	

    }

    public static function SqlliteLog($data, $type) {
	$log = new MfSqliteLog();
	$log->SetType($type);
	$log->SetMessage($data);
	$dalObj = MfSqliteLogDAL::GetDalDataObj();
	$dalObj->setObj($log);
	MfSqliteLogDAL::Save($dalObj);
    }

}

?>