init Abgabe
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
class BenutzerModel extends Model {
|
||||
|
||||
protected $vorname;
|
||||
protected $nachname;
|
||||
protected $password;
|
||||
protected $passwordWid;
|
||||
protected $email;
|
||||
protected $errorArray = [];
|
||||
|
||||
// --- Getter / Setter ---
|
||||
public function getVorname() { return $this->vorname; }
|
||||
public function setVorname($vorname) { $this->vorname = trim($vorname); }
|
||||
|
||||
public function getNachname() { return $this->nachname; }
|
||||
public function setNachname($nachname) { $this->nachname = trim($nachname); }
|
||||
|
||||
public function getPassword() { return $this->password; }
|
||||
public function setPassword($password) { $this->password = trim($password); }
|
||||
|
||||
public function getPasswordWid() { return $this->passwordWid; }
|
||||
public function setPasswordWid($passwordWid) { $this->passwordWid = trim($passwordWid); }
|
||||
|
||||
public function getEmail() { return $this->email; }
|
||||
public function setEmail($email) { $this->email = trim($email); }
|
||||
|
||||
public function getErrors() { return $this->errorArray; }
|
||||
|
||||
// --- Registrierung ---
|
||||
public function insert() {
|
||||
$this->validate();
|
||||
|
||||
if (!empty($this->errorArray)) return false;
|
||||
|
||||
$db = Database::getInstance()->getConnection();
|
||||
|
||||
// Passwort hashen (kein extra Salt nötig)
|
||||
$hashedPassword = password_hash($this->password, PASSWORD_DEFAULT);
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO user (vorname, nachname, email, passwort) VALUES (?, ?, ?, ?)");
|
||||
if (!$stmt) {
|
||||
$this->errorArray['db'] = "Fehler beim Vorbereiten der DB-Anfrage: " . $db->error;
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmt->bind_param(
|
||||
"ssss",
|
||||
$this->vorname,
|
||||
$this->nachname,
|
||||
$this->email,
|
||||
$hashedPassword
|
||||
);
|
||||
|
||||
if (!$stmt->execute()) {
|
||||
$this->errorArray['db'] = "Fehler beim Einfügen in die DB: " . $stmt->error;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// --- Validierung ---
|
||||
protected function validate() {
|
||||
$errors = [];
|
||||
|
||||
if (empty($this->vorname) || strlen($this->vorname) < 2) {
|
||||
$errors['vorname'] = "Bitte gib einen gültigen Vornamen ein (mind. 2 Zeichen).";
|
||||
}
|
||||
|
||||
if (empty($this->nachname) || strlen($this->nachname) < 2) {
|
||||
$errors['nachname'] = "Bitte gib einen gültigen Nachnamen ein (mind. 2 Zeichen).";
|
||||
}
|
||||
|
||||
if (empty($this->email) || !filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors['email'] = "Bitte gib eine gültige E-Mail-Adresse ein.";
|
||||
}
|
||||
|
||||
if (empty($this->password) || strlen($this->password) < 6) {
|
||||
$errors['password'] = "Das Passwort muss mindestens 6 Zeichen haben.";
|
||||
}
|
||||
|
||||
if ($this->password !== $this->passwordWid) {
|
||||
$errors['passwordWid'] = "Die Passwörter stimmen nicht überein.";
|
||||
}
|
||||
|
||||
$this->errorArray = $errors;
|
||||
return empty($errors);
|
||||
}
|
||||
|
||||
// --- Nutzer laden ---
|
||||
public function getUserById($id) {
|
||||
$db = Database::getInstance()->getConnection();
|
||||
$stmt = $db->prepare("SELECT id, vorname, nachname, email FROM user WHERE id = ?");
|
||||
$stmt->bind_param("i", $id);
|
||||
$stmt->execute();
|
||||
return $stmt->get_result()->fetch_assoc();
|
||||
}
|
||||
|
||||
public function getAllUsers() {
|
||||
$db = Database::getInstance()->getConnection();
|
||||
$stmt = $db->prepare("SELECT id, vorname, nachname FROM user");
|
||||
$stmt->execute();
|
||||
return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
class ChatModel {
|
||||
|
||||
public $senderID;
|
||||
public $empfaengerID;
|
||||
public $nachricht;
|
||||
|
||||
// Nachricht in DB speichern
|
||||
public function insert() {
|
||||
$db = Database::getInstance()->getConnection();
|
||||
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO chat (user_sender, user_empfaenger, nachricht)
|
||||
VALUES (?, ?, ?)
|
||||
");
|
||||
if (!$stmt) {
|
||||
throw new Exception("Fehler beim Vorbereiten der DB-Anfrage: " . $db->error);
|
||||
}
|
||||
|
||||
$stmt->bind_param("iis", $this->senderID, $this->empfaengerID, $this->nachricht);
|
||||
if (!$stmt->execute()) {
|
||||
throw new Exception("Fehler beim Einfügen in die DB: " . $stmt->error);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Nachrichten zwischen zwei Usern abrufen
|
||||
public function getMessagesBetweenUsers($user1, $user2) {
|
||||
$db = Database::getInstance()->getConnection();
|
||||
|
||||
$stmt = $db->prepare("
|
||||
SELECT * FROM chat
|
||||
WHERE (user_sender = ? AND user_empfaenger = ?)
|
||||
OR (user_sender = ? AND user_empfaenger = ?)
|
||||
ORDER BY id ASC
|
||||
");
|
||||
|
||||
if (!$stmt) {
|
||||
throw new Exception("Fehler beim Vorbereiten der DB-Abfrage: " . $db->error);
|
||||
}
|
||||
|
||||
$stmt->bind_param("iiii", $user1, $user2, $user2, $user1);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
return $result->fetch_all(MYSQLI_ASSOC);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
class LoginModel extends Model {
|
||||
|
||||
protected $email;
|
||||
protected $passwort;
|
||||
protected $errorArray = [];
|
||||
|
||||
// Getter und Setter für $email
|
||||
public function getEmail() {
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setEmail($email) {
|
||||
$this->email = trim($email); // trim entfernt Leerzeichen vorne und hinten
|
||||
}
|
||||
|
||||
// Getter und Setter für $passwort
|
||||
public function getPasswort() {
|
||||
return $this->passwort;
|
||||
}
|
||||
|
||||
public function setPasswort($passwort) {
|
||||
$this->passwort = trim($passwort); // trim entfernt Leerzeichen vorne und hinten
|
||||
}
|
||||
|
||||
|
||||
public function getErrors() {
|
||||
return $this->errorArray;
|
||||
}
|
||||
|
||||
|
||||
public function login() {
|
||||
if (!$this->validate()) return false;
|
||||
|
||||
$db = Database::getInstance()->getConnection();
|
||||
$stmt = $db->prepare("SELECT id, passwort, vorname, nachname FROM user WHERE email = ?");
|
||||
$stmt->bind_param("s", $this->email);
|
||||
$stmt->execute();
|
||||
$stmt->store_result();
|
||||
|
||||
if ($stmt->num_rows !== 1) {
|
||||
$this->errorArray['login'] = "E-Mail oder Passwort falsch.";
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmt->bind_result($id, $dbHash, $vorname, $nachname);
|
||||
$stmt->fetch();
|
||||
|
||||
if (!password_verify(trim($this->passwort), $dbHash)) {
|
||||
$this->errorArray['login'] = "E-Mail oder Passwort falsch.";
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$_SESSION['user_id'] = $id; // nur die User-ID
|
||||
$_SESSION['vorname'] = $vorname; // optional für Anzeige
|
||||
$_SESSION['nachname'] = $nachname; // optional für Anzeige
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// --- Validierung ---
|
||||
protected function validate() {
|
||||
$errors = [];
|
||||
if (empty($this->email)) $errors['email'] = "Bitte E-Mail eingeben.";
|
||||
if (empty($this->passwort)) $errors['passwort'] = "Bitte Passwort eingeben.";
|
||||
$this->errorArray = $errors;
|
||||
return empty($errors);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user