users.php 23 KB

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