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

264 lines
15 KiB
PHP
Executable File

<?php
session_start();
$base_url = 'http://89.167.40.150:5821';
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' => '', 'status' => 'Activated', 'region' => '', 'application_key' => '', 'secret_key' => '', 'consumer_key' => '', 'project_key' => ''];
$is_edit = false;
$error = '';
// Régions Huawei Cloud
$regions = [
'af-south-1' => 'Africa - Johannesburg',
'ap-southeast-1' => 'Asia Pacific - Hong Kong',
'ap-southeast-2' => 'Asia Pacific - Bangkok',
'ap-southeast-3' => 'Asia Pacific - Singapore',
'cn-east-2' => 'China - Shanghai',
'cn-east-3' => 'China - Shanghai 2',
'cn-north-1' => 'China - Beijing',
'cn-north-4' => 'China - Beijing 4',
'cn-south-1' => 'China - Guangzhou',
'eu-west-0' => 'Europe - Paris',
'la-north-2' => 'Latin America - Mexico City',
'la-south-2' => 'Latin America - Santiago',
'na-mexico-1' => 'North America - Mexico',
'ru-northwest-2' => 'Russia - Moscow',
'sa-brazil-1' => 'South America - Sao Paulo'
];
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$stmt = $pdo->prepare("SELECT * FROM admin.huawei_accounts WHERE id = ?");
$stmt->execute([$_GET['id']]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
if ($account) $is_edit = true;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$status = $_POST['status'] ?? 'Activated';
$region = trim($_POST['region'] ?? '');
$application_key = trim($_POST['application_key'] ?? '');
$secret_key = trim($_POST['secret_key'] ?? '');
$consumer_key = trim($_POST['consumer_key'] ?? '');
$project_key = trim($_POST['project_key'] ?? '');
$id = $_POST['id'] ?? '';
if (empty($name) || empty($region) || empty($application_key) || empty($secret_key) || empty($consumer_key)) {
$error = 'Name, Region, Application Key, Secret Key and Consumer Key are required';
} else {
try {
if ($id) {
$stmt = $pdo->prepare("UPDATE admin.huawei_accounts SET name = ?, status = ?, region = ?, application_key = ?, secret_key = ?, consumer_key = ?, project_key = ?, last_updated_by = 'admin', last_updated_date = CURRENT_DATE WHERE id = ?");
$stmt->execute([$name, $status, $region, $application_key, $secret_key, $consumer_key, $project_key, $id]);
} else {
$stmt = $pdo->prepare("INSERT INTO admin.huawei_accounts (name, status, region, application_key, secret_key, consumer_key, project_key, proxy_status, created_by, created_date) VALUES (?, ?, ?, ?, ?, ?, ?, 'Disabled', 'admin', CURRENT_DATE)");
$stmt->execute([$name, $status, $region, $application_key, $secret_key, $consumer_key, $project_key]);
}
header("Location: huawei-accounts.php?saved=1");
exit;
} catch (Exception $e) {
$error = 'Database error: ' . $e->getMessage();
}
}
$account = ['id' => $id, 'name' => $name, 'status' => $status, 'region' => $region, 'application_key' => $application_key, 'secret_key' => $secret_key, 'consumer_key' => $consumer_key, 'project_key' => $project_key];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WEVAL APP | <?php echo $is_edit ? 'Edit' : 'Add'; ?> Huawei 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/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: 900px; margin: 0 auto; padding: 20px; }
.back-link { color: #e53935; text-decoration: none; display: inline-flex; align-items: center; margin-bottom: 20px; }
.back-link:hover { color: #c62828; }
.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: #e53935; 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: #e53935; 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: #e53935; 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: #e53935; box-shadow: 0 0 0 3px rgba(229,57,53,0.1); outline: none; }
select.form-control { height: 42px; }
.btn-save { background: linear-gradient(135deg, #e53935 0%, #c62828 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, #c62828 0%, #b71c1c 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-info { background: #fef3c7; border: 1px solid #f59e0b; color: #92400e; 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; }
.row { display: flex; margin: 0 -10px; }
.col-md-6 { width: 50%; padding: 0 10px; }
#test-result { margin-top: 10px; }
</style>
</head>
<body>
<div class="page-container">
<a href="huawei-accounts.php" class="back-link"><i class="fa fa-arrow-left"></i> Back to Accounts</a>
<div class="page-header">
<h1><i class="fa fa-cloud"></i> <?php echo $is_edit ? 'Edit' : 'Add'; ?> Huawei Cloud Account</h1>
<p><?php echo $is_edit ? 'Update your Huawei Cloud API credentials' : 'Add a new Huawei Cloud account for server deployment'; ?></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 credentials:</strong><br>
1. Go to <a href="https://console.huaweicloud.com/iam/" target="_blank">Huawei Cloud IAM Console</a><br>
2. Navigate to "My Credentials" → "Access Keys"<br>
3. Create a new Access Key and download the CSV file
</div>
<form method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($account['id']); ?>">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label><i class="fa fa-tag"></i> Account Name</label>
<input type="text" name="name" class="form-control" placeholder="My Huawei Account" value="<?php echo htmlspecialchars($account['name']); ?>" required>
</div>
</div>
<div class="col-md-6">
<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>
</div>
<div class="form-group">
<label><i class="fa fa-globe"></i> Region</label>
<select name="region" class="form-control" required>
<option value="">-- Select Region --</option>
<?php foreach ($regions as $code => $name): ?>
<option value="<?php echo $code; ?>" <?php echo $account['region'] == $code ? 'selected' : ''; ?>><?php echo $name; ?> (<?php echo $code; ?>)</option>
<?php endforeach; ?>
</select>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label><i class="fa fa-key"></i> Application Key (AK)</label>
<input type="text" name="application_key" class="form-control" placeholder="ABCDEFGHIJKLMNOPQRST" value="<?php echo htmlspecialchars($account['application_key']); ?>" required>
<div class="hint">Access Key ID from your credentials</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label><i class="fa fa-lock"></i> Secret Key (SK)</label>
<input type="password" name="secret_key" class="form-control" placeholder="Your secret key" value="<?php echo htmlspecialchars($account['secret_key']); ?>" required>
<div class="hint">Secret Access Key from your credentials</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label><i class="fa fa-user"></i> Consumer Key (Domain ID)</label>
<input type="text" name="consumer_key" class="form-control" placeholder="Domain ID" value="<?php echo htmlspecialchars($account['consumer_key']); ?>" required>
<div class="hint">Your Huawei Cloud Domain ID</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label><i class="fa fa-folder"></i> Project Key (Optional)</label>
<input type="text" name="project_key" class="form-control" placeholder="Project ID" value="<?php echo htmlspecialchars($account['project_key'] ?? ''); ?>">
<div class="hint">Optional: Specific project ID</div>
</div>
</div>
</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="huawei-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 ak = $('input[name="application_key"]').val();
var sk = $('input[name="secret_key"]').val();
var region = $('select[name="region"]').val();
if (!ak || !sk || !region) {
$('#test-result').html('<div class="alert-error"><i class="fa fa-times"></i> Please fill Application Key, Secret Key and Region 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 Huawei Cloud...</div>');
$.ajax({
url: 'huawei-test.php',
method: 'POST',
data: { ak: ak, sk: sk, region: region },
dataType: 'json',
success: function(r) {
if (r.status === 'success') {
$('#test-result').html('<div style="background:#d1fae5;border:1px solid #10b981;color:#065f46;padding:12px;border-radius:6px;"><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>
<?php include("includes/chatbot-widget.php"); ?>
</body>
</html>