306 lines
14 KiB
PHP
306 lines
14 KiB
PHP
<?php
|
|
require_once 'auth.php';
|
|
require_once 'db/config.php';
|
|
require_once 'includes/routeros_api.class.php';
|
|
|
|
// Restrict access to Administrators
|
|
if ($_SESSION['user']['role'] !== 'Administrator') {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
$page_title = 'Routers';
|
|
$errors = [];
|
|
$success = '';
|
|
|
|
$API = new RouterosAPI();
|
|
$API->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';
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<h1 class="h3 mb-0 text-gray-800"><?php echo htmlspecialchars($page_title); ?></h1>
|
|
<a href="index.php" class="btn btn-secondary"><i data-feather="arrow-left" class="mr-1"></i> Kembali</a>
|
|
</div>
|
|
<hr>
|
|
|
|
<?php if (!empty($errors)):
|
|
foreach ($errors as $error):
|
|
?><div class="alert alert-danger"><p class="mb-0"><?php echo $error; ?></p></div><?php
|
|
endforeach;
|
|
endif; ?>
|
|
|
|
<?php if ($success):
|
|
?><div class="alert alert-success"><?php echo $success; ?></div><?php
|
|
endif; ?>
|
|
|
|
<div class="row">
|
|
<div class="col-md-4">
|
|
<div class="card shadow mb-4">
|
|
<div class="card-header py-3">
|
|
<h6 class="m-0 font-weight-bold text-primary"><?php echo $edit_router ? 'Edit Router' : 'Add New Router'; ?></h6>
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="routers.php" method="POST">
|
|
<input type="hidden" name="id" value="<?php echo $edit_router['id'] ?? ''; ?>">
|
|
<div class="form-group">
|
|
<label for="name">Router Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($edit_router['name'] ?? ''); ?>" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="ip_address">IP Address</label>
|
|
<input type="text" class="form-control" id="ip_address" name="ip_address" value="<?php echo htmlspecialchars($edit_router['ip_address'] ?? ''); ?>" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="username">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($edit_router['username'] ?? ''); ?>" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" class="form-control" id="password" name="password" <?php echo $edit_router ? '' : 'required'; ?> >
|
|
<?php if ($edit_router):
|
|
?><small class="form-text text-muted">Leave blank to keep the current password.</small><?php
|
|
endif; ?>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="description">Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"><?php echo htmlspecialchars($edit_router['description'] ?? ''); ?></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary"><?php echo $edit_router ? 'Update Router' : 'Add Router'; ?></button>
|
|
<?php if ($edit_router):
|
|
?><a href="routers.php" class="btn btn-secondary">Cancel Edit</a><?php
|
|
endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-8">
|
|
<div class="card shadow mb-4">
|
|
<div class="card-header py-3">
|
|
<h6 class="m-0 font-weight-bold text-primary">Router List</h6>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>IP Address</th>
|
|
<th>Username</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($routers as $router):
|
|
?><?php /* Check if $routers is empty before rendering rows */ if (!empty($routers)) { ?><?php // This check is redundant if the loop condition is correct, but kept for clarity if needed
|
|
?><tr >
|
|
<td><?php echo htmlspecialchars($router['name']); ?></td>
|
|
<td><?php echo htmlspecialchars($router['ip_address']); ?></td>
|
|
<td><?php echo htmlspecialchars($router['username']); ?></td>
|
|
<td>
|
|
<button class="btn btn-info btn-sm test-connection" data-id="<?php echo $router['id']; ?>" title="Test Connection">
|
|
<i data-feather="zap"></i>
|
|
</button>
|
|
<a href="routers.php?action=edit&id=<?php echo $router['id']; ?>" class="btn btn-warning btn-sm" title="Edit">
|
|
<i data-feather="edit-2"></i>
|
|
</a>
|
|
<a href="#" class="btn btn-danger btn-sm delete-router" data-id="<?php echo $router['id']; ?>" title="Delete">
|
|
<i data-feather="trash-2"></i>
|
|
</a>
|
|
</td>
|
|
</tr><?php } ?><?php endforeach; ?>
|
|
<?php if (empty($routers)):
|
|
?><td colspan="4" class="text-center">No routers found. Add one to get started.</td><?php
|
|
endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Delete Confirmation Modal -->
|
|
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog">
|
|
<div class="modal-dialog" role="document">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Confirm Delete</h5>
|
|
<button type="button" class="close" data-dismiss="modal">
|
|
<span>×</span>
|
|
</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p>Are you sure you want to delete this router?</p>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
|
|
<a href="#" id="confirmDelete" class="btn btn-danger">Delete</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Test Connection Modal -->
|
|
<div class="modal fade" id="testResultModal" tabindex="-1" role="dialog">
|
|
<div class="modal-dialog" role="document">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Connection Test Result</h5>
|
|
<button type="button" class="close" data-dismiss="modal">
|
|
<span>×</span>
|
|
</button>
|
|
</div>
|
|
<div class="modal-body" id="testResultBody">
|
|
<!-- Result will be injected here -->
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'partials/footer.php'; ?>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Delete modal
|
|
const deleteButtons = document.querySelectorAll('.delete-router');
|
|
const confirmDelete = document.getElementById('confirmDelete');
|
|
deleteButtons.forEach(button => {
|
|
button.addEventListener('click', function(e) {
|
|
e.preventDefault();
|
|
const id = this.dataset.id;
|
|
confirmDelete.href = `routers.php?action=delete&id=${id}`;
|
|
new bootstrap.Modal(document.getElementById('deleteModal')).show();
|
|
});
|
|
});
|
|
|
|
// Test connection
|
|
const testButtons = document.querySelectorAll('.test-connection');
|
|
const testResultBody = document.getElementById('testResultBody');
|
|
const testResultModal = new bootstrap.Modal(document.getElementById('testResultModal'));
|
|
|
|
testButtons.forEach(button => {
|
|
button.addEventListener('click', function() {
|
|
const id = this.dataset.id;
|
|
testResultBody.innerHTML = '<p>Testing connection...</p>';
|
|
testResultModal.show();
|
|
|
|
fetch(`routers.php?action=test_connection&id=${id}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
let content = `<h6>${data.message}</h6>`;
|
|
if (data.success && data.data) {
|
|
content += '<pre class="bg-light p-2 rounded"><code class="json">';
|
|
content += `Board Name: ${data.data['board-name']}\n`;
|
|
content += `Version: ${data.data['version']}\n`;
|
|
content += `Uptime: ${data.data['uptime']}`;
|
|
content += '</code></pre>';
|
|
}
|
|
testResultBody.innerHTML = content;
|
|
})
|
|
.catch(error => {
|
|
testResultBody.innerHTML = '<p class="text-danger">An error occurred while testing the connection.</p>';
|
|
console.error('Error:', error);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
</script>
|