45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
include 'auth.php';
|
|
|
|
function bg_scan_events_payload() {
|
|
$res = bg_inotify_monitor_events(220);
|
|
$status = bg_inotify_monitor_status();
|
|
return [
|
|
'ok' => ((int)($res['code'] ?? 1) === 0),
|
|
'events' => (string)($res['output'] ?? ''),
|
|
'status' => (string)($status['output'] ?? ''),
|
|
'code' => (int)($res['code'] ?? 0),
|
|
'time' => date('c'),
|
|
];
|
|
}
|
|
|
|
if (isset($_GET['stream']) && $_GET['stream'] === '1') {
|
|
@set_time_limit(0);
|
|
@ini_set('output_buffering', 'off');
|
|
@ini_set('zlib.output_compression', '0');
|
|
header('Content-Type: text/event-stream; charset=utf-8');
|
|
header('Cache-Control: no-cache, no-transform');
|
|
header('X-Accel-Buffering: no');
|
|
|
|
$lastHash = '';
|
|
for ($i = 0; $i < 90; $i++) {
|
|
if (connection_aborted()) break;
|
|
$payload = bg_scan_events_payload();
|
|
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
$hash = sha1($json ?: '');
|
|
if ($hash !== $lastHash || $i % 15 === 0) {
|
|
echo "event: scan\n";
|
|
echo 'data: ' . $json . "\n\n";
|
|
$lastHash = $hash;
|
|
@ob_flush(); @flush();
|
|
} else {
|
|
echo ": heartbeat\n\n";
|
|
@ob_flush(); @flush();
|
|
}
|
|
sleep(1);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode(bg_scan_events_payload(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|