<?php
$file = "/var/www/html/mikrotik/views/device_detail.php";
$content = file_get_contents($file);

// Corrige a linha quebrada onde $url ficou vazio
$old = "     = NMS_URL . '?page=nms_device_detail&id=' . \$id;
    echo '<script>location.href=' . json_encode(\$url) . ';</script>';
    exit;";

$new = "    \$url = NMS_URL . '?page=nms_device_detail&id=' . \$id;
    echo '<script>location.href=' . json_encode(\$url) . ';</script>';
    exit;";

if (strpos($content, $old) !== false) {
    $content = str_replace($old, $new, $content);
    file_put_contents($file, $content);
    echo "PATCH_OK\n";
} else {
    echo "NAO_ENCONTRADO — conteudo atual:\n";
    preg_match('/NMS_URL.*?exit;/s', $content, $m);
    echo htmlspecialchars($m[0] ?? "nada encontrado");
}
