logsPath = WRITEPATH . 'logs/'; } public function index() { $files = []; if (is_dir($this->logsPath)) { $items = scandir($this->logsPath); foreach ($items as $item) { if ($item === '.' || $item === '..') { continue; } $fullPath = $this->logsPath . $item; if (is_file($fullPath)) { $files[] = [ 'name' => $item, 'size' => $this->formatSize(filesize($fullPath)), 'modified' => date('Y-m-d H:i:s', filemtime($fullPath)), ]; } } } usort($files, function ($a, $b) { return strcmp($b['modified'], $a['modified']); }); $showPayment = $this->request->getGet('payment') === '1'; $entries = []; $lineCount = 0; if ($showPayment) { foreach ($files as $file) { $fullPath = $this->logsPath . $file['name']; $lines = $this->extractPaymentLogLines($fullPath); if (!empty($lines)) { $entries[] = [ 'file' => $file['name'], 'lines' => $lines, ]; $lineCount += count($lines); } } } $data['files'] = $files; $data['showPayment'] = $showPayment; $data['entries'] = $entries; $data['lineCount'] = $lineCount; $data['page_name'] = $showPayment ? 'Payment Logs' : 'Log Files'; $this->render_page('logviewer/index', $data); } public function view($filename = null) { $filename = $this->sanitizeFilename($filename); if (!$filename) { return redirect()->to('/logs')->with('error', 'Invalid file name.'); } $fullPath = $this->logsPath . $filename; if (!is_file($fullPath)) { return redirect()->to('/logs')->with('error', 'File not found.'); } $showPayment = $this->request->getGet('payment') === '1'; $content = file_get_contents($fullPath); if ($showPayment) { $lines = $this->extractPaymentLogLines($fullPath); $content = !empty($lines) ? implode("\n", $lines) : ''; } $data['filename'] = $filename; $data['content'] = $content; $data['showPayment'] = $showPayment; $data['page_name'] = $showPayment ? 'Payment Logs' : 'View Log'; $this->render_page('logviewer/view', $data); } public function download($filename = null) { $filename = $this->sanitizeFilename($filename); if (!$filename) { return redirect()->to('/logs')->with('error', 'Invalid file name.'); } $fullPath = $this->logsPath . $filename; if (!is_file($fullPath)) { return redirect()->to('/logs')->with('error', 'File not found.'); } return $this->response->download($fullPath, null); } private function extractPaymentLogLines(string $fullPath): array { if (!is_readable($fullPath)) { return []; } $lines = []; $handle = fopen($fullPath, 'r'); if ($handle === false) { return []; } while (($line = fgets($handle)) !== false) { if (strpos($line, '[PAYMENT]') !== false) { $lines[] = rtrim($line, "\r\n"); } } fclose($handle); return $lines; } private function sanitizeFilename($filename) { if (!$filename) { return false; } $filename = basename($filename); if (!preg_match('/^[A-Za-z0-9_\-\.]+\.(log|php|txt|html)$/', $filename)) { return false; } return $filename; } private function formatSize($bytes) { if ($bytes >= 1073741824) { return number_format($bytes / 1073741824, 2) . ' GB'; } elseif ($bytes >= 1048576) { return number_format($bytes / 1048576, 2) . ' MB'; } elseif ($bytes >= 1024) { return number_format($bytes / 1024, 2) . ' KB'; } return $bytes . ' bytes'; } }