auth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Authentication Class
  4. * Handles user login, logout, and session management
  5. */
  6. require_once 'database.php';
  7. require_once 'translation.php';
  8. class Auth {
  9. private $db;
  10. private $ldap;
  11. public function __construct() {
  12. $this->db = Database::getInstance();
  13. if (LDAP_ENABLED) {
  14. require_once 'ldap.php';
  15. $this->ldap = new LDAPAuth();
  16. }
  17. }
  18. public function login($username, $password) {
  19. // First, try to find user in database
  20. $sql = "SELECT * FROM users WHERE username = ?";
  21. $user = $this->db->fetch($sql, [$username]);
  22. if (!$user) {
  23. return false;
  24. }
  25. // Check authentication based on user's auth type
  26. if ($user['auth_type'] === 'ldap' && LDAP_ENABLED) {
  27. // LDAP authentication
  28. if ($this->ldap->authenticate($username, $password)) {
  29. // Get LDAP user info and update database
  30. $ldapUserInfo = $this->ldap->getUserInfo($username);
  31. if ($ldapUserInfo) {
  32. $this->updateUserFromLDAP($user['id'], $ldapUserInfo);
  33. }
  34. $this->createSession($user);
  35. $this->updateLastLogin($user['id']);
  36. return true;
  37. }
  38. } elseif ($user['auth_type'] === 'local') {
  39. // Local authentication
  40. if ($user['password'] && password_verify($password, $user['password'])) {
  41. $this->createSession($user);
  42. $this->updateLastLogin($user['id']);
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. public function logout() {
  49. session_destroy();
  50. unset($_SESSION);
  51. }
  52. public function isLoggedIn() {
  53. return isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true;
  54. }
  55. public function requireAuth() {
  56. if (!$this->isLoggedIn()) {
  57. header('Location: login.php');
  58. exit;
  59. }
  60. }
  61. public function getUser() {
  62. if ($this->isLoggedIn()) {
  63. return [
  64. 'id' => $_SESSION['user_id'],
  65. 'username' => $_SESSION['username'],
  66. 'role' => $_SESSION['role']
  67. ];
  68. }
  69. return null;
  70. }
  71. public function isAdmin() {
  72. return $this->isLoggedIn() && $_SESSION['role'] === 'admin';
  73. }
  74. private function createSession($user) {
  75. $_SESSION['user_id'] = $user['id'];
  76. $_SESSION['username'] = $user['username'];
  77. $_SESSION['role'] = $user['role'];
  78. $_SESSION['auth_type'] = $user['auth_type'];
  79. $_SESSION['logged_in'] = true;
  80. }
  81. private function updateLastLogin($userId) {
  82. $sql = "UPDATE users SET last_login = NOW() WHERE id = ?";
  83. $this->db->query($sql, [$userId]);
  84. }
  85. private function updateUserFromLDAP($userId, $ldapUserInfo) {
  86. $updateData = [];
  87. if (isset($ldapUserInfo['email'])) {
  88. $updateData['email'] = $ldapUserInfo['email'];
  89. }
  90. if (isset($ldapUserInfo['ldap_dn'])) {
  91. $updateData['ldap_dn'] = $ldapUserInfo['ldap_dn'];
  92. }
  93. if (!empty($updateData)) {
  94. $this->db->update('users', $updateData, 'id = ?', [$userId]);
  95. }
  96. }
  97. public function createLDAPUser($username, $ldapUserInfo) {
  98. // Check if user already exists
  99. $sql = "SELECT id FROM users WHERE username = ?";
  100. $existing = $this->db->fetch($sql, [$username]);
  101. if ($existing) {
  102. return $existing['id'];
  103. }
  104. // Create new user from LDAP
  105. $userData = [
  106. 'username' => $username,
  107. 'email' => $ldapUserInfo['email'] ?? '',
  108. 'role' => 'editor', // Default role for LDAP users
  109. 'auth_type' => 'ldap',
  110. 'ldap_dn' => $ldapUserInfo['ldap_dn'] ?? ''
  111. ];
  112. return $this->db->insert('users', $userData);
  113. }
  114. public function getAuthType() {
  115. return $_SESSION['auth_type'] ?? 'local';
  116. }
  117. }
  118. // Initialize session
  119. if (session_status() === PHP_SESSION_NONE) {
  120. session_start();
  121. }