logFilename = $logFilename;
if (!$this->hasLogsPermission()) {
abort(403, 'Unauthorized');
}
$this->logTitle = $logTitle ?? $logFilename;
$this->loadLogContent();
}
protected function hasLogsPermission(): bool
{
$permission = match (true) {
str_contains($this->logFilename, 'inactive-profiles') => 'view inactive-profiles-log',
str_contains($this->logFilename, 'scout-reindex') => 'view search-reindex-log',
default => null,
};
if (!$permission) return false;
$webUser = \Illuminate\Support\Facades\Auth::guard('web')->user();
$activeId = session('activeProfileId');
if (!$webUser || !$activeId) return false;
$prefix = 'Admin\\' . $activeId . '\\';
$scopedRoles = $webUser->roles->filter(fn($r) => str_starts_with($r->name, $prefix));
foreach ($scopedRoles as $scopedRole) {
$roleName = str_replace($prefix, '', $scopedRole->name);
$adminRole = \App\Models\AdminRole::where('name', $roleName)->first();
if ($adminRole && in_array($permission, $adminRole->permissions ?? [])) {
return true;
}
}
return false;
}
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');
}
}