false]); exit; } $msgs = json_decode(file_get_contents($chatFile), true) ?: []; // comando if (strpos($msg, '/') === 0) { $cmd = strtolower(explode(' ', $msg)[0]); if ($cmd === '/clear') { file_put_contents($chatFile, json_encode([])); echo json_encode(['ok'=>true,'clear'=>true]); exit; } if ($cmd === '/help') { echo json_encode(['ok'=>true,'system'=>"Comandos: /clear - borrar chat | /help - ayuda | /nick [nombre] - cambiar nombre | /time - hora"]); exit; } } $msgs[] = ['user'=>$user, 'msg'=>$msg, 'time'=>date('H:i:s'), 'date'=>date('d/m/Y'), 'ts'=>time()]; if (count($msgs) > 200) $msgs = array_slice($msgs, -200); file_put_contents($chatFile, json_encode($msgs)); echo json_encode(['ok'=>true]); exit; } if ($action === 'get') { $since = intval($_POST['since'] ?? 0); $msgs = json_decode(file_get_contents($chatFile), true) ?: []; $new = array_filter($msgs, fn($m) => $m['ts'] > $since); echo json_encode(['msgs'=>array_values($msgs), 'new'=>array_values($new)]); exit; } } ?>