implemented spaces, fixed login and signup, thought creation and space creation.

This commit is contained in:
2025-05-26 13:00:12 -06:00
parent 54a01f0f1f
commit d3fa1733f4
32 changed files with 1499 additions and 1132 deletions
+103 -82
View File
@@ -33,15 +33,13 @@ class User
private int $id;
private ?array $spaces;
private string $username;
private ?DateTime $createdAt;
private ?DateTime $lastActive;
private bool $isActive;
private ?string $profilePicture;
private ?string $bio;
private ?string $website;
private int $role;
public function __construct(int $id = -1, ?array $spaces, string $username, ?string $profilePicture = null, ?string $bio = null, ?string $website = null, int $role = 3)
public function __construct(int $id = -1, ?array $spaces, string $username, ?string $profilePicture = null, ?string $bio = null, ?string $website = null, int $role = 3, ?string $passwordHash)
{
// Basic XSS prevention on construction (can be enhanced)
$username = htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
@@ -50,77 +48,81 @@ class User
$profilePicture = htmlspecialchars($profilePicture ?? 'default-profile.png', ENT_QUOTES, 'UTF-8');
// Basic SQL injection prevention (should primarily rely on prepared statements)
if (preg_match('/[\'";\-\_]/', $username) || preg_match('/[\'";\-\_]/', $bio) || preg_match('/[\'";\-\_]/', $website) || preg_match('/[\'";\-\_]/', $profilePicture)) {
throw new InvalidArgumentException('Invalid characters in user data.');
}
/* if (preg_match('/[\'";\-\_]/', $username) || preg_match('/[\'";\-\_]/', $bio) || preg_match('/[\'";\-\_]/', $website) || preg_match('/[\'";\-\_]/', $profilePicture)) { */
/* throw new InvalidArgumentException('Invalid characters in user data.'); */
/* } */
$this->username = $username;
$this->profilePicture = $profilePicture;
$this->bio = $bio;
$this->website = $website;
$this->createdAt = new DateTime();
$this->lastActive = null;
$this->isActive = false;
$this->role = $role;
$this->spaces = null;
if ($id == -1) {
$this->id = randomId(0);
$stmt = 'INSERT INTO user (userId, username, profilePicture, bio, website) VALUES (?, ?, ?, ?, ?)';
exec_stmt($stmt, 'issss', $this->id, $this->username, $this->profilePicture, $this->bio, $this->website);
if ($id < 0) {
$this->id = $id * -1;
$stmt = 'INSERT INTO user (id, username, passwordHash, profilePicture, bio, website) VALUES (?, ?, ?, ?, ?, ?)';
exec_stmt($stmt, 'isssss', $this->id, $this->username, $passwordHash, $this->profilePicture, $this->bio, $this->website);
Space::addUserToSpace($this->id, 2025);
new Space($id, $this->username, $this->username . "'s private Space.", 5, [$this], [], null, true);
} else
$this->id = $id;
}
public static function exists(int $id): bool
public static function exists($identifier): bool
{
$stmt = 'SELECT count(id) FROM user WHERE id = ?';
$result = exec_stmt($stmt, 'i', $id)->fetch_assoc();
if (is_int($identifier)) {
$stmt = 'SELECT count(id) FROM user WHERE id = ?';
$result = exec_stmt($stmt, 'i', $identifier)->fetch_assoc();
if ($result['count(id)'] == 1)
return true;
else if ($result['count(id)'] > 1)
return true;
else
return false;
if ($result['count(id)'] == 1)
return true;
else if ($result['count(id)'] > 1)
return true;
else
return false;
} else if (is_string($identifier)) {
$stmt = 'SELECT count(id) FROM user WHERE username = ?';
$result = exec_stmt($stmt, 's', $identifier)->fetch_assoc();
if ($result['count(id)'] == 1)
return true;
else if ($result['count(id)'] > 1)
return true;
else
return false;
}
return true;
}
public static function retrieveFromDB(int $userId): ?User
{
$stmt = 'SELECT id, username, profilePicture, bio, website FROM user WHERE id = ?';
$stmt = 'SELECT id, username, profilePicture, bio, website, role FROM user WHERE id = ?';
$user = exec_stmt($stmt, 'i', $userId)->fetch_assoc();
if ($user) {
return new User($user['id'], null, $user['username'], $user['profilePicture'], $user['bio'], $user['website']);
} else {
if ($user)
return new User($user['id'], null, $user['username'], $user['profilePicture'], $user['bio'], $user['website'], $user['role'], null);
else
return null;
}
}
public static function getSignupHTML()
{
$html = '
<div class="form_card">
<h3 class="underline">Sign Up</h3>
$experimentalHTML = '
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="form_id" value="signup_form">
<input name="required_field" id="required_field" value="">
<input type="hidden" name="crop_x" value="" id="crop_x">
<input type="hidden" name="crop_y" value="" id="crop_y">
<input type="hidden" name="crop_width" value="" id="crop_width">
<input name="required_field" id="required_field" value="">
<label for="username">Username</label>
<input id="username" name="username" required>
<label for="email">Email Address</label>
<input id="email" name="email" required>
<label for="password">Password</label>
<input id="password" name="password" type="password" required>
<label for="verify_password">Verify Password</label>
<input id="verify_password" name="verify_password" type="password" required>
<label for="profile_picture_input" class="upload_button modal_button" id="profile_cropper_button">Upload a Profile Picture</label>
<input id="profile_picture_input" class="file_input" name="profile_picture" type="file" accept="image/png, image/jpeg, image/jpg">
<small class="file_input_feedback">No file selected.</small>
@@ -129,17 +131,28 @@ class User
<input name="stay_logged_in" type="checkbox" value="true">
<span class="checkmark"></span>
</label>
';
$html = '
<div class="med_width std_border padding center modalContent">
<h3 class="underline">Sign Up</h3>
<div class="line"></div>
<form method="post" action="director.php">
<input type="hidden" name="formID" value="signup">
<label for="username">Username</label>
<input id="username" name="username" required autofocus>
<label for="password">Password</label>
<input id="password" name="password" type="password" required>
<label for="vPassword">Verify Password</label>
<input id="vPassword" name="vPassword" type="password" required>
<input class="button" type="submit" value="Sign Up">
</form>
<div id="modal" class="modal">
<div id="display_modal"></div>
</div>
<div style="display: none;">
<div id="profile_cropper" class="modal_form">' . profile_cropper() . '</div>
</div>
<div id="signupError"></div>
</div>
';
@@ -148,28 +161,30 @@ class User
public static function getLoginHTML()
{
$html = '
<div class="form_card">
<h3 class="underline">Log In</h3>
<form method="post" id="login_form" action="director.php">
<input type="hidden" name="form_id" value="login_form">
<input type="hidden" name="password_hash" value="" id="password_hash">
$stayCheckedIn = '
<label class="form_checkbox_container">Stay Logged In?
<input name="stay_logged_in" type="checkbox" value="true">
<span class="checkmark"></span>
</label>
';
<label for="username">Username / Email</label>
$html = '
<div class="med_width std_border padding center modalContent">
<h3 class="underline">Log In</h3>
<div class="line"></div>
<form method="post" action="director.php">
<input type="hidden" name="formID" value="login">
<label for="username">Username</label>
<input id="username" name="username" spellcheck="false" required autofocus>
<label for="password">Password</label>
<input id="password" name="password" type="password" spellcheck="false" required>
<label class="form_checkbox_container">Stay Logged In?
<input name="stay_logged_in" type="checkbox" value="true">
<span class="checkmark"></span>
</label>
<input class="button" type="submit" value="Log In">
</form>
<script src="js/login.js" defer></script>
<div id="loginError"></div>
</div>
';
@@ -183,8 +198,17 @@ class User
if (!password_verify($credentials->getPassword(), $user['passwordHash']))
return null;
else
return new User($user['id'], null, $user['username'], $user['profilePicture'], $user['bio'], $user['website']);
else {
$spaces = [];
$stmt = 'SELECT space FROM spaceMembers WHERE user = ?';
$rawSpaces = exec_stmt($stmt, 'i', $user['id'])->fetch_assoc();
if ($rawSpaces)
foreach ($rawSpaces as $s)
$spaces[] = serialize(Space::retrieveFromDB($s));
return new User($user['id'], $spaces, $user['username'], $user['profilePicture'], $user['bio'], $user['website'], $user['role'], null);
}
}
public static function logout(): void
@@ -205,14 +229,26 @@ class User
public function getSpaces()
{
$stmt = 'SELECT * FROM spaceMembers WHERE user = ?';
$spaces = [];
$stmt = 'SELECT space FROM spaceMembers WHERE user = ?';
$rawSpaces = exec_stmt($stmt, 'i', $this->id);
while ($row = $rawSpaces->fetch_assoc()) {
$spaces[] = Space::retrieveFromDB($row['space'], $row['user']);
$spaces = [];
while ($r = $rawSpaces->fetch_assoc()) {
$space = Space::retrieveFromDB($r['space']);
if (!$space->getParentSpace())
$spaces[] = $space;
}
$this->spaces = $spaces;
return $spaces;
}
public function getAllSpaces()
{
$stmt = 'SELECT space FROM spaceMembers WHERE user = ?';
$rawSpaces = exec_stmt($stmt, 'i', $this->id);
$spaces = [];
while ($r = $rawSpaces->fetch_assoc())
$spaces[] = Space::retrieveFromDB($r['space']);
return $spaces;
}
@@ -231,16 +267,6 @@ class User
return $this->username;
}
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
public function getLastActive(): ?DateTime
{
return $this->lastActive;
}
public function isActive(): bool
{
return $this->isActive;
@@ -261,11 +287,6 @@ class User
return $this->website;
}
public function setLastActive(?DateTime $lastActive): void
{
$this->lastActive = $lastActive;
}
public function setIsActive(bool $isActive): void
{
$this->isActive = $isActive;