<?php
Class Logger {
	
    public function __construct() {
      //  $this->includes();
    }
	
    private function includes()
    {}

    // Colorize log lines based on type and label
    private function colorize($msg) {
        $colors = [
            'INFO' => "\033[32m",         // Green
            'WARNING' => "\033[33m",      // Yellow
            'ERROR' => "\033[31m",        // Red
            'SCAN_LOG' => "\033[36m",     // Cyan
            'SYSTEM_LOG' => "\033[35m",   // Magenta
            'TIMING' => "\033[34m",     // Blue
            'SMTP' => "\033[36m",       // Cyan
            'EMAIL VALIDATION START' => "\033[92m", // Bright Green
            'EMAIL VALIDATION END' => "\033[91m",   // Bright Red
            'EMAIL VALIDATION RESULT' => "\033[92m", // Bright Green
            'MX record selected' => "\033[96m", // Bright Cyan
            'Retrieving MX records for domain' => "\033[96m", // Bright Cyan
        ];
        $reset = "\033[0m";
        foreach ($colors as $key => $color) {
            if (stripos($msg, $key) !== false) {
                return $color . $msg . $reset;
            }
        }
        return $msg;
    }

    // Log rotation helper
    private function rotateLog($file, $maxSize = 5242880, $maxFiles = 5) { // 5MB default
        if (file_exists($file) && filesize($file) >= $maxSize) {
            $dir = dirname($file);
            $base = basename($file);
            // Rotate old logs
            for ($i = $maxFiles - 1; $i >= 1; $i--) {
                $older = "$dir/{$base}.{$i}";
                $newer = "$dir/{$base}." . ($i + 1);
                if (file_exists($older)) {
                    rename($older, $newer);
                }
            }
            // Move current log to .1
            rename($file, "$file.1");
        }
    }
		
    public function system_debug($strMsg){
		global $system_debug;
		if($system_debug == 'true'){
			$phpver = (float)phpversion();
			if ($phpver >= 7.0) {
			   $prev_path = dirname(__DIR__, 1);
			}
			 $logFile = "$prev_path/tmp/logs/system_debug.log";
            $this->rotateLog($logFile);
			$timestamp = date('[Y-m-d H:i:s]');
			$colored = $this->colorize($strMsg);
			file_put_contents($logFile, $timestamp . ' ' . $colored . PHP_EOL . PHP_EOL , FILE_APPEND | LOCK_EX);
		}
    }
			
    public function scan_debug($strMsg){
		global $scan_debug;
		if($scan_debug == 'true'){
			$phpver = (float)phpversion();
			if ($phpver >= 7.0) {
			   $prev_path = dirname(__DIR__, 1);
			}
			 $logFile = "$prev_path/tmp/logs/scan_debug.log";
            $this->rotateLog($logFile);
			$timestamp = date('[Y-m-d H:i:s]');
			$colored = $this->colorize($strMsg);
			file_put_contents($logFile, $timestamp . ' ' . $colored . PHP_EOL . PHP_EOL , FILE_APPEND | LOCK_EX);
		}
    }
				
    public function smtp_debug($strMsg){
		global $smtp_debug;
		if($smtp_debug == 'true'){
			$phpver = (float)phpversion();
			if ($phpver >= 7.0) {
			   $prev_path = dirname(__DIR__, 1);
			 }
			 $logFile = "$prev_path/tmp/logs/smtp_debug.log";
            $this->rotateLog($logFile);
			$timestamp = date('[Y-m-d H:i:s]');
			$colored = $this->colorize($strMsg);
			file_put_contents($logFile, $timestamp . ' ' . $colored . PHP_EOL . PHP_EOL , FILE_APPEND | LOCK_EX);
		}
    }
	
	public function ipn_debug($var, $msg){
		ob_start();
		$logFilePath = dirname(__DIR__) . '/tmp/logs/ipn_debug.log';
		$currentTime = date('[Y-m-d H:i e] ');
			if(!empty($var) && !empty($msg)){
				//	error_log($currentTime. $msg . PHP_EOL, 3, $logFilePath);
				 echo "\n$currentTime $msg";
						$logFile = fopen($logFilePath, 'a');
						fwrite($logFile, ob_get_clean());
						fclose($logFile);
			}
	}

	public function log_delete($log_path){
		// Log cleaner
			if(file_exists($log_path)) {
			$clearfile = fopen( "$log_path", 'w' );
			fclose($clearfile);
			$error = false;
			$message = 'Log Data Cleared';
			$response = array(
				'success' => $message
			);
			exit(json_encode($response));
			} else {
				$error = true;
				$message = 'Couldn\'t delete log data. Log file don\'t exists!';
				$response = array(
					'error' => $message
				);
				exit(json_encode($response));
			}
	}
}