users.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. // Start session for language preference
  3. if (session_status() === PHP_SESSION_NONE) {
  4. session_start();
  5. }
  6. require_once '../includes/config.php';
  7. require_once '../includes/database.php';
  8. require_once '../includes/auth.php';
  9. require_once '../includes/translation.php';
  10. // Initialize translation system
  11. try {
  12. $translation = Translation::getInstance();
  13. } catch (Exception $e) {
  14. // Fallback to basic translations if translation system fails
  15. $translation = null;
  16. }
  17. // Check if user is logged in and has admin privileges
  18. $auth = new Auth();
  19. if (!$auth->isLoggedIn() || !$auth->isAdmin()) {
  20. header('Location: login.php');
  21. exit;
  22. }
  23. // Handle user actions
  24. $action = $_GET['action'] ?? 'list';
  25. $user_id = $_GET['id'] ?? null;
  26. $message = '';
  27. $error = '';
  28. // Get database instance
  29. $db = Database::getInstance();
  30. switch ($action) {
  31. case 'list':
  32. // Get filter parameters
  33. $search = trim($_GET['search'] ?? '');
  34. $role_filter = $_GET['role'] ?? 'all';
  35. $status_filter = $_GET['status'] ?? 'all';
  36. $page = max(1, (int)($_GET['page'] ?? 1));
  37. $limit = 20;
  38. $offset = ($page - 1) * $limit;
  39. // Build WHERE clause for filtering
  40. $where_conditions = [];
  41. $params = [];
  42. if (!empty($search)) {
  43. $where_conditions[] = "(username LIKE ? OR email LIKE ?)";
  44. $params[] = "%$search%";
  45. $params[] = "%$search%";
  46. }
  47. if ($role_filter !== 'all') {
  48. $where_conditions[] = "role = ?";
  49. $params[] = $role_filter;
  50. }
  51. if ($status_filter !== 'all') {
  52. $where_conditions[] = "status = ?";
  53. $params[] = $status_filter;
  54. }
  55. $where_clause = !empty($where_conditions) ? "WHERE " . implode(" AND ", $where_conditions) : "";
  56. // Get total users count with filters
  57. $count_sql = "SELECT COUNT(*) as count FROM users $where_clause";
  58. $total_users = $db->fetch($count_sql, $params)['count'];
  59. $total_pages = ceil($total_users / $limit);
  60. // Get users for current page with filters
  61. $sql = "SELECT id, username, email, auth_type, role, status, created_at, last_login FROM users $where_clause ORDER BY created_at DESC LIMIT ? OFFSET ?";
  62. $query_params = array_merge($params, [$limit, $offset]);
  63. $users = $db->fetchAll($sql, $query_params);
  64. break;
  65. case 'edit':
  66. if (!$user_id) {
  67. header('Location: users.php');
  68. exit;
  69. }
  70. // Handle form submission for user update
  71. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  72. $username = trim($_POST['username'] ?? '');
  73. $email = trim($_POST['email'] ?? '');
  74. $auth_type = trim($_POST['auth_type'] ?? 'local');
  75. $role = trim($_POST['role'] ?? 'user');
  76. $status = trim($_POST['status'] ?? 'active');
  77. $password = trim($_POST['password'] ?? '');
  78. // Validation
  79. if (empty($username)) $error = t('admin_username_required');
  80. elseif (empty($email)) $error = t('admin_email_required');
  81. elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) $error = t('admin_email_invalid');
  82. if (!$error) {
  83. try {
  84. $update_data = [
  85. 'username' => $username,
  86. 'email' => $email,
  87. 'auth_type' => $auth_type,
  88. 'role' => $role,
  89. 'status' => $status
  90. ];
  91. // Update password if provided
  92. if (!empty($password)) {
  93. $update_data['password'] = password_hash($password, PASSWORD_DEFAULT);
  94. }
  95. $db->update('users', $update_data, 'id = ?', [$user_id]);
  96. $message = t('admin_user_updated_success');
  97. // Redirect to avoid form resubmission
  98. header('Location: users.php?message=' . urlencode($message));
  99. exit;
  100. } catch (Exception $e) {
  101. if (strpos($e->getMessage(), 'Duplicate') !== false) {
  102. $error = t('admin_username_exists');
  103. } else {
  104. $error = t('admin_user_update_error') . ' ' . $e->getMessage();
  105. }
  106. }
  107. }
  108. }
  109. // Get user details
  110. $user = $db->fetch("SELECT * FROM users WHERE id = ?", [$user_id]);
  111. if (!$user) {
  112. $error = t('admin_user_not_found');
  113. break;
  114. }
  115. break;
  116. case 'delete':
  117. if (!$user_id) {
  118. header('Location: users.php');
  119. exit;
  120. }
  121. // Don't allow deletion of the currently logged-in user
  122. if ($user_id == $_SESSION['user_id']) {
  123. $error = t('admin_user_cannot_delete_own');
  124. break;
  125. }
  126. // Delete user
  127. $db->delete('users', 'id = ?', [$user_id]);
  128. $message = t('admin_user_deleted_success');
  129. // Redirect back to user list
  130. header('Location: users.php?action=list&message=' . urlencode($message));
  131. exit;
  132. case 'create':
  133. // Handle user creation
  134. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  135. $username = trim($_POST['username'] ?? '');
  136. $email = trim($_POST['email'] ?? '');
  137. $password = trim($_POST['password'] ?? '');
  138. $auth_type = trim($_POST['auth_type'] ?? 'local');
  139. // Validation
  140. if (empty($username)) $error = t('admin_username_required');
  141. elseif (empty($email)) $error = t('admin_email_required');
  142. elseif (empty($password) && $auth_type === 'local') $error = t('admin_password_required_local');
  143. elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) $error = t('admin_email_invalid');
  144. // Get role and status from form
  145. $role = trim($_POST['role'] ?? 'user');
  146. $status = trim($_POST['status'] ?? 'active');
  147. if (!$error) {
  148. // Check if username already exists
  149. $existing_user = $db->fetch("SELECT id FROM users WHERE username = ?", [$username]);
  150. if ($existing_user) {
  151. $error = t('admin_username_exists');
  152. } else {
  153. // Create new user
  154. $hashed_password = password_hash($password, PASSWORD_DEFAULT);
  155. $sql = "INSERT INTO users (username, email, password, auth_type, role, status, created_at) VALUES (?, ?, ?, ?, ?, ?, NOW())";
  156. $db->query($sql, [$username, $email, $hashed_password, $auth_type, $role, $status]);
  157. $message = t('admin_user_created_success');
  158. // If LDAP is enabled, we could also create LDAP user here
  159. if ($auth_type === 'ldap' && LDAP_ENABLED) {
  160. // Additional LDAP user creation logic could be added here
  161. }
  162. }
  163. }
  164. }
  165. break;
  166. default:
  167. header('Location: users.php');
  168. exit;
  169. }
  170. ?>
  171. <!DOCTYPE html>
  172. <html lang="<?php echo getCurrentLanguage(); ?>">
  173. <head>
  174. <meta charset="UTF-8">
  175. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  176. <title><?php echo t('manage_users'); ?> - <?php echo SITE_TITLE; ?></title>
  177. <link rel="stylesheet" href="../css/style.css">
  178. <link rel="stylesheet" href="../css/admin-users.css">
  179. </head>
  180. <body>
  181. <header class="admin-header">
  182. <div class="container">
  183. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  184. <nav class="admin-nav">
  185. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  186. <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
  187. <a href="categories.php" class="nav-link"><?php echo t('admin_nav_categories'); ?></a>
  188. <a href="users.php" class="nav-link active"><?php echo t('manage_users'); ?></a>
  189. <?php if (LDAP_ENABLED): ?>
  190. <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
  191. <?php endif; ?>
  192. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  193. </nav>
  194. </div>
  195. </header>
  196. <main class="admin-main">
  197. <div class="container">
  198. <?php if ($message): ?>
  199. <div class="alert alert-success">
  200. <?php echo htmlspecialchars($message); ?>
  201. </div>
  202. <?php endif; ?>
  203. <?php if ($error): ?>
  204. <div class="alert alert-error">
  205. <?php echo htmlspecialchars($error); ?>
  206. </div>
  207. <?php endif; ?>
  208. <?php if ($action === 'list'): ?>
  209. <div class="users-management">
  210. <div class="section-header">
  211. <h2><?php echo t('manage_users'); ?></h2>
  212. <div class="section-actions">
  213. <a href="users.php?action=create" class="btn btn-primary"><?php echo t('create_user'); ?></a>
  214. </div>
  215. </div>
  216. <div class="filters">
  217. <form method="get" class="filter-form">
  218. <div class="filter-group">
  219. <label for="search"><?php echo t('search'); ?>:</label>
  220. <input type="text" id="search" name="search" value="<?php echo htmlspecialchars($search); ?>" placeholder="<?php echo t('search_users_placeholder'); ?>">
  221. </div>
  222. <div class="filter-group">
  223. <label for="role"><?php echo t('role'); ?>:</label>
  224. <select id="role" name="role">
  225. <option value="all" <?php echo $role_filter === 'all' ? 'selected' : ''; ?>><?php echo t('filter_all'); ?></option>
  226. <option value="admin" <?php echo $role_filter === 'admin' ? 'selected' : ''; ?>><?php echo t('role_admin'); ?></option>
  227. <option value="user" <?php echo $role_filter === 'user' ? 'selected' : ''; ?>><?php echo t('role_user'); ?></option>
  228. </select>
  229. </div>
  230. <div class="filter-group">
  231. <label for="status"><?php echo t('status'); ?>:</label>
  232. <select id="status" name="status">
  233. <option value="all" <?php echo $status_filter === 'all' ? 'selected' : ''; ?>><?php echo t('filter_all'); ?></option>
  234. <option value="active" <?php echo $status_filter === 'active' ? 'selected' : ''; ?>><?php echo t('status_active'); ?></option>
  235. <option value="inactive" <?php echo $status_filter === 'inactive' ? 'selected' : ''; ?>><?php echo t('status_inactive'); ?></option>
  236. </select>
  237. </div>
  238. <button type="submit" class="btn btn-sm"><?php echo t('filter'); ?></button>
  239. <?php if ($search || $role_filter !== 'all' || $status_filter !== 'all'): ?>
  240. <a href="users.php" class="btn btn-sm btn-secondary"><?php echo t('clear'); ?></a>
  241. <?php endif; ?>
  242. </form>
  243. </div>
  244. <div class="users-table">
  245. <table class="admin-table">
  246. <thead>
  247. <tr>
  248. <th><?php echo t('username'); ?></th>
  249. <th><?php echo t('email'); ?></th>
  250. <th><?php echo t('auth_type'); ?></th>
  251. <th><?php echo t('role'); ?></th>
  252. <th><?php echo t('status'); ?></th>
  253. <th><?php echo t('created'); ?></th>
  254. <th><?php echo t('last_login'); ?></th>
  255. <th><?php echo t('actions'); ?></th>
  256. </tr>
  257. </thead>
  258. <tbody>
  259. <?php foreach ($users as $user): ?>
  260. <tr>
  261. <td><?php echo htmlspecialchars($user['username']); ?></td>
  262. <td><?php echo htmlspecialchars($user['email']); ?></td>
  263. <td>
  264. <span class="auth-type <?php echo $user['auth_type']; ?>">
  265. <?php echo ucfirst($user['auth_type']); ?>
  266. </span>
  267. </td>
  268. <td>
  269. <span class="role-badge role-<?php echo $user['role']; ?>">
  270. <?php echo t('role_' . $user['role']); ?>
  271. </span>
  272. </td>
  273. <td>
  274. <span class="status-badge status-<?php echo $user['status']; ?>">
  275. <?php echo t('status_' . $user['status']); ?>
  276. </span>
  277. </td>
  278. <td><?php echo date('Y-m-d H:i', strtotime($user['created_at'])); ?></td>
  279. <td><?php echo $user['last_login'] ? date('Y-m-d H:i', strtotime($user['last_login'])) : 'Never'; ?></td>
  280. <td>
  281. <a href="users.php?action=edit&id=<?php echo $user['id']; ?>" class="btn btn-sm"><?php echo t('edit'); ?></a>
  282. <?php if ($user['id'] != $_SESSION['user_id']): ?>
  283. <a href="users.php?action=delete&id=<?php echo $user['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('<?php echo t('delete_user_confirm'); ?>')"><?php echo t('delete'); ?></a>
  284. <?php endif; ?>
  285. </td>
  286. </tr>
  287. <?php endforeach; ?>
  288. </tbody>
  289. </table>
  290. <?php if ($total_pages > 1): ?>
  291. <div class="pagination">
  292. <?php if ($page > 1): ?>
  293. <a href="users.php?page=<?php echo $page - 1; ?>" class="btn"><?php echo t('previous'); ?></a>
  294. <?php endif; ?>
  295. <span class="page-info">
  296. <?php echo t('page'); ?> <?php echo $page; ?> <?php echo t('of'); ?> <?php echo $total_pages; ?>
  297. </span>
  298. <?php if ($page < $total_pages): ?>
  299. <a href="users.php?page=<?php echo $page + 1; ?>" class="btn"><?php echo t('next'); ?></a>
  300. <?php endif; ?>
  301. </div>
  302. <?php endif; ?>
  303. </div>
  304. </div>
  305. <?php elseif ($action === 'edit' && isset($user)): ?>
  306. <div class="user-edit">
  307. <div class="section-header">
  308. <h2><?php echo t('edit_user'); ?></h2>
  309. </div>
  310. <form method="post" class="admin-form">
  311. <div class="form-group">
  312. <label for="username"><?php echo t('username'); ?>:</label>
  313. <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required>
  314. </div>
  315. <div class="form-group">
  316. <label for="email"><?php echo t('email'); ?>:</label>
  317. <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
  318. </div>
  319. <div class="form-group">
  320. <label for="auth_type"><?php echo t('auth_type'); ?>:</label>
  321. <select id="auth_type" name="auth_type">
  322. <option value="local" <?php echo $user['auth_type'] === 'local' ? 'selected' : ''; ?>><?php echo t('local'); ?></option>
  323. <option value="ldap" <?php echo $user['auth_type'] === 'ldap' ? 'selected' : ''; ?>><?php echo t('ldap'); ?></option>
  324. </select>
  325. </div>
  326. <div class="form-group">
  327. <label for="role"><?php echo t('role'); ?>:</label>
  328. <select id="role" name="role">
  329. <option value="user" <?php echo ($user['role'] ?? 'user') === 'user' ? 'selected' : ''; ?>><?php echo t('role_user'); ?></option>
  330. <option value="admin" <?php echo ($user['role'] ?? 'user') === 'admin' ? 'selected' : ''; ?>><?php echo t('role_admin'); ?></option>
  331. </select>
  332. </div>
  333. <div class="form-group">
  334. <label for="status"><?php echo t('status'); ?>:</label>
  335. <select id="status" name="status">
  336. <option value="active" <?php echo ($user['status'] ?? 'active') === 'active' ? 'selected' : ''; ?>><?php echo t('status_active'); ?></option>
  337. <option value="inactive" <?php echo ($user['status'] ?? 'active') === 'inactive' ? 'selected' : ''; ?>><?php echo t('status_inactive'); ?></option>
  338. </select>
  339. </div>
  340. <div class="form-actions">
  341. <button type="submit" class="btn btn-primary"><?php echo t('update'); ?></button>
  342. <a href="users.php" class="btn"><?php echo t('cancel'); ?></a>
  343. </div>
  344. </form>
  345. </div>
  346. <?php elseif ($action === 'create'): ?>
  347. <div class="user-create">
  348. <div class="section-header">
  349. <h2><?php echo t('create_user'); ?></h2>
  350. </div>
  351. <form method="post" class="admin-form">
  352. <div class="form-group">
  353. <label for="username"><?php echo t('username'); ?>:</label>
  354. <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username ?? ''); ?>" required>
  355. </div>
  356. <div class="form-group">
  357. <label for="email"><?php echo t('email'); ?>:</label>
  358. <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email ?? ''); ?>" required>
  359. </div>
  360. <div class="form-group">
  361. <label for="password"><?php echo t('password'); ?>:</label>
  362. <input type="password" id="password" name="password" value="<?php echo htmlspecialchars($password ?? ''); ?>" required>
  363. </div>
  364. <div class="form-group">
  365. <label for="auth_type"><?php echo t('auth_type'); ?>:</label>
  366. <select id="auth_type" name="auth_type">
  367. <option value="local"><?php echo t('local'); ?></option>
  368. <?php if (LDAP_ENABLED): ?>
  369. <option value="ldap"><?php echo t('ldap'); ?></option>
  370. <?php endif; ?>
  371. </select>
  372. </div>
  373. <div class="form-group">
  374. <label for="role"><?php echo t('role'); ?>:</label>
  375. <select id="role" name="role">
  376. <option value="user"><?php echo t('role_user'); ?></option>
  377. <option value="admin"><?php echo t('role_admin'); ?></option>
  378. </select>
  379. </div>
  380. <div class="form-group">
  381. <label for="status"><?php echo t('status'); ?>:</label>
  382. <select id="status" name="status">
  383. <option value="active"><?php echo t('status_active'); ?></option>
  384. <option value="inactive"><?php echo t('status_inactive'); ?></option>
  385. </select>
  386. </div>
  387. <div class="form-actions">
  388. <button type="submit" class="btn btn-primary"><?php echo t('create'); ?></button>
  389. <a href="users.php" class="btn"><?php echo t('cancel'); ?></a>
  390. </div>
  391. </form>
  392. </div>
  393. <?php endif; ?>
  394. </div>
  395. </main>
  396. <footer>
  397. <p>&copy; <?php echo date('Y'); ?> <?php echo t('copyright', ['site' => SITE_TITLE]); ?></p>
  398. </footer>
  399. </body>
  400. </html>