PNG IHDR x sBIT|d pHYs + tEXtSoftware www.inkscape.org< ,tEXtComment
<?php
// 1. Enable Error Reporting (Debug Mode)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
// 2. Check Login
if (!isset($_SESSION['admin_logged_in'])) {
header("Location: login.php");
exit;
}
// 3. Robust Database Connection
$configPath = '../config/database.php';
if (!file_exists($configPath)) {
die("<div class='p-4 bg-red-100 text-red-700'>Config file missing.</div>");
}
require_once $configPath;
try {
$database = new Database();
$db = $database->getConnection();
} catch (Exception $e) {
die("<div class='p-4 bg-red-100 text-red-700'>Database Error: " . $e->getMessage() . "</div>");
}
$message = '';
$error = '';
// 4. Handle Form Submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// --- A. Handle Password Update ---
if (isset($_POST['update_password'])) {
$new_pass = $_POST['new_password'];
$confirm_pass = $_POST['confirm_password'];
if (!empty($new_pass) && $new_pass === $confirm_pass) {
try {
$hashed = password_hash($new_pass, PASSWORD_DEFAULT);
$admin_id = $_SESSION['admin_id'] ?? 1; // Fallback ID if session empty
// Use generic update if ID not in session
if(isset($_SESSION['admin_id'])) {
$stmt = $db->prepare("UPDATE users SET password = :pwd WHERE id = :id");
$stmt->execute([':pwd' => $hashed, ':id' => $admin_id]);
} else {
// Update the default admin
$stmt = $db->prepare("UPDATE users SET password = :pwd WHERE email = 'admin@swift.com'");
$stmt->execute([':pwd' => $hashed]);
}
$message = "Password updated successfully!";
} catch (Exception $e) {
$error = "Error: " . $e->getMessage();
}
} else {
$error = "Passwords do not match.";
}
}
// --- B. Handle General Settings Update ---
else {
$keys = ['site_name', 'contact_email', 'contact_phone', 'address', 'bank_details', 'crypto_wallet'];
try {
$db->beginTransaction();
foreach ($keys as $key) {
if (isset($_POST[$key])) {
$val = $_POST[$key];
// Check if setting exists
$check = $db->prepare("SELECT id FROM settings WHERE setting_key = ?");
$check->execute([$key]);
if($check->rowCount() > 0) {
$stmt = $db->prepare("UPDATE settings SET setting_value = ? WHERE setting_key = ?");
$stmt->execute([$val, $key]);
} else {
$stmt = $db->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?)");
$stmt->execute([$key, $val]);
}
}
}
$db->commit();
$message = "Settings saved successfully!";
} catch (Exception $e) {
$db->rollBack();
$error = "Error: " . $e->getMessage();
}
}
}
// 5. Fetch Current Settings
$settings = [];
try {
$query = "SELECT setting_key, setting_value FROM settings";
$stmt = $db->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$settings[$row['setting_key']] = $row['setting_value'];
}
} catch (Exception $e) {
// Silent fail on read
}
function getVal($key, $data) {
return htmlspecialchars($data[$key] ?? '');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Settings | Ocenicargo Admin</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
<style> [x-cloak] { display: none !important; } </style>
</head>
<body class="bg-gray-100 flex h-screen overflow-hidden" x-data="{ sidebarOpen: false }">
<!-- Mobile Sidebar Overlay -->
<div x-show="sidebarOpen" @click="sidebarOpen = false" class="fixed inset-0 bg-gray-900/80 z-20 md:hidden" x-cloak></div>
<!-- Sidebar -->
<aside :class="sidebarOpen ? 'translate-x-0' : '-translate-x-full'" class="fixed inset-y-0 left-0 z-30 w-64 bg-gray-900 text-white transition-transform duration-300 ease-in-out md:relative md:translate-x-0 flex flex-col h-full">
<div class="p-6 border-b border-gray-800 flex justify-between items-center">
<span class="text-xl font-bold">Ocenicargo<span class="text-blue-500">Admin</span></span>
<button @click="sidebarOpen = false" class="md:hidden text-gray-400"><i data-lucide="x" class="w-6 h-6"></i></button>
</div>
<nav class="flex-1 p-4 space-y-2">
<a href="index.php" class="flex items-center gap-3 px-4 py-3 text-gray-400 hover:bg-gray-800 hover:text-white rounded-lg transition-colors">
<i data-lucide="layout-dashboard" class="w-5 h-5"></i> Dashboard
</a>
<a href="shipments.php" class="flex items-center gap-3 px-4 py-3 text-gray-400 hover:bg-gray-800 hover:text-white rounded-lg transition-colors">
<i data-lucide="package" class="w-5 h-5"></i> Shipments
</a>
<a href="settings.php" class="flex items-center gap-3 px-4 py-3 bg-blue-600 text-white rounded-lg shadow-md">
<i data-lucide="settings" class="w-5 h-5"></i> Settings
</a>
</nav>
<div class="p-4 border-t border-gray-800">
<a href="logout.php" class="flex items-center gap-3 px-4 py-2 text-red-400 hover:text-red-300 rounded-lg transition-colors text-sm">
<i data-lucide="log-out" class="w-5 h-5"></i> Logout
</a>
</div>
</aside>
<!-- Main Content -->
<main class="flex-1 h-full overflow-y-auto bg-gray-50 p-4 md:p-8">
<!-- Mobile Header -->
<div class="md:hidden flex items-center justify-between mb-6 pb-4 border-b border-gray-200">
<div class="flex items-center gap-3">
<button @click="sidebarOpen = true" class="text-gray-600"><i data-lucide="menu" class="w-6 h-6"></i></button>
<h1 class="text-lg font-bold text-gray-900">Settings</h1>
</div>
</div>
<div class="hidden md:block mb-8">
<h1 class="text-2xl font-bold text-gray-900">System Settings</h1>
<p class="text-sm text-gray-500 mt-1">Manage global configurations and security</p>
</div>
<!-- Alerts -->
<?php if($message): ?>
<div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg mb-6 flex items-center gap-2 animate-fade-in">
<i data-lucide="check-circle" class="w-5 h-5"></i> <?php echo $message; ?>
</div>
<?php endif; ?>
<?php if($error): ?>
<div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6 flex items-center gap-2 animate-fade-in">
<i data-lucide="alert-circle" class="w-5 h-5"></i> <?php echo $error; ?>
</div>
<?php endif; ?>
<div class="max-w-4xl space-y-8">
<!-- GENERAL SETTINGS FORM -->
<form method="POST" class="space-y-8">
<!-- 1. General Information -->
<div class="bg-white p-6 rounded-xl shadow-sm border border-gray-200">
<h2 class="text-lg font-bold text-gray-900 mb-6 border-b pb-2 flex items-center gap-2">
<i data-lucide="globe" class="w-5 h-5 text-blue-600"></i> General Information
</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1">Site Name</label>
<input type="text" name="site_name" value="<?php echo getVal('site_name', $settings); ?>" class="w-full p-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none transition-colors">
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1">Office Address</label>
<input type="text" name="address" value="<?php echo getVal('address', $settings); ?>" class="w-full p-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none transition-colors">
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1">Support Email</label>
<input type="email" name="contact_email" value="<?php echo getVal('contact_email', $settings); ?>" class="w-full p-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none transition-colors">
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1">Support Phone</label>
<input type="text" name="contact_phone" value="<?php echo getVal('contact_phone', $settings); ?>" class="w-full p-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none transition-colors">
</div>
</div>
</div>
<!-- 2. Payment Gateways -->
<div class="bg-white p-6 rounded-xl shadow-sm border border-gray-200">
<h2 class="text-lg font-bold text-gray-900 mb-6 border-b pb-2 flex items-center gap-2">
<i data-lucide="credit-card" class="w-5 h-5 text-green-600"></i> Payment Configuration
</h2>
<div class="space-y-6">
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-2">Bank Transfer Details (Visible to User)</label>
<textarea name="bank_details" rows="4" class="w-full p-3 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none font-mono text-sm bg-gray-50" placeholder="Bank Name: ... Account No: ..."><?php echo getVal('bank_details', $settings); ?></textarea>
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-2">Crypto Wallet Address (USDT - TRC20)</label>
<div class="relative">
<div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
<i data-lucide="bitcoin" class="w-4 h-4 text-gray-400"></i>
</div>
<input type="text" name="crypto_wallet" value="<?php echo getVal('crypto_wallet', $settings); ?>" class="w-full pl-10 p-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none font-mono text-sm">
</div>
</div>
</div>
</div>
<div class="flex justify-end">
<button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2.5 rounded-lg font-bold shadow-md transition-all flex items-center gap-2">
<i data-lucide="save" class="w-4 h-4"></i> Save Changes
</button>
</div>
</form>
<!-- 3. Security Settings -->
<form method="POST" class="bg-white p-6 rounded-xl shadow-sm border border-red-100">
<h2 class="text-lg font-bold text-gray-900 mb-6 border-b pb-2 flex items-center gap-2">
<i data-lucide="shield-alert" class="w-5 h-5 text-red-600"></i> Security Settings
</h2>
<input type="hidden" name="update_password" value="1">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1">New Password</label>
<input type="password" name="new_password" required class="w-full p-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-red-500 outline-none transition-colors">
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1">Confirm Password</label>
<input type="password" name="confirm_password" required class="w-full p-2.5 border border-gray-200 rounded-lg focus:ring-2 focus:ring-red-500 outline-none transition-colors">
</div>
</div>
<div class="flex justify-end mt-6">
<button type="submit" class="bg-gray-900 hover:bg-black text-white px-6 py-2.5 rounded-lg font-bold shadow-md transition-all flex items-center gap-2">
<i data-lucide="key" class="w-4 h-4"></i> Update Password
</button>
</div>
</form>
</div>
<br>
<br>
<br>
<br>
<br>
</main>
<script>
lucide.createIcons();
</script>
</body>
</html>
b IDATxytVսϓ22 A@IR:hCiZ[v*E:WũZA ^dQeQ @ !jZ'>gsV仿$|?g)&x-E