99 lines
2.8 KiB
PHP
99 lines
2.8 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
|
|
{
|
|
/** @var list<string> */
|
|
private const KINDS = ['c2', 'xxbb'];
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$kind = $this->kind($request);
|
|
$path = trim((string) $request->query('path', ''));
|
|
$device = trim((string) $request->query('device', ''));
|
|
|
|
$groups = [];
|
|
foreach (self::KINDS as $k) {
|
|
$groups[$k] = $this->listFiles($k);
|
|
}
|
|
|
|
return view('admin.logs.index', compact('groups', 'kind', 'path', 'device'));
|
|
}
|
|
|
|
public function show(Request $request, string $file)
|
|
{
|
|
$kind = $this->kind($request);
|
|
$safe = basename($file);
|
|
if (! preg_match('/^\d{8}\.log$/', $safe)) {
|
|
abort(404);
|
|
}
|
|
$full = public_path('log/'.$kind.'/'.$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', [
|
|
'kind' => $kind,
|
|
'file' => $safe,
|
|
'entries' => $entries,
|
|
'path' => $path,
|
|
'device' => $device,
|
|
]);
|
|
}
|
|
|
|
private function kind(Request $request): string
|
|
{
|
|
$kind = (string) $request->query('kind', 'c2');
|
|
|
|
return in_array($kind, self::KINDS, true) ? $kind : 'c2';
|
|
}
|
|
|
|
/**
|
|
* @return list<array{name: string, size: int, mtime: string}>
|
|
*/
|
|
private function listFiles(string $kind): array
|
|
{
|
|
$dir = public_path('log/'.$kind);
|
|
$files = [];
|
|
if (! is_dir($dir)) {
|
|
return $files;
|
|
}
|
|
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']));
|
|
|
|
return $files;
|
|
}
|
|
}
|