logFilename = $logFilename; $this->logTitle = $logTitle ?? $logFilename; $this->loadLogContent(); } public function loadLogContent() { $logPath = storage_path('logs/' . $this->logFilename); // Security: prevent directory traversal attacks $realPath = realpath($logPath); $logsDir = realpath(storage_path('logs')); if (!$realPath || strpos($realPath, $logsDir) !== 0) { $this->message = 'Error: Invalid log file path'; return; } if (file_exists($logPath)) { // Get file info $this->fileSize = $this->formatBytes(filesize($logPath)); $this->lastModified = date('Y-m-d H:i:s', filemtime($logPath)); // Load log content using tail (avoid file() which loads entire file into memory) $logLines = (int) timebank_config('admin_settings.log_lines', 100); $this->logContent = shell_exec("tail -n {$logLines} " . escapeshellarg($logPath)); $recentLines = explode("\n", $this->logContent ?? ''); // Check for warnings/errors in log content $messages = []; if (collect($recentLines)->contains(fn ($line) => stripos($line, 'WARNING') !== false)) { $messages[] = 'Warning detected in the recent log output'; } if (collect($recentLines)->contains(fn ($line) => stripos($line, 'ERROR') !== false)) { $messages[] = 'Error detected in the recent log output'; } if (collect($recentLines)->contains(fn ($line) => stripos($line, 'CRITICAL') !== false)) { $messages[] = 'Critical issue detected in the recent log output'; } if (collect($recentLines)->contains(fn ($line) => stripos($line, 'ALERT') !== false)) { $messages[] = 'Alert detected in the recent log output - IMMEDIATE ACTION REQUIRED'; } if (!empty($messages)) { $this->message = implode('
', $messages); } } else { $this->message = 'Log file not found or empty'; } } public function downloadLog() { $logPath = storage_path('logs/' . $this->logFilename); // Security: prevent directory traversal attacks $realPath = realpath($logPath); $logsDir = realpath(storage_path('logs')); if (!$realPath || strpos($realPath, $logsDir) !== 0) { session()->flash('error', 'Invalid log file path'); return; } if (file_exists($logPath)) { return response()->download($logPath, $this->logFilename . '-' . date('Y-m-d') . '.log'); } session()->flash('error', 'Log file not found'); } public function refreshLog() { $this->loadLogContent(); $this->dispatch('logRefreshed'); } // Helper to format bytes public function formatBytes($bytes, $precision = 2) { $units = ['B', 'KB', 'MB', 'GB', 'TB']; $bytes = max($bytes, 0); $pow = $bytes > 0 ? floor(log($bytes) / log(1024)) : 0; $pow = min($pow, count($units) - 1); $bytes /= (1 << (10 * $pow)); return round($bytes, $precision) . ' ' . $units[$pow]; } public function render() { return view('livewire.admin.log-viewer'); } }