|
|
<?php |
|
|
|
|
|
|
|
|
class Database { |
|
|
private $host = "127.0.0.1"; |
|
|
private $db_name = "jmdb"; |
|
|
private $username = "root"; |
|
|
private $password = "YourStrongPassword123"; |
|
|
public $conn; |
|
|
|
|
|
public function getConnection() { |
|
|
$this->conn = null; |
|
|
|
|
|
try { |
|
|
$this->conn = new PDO( |
|
|
"mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4", |
|
|
$this->username, |
|
|
$this->password |
|
|
); |
|
|
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
|
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
|
|
} catch(PDOException $exception) { |
|
|
error_log("Database connection error: " . $exception->getMessage()); |
|
|
throw new Exception("Database connection failed: " . $exception->getMessage()); |
|
|
} |
|
|
|
|
|
return $this->conn; |
|
|
} |
|
|
} |
|
|
|
|
|
class SessionManager { |
|
|
private $db; |
|
|
|
|
|
public function __construct($database) { |
|
|
$this->db = $database->getConnection(); |
|
|
} |
|
|
|
|
|
|
|
|
public function createSession($user_id, $ip_address = null, $user_agent = null) { |
|
|
$session_id = bin2hex(random_bytes(64)); |
|
|
$expires_at = date('Y-m-d H:i:s', strtotime('+24 hours')); |
|
|
|
|
|
$query = "INSERT INTO user_sessions |
|
|
SET user_id = :user_id, session_id = :session_id, ip_address = :ip_address, |
|
|
user_agent = :user_agent, expires_at = :expires_at"; |
|
|
|
|
|
$stmt = $this->db->prepare($query); |
|
|
$stmt->bindParam(":user_id", $user_id); |
|
|
$stmt->bindParam(":session_id", $session_id); |
|
|
$stmt->bindParam(":ip_address", $ip_address); |
|
|
$stmt->bindParam(":user_agent", $user_agent); |
|
|
$stmt->bindParam(":expires_at", $expires_at); |
|
|
|
|
|
if ($stmt->execute()) { |
|
|
return $session_id; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
public function validateSession($session_id) { |
|
|
$query = "SELECT us.*, u.* |
|
|
FROM user_sessions us |
|
|
JOIN users u ON us.user_id = u.id |
|
|
WHERE us.session_id = :session_id |
|
|
AND us.is_active = 1 |
|
|
AND us.expires_at > NOW()"; |
|
|
|
|
|
$stmt = $this->db->prepare($query); |
|
|
$stmt->bindParam(":session_id", $session_id); |
|
|
$stmt->execute(); |
|
|
|
|
|
return $stmt->fetch(PDO::FETCH_ASSOC); |
|
|
} |
|
|
|
|
|
|
|
|
public function logActivity($user_id, $activity_type, $description = null, $ip_address = null, $user_agent = null) { |
|
|
$query = "INSERT INTO user_activity_log |
|
|
SET user_id = :user_id, activity_type = :activity_type, description = :description, |
|
|
ip_address = :ip_address, user_agent = :user_agent"; |
|
|
|
|
|
$stmt = $this->db->prepare($query); |
|
|
$stmt->bindParam(":user_id", $user_id); |
|
|
$stmt->bindParam(":activity_type", $activity_type); |
|
|
$stmt->bindParam(":description", $description); |
|
|
$stmt->bindParam(":ip_address", $ip_address); |
|
|
$stmt->bindParam(":user_agent", $user_agent); |
|
|
|
|
|
return $stmt->execute(); |
|
|
} |
|
|
|
|
|
|
|
|
public function updateLastLogin($user_id) { |
|
|
$query = "UPDATE users SET last_login = NOW() WHERE id = :user_id"; |
|
|
$stmt = $this->db->prepare($query); |
|
|
$stmt->bindParam(":user_id", $user_id); |
|
|
return $stmt->execute(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
$database = new Database(); |
|
|
$db = $database->getConnection(); |
|
|
$sessionManager = new SessionManager($database); |
|
|
} catch(Exception $e) { |
|
|
error_log("Initialization error: " . $e->getMessage()); |
|
|
$db = null; |
|
|
$sessionManager = null; |
|
|
} |
|
|
?> |