201 lines
9.8 KiB
PHP
201 lines
9.8 KiB
PHP
<?php
|
|
require_once __DIR__ . '/api/keys.php';
|
|
|
|
$generated_key = null;
|
|
$error = null;
|
|
$success = null;
|
|
|
|
// Handle POST requests for key management
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['generate_api_key'])) {
|
|
$keyName = trim($_POST['key_name']);
|
|
$integrationType = trim($_POST['integration_type']);
|
|
$result = generateApiKey($keyName, $integrationType);
|
|
if ($result['success']) {
|
|
$generated_key = $result;
|
|
$success = "Successfully generated new API key. Please copy it now, it won't be shown again.";
|
|
} else {
|
|
$error = $result['message'];
|
|
}
|
|
} elseif (isset($_POST['deactivate_api_key'])) {
|
|
$keyId = $_POST['key_id'];
|
|
$result = deactivateApiKey($keyId);
|
|
if ($result['success']) {
|
|
$success = $result['message'];
|
|
} else {
|
|
$error = $result['message'];
|
|
}
|
|
} elseif (isset($_POST['delete_api_key'])) {
|
|
$keyId = $_POST['key_id'];
|
|
$result = deleteApiKey($keyId);
|
|
if ($result['success']) {
|
|
$success = $result['message'];
|
|
} else {
|
|
$error = $result['message'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch all existing API keys to display
|
|
$api_keys = listApiKeys();
|
|
$project_name = "HVAC Command Center";
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>API Key Management - <?= htmlspecialchars($project_name) ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<header class="header text-white">
|
|
<div class="container-fluid">
|
|
<h1 class="display-6 m-0"><?= htmlspecialchars($project_name) ?></h1>
|
|
</div>
|
|
</header>
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
|
<div class="container-fluid">
|
|
<a class="navbar-brand" href="index.php">Dashboard</a>
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav">
|
|
<li class="nav-item"><a class="nav-link" href="customers.php">Customers</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="bookings.php">Bookings</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="ai-call-logs.php">AI Call Logs</a></li>
|
|
<li class="nav-item"><a class="nav-link active" href="api-keys.php">API Keys</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container-fluid mt-4">
|
|
<h2 class="mb-4"><i class="bi bi-key-fill me-2"></i>API Key Management</h2>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><i class="bi bi-exclamation-triangle-fill me-2"></i><?= htmlspecialchars($error) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success"><i class="bi bi-check-circle-fill me-2"></i><?= htmlspecialchars($success) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($generated_key): ?>
|
|
<div class="alert alert-warning" id="generated-key-alert">
|
|
<h5 class="alert-heading"><i class="bi bi-shield-lock-fill me-2"></i>Your New API Key</h5>
|
|
<p>Please copy it now. It will not be shown again.</p>
|
|
<div class="input-group">
|
|
<input type="text" id="new-api-key" class="form-control form-control-lg bg-white" value="<?= htmlspecialchars($generated_key['api_key']) ?>" readonly style="font-family: monospace;">
|
|
<button class="btn btn-dark" type="button" onclick="copyToClipboard()">
|
|
<i class="bi bi-clipboard-check-fill me-2"></i>Copy
|
|
</button>
|
|
</div>
|
|
<hr>
|
|
<p class="mb-0">Use this key in the <code>Authorization</code> header as a Bearer token: <code>Bearer <?= htmlspecialchars($generated_key['api_key']) ?></code></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header">
|
|
<h5 class="m-0">Create New API Key</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST" action="api-keys.php">
|
|
<div class="row g-3 align-items-end">
|
|
<div class="col-md-5">
|
|
<label for="key_name" class="form-label">Key Name</label>
|
|
<input type="text" class="form-control" id="key_name" name="key_name" placeholder="e.g., N8N Production" required>
|
|
</div>
|
|
<div class="col-md-5">
|
|
<label for="integration_type" class="form-label">Integration Type</label>
|
|
<select class="form-select" id="integration_type" name="integration_type" required>
|
|
<option value="n8n">n8n</option>
|
|
<option value="callrail">CallRail</option>
|
|
<option value="twilio">Twilio</option>
|
|
<option value="giveme5">Giveme5.ai</option>
|
|
<option value="google">Google</option>
|
|
<option value="other">Other</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<button type="submit" name="generate_api_key" class="btn btn-primary w-100">Generate Key</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card shadow-sm">
|
|
<div class="card-header">
|
|
<h5 class="m-0">Existing API Keys</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Integration</th>
|
|
<th>Status</th>
|
|
<th>Created</th>
|
|
<th>Last Used</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($api_keys)): ?>
|
|
<tr><td colspan="6" class="text-center text-muted">No API keys found.</td></tr>
|
|
<?php else: ?>
|
|
<?php foreach ($api_keys as $key): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($key['key_name']) ?></td>
|
|
<td><span class="badge bg-secondary"><?= htmlspecialchars($key['integration_type']) ?></span></td>
|
|
<td><span class="badge bg-<?= $key['is_active'] ? 'success' : 'secondary' ?>"><?= $key['is_active'] ? 'Active' : 'Inactive' ?></span></td>
|
|
<td><?= htmlspecialchars(date("M d, Y", strtotime($key['created_at']))) ?></td>
|
|
<td><?= $key['last_used_at'] ? htmlspecialchars(date("M d, Y H:i", strtotime($key['last_used_at']))) : 'Never' ?></td>
|
|
<td>
|
|
<form method="POST" action="api-keys.php" onsubmit="return confirm('Are you sure?');" class="d-inline">
|
|
<input type="hidden" name="key_id" value="<?= $key['id'] ?>">
|
|
<?php if ($key['is_active']): ?>
|
|
<button type="submit" name="deactivate_api_key" class="btn btn-sm btn-warning" title="Deactivate"><i class="bi bi-pause-circle-fill"></i></button>
|
|
<?php endif; ?>
|
|
<button type="submit" name="delete_api_key" class="btn btn-sm btn-danger" title="Delete"><i class="bi bi-trash-fill"></i></button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="container-fluid text-center text-muted py-3 mt-4">
|
|
<small>Powered by Flatlogic</small>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
function copyToClipboard() {
|
|
const keyInput = document.getElementById('new-api-key');
|
|
keyInput.select();
|
|
keyInput.setSelectionRange(0, 99999); // For mobile devices
|
|
document.execCommand('copy');
|
|
// Optional: Provide feedback to the user
|
|
const originalBtn = document.querySelector('#generated-key-alert button');
|
|
originalBtn.innerHTML = '<i class="bi bi-check-lg me-2"></i>Copied!';
|
|
setTimeout(() => {
|
|
originalBtn.innerHTML = '<i class="bi bi-clipboard-check-fill me-2"></i>Copy';
|
|
}, 2000);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|