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
+4 -2
View File
@@ -23,9 +23,11 @@ function randomId(int $mode)
return randomId($mode);
else
return $id;
break;
case 2:
break;
if (Space::exists($id))
return randomId($mode);
else
return $id;
}
}
+4 -5
View File
@@ -12,14 +12,13 @@ require_once 'functions/user.php';
require_once 'functions/post.php';
require_once 'functions/space.php';
/**/
// Set SESSION variable.
if (!isset($_SESSION['initialized'])) {
$_SESSION['initialized'] = true;
$_SESSION['theme'] = 'dark';
$_SESSION['initialized'] = true;
$_SESSION['theme'] = 'dark';
} else if (isset($_POST['theme'])) {
$_SESSION['theme'] = $_POST['theme'];
unset($_POST['theme']);
$_SESSION['theme'] = $_POST['theme'];
unset($_POST['theme']);
}
define('owner', 1);
+46 -35
View File
@@ -60,10 +60,6 @@ class Post
$result = exec_stmt($stmt, 'i', $id)->fetch_assoc();
if ($result['count(id)'] == 1)
return true;
else if ($result['count(id)'] > 1) {
// TODO: Add additional logging here, since that means multiple posts have the same ID.
return true;
}
return false;
}
@@ -80,11 +76,10 @@ class Post
return new Essay(null, $id, $creator, 1, $post['status'], $post['title'], $post['markdown'], $post['excerpt']);
case 3:
return new Thought(null, $post['id'], $creator, 3, $post['status'], $post['thought']);
default:
return null;
}
} else
return null;
}
return null;
}
public function setId(int $id): void
@@ -92,11 +87,6 @@ class Post
$this->id = $id;
}
public function authUser(User $user): bool
{
return true;
}
public function getId(): int
{
return $this->id;
@@ -149,6 +139,10 @@ class Post
public function getSpace(): ?Space
{
if (!isset($this->space)) {
$stmt = 'SELECT space from spacePosts WHERE post = ?';
$rawSpace = exec_stmt($stmt, 'i', $this->id)->fetch_assoc();
}
return $this->space;
}
}
@@ -156,21 +150,17 @@ class Post
class Essay extends Post
{
private string $title;
private string $html;
private string $markdown;
private string $excerpt;
/* private array $files; */
public function __construct(?Space $space = null, ?int $id = null, User $creator, int $postType, int $status, string $title, string $markdown, string $excerpt)
{
parent::__construct($space, $id, $creator, 1, $postType, $status);
require_once 'vendor/autoload.php';
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
$this->title = $title;
$this->excerpt = $excerpt;
$this->html = $parsedown->text($markdown);
$this->markdown = $markdown;
}
public function getCardHTML(): ?string
@@ -184,8 +174,8 @@ class Essay extends Post
<h4>' . $this->title . '</h4>
</div>
<div class="">
<span>Something</span>
<div class="sm_width">
<i class="nf nf-md-dots_horizontal"></i>
</div>
</div>
<div class="row full_width">
@@ -198,14 +188,19 @@ class Essay extends Post
return $html;
}
public function allowUser(User $user): bool
{
return parent::getSpace()->allowUser($user);
}
public function getTitle(): ?string
{
return $this->title;
}
public function getHTML(): ?string
public function getMarkdown(): ?string
{
return $this->html;
return $this->markdown;
}
public function getExcerpt(): ?string
@@ -215,16 +210,18 @@ class Essay extends Post
public function setMarkdown(?string $markdown): void
{
require_once 'vendor/autoload.php';
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
$this->html = $parsedown->text($markdown);
$this->markdown = $markdown;
}
public function setExcerpt(?string $excerpt): void
{
$this->excerpt = $excerpt;
}
public function getSpace(): ?Space
{
return parent::getSpace();
}
}
class Moment extends Post
@@ -269,27 +266,41 @@ class Thought extends Post
parent::__construct($space, $id, $creator, $postType, $status);
$this->thought = $thought;
if ($id == null) {
if ($id < 0) {
$stmt = 'INSERT INTO post (id, parentSpace, creator, postType, status, thought) VALUES (?, ?, ?, ?, ?, ?)';
$newID = randomId(1);
parent::setId($newID);
parent::setId($id * -1);
$space = isset($space) ? $space->getId() : $creator->getHomeSpace()->getId();
exec_stmt($stmt, 'iiiiis', $newID, $space, $creator->getId(), 3, 2, $this->thought);
exec_stmt($stmt, 'iiiiis', $id * -1, $space, $creator->getId(), 3, 2, $this->thought);
$stmt = 'INSERT INTO spacePosts (post, space) VALUES (?, ?)';
exec_stmt($stmt, 'ii', $newID, $space);
exec_stmt($stmt, 'ii', $id * -1, $space);
}
}
public function getCardHTML(): ?string
{
$html = '
<div class="std_border padding center full_width top_margin bottom_margin">
<div class="std_border padding center full_width top_margin bottom_margin rel" id="post-' . parent::getId() . '">
<div class="row space_between full_width">
<a href="user.php?view=display&id=' . parent::getCreator()->getId() . '">@' . parent::getCreator()->getUsername() . '</a>
<div class="">
<span>Something</span>
';
if (isset($_SESSION['user'])) {
$user = unserialize($_SESSION['user']);
if ($user->getId() == parent::getCreator()->getId()) {
$html .= '
<div class="dropdown" style="z-index: 0;">
<i class="nf nf-md-dots_horizontal"></i>
<div class="sm_border padding glass dropdownContent">
<a class="underline link" onclick="deletePost(' . parent::getId() . ')">Delete</a>
</div>
</div>
';
}
}
$html .= '
</div>
<div class="line"></div>
<div class="row full_width">
+96 -38
View File
@@ -10,13 +10,12 @@ class Space
private string $name;
private string $description;
private int $visibility;
private array $members;
private array $posts;
private ?array $members;
private ?array $posts;
private ?Space $parent;
public function __construct(?int $id, string $name, string $description, int $visibility, array $members, array $posts, ?Space $parent = null)
public function __construct(int $id, string $name, string $description, int $visibility, ?array $members, ?array $posts, ?Space $parent = null, bool $favorited = false)
{
$this->id = $id ?? randomId(2);
$this->name = $name;
$this->description = $description;
$this->visibility = $visibility;
@@ -24,33 +23,101 @@ class Space
$this->posts[] = $posts;
$this->parent = $parent;
/* $stmt = 'INSERT INTO spaces (spaceId, name, description, parentSpaceIdk)'; */
if ($id < 0) {
$this->id = $id * -1;
error_log('ID: ' . $this->id);
$stmt = 'INSERT INTO space (id, name, description, visibility, parentSpace) VALUES (?, ?, ?, ?, ?)';
exec_stmt($stmt, 'issii', $this->id, $name, $description, $visibility, (isset($parent) ? $parent->getId() : null));
error_log('Added space in DB');
} else
$this->id = $id;
}
public static function exists(int $id): bool
{
$stmt = 'SELECT count(id) FROM space WHERE id = ?';
$result = exec_stmt($stmt, 'i', $id)->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 addUserToSpace(int $user, int $space, int $role = 3, bool $favorited = false)
{
$stmt = 'INSERT INTO spaceMembers (user, space, role, favorited) VALUES (?, ?, ?, ?)';
exec_stmt($stmt, 'iiii', $user, $space, $role, $favorited);
}
public function getSubSpaces(): ?array
{
$stmt = 'SELECT id FROM space WHERE parentSpace = ?';
$rawSubSpaces = exec_stmt($stmt, 'i', $this->id);
$subSpaces = [];
foreach ($rawSubSpaces as $ss) {
$subSpaces[] = Space::retrieveFromDB($ss['id']);
}
return $subSpaces;
}
public function getParentSpace(): ?Space
{
return $this->parent;
}
public function getCardHTML(): string
{
$html = '
<div class="full_width std_border padding center top_margin bottom_margin">
<a href="spaces.php?view=show&id=' . $this->id . '" class="divLink">
<div class="full_width std_border padding center top_margin bottom_margin rel" id="space-' . $this->id . '">
<a href="spaces.php?view=show&id=' . $this->id . '" class="fillDiv spaceCard"></a>
<div class="row space_between full_width">
<div class="column std_width">
<h4>' . $this->name . '</h4>
</div>
<div class="">
<span>Something</span>
';
if (isset($_SESSION['user'])) {
$user = unserialize($_SESSION['user']);
if ($user->getId() == $this->getOwner()->getId()) {
$html .= '
<div class="dropdown">
<i class="nf nf-md-dots_horizontal"></i>
<div class="sm_border padding glass dropdownContent">
<a class="underline link" onclick="deleteSpace(' . $this->id . ')">Delete</a>
</div>
</div>
';
}
}
$html .= '
</div>
<div class="row full_width">
<p>' . $this->description . '</p>
</div>
</a>
</div>
';
return $html;
}
public function getOwner(): User
{
$stmt = 'SELECT user FROM spaceMembers WHERE space = ? AND role = ?';
$user = exec_stmt($stmt, 'ii', $this->id, 1)->fetch_assoc();
return User::retrieveFromDB($user['user']);
}
public static function retrieveFromDB(int $space, int $user = -1): ?Space
{
if ($user != -1) {
@@ -68,41 +135,32 @@ class Space
$stmt = 'SELECT * FROM space WHERE id = ?';
$rawSpace = exec_stmt($stmt, 'i', $space)->fetch_assoc();
if (!empty($rawSpace['parentSpace']))
$parent = Space::retrieveFromDB($rawSpace['parentSpace'], $user);
else
$parent = null;
if ($rawSpace) {
if (!empty($rawSpace['parentSpace']))
$parent = Space::retrieveFromDB($rawSpace['parentSpace'], $user);
else
$parent = null;
$stmt = 'SELECT user FROM spaceMembers WHERE space = ?';
$members = [];
$rawMembers = exec_stmt($stmt, 'i', $rawSpace['id']);
foreach ($rawMembers as $m) {
$members[] = User::retrieveFromDB($m['user']);
}
$stmt = 'SELECT user FROM spaceMembers WHERE space = ?';
$members = [];
$rawMembers = exec_stmt($stmt, 'i', $rawSpace['id']);
foreach ($rawMembers as $m)
$members[] = User::retrieveFromDB($m['user']);
$stmt = 'SELECT post FROM spacePosts WHERE space = ?';
$posts = [];
$rawPosts = exec_stmt($stmt, 'i', $rawSpace['id']);
while ($row = $rawPosts->fetch_assoc()) {
$posts[] = Post::retrieveFromDB($row['post']);
}
$stmt = 'SELECT post FROM spacePosts INNER JOIN post ON spacePosts.post = post.id WHERE space = ? ORDER BY post.createdAt DESC';
$posts = [];
$rawPosts = exec_stmt($stmt, 'i', $rawSpace['id']);
while ($row = $rawPosts->fetch_assoc())
$posts[] = Post::retrieveFromDB($row['post']);
$space = new Space($rawSpace['id'], $rawSpace['name'], $rawSpace['description'], $rawSpace['visibility'], $members, $posts, $parent);
foreach ($posts as $p)
$p->setSpace($space);
return $space;
return new Space($rawSpace['id'], $rawSpace['name'], $rawSpace['description'], $rawSpace['visibility'], $members, $posts, $parent);
} else
return null;
}
public function allowUser(User $user)
{
$stmt = 'SELECT space FROM spaceMembers WHERE user = ?';
$result = exec_stmt($stmt, 'i', $user->getId())->fetch_assoc();
if ($result['space'] == $this->id)
return true;
else
return false;
return in_array($user, $this->members);
}
public function getPosts()
+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;