Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
/**
* User Model
* Handles all user-related database operations
*/
require_once __DIR__ . '/../config/database.php';
class User {
private $db;
private $table = 'users';
public $id;
public $username;
public $email;
public $password_hash;
public $role;
public $created_at;
public function __construct() {
$this->db = Database::getInstance()->getConnection();
}
/**
* Create a new user
*/
public function create($username, $email, $password, $role = 'volunteer') {
$query = "INSERT INTO {$this->table} (username, email, password_hash, role)
VALUES (:username, :email, :password_hash, :role)";
$stmt = $this->db->prepare($query);
$password_hash = password_hash($password, PASSWORD_BCRYPT);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password_hash', $password_hash);
$stmt->bindParam(':role', $role);
if ($stmt->execute()) {
return $this->db->lastInsertId();
}
return false;
}
/**
* Find user by email
*/
public function findByEmail($email) {
$query = "SELECT * FROM {$this->table} WHERE email = :email LIMIT 1";
$stmt = $this->db->prepare($query);
$stmt->bindParam(':email', $email);
$stmt->execute();
return $stmt->fetch();
}
/**
* Find user by ID
*/
public function findById($id) {
$query = "SELECT * FROM {$this->table} WHERE id = :id LIMIT 1";
$stmt = $this->db->prepare($query);
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt->fetch();
}
/**
* Get all users
*/
public function getAll() {
$query = "SELECT id, username, email, role, created_at FROM {$this->table} ORDER BY created_at DESC";
$stmt = $this->db->prepare($query);
$stmt->execute();
return $stmt->fetchAll();
}
/**
* Check if email exists
*/
public function emailExists($email, $excludeId = null) {
$query = "SELECT id FROM {$this->table} WHERE email = :email";
if ($excludeId) {
$query .= " AND id != :excludeId";
}
$stmt = $this->db->prepare($query);
$stmt->bindParam(':email', $email);
if ($excludeId) {
$stmt->bindParam(':excludeId', $excludeId);
}
$stmt->execute();
return $stmt->rowCount() > 0;
}
/**
* Verify password
*/
public function verifyPassword($password, $hash) {
return password_verify($password, $hash);
}
}