users.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 all users with pagination
  31. $page = max(1, (int)($_GET['page'] ?? 1));
  32. $limit = 20;
  33. $offset = ($page - 1) * $limit;
  34. // Get total users count
  35. $total_users = $db->fetch("SELECT COUNT(*) as count FROM users")['count'];
  36. $total_pages = ceil($total_users / $limit);
  37. // Get users for current page
  38. $sql = "SELECT id, username, email, auth_type, created_at, last_login FROM users ORDER BY created_at DESC LIMIT ? OFFSET ?";
  39. $users = $db->fetchAll($sql, [$limit, $offset]);
  40. break;
  41. case 'edit':
  42. if (!$user_id) {
  43. header('Location: users.php');
  44. exit;
  45. }
  46. // Get user details
  47. $user = $db->fetch("SELECT * FROM users WHERE id = ?", [$user_id]);
  48. if (!$user) {
  49. $error = t('admin_user_not_found');
  50. break;
  51. }
  52. break;
  53. case 'delete':
  54. if (!$user_id) {
  55. header('Location: users.php');
  56. exit;
  57. }
  58. // Don't allow deletion of the currently logged-in user
  59. if ($user_id == $_SESSION['user_id']) {
  60. $error = t('admin_user_cannot_delete_own');
  61. break;
  62. }
  63. // Delete user
  64. $db->delete('users', 'id = ?', [$user_id]);
  65. $message = t('admin_user_deleted_success');
  66. // Redirect back to user list
  67. header('Location: users.php?action=list&message=' . urlencode($message));
  68. exit;
  69. case 'create':
  70. // Handle user creation
  71. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  72. $username = trim($_POST['username'] ?? '');
  73. $email = trim($_POST['email'] ?? '');
  74. $password = trim($_POST['password'] ?? '');
  75. $auth_type = trim($_POST['auth_type'] ?? 'local');
  76. // Validation
  77. if (empty($username)) $error = t('admin_username_required');
  78. elseif (empty($email)) $error = t('admin_email_required');
  79. elseif (empty($password) && $auth_type === 'local') $error = t('admin_password_required_local');
  80. elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) $error = t('admin_email_invalid');
  81. if (!$error) {
  82. // Check if username already exists
  83. $existing_user = $db->fetch("SELECT id FROM users WHERE username = ?", [$username]);
  84. if ($existing_user) {
  85. $error = t('admin_username_exists');
  86. } else {
  87. // Create new user
  88. $hashed_password = password_hash($password, PASSWORD_DEFAULT);
  89. $sql = "INSERT INTO users (username, email, password, auth_type, created_at) VALUES (?, ?, ?, ?, NOW())";
  90. $db->query($sql, [$username, $email, $hashed_password, $auth_type]);
  91. $message = t('admin_user_created_success');
  92. // If LDAP is enabled, we could also create LDAP user here
  93. if ($auth_type === 'ldap' && LDAP_ENABLED) {
  94. // Additional LDAP user creation logic could be added here
  95. }
  96. }
  97. }
  98. }
  99. break;
  100. default:
  101. header('Location: users.php');
  102. exit;
  103. }
  104. ?>
  105. <!DOCTYPE html>
  106. <html lang="<?php echo getCurrentLanguage(); ?>">
  107. <head>
  108. <meta charset="UTF-8">
  109. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  110. <title><?php echo t('manage_users'); ?> - <?php echo SITE_TITLE; ?></title>
  111. <link rel="stylesheet" href="../css/style.css">
  112. </head>
  113. <body>
  114. <header class="admin-header">
  115. <div class="container">
  116. <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
  117. <nav class="admin-nav">
  118. <a href="index.php"><?php echo t('nav_dashboard'); ?></a>
  119. <a href="edit.php"><?php echo t('manage_publications'); ?></a>
  120. <a href="publications.php"><?php echo t('manage_publications'); ?></a>
  121. <a href="categories.php"><?php echo t('manage_categories'); ?></a>
  122. <a href="users.php" class="active"><?php echo t('manage_users'); ?></a>
  123. <?php if (LDAP_ENABLED): ?>
  124. <a href="ldap-users.php"><?php echo t('nav_ldap_users'); ?></a>
  125. <?php endif; ?>
  126. <a href="logout.php"><?php echo t('logout'); ?></a>
  127. </nav>
  128. </div>
  129. </header>
  130. <main class="admin-main">
  131. <div class="container">
  132. <?php if ($message): ?>
  133. <div class="alert alert-success">
  134. <?php echo htmlspecialchars($message); ?>
  135. </div>
  136. <?php endif; ?>
  137. <?php if ($error): ?>
  138. <div class="alert alert-error">
  139. <?php echo htmlspecialchars($error); ?>
  140. </div>
  141. <?php endif; ?>
  142. <?php if ($action === 'list'): ?>
  143. <div class="users-management">
  144. <div class="section-header">
  145. <h2><?php echo t('manage_users'); ?></h2>
  146. <div class="section-actions">
  147. <a href="users.php?action=create" class="btn btn-primary"><?php echo t('create_user'); ?></a>
  148. </div>
  149. </div>
  150. <div class="users-table">
  151. <table class="admin-table">
  152. <thead>
  153. <tr>
  154. <th><?php echo t('username'); ?></th>
  155. <th><?php echo t('email'); ?></th>
  156. <th><?php echo t('auth_type'); ?></th>
  157. <th><?php echo t('created'); ?></th>
  158. <th><?php echo t('last_login'); ?></th>
  159. <th><?php echo t('actions'); ?></th>
  160. </tr>
  161. </thead>
  162. <tbody>
  163. <?php foreach ($users as $user): ?>
  164. <tr>
  165. <td><?php echo htmlspecialchars($user['username']); ?></td>
  166. <td><?php echo htmlspecialchars($user['email']); ?></td>
  167. <td>
  168. <span class="auth-type <?php echo $user['auth_type']; ?>">
  169. <?php echo ucfirst($user['auth_type']); ?>
  170. </span>
  171. </td>
  172. <td><?php echo date('Y-m-d H:i', strtotime($user['created_at'])); ?></td>
  173. <td><?php echo $user['last_login'] ? date('Y-m-d H:i', strtotime($user['last_login'])) : 'Never'; ?></td>
  174. <td>
  175. <a href="users.php?action=edit&id=<?php echo $user['id']; ?>" class="btn btn-sm"><?php echo t('edit'); ?></a>
  176. <?php if ($user['id'] != $_SESSION['user_id']): ?>
  177. <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>
  178. <?php endif; ?>
  179. </td>
  180. </tr>
  181. <?php endforeach; ?>
  182. </tbody>
  183. </table>
  184. <?php if ($total_pages > 1): ?>
  185. <div class="pagination">
  186. <?php if ($page > 1): ?>
  187. <a href="users.php?page=<?php echo $page - 1; ?>" class="btn"><?php echo t('previous'); ?></a>
  188. <?php endif; ?>
  189. <span class="page-info">
  190. <?php echo t('page'); ?> <?php echo $page; ?> <?php echo t('of'); ?> <?php echo $total_pages; ?>
  191. </span>
  192. <?php if ($page < $total_pages): ?>
  193. <a href="users.php?page=<?php echo $page + 1; ?>" class="btn"><?php echo t('next'); ?></a>
  194. <?php endif; ?>
  195. </div>
  196. <?php endif; ?>
  197. </div>
  198. </div>
  199. <?php elseif ($action === 'edit' && isset($user)): ?>
  200. <div class="user-edit">
  201. <div class="section-header">
  202. <h2><?php echo t('edit_user'); ?></h2>
  203. </div>
  204. <form method="post" class="admin-form">
  205. <div class="form-group">
  206. <label for="username"><?php echo t('username'); ?>:</label>
  207. <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required>
  208. </div>
  209. <div class="form-group">
  210. <label for="email"><?php echo t('email'); ?>:</label>
  211. <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
  212. </div>
  213. <div class="form-group">
  214. <label for="auth_type"><?php echo t('auth_type'); ?>:</label>
  215. <select id="auth_type" name="auth_type">
  216. <option value="local" <?php echo $user['auth_type'] === 'local' ? 'selected' : ''; ?>><?php echo t('local'); ?></option>
  217. <option value="ldap" <?php echo $user['auth_type'] === 'ldap' ? 'selected' : ''; ?>><?php echo t('ldap'); ?></option>
  218. </select>
  219. </div>
  220. <div class="form-actions">
  221. <button type="submit" class="btn btn-primary"><?php echo t('update'); ?></button>
  222. <a href="users.php" class="btn"><?php echo t('cancel'); ?></a>
  223. </div>
  224. </form>
  225. </div>
  226. <?php elseif ($action === 'create'): ?>
  227. <div class="user-create">
  228. <div class="section-header">
  229. <h2><?php echo t('create_user'); ?></h2>
  230. </div>
  231. <form method="post" class="admin-form">
  232. <div class="form-group">
  233. <label for="username"><?php echo t('username'); ?>:</label>
  234. <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username ?? ''); ?>" required>
  235. </div>
  236. <div class="form-group">
  237. <label for="email"><?php echo t('email'); ?>:</label>
  238. <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email ?? ''); ?>" required>
  239. </div>
  240. <div class="form-group">
  241. <label for="password"><?php echo t('password'); ?>:</label>
  242. <input type="password" id="password" name="password" value="<?php echo htmlspecialchars($password ?? ''); ?>" required>
  243. </div>
  244. <div class="form-group">
  245. <label for="auth_type"><?php echo t('auth_type'); ?>:</label>
  246. <select id="auth_type" name="auth_type">
  247. <option value="local"><?php echo t('local'); ?></option>
  248. <?php if (LDAP_ENABLED): ?>
  249. <option value="ldap"><?php echo t('ldap'); ?></option>
  250. <?php endif; ?>
  251. </select>
  252. </div>
  253. <div class="form-actions">
  254. <button type="submit" class="btn btn-primary"><?php echo t('create'); ?></button>
  255. <a href="users.php" class="btn"><?php echo t('cancel'); ?></a>
  256. </div>
  257. </form>
  258. </div>
  259. <?php endif; ?>
  260. </div>
  261. </main>
  262. <footer>
  263. <p>&copy; <?php echo date('Y'); ?> <?php echo t('copyright', ['site' => SITE_TITLE]); ?></p>
  264. </footer>
  265. </body>
  266. </html>