dj_mix_hosting_software/classes/User.php
2025-01-09 11:57:11 -08:00

69 lines
No EOL
1.8 KiB
PHP

<?php
namespace DJMixHosting;
use Random\RandomException;
Class User{
private $db;
private $id;
private $username;
private $email;
private $location;
private $bio;
private $created;
private $updated;
private $verified;
private $role;
private $img;
private $api_key;
public function __construct($db){
$this->db = $db;
}
/**
* @throws RandomException
*/
public function newUser($username, $password, $email){
if ($this->check_existing_user($username, $email)){
throw new RandomException("User already exists");
}
$this->username = $username;
$this->email = $email;
$password2 = password_hash($password, PASSWORD_DEFAULT);
$this->password = $password2;
$this->location = "";
$this->bio = "";
$this->created = date('Y-m-d H:i:s');
$this->updated = date('Y-m-d H:i:s');
$this->verified = 0;
$this->role = "user";
$this->img = "";
$this->api_key = bin2hex(random_bytes(32));
$stmt = $this->db->prepare("INSERT INTO users (username, password, email, img) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $this->username, $this->password, $this->email, $this->img);
$stmt->execute();
$stmt->close();
}
private function check_existing_user($username, $email){
$stmt = $this->db->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
return $user;
}
public function login(mixed $username, mixed $password)
{
}
}