Files
coruna-lab/app/Http/Controllers/Admin/RawLogController.php
T
hashbro 74a67c3900 init
2026-08-08 05:09:01 +08:00

72 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
class RawLogController extends Controller
{
public function index(Request $request)
{
$dir = public_path('log/c2');
$files = [];
if (is_dir($dir)) {
foreach (File::files($dir) as $file) {
if ($file->getExtension() !== 'log') {
continue;
}
$files[] = [
'name' => $file->getFilename(),
'size' => $file->getSize(),
'mtime' => date('Y-m-d H:i:s', $file->getMTime()),
];
}
usort($files, fn ($a, $b) => strcmp($b['name'], $a['name']));
}
$path = trim((string) $request->query('path', ''));
$device = trim((string) $request->query('device', ''));
return view('admin.logs.index', compact('files', 'path', 'device'));
}
public function show(Request $request, string $file)
{
$safe = basename($file);
if (! preg_match('/^\d{8}\.log$/', $safe)) {
abort(404);
}
$full = public_path('log/c2/'.$safe);
abort_unless(is_file($full), 404);
$path = trim((string) $request->query('path', ''));
$device = trim((string) $request->query('device', ''));
$raw = (string) file_get_contents($full);
$chunks = preg_split("/\r\n\r\n/", $raw) ?: [];
$entries = [];
foreach ($chunks as $chunk) {
$chunk = trim($chunk);
if ($chunk === '') {
continue;
}
if ($path !== '' && ! str_contains($chunk, $path)) {
continue;
}
if ($device !== '' && ! str_contains($chunk, $device)) {
continue;
}
$entries[] = $chunk;
}
$entries = array_reverse($entries);
return view('admin.logs.show', [
'file' => $safe,
'entries' => $entries,
'path' => $path,
'device' => $device,
]);
}
}