users.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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"><?php echo t('nav_dashboard'); ?></a>
  186. <a href="edit.php"><?php echo t('create_publication'); ?></a>
  187. <a href="publications.php"><?php echo t('manage_publications'); ?></a>
  188. <a href="categories.php"><?php echo t('manage_categories'); ?></a>
  189. <a href="users.php" class="active"><?php echo t('manage_users'); ?></a>
  190. <?php if (LDAP_ENABLED): ?>
  191. <a href="ldap-users.php"><?php echo t('nav_ldap_users'); ?></a>
  192. <?php endif; ?>
  193. <a href="logout.php"><?php echo t('logout'); ?></a>
  194. </nav>
  195. </div>
  196. </header>
  197. <main class="admin-main">
  198. <div class="container">
  199. <?php if ($message): ?>
  200. <div class="alert alert-success">
  201. <?php echo htmlspecialchars($message); ?>
  202. </div>
  203. <?php endif; ?>
  204. <?php if ($error): ?>
  205. <div class="alert alert-error">
  206. <?php echo htmlspecialchars($error); ?>
  207. </div>
  208. <?php endif; ?>
  209. <?php if ($action === 'list'): ?>
  210. <div class="users-management">
  211. <div class="section-header">
  212. <h2><?php echo t('manage_users'); ?></h2>
  213. <div class="section-actions">
  214. <a href="users.php?action=create" class="btn btn-primary"><?php echo t('create_user'); ?></a>
  215. </div>
  216. </div>
  217. <div class="filters">
  218. <form method="get" class="filter-form">
  219. <div class="filter-group">
  220. <label for="search"><?php echo t('search'); ?>:</label>
  221. <input type="text" id="search" name="search" value="<?php echo htmlspecialchars($search); ?>" placeholder="<?php echo t('search_users_placeholder'); ?>">
  222. </div>
  223. <div class="filter-group">
  224. <label for="role"><?php echo t('role'); ?>:</label>
  225. <select id="role" name="role">
  226. <option value="all" <?php echo $role_filter === 'all' ? 'selected' : ''; ?>><?php echo t('filter_all'); ?></option>
  227. <option value="admin" <?php echo $role_filter === 'admin' ? 'selected' : ''; ?>><?php echo t('role_admin'); ?></option>
  228. <option value="user" <?php echo $role_filter === 'user' ? 'selected' : ''; ?>><?php echo t('role_user'); ?></option>
  229. </select>
  230. </div>
  231. <div class="filter-group">
  232. <label for="status"><?php echo t('status'); ?>:</label>
  233. <select id="status" name="status">
  234. <option value="all" <?php echo $status_filter === 'all' ? 'selected' : ''; ?>><?php echo t('filter_all'); ?></option>
  235. <option value="active" <?php echo $status_filter === 'active' ? 'selected' : ''; ?>><?php echo t('status_active'); ?></option>
  236. <option value="inactive" <?php echo $status_filter === 'inactive' ? 'selected' : ''; ?>><?php echo t('status_inactive'); ?></option>
  237. </select>
  238. </div>
  239. <button type="submit" class="btn btn-sm"><?php echo t('filter'); ?></button>
  240. <?php if ($search || $role_filter !== 'all' || $status_filter !== 'all'): ?>
  241. <a href="users.php" class="btn btn-sm btn-secondary"><?php echo t('clear'); ?></a>
  242. <?php endif; ?>
  243. </form>
  244. </div>
  245. <div class="users-table">
  246. <table class="admin-table">
  247. <thead>
  248. <tr>
  249. <th><?php echo t('username'); ?></th>
  250. <th><?php echo t('email'); ?></th>
  251. <th><?php echo t('auth_type'); ?></th>
  252. <th><?php echo t('role'); ?></th>
  253. <th><?php echo t('status'); ?></th>
  254. <th><?php echo t('created'); ?></th>
  255. <th><?php echo t('last_login'); ?></th>
  256. <th><?php echo t('actions'); ?></th>
  257. </tr>
  258. </thead>
  259. <tbody>
  260. <?php foreach ($users as $user): ?>
  261. <tr>
  262. <td><?php echo htmlspecialchars($user['username']); ?></td>
  263. <td><?php echo htmlspecialchars($user['email']); ?></td>
  264. <td>
  265. <span class="auth-type <?php echo $user['auth_type']; ?>">
  266. <?php echo ucfirst($user['auth_type']); ?>
  267. </span>
  268. </td>
  269. <td>
  270. <span class="role-badge role-<?php echo $user['role']; ?>">
  271. <?php echo t('role_' . $user['role']); ?>
  272. </span>
  273. </td>
  274. <td>
  275. <span class="status-badge status-<?php echo $user['status']; ?>">
  276. <?php echo t('status_' . $user['status']); ?>
  277. </span>
  278. </td>
  279. <td><?php echo date('Y-m-d H:i', strtotime($user['created_at'])); ?></td>
  280. <td><?php echo $user['last_login'] ? date('Y-m-d H:i', strtotime($user['last_login'])) : 'Never'; ?></td>
  281. <td>
  282. <a href="users.php?action=edit&id=<?php echo $user['id']; ?>" class="btn btn-sm"><?php echo t('edit'); ?></a>
  283. <?php if ($user['id'] != $_SESSION['user_id']): ?>
  284. <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>
  285. <?php endif; ?>
  286. </td>
  287. </tr>
  288. <?php endforeach; ?>
  289. </tbody>
  290. </table>
  291. <?php if ($total_pages > 1): ?>
  292. <div class="pagination">
  293. <?php if ($page > 1): ?>
  294. <a href="users.php?page=<?php echo $page - 1; ?>" class="btn"><?php echo t('previous'); ?></a>
  295. <?php endif; ?>
  296. <span class="page-info">
  297. <?php echo t('page'); ?> <?php echo $page; ?> <?php echo t('of'); ?> <?php echo $total_pages; ?>
  298. </span>
  299. <?php if ($page < $total_pages): ?>
  300. <a href="users.php?page=<?php echo $page + 1; ?>" class="btn"><?php echo t('next'); ?></a>
  301. <?php endif; ?>
  302. </div>
  303. <?php endif; ?>
  304. </div>
  305. </div>
  306. <?php elseif ($action === 'edit' && isset($user)): ?>
  307. <div class="user-edit">
  308. <div class="section-header">
  309. <h2><?php echo t('edit_user'); ?></h2>
  310. </div>
  311. <form method="post" class="admin-form">
  312. <div class="form-group">
  313. <label for="username"><?php echo t('username'); ?>:</label>
  314. <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required>
  315. </div>
  316. <div class="form-group">
  317. <label for="email"><?php echo t('email'); ?>:</label>
  318. <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
  319. </div>
  320. <div class="form-group">
  321. <label for="auth_type"><?php echo t('auth_type'); ?>:</label>
  322. <select id="auth_type" name="auth_type">
  323. <option value="local" <?php echo $user['auth_type'] === 'local' ? 'selected' : ''; ?>><?php echo t('local'); ?></option>
  324. <option value="ldap" <?php echo $user['auth_type'] === 'ldap' ? 'selected' : ''; ?>><?php echo t('ldap'); ?></option>
  325. </select>
  326. </div>
  327. <div class="form-group">
  328. <label for="role"><?php echo t('role'); ?>:</label>
  329. <select id="role" name="role">
  330. <option value="user" <?php echo ($user['role'] ?? 'user') === 'user' ? 'selected' : ''; ?>><?php echo t('role_user'); ?></option>
  331. <option value="admin" <?php echo ($user['role'] ?? 'user') === 'admin' ? 'selected' : ''; ?>><?php echo t('role_admin'); ?></option>
  332. </select>
  333. </div>
  334. <div class="form-group">
  335. <label for="status"><?php echo t('status'); ?>:</label>
  336. <select id="status" name="status">
  337. <option value="active" <?php echo ($user['status'] ?? 'active') === 'active' ? 'selected' : ''; ?>><?php echo t('status_active'); ?></option>
  338. <option value="inactive" <?php echo ($user['status'] ?? 'active') === 'inactive' ? 'selected' : ''; ?>><?php echo t('status_inactive'); ?></option>
  339. </select>
  340. </div>
  341. <div class="form-actions">
  342. <button type="submit" class="btn btn-primary"><?php echo t('update'); ?></button>
  343. <a href="users.php" class="btn"><?php echo t('cancel'); ?></a>
  344. </div>
  345. </form>
  346. </div>
  347. <?php elseif ($action === 'create'): ?>
  348. <div class="user-create">
  349. <div class="section-header">
  350. <h2><?php echo t('create_user'); ?></h2>
  351. </div>
  352. <form method="post" class="admin-form">
  353. <div class="form-group">
  354. <label for="username"><?php echo t('username'); ?>:</label>
  355. <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username ?? ''); ?>" required>
  356. </div>
  357. <div class="form-group">
  358. <label for="email"><?php echo t('email'); ?>:</label>
  359. <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email ?? ''); ?>" required>
  360. </div>
  361. <div class="form-group">
  362. <label for="password"><?php echo t('password'); ?>:</label>
  363. <input type="password" id="password" name="password" value="<?php echo htmlspecialchars($password ?? ''); ?>" required>
  364. </div>
  365. <div class="form-group">
  366. <label for="auth_type"><?php echo t('auth_type'); ?>:</label>
  367. <select id="auth_type" name="auth_type">
  368. <option value="local"><?php echo t('local'); ?></option>
  369. <?php if (LDAP_ENABLED): ?>
  370. <option value="ldap"><?php echo t('ldap'); ?></option>
  371. <?php endif; ?>
  372. </select>
  373. </div>
  374. <div class="form-group">
  375. <label for="role"><?php echo t('role'); ?>:</label>
  376. <select id="role" name="role">
  377. <option value="user"><?php echo t('role_user'); ?></option>
  378. <option value="admin"><?php echo t('role_admin'); ?></option>
  379. </select>
  380. </div>
  381. <div class="form-group">
  382. <label for="status"><?php echo t('status'); ?>:</label>
  383. <select id="status" name="status">
  384. <option value="active"><?php echo t('status_active'); ?></option>
  385. <option value="inactive"><?php echo t('status_inactive'); ?></option>
  386. </select>
  387. </div>
  388. <div class="form-actions">
  389. <button type="submit" class="btn btn-primary"><?php echo t('create'); ?></button>
  390. <a href="users.php" class="btn"><?php echo t('cancel'); ?></a>
  391. </div>
  392. </form>
  393. </div>
  394. <?php endif; ?>
  395. </div>
  396. </main>
  397. <footer>
  398. <p>&copy; <?php echo date('Y'); ?> <?php echo t('copyright', ['site' => SITE_TITLE]); ?></p>
  399. </footer>
  400. </body>
  401. </html>