debug = false; // Handle Test Connection (AJAX) if (isset($_GET['action']) && $_GET['action'] == 'test_connection') { header('Content-Type: application/json'); $id = $_GET['id'] ?? 0; $stmt = db()->prepare("SELECT * FROM routers WHERE id = ?"); $stmt->execute([$id]); $router = $stmt->fetch(); if ($router) { $password = decrypt($router['password']); if ($API->connect($router['ip_address'], $router['username'], $password)) { $API->write('/system/resource/print'); $resource = $API->read(); $API->disconnect(); echo json_encode(['success' => true, 'message' => 'Connection successful!', 'data' => $resource[0]]); } else { echo json_encode(['success' => false, 'message' => 'Connection failed. Check IP, username, and password.']); } } else { echo json_encode(['success' => false, 'message' => 'Router not found.']); } exit; } // Handle form submissions (Add/Edit) if ($_SERVER['REQUEST_METHOD'] === 'POST') { $id = $_POST['id'] ?? null; $name = trim($_POST['name']); $ip_address = trim($_POST['ip_address']); $username = trim($_POST['username']); $password = $_POST['password']; $description = trim($_POST['description']); if (empty($name)) $errors[] = 'Router name is required.'; if (empty($ip_address) || !filter_var($ip_address, FILTER_VALIDATE_IP)) $errors[] = 'A valid IP address is required.'; if (empty($username)) $errors[] = 'Username is required.'; if (empty($id) && empty($password)) $errors[] = 'Password is required for a new router.'; if (empty($errors)) { if ($id) { // Update if (!empty($password)) { $encrypted_password = encrypt($password); $stmt = db()->prepare("UPDATE routers SET name = ?, ip_address = ?, username = ?, password = ?, description = ? WHERE id = ?"); $stmt->execute([$name, $ip_address, $username, $encrypted_password, $description, $id]); } else { $stmt = db()->prepare("UPDATE routers SET name = ?, ip_address = ?, username = ?, description = ? WHERE id = ?"); $stmt->execute([$name, $ip_address, $username, $description, $id]); } $success = "Router updated successfully!"; } else { // Insert $encrypted_password = encrypt($password); $stmt = db()->prepare("INSERT INTO routers (name, ip_address, username, password, description) VALUES (?, ?, ?, ?, ?)"); try { $stmt->execute([$name, $ip_address, $username, $encrypted_password, $description]); $success = "Router added successfully!"; } catch (PDOException $e) { if ($e->errorInfo[1] == 1062) { // Duplicate entry $errors[] = "A router with this IP address already exists."; } else { $errors[] = "Database error: " . $e->getMessage(); } } } } } // Handle Delete if (isset($_GET['action']) && $_GET['action'] == 'delete') { $id = $_GET['id'] ?? 0; $stmt = db()->prepare("DELETE FROM routers WHERE id = ?"); $stmt->execute([$id]); header('Location: routers.php?deleted=true'); exit; } if(isset($_GET['deleted'])) { $success = "Router deleted successfully!"; } // Fetch all routers $routers = db()->query("SELECT * FROM routers ORDER BY name ASC")->fetchAll(); // Fetch router for editing $edit_router = null; if (isset($_GET['action']) && $_GET['action'] == 'edit') { $id = $_GET['id'] ?? 0; $stmt = db()->prepare("SELECT * FROM routers WHERE id = ?"); $stmt->execute([$id]); $edit_router = $stmt->fetch(); } require_once 'partials/header.php'; ?>

Kembali

> Leave blank to keep the current password.
Cancel Edit
Router List
Name IP Address Username Actions
No routers found. Add one to get started.