Files
wevads-platform/scripts/cloudflare-accounts-add.php
2026-02-26 04:53:11 +01:00

218 lines
12 KiB
PHP
Executable File

<?php
session_start();
$base_url = 'http://89.167.40.150:5821';
// Connexion DB
try {
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
die("Database connection failed: " . $e->getMessage());
}
$account = ['id' => '', 'name' => '', 'api_email' => '', 'api_key' => '', 'account_id' => '', 'status' => 'Activated'];
$is_edit = false;
$error = '';
$success = '';
// Charger compte existant pour édition
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$stmt = $pdo->prepare("SELECT * FROM admin.cloudflare_accounts WHERE id = ?");
$stmt->execute([$_GET['id']]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
if ($account) $is_edit = true;
}
// Traitement du formulaire
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$api_email = trim($_POST['api_email'] ?? '');
$api_key = trim($_POST['api_key'] ?? '');
$account_id = trim($_POST['account_id'] ?? '');
$status = $_POST['status'] ?? 'Activated';
$id = $_POST['id'] ?? '';
if (empty($name) || empty($api_email) || empty($api_key)) {
$error = 'Name, API Email and API Key are required';
} else {
try {
if ($id) {
// Update
$stmt = $pdo->prepare("UPDATE admin.cloudflare_accounts SET name = ?, api_email = ?, api_key = ?, account_id = ?, status = ?, last_updated_date = CURRENT_DATE WHERE id = ?");
$stmt->execute([$name, $api_email, $api_key, $account_id, $status, $id]);
} else {
// Insert
$stmt = $pdo->prepare("INSERT INTO admin.cloudflare_accounts (name, api_email, api_key, account_id, status) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$name, $api_email, $api_key, $account_id, $status]);
}
header("Location: cloudflare-accounts.php?saved=1");
exit;
} catch (Exception $e) {
$error = 'Database error: ' . $e->getMessage();
}
}
// Repopulate form on error
$account = ['id' => $id, 'name' => $name, 'api_email' => $api_email, 'api_key' => $api_key, 'account_id' => $account_id, 'status' => $status];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WEVADS APP | <?php echo $is_edit ? 'Edit' : 'Add'; ?> Cloudflare Account</title>
<meta content="width=device-width, initial-scale=1" name="viewport" />
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js"></script>
<script>WebFont.load({google: {"families":["Poppins:300,400,500,600,700"]}});</script>
<link href="<?php echo $base_url; ?>/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
<link href="<?php echo $base_url; ?>/plugins/fontawesome5/css/all.min.css" rel="stylesheet" />
<link href="<?php echo $base_url; ?>/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link rel="shortcut icon" href="<?php echo $base_url; ?>/images/logos/favicon.ico" />
<style>
body { background: #f1f5f9 !important; font-family: 'Poppins', sans-serif; }
.page-container { max-width: 800px; margin: 0 auto; padding: 20px; }
.back-link { color: #00e5ff; text-decoration: none; display: inline-flex; align-items: center; margin-bottom: 20px; }
.back-link:hover { color: #1e3a5f; }
.back-link i { margin-right: 8px; }
.page-header { background: #ffffff; border-radius: 8px; padding: 25px; margin-bottom: 25px; border: 1px solid #e2e8f0; }
.page-header h1 { color: #00e5ff; margin: 0; font-size: 24px; }
.page-header p { color: #64748b; margin: 5px 0 0 0; }
.card-light { background: #ffffff; border-radius: 8px; border: 1px solid #e2e8f0; }
.card-light .card-header { padding: 15px 20px; border-bottom: 1px solid #e2e8f0; background: #f8fafc; border-radius: 8px 8px 0 0; }
.card-light .card-header h4 { margin: 0; color: #1e293b; font-size: 16px; }
.card-light .card-header h4 i { color: #00e5ff; margin-right: 10px; }
.card-light .card-body { padding: 20px; }
.form-group { margin-bottom: 20px; }
.form-group label { color: #1e293b; font-weight: 500; margin-bottom: 8px; display: block; font-size: 13px; }
.form-group label i { color: #00e5ff; margin-right: 5px; }
.form-group .hint { color: #64748b; font-size: 12px; margin-top: 5px; }
.form-control { border: 1px solid #e2e8f0; border-radius: 6px; padding: 10px 15px; width: 100%; font-size: 14px; }
.form-control:focus { border-color: #00e5ff; box-shadow: 0 0 0 3px rgba(0,229,255,0.1); outline: none; }
select.form-control { height: 42px; }
.btn-save { background: linear-gradient(135deg, #00e5ff 0%, #00b8d4 100%); color: #fff; border: none; padding: 12px 30px; border-radius: 6px; font-weight: 600; cursor: pointer; font-size: 15px; }
.btn-save:hover { background: linear-gradient(135deg, #00b8d4 0%, #0097a7 100%); }
.btn-cancel { background: #e2e8f0; color: #475569; border: none; padding: 12px 30px; border-radius: 6px; font-weight: 600; text-decoration: none; margin-left: 10px; }
.btn-cancel:hover { background: #cbd5e1; color: #1e293b; text-decoration: none; }
.btn-test { background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%); color: #fff; border: none; padding: 8px 15px; border-radius: 6px; font-weight: 500; cursor: pointer; font-size: 13px; }
.btn-test:hover { background: linear-gradient(135deg, #7c3aed 0%, #6d28d9 100%); }
.alert-error { background: #fee2e2; border: 1px solid #ef4444; color: #991b1b; padding: 12px 15px; border-radius: 6px; margin-bottom: 20px; }
.alert-success { background: #d1fae5; border: 1px solid #10b981; color: #065f46; padding: 12px 15px; border-radius: 6px; margin-bottom: 20px; }
.alert-info { background: #e0f2fe; border: 1px solid #0ea5e9; color: #0369a1; padding: 12px 15px; border-radius: 6px; margin-bottom: 20px; }
.form-actions { display: flex; align-items: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid #e2e8f0; }
#test-result { margin-top: 10px; }
</style>
</head>
<body>
<div class="page-container">
<a href="cloudflare-accounts.php" class="back-link"><i class="fa fa-arrow-left"></i> Back to Accounts</a>
<div class="page-header">
<h1><i class="fab fa-cloudflare"></i> <?php echo $is_edit ? 'Edit' : 'Add'; ?> Cloudflare Account</h1>
<p><?php echo $is_edit ? 'Update your Cloudflare API credentials' : 'Add a new Cloudflare account for DNS management'; ?></p>
</div>
<?php if ($error): ?>
<div class="alert-error"><i class="fa fa-exclamation-circle"></i> <?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<div class="card-light">
<div class="card-header">
<h4><i class="fa fa-key"></i> API Credentials</h4>
</div>
<div class="card-body">
<div class="alert-info">
<i class="fa fa-info-circle"></i> <strong>How to get your API Key:</strong><br>
1. Go to <a href="https://dash.cloudflare.com/profile/api-tokens" target="_blank">Cloudflare Dashboard → Profile → API Tokens</a><br>
2. Scroll to "Global API Key" and click "View"<br>
3. Copy the key and paste it below
</div>
<form method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($account['id']); ?>">
<div class="form-group">
<label><i class="fa fa-tag"></i> Account Name</label>
<input type="text" name="name" class="form-control" placeholder="My Cloudflare Account" value="<?php echo htmlspecialchars($account['name']); ?>" required>
<div class="hint">A friendly name to identify this account</div>
</div>
<div class="form-group">
<label><i class="fa fa-envelope"></i> API Email</label>
<input type="email" name="api_email" class="form-control" placeholder="your@email.com" value="<?php echo htmlspecialchars($account['api_email']); ?>" required>
<div class="hint">The email address associated with your Cloudflare account</div>
</div>
<div class="form-group">
<label><i class="fa fa-key"></i> Global API Key</label>
<input type="text" name="api_key" id="api_key" class="form-control" placeholder="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" value="<?php echo htmlspecialchars($account['api_key']); ?>" required>
<div class="hint">Your Cloudflare Global API Key (not API Token)</div>
</div>
<div class="form-group">
<label><i class="fa fa-id-card"></i> Account ID (Optional)</label>
<input type="text" name="account_id" class="form-control" placeholder="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" value="<?php echo htmlspecialchars($account['account_id']); ?>">
<div class="hint">Optional: Found in Cloudflare dashboard URL or overview page</div>
</div>
<div class="form-group">
<label><i class="fa fa-toggle-on"></i> Status</label>
<select name="status" class="form-control">
<option value="Activated" <?php echo $account['status'] == 'Activated' ? 'selected' : ''; ?>>Activated</option>
<option value="Disabled" <?php echo $account['status'] == 'Disabled' ? 'selected' : ''; ?>>Disabled</option>
</select>
</div>
<div class="form-group">
<button type="button" id="btn-test" class="btn-test"><i class="fa fa-plug"></i> Test Connection</button>
<div id="test-result"></div>
</div>
<div class="form-actions">
<button type="submit" class="btn-save"><i class="fa fa-save"></i> <?php echo $is_edit ? 'Update' : 'Save'; ?> Account</button>
<a href="cloudflare-accounts.php" class="btn-cancel">Cancel</a>
</div>
</form>
</div>
</div>
</div>
<script src="<?php echo $base_url; ?>/plugins/jquery.min.js"></script>
<script>
$('#btn-test').on('click', function() {
var btn = $(this);
var email = $('input[name="api_email"]').val();
var key = $('input[name="api_key"]').val();
if (!email || !key) {
$('#test-result').html('<div class="alert-error"><i class="fa fa-times"></i> Please enter email and API key first</div>');
return;
}
btn.html('<i class="fa fa-spinner fa-spin"></i> Testing...').prop('disabled', true);
$('#test-result').html('<div class="alert-info"><i class="fa fa-spinner fa-spin"></i> Connecting to Cloudflare...</div>');
$.ajax({
url: 'cloudflare-test.php',
method: 'POST',
data: { email: email, api_key: key },
dataType: 'json',
success: function(r) {
if (r.status === 'success') {
$('#test-result').html('<div class="alert-success"><i class="fa fa-check-circle"></i> ' + r.message + '</div>');
} else {
$('#test-result').html('<div class="alert-error"><i class="fa fa-times-circle"></i> ' + r.message + '</div>');
}
},
error: function() {
$('#test-result').html('<div class="alert-error"><i class="fa fa-times-circle"></i> Connection error</div>');
},
complete: function() {
btn.html('<i class="fa fa-plug"></i> Test Connection').prop('disabled', false);
}
});
});
</script>
</body>
</html>