users.php 23 KB

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