-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-settings.php
More file actions
262 lines (231 loc) · 10.7 KB
/
Copy pathadmin-settings.php
File metadata and controls
262 lines (231 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
include 'security-check.php';
include 'db_connect.php';
// Admin only
if (!isset($_SESSION['staff_user']) || $_SESSION['role'] != 'Admin') {
header("Location: login.php"); exit();
}
// Fetch current settings from DB
$settings = [];
$res = mysqli_query($conn, "SELECT * FROM system_settings");
while ($row = mysqli_fetch_assoc($res)) {
$settings[$row['setting_key']] = $row['setting_value'];
}
// Background image upload helper
// Defined here (outside POST block) so it's always accessible
function saveBG($conn, $key) {
if (!empty($_FILES[$key]['name']) && $_FILES[$key]['error'] === UPLOAD_ERR_OK) {
$dir = "uploads/bg/";
if (!is_dir($dir)) mkdir($dir, 0755, true);
// MIME type validation — only real images allowed
$allowed_mime = ['image/jpeg', 'image/png', 'image/webp', 'image/gif'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES[$key]['tmp_name']);
finfo_close($finfo);
if (!in_array($mime, $allowed_mime)) return; // silently skip invalid files
$ext = pathinfo($_FILES[$key]['name'], PATHINFO_EXTENSION);
$name = time() . '_' . $key . '.' . strtolower($ext);
move_uploaded_file($_FILES[$key]['tmp_name'], $dir . $name);
$stmt = mysqli_prepare($conn, "INSERT INTO system_settings (setting_key, setting_value)
VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value=?");
mysqli_stmt_bind_param($stmt, "sss", $key, $name, $name);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
}
// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Background images
saveBG($conn, 'bg_login');
saveBG($conn, 'bg_dashboard');
// Admin reset secret code (hashed, never plain text)
if (!empty($_POST['reset_secret']) && strlen(trim($_POST['reset_secret'])) >= 4) {
$hashed = password_hash(trim($_POST['reset_secret']), PASSWORD_DEFAULT);
$stmt = mysqli_prepare($conn, "INSERT INTO system_settings (setting_key, setting_value)
VALUES ('reset_secret_hash', ?)
ON DUPLICATE KEY UPDATE setting_value=?");
mysqli_stmt_bind_param($stmt, "ss", $hashed, $hashed);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
// Save success — store message in session and redirect (PRG pattern)
// PRG = Post/Redirect/Get — prevents form resubmission on refresh
$_SESSION['settings_saved'] = true;
header("Location: admin-settings.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Theme Settings | Hostel Management</title>
<?php include 'session-monitor-header.php'; ?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="stylesheet" href="toast.css">
<script src="toast.js"></script>
<style>
/* ── Microsoft Fluent Override ── */
:root {
--ms-blue: #0078d4;
--ms-bg: #faf9f7;
--ms-card: #ffffff;
--ms-border: #e1dfdd;
--ms-text: #201f1e;
--ms-muted: #8a8886;
--ms-shadow: 0 1.6px 3.6px rgba(0,0,0,.07), 0 0.3px 0.9px rgba(0,0,0,.05);
}
body {
background: var(--ms-bg) !important;
color: var(--ms-text) !important;
font-family: 'Segoe UI', 'DM Sans', sans-serif !important;
}
.card, .action-card, .staff-card, .role-card, .stat-card, .choice-card {
background: var(--ms-card) !important;
border: 1px solid var(--ms-border) !important;
border-radius: 8px !important;
box-shadow: var(--ms-shadow) !important;
color: var(--ms-text) !important;
}
.card:hover, .action-card:hover, .staff-card:hover {
box-shadow: 0 3.2px 7.2px rgba(0,0,0,.09) !important;
transform: translateY(-2px) !important;
}
.welcome-banner, .dashboard-header {
background: linear-gradient(135deg, #0f3460, #16213e) !important;
}
thead tr, thead th {
background: #0078d4 !important;
}
.btn-primary, button[type="submit"]:not(.btn-danger):not(.btn-warning) {
background: #0078d4 !important;
border-color: #0078d4 !important;
}
.btn-primary:hover, button[type="submit"]:not(.btn-danger):not(.btn-warning):hover {
background: #106ebe !important;
}
a { color: #0078d4; }
a:hover { color: #005a9e; }
body { padding: 20px; font-family: 'Segoe UI', sans-serif; }
.container {
max-width: 700px; margin: auto; background: white;
padding: 40px; border-radius: 12px; box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
h3 { color: #166534; margin-bottom: 25px; text-align: center; font-size: 24px; }
.form-group { margin-bottom: 30px; }
.form-group > label {
display: block; font-weight: bold; margin-bottom: 12px;
color: #555; font-size: 16px;
}
input[type=file] {
margin-bottom: 15px; display: block; width: 100%;
padding: 10px; border: 2px dashed #ddd; border-radius: 8px;
background: #f9f9f9;
}
.btn {
background: linear-gradient(135deg, #166534, #15803d);
color: white; padding: 15px; border: none; width: 100%;
cursor: pointer; border-radius: 8px; font-weight: bold;
font-size: 17px; transition: all 0.3s;
}
.btn:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(22,101,52,0.4); }
.info-box {
background: linear-gradient(135deg, #e3f2fd, #f3e5f5);
padding: 20px; border-radius: 8px;
margin-bottom: 25px; border-left: 5px solid #2196f3;
}
.info-box p { margin: 8px 0; font-size: 14px; color: #555; }
.info-box strong { color: #166534; }
.preview-img { width: 100%; height: 120px; object-fit: cover; border-radius: 8px; margin-top: 10px; border: 2px solid #e2e8f0; }
.section-divider { margin: 40px 0 30px; border-top: 2px solid #e2e8f0; padding-top: 30px; }
.reset-box { margin-top: 30px; padding: 20px; background: #f8f9fa; border-radius: 10px; border: 1px solid #dee2e6; }
.reset-box h4 { margin-bottom: 6px; color: #333; }
.reset-box p { font-size: 13px; color: #666; margin-bottom: 14px; }
.reset-box input {
width: 100%; padding: 10px 14px; border: 1px solid #ddd;
border-radius: 8px; font-size: 14px; outline: none;
}
.reset-box .hint { font-size: 11px; color: #aaa; margin-top: 5px; }
</style>
</head>
<body>
<?php include 'navbar.php'; ?>
<?php include 'lang-switcher.php'; ?>
<?php include 'sidebar.php'; ?>
<div class="container">
<h3><i class="fas fa-magic"></i> System Appearance Settings</h3>
<!-- Current config summary -->
<div class="info-box">
<p><strong><i class="fas fa-info-circle"></i> Current Configuration:</strong></p>
<p>Login Background: <strong><?= htmlspecialchars($settings['bg_login'] ?? 'default.jpg') ?></strong></p>
<p>Dashboard Background: <strong><?= htmlspecialchars($settings['bg_dashboard'] ?? 'default.jpg') ?></strong></p>
</div>
<form method="POST" enctype="multipart/form-data">
<div class="section-divider"></div>
<!-- Background Images -->
<div class="form-group">
<label><i class="fas fa-image"></i> Login Page Background</label>
<input type="file" name="bg_login" accept="image/*" onchange="previewImage(this, 'login-preview')">
<small style="color:#666">📌 Recommended: 1920×1080px | JPG, PNG, WebP</small>
<?php if (!empty($settings['bg_login']) && file_exists('uploads/bg/'.$settings['bg_login'])): ?>
<img src="uploads/bg/<?= htmlspecialchars($settings['bg_login']) ?>" class="preview-img" id="login-preview">
<?php endif; ?>
</div>
<div class="form-group">
<label><i class="fas fa-image"></i> Dashboard Background</label>
<input type="file" name="bg_dashboard" accept="image/*" onchange="previewImage(this, 'dash-preview')">
<small style="color:#666">📌 Recommended: 1920×1080px | JPG, PNG, WebP</small>
<?php if (!empty($settings['bg_dashboard']) && file_exists('uploads/bg/'.$settings['bg_dashboard'])): ?>
<img src="uploads/bg/<?= htmlspecialchars($settings['bg_dashboard']) ?>" class="preview-img" id="dash-preview">
<?php endif; ?>
</div>
<!-- Admin Reset Secret -->
<div class="reset-box">
<h4><i class="fas fa-key"></i> Admin Reset Secret Code</h4>
<p>
Required to reset admin password from the login page.
<?php if (!empty($settings['reset_secret_hash'])): ?>
<br><span style="color:green">✅ Secret code is already set.</span>
<?php else: ?>
<br><span style="color:red">⚠️ No secret code set yet!</span>
<?php endif; ?>
</p>
<input type="password" name="reset_secret" placeholder="Enter new secret code (min 4 chars) — leave blank to keep current">
<p class="hint">Leave blank to keep existing code unchanged.</p>
</div>
<br>
<button type="submit" class="btn">
<i class="fas fa-save"></i> Save Settings & Apply Changes
</button>
</form>
</div>
<script>
// Image preview on file select
function previewImage(input, previewId) {
if (input.files && input.files[0]) {
const reader = new FileReader();
reader.onload = e => {
let img = document.getElementById(previewId);
if (!img) {
img = document.createElement('img');
img.id = previewId;
img.className = 'preview-img';
input.parentElement.appendChild(img);
}
img.src = e.target.result;
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
<div id="toast-container"></div>
<?php include 'staff-footer.php'; ?>
<?php if (!empty($_SESSION['settings_saved'])): unset($_SESSION['settings_saved']); ?>
<script>
// Show success toast after PRG redirect
document.addEventListener('DOMContentLoaded', function() {
showToast('Settings saved successfully! ✅', 'success', 3000);
});
</script>
<?php endif; ?>
</body>
</html>