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
+209 -18
View File
@@ -7,41 +7,218 @@ if (isset($_POST['ajaxId'])) {
case 'login':
handle_login();
break;
case 'loginHTML':
handleLoginHTML();
break;
case 'signupHTML':
handleSignupHTML();
break;
case 'usernameCheck':
handleUsernameCheck();
break;
case 'logout':
User::logout();
break;
case 'newPostHTML':
handleNewPostHTML();
break;
case 'newThought':
handleNewThought();
case 'newSpaceHTML':
handleNewSpaceHTML();
break;
case 'deletePost':
handleDeletePost();
break;
case 'deleteSpace':
handleDeleteSpace();
break;
}
} else if (isset($_POST['formID'])) {
switch ($_POST['formID']) {
case 'spaceNew':
break;
case 'signup':
handleSignup();
break;
case 'newPost':
handleNewPost();
break;
case 'newSpace':
handleNewSpace();
break;
}
} else {
$response = [
'error' => pretty_dump($_POST),
];
echo json_encode($response);
if (!isset($_POST['redirect']))
header('Location: .');
else
header('Location: ' . $_POST['redirect']);
}
function handleNewThought()
function handleDeleteSpace()
{
$thought = new Thought(Space::retrieveFromDB($_POST['space']), null, unserialize($_SESSION['user']), 3, 2, urldecode($_POST['thought']));
echo json_encode(['log' => 'success']);
if (!isset($_POST['id'])) {
echo json_encode(['error' => 'Missing required Space ID.']);
return;
}
$stmt = 'DELETE FROM spaceMembers where space = ?';
exec_stmt($stmt, 'i', $_POST['id']);
$stmt = 'DELETE FROM space WHERE id = ?';
exec_stmt($stmt, 'i', $_POST['id']);
}
function handleNewSpace()
{
if (!isset($_POST['name']) || !isset($_POST['description']) || !isset($_POST['visibility'])) {
error_log('ruh roh');
return;
}
$id = randomId(2) * -1;
/* $name = htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8'); */
/* $description = htmlspecialchars($_POST['description'], ENT_QUOTES, 'UTF-8'); */
$space = new Space($id, $_POST['name'], $_POST['description'], $_POST['visibility'], null, null, isset($_POST['space']) ? Space::retrieveFromDB($_POST['space']) : null, true);
error_log(pretty_dump($space));
Space::addUserToSpace(unserialize($_SESSION['user'])->getId(), $id * -1, 1, true);
}
function handleNewPost()
{
if (!isset($_POST['postType']) || !isset($_POST['space']))
return;
switch ($_POST['postType']) {
case 'Thought':
if (!isset($_POST['thought']))
return;
$id = randomId(1) * -1;
new Thought(Space::retrieveFromDB($_POST['space']), $id, unserialize($_SESSION['user']), 3, 2, urldecode($_POST['thought']));
break;
case 'Moment':
break;
case 'Essay':
new Essay(Space::retrieveFromDB($_POST['space']), null, unserialize($_SESSION['user']), 3, 2, $_POST['title'], $_POST['markdown'], $_POST['excerpt']);
break;
}
}
function handleSignup()
{
if (!isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['vPassword']))
return;
if ($_POST['password'] != $_POST['vPassword'])
return;
$_SESSION['user'] = serialize(new User(randomId(0) * -1, null, $_POST['username'], null, null, null, 3, password_hash($_POST['password'], PASSWORD_DEFAULT)));
header('Location: .');
}
function handleUsernameCheck()
{
if (!isset($_POST['username']))
return;
echo json_encode(['exists' => User::exists($_POST['username'])]);
}
function handleSignupHTML()
{
if (isset($_SESSION['user']))
header('Location: .');
echo json_encode(['html' => User::getSignupHTML()]);
}
function handleLoginHTML()
{
if (isset($_SESSION['user']))
header('Location: .');
echo json_encode(['html' => User::getLoginHTML()]);
}
function handleDeletePost()
{
if (!isset($_POST['id'])) {
echo json_encode(['error' => 'Missing required Post ID.']);
return;
}
$stmt = 'DELETE FROM post WHERE id = ?';
exec_stmt($stmt, 'i', $_POST['id']);
}
function handleNewSpaceHTML()
{
$user = unserialize($_SESSION['user']);
$html = '
<div class="med_width std_border center padding modalContent">
<h3>New Space</h3>
<form method="post" action="director.php">
<input type="hidden" name="formID" value="newSpace">
<input type="hidden" name="redirect" value="" id="redirect">
<input name="name" placeholder="Name" required autofocus>
<input name="description" placeholder="Description" required>
<select name="visibility">
<option value="1">Open</option>
<option value="2">Public</option>
<option value="3">Request</option>
<option value="4">Invite</option>
<option value="5">Private</option>
</select>
';
$spaces = $user->getAllSpaces();
foreach ($spaces as $s) {
$html .= '
<label class="form_checkbox_container">' . $s->getName() . ' <i class="nf ' . getSpaceIcon($s) . ' left_margin"></i>
<input type="radio" name="space" value="' . $s->getId() . '" id="space-' . $s->getId() . '">
<span class="checkmark"></span>
</label>
';
}
$html .= '
<input class="button" type="submit" value="Create" id="create">
</form>
</div>
';
echo json_encode(['html' => $html]);
}
function getSpaceIcon($space)
{
switch ($space->getVisibility()) {
case 1:
return 'nf-cod-globe';
case 2:
return 'nf-fa-group';
case 3:
case 4:
return 'nf-fa-user_group';
case 5:
return 'nf-fa-lock';
}
}
function handleNewPostHTML()
{
$user = unserialize($_SESSION['user']);
// TODO: Need to plumb this into the DB via AJAX.
// TODO: Need to store the current space in SESSION and use that to auto-select a Space
// TODO: List the user's spaces or let them search for a space or quick select a favorited space
$html = '
<div class="med_width std_border center padding">
<div class="med_width std_border center padding modalContent">
<h3 id="newPostLabel">New Thought</h3>
<form method="post" class="center">
<input type="hidden" name="userID" value="' . unserialize($_SESSION['user'])->getId() . '">
<form method="post" action="director.php">
<input type="hidden" name="formID" value="newPost">
<input type="hidden" name="redirect" value="" id="redirect">
<input type="hidden" name="postType" value="Thought" id="postType">
<input type="hidden" name="space" value="' . $_GET['id'] . '">
<div id="newThought" class="column">
<input type="text" name="thought" placeholder="Jot a Thought." autofocus id="thought">
@@ -57,6 +234,22 @@ function handleNewPostHTML()
<input type="text" name="excerpt" placeholder="A 2-3 sentence summary." id="excerpt">
</div>
<div class="column" id="postSpaces">
';
$spaces = $user->getAllSpaces();
foreach ($spaces as $s) {
$html .= '
<label class="form_checkbox_container">' . $s->getName() . ' <i class="nf ' . getSpaceIcon($s) . ' left_margin"></i>
<input type="radio" name="space" value="' . $s->getId() . '" id="space-' . $s->getId() . '">
<span class="checkmark"></span>
</label>
';
}
$html .= '
</div>
<input class="button" type="submit" value="Share" id="share">
</form>
<div class="line std_border"></div>
@@ -93,20 +286,18 @@ function handle_login()
// Serialize the user object and store it in the session
$_SESSION['user'] = serialize($user);
// You might want to store specific user data instead of the whole serialized object,
// e.g., $_SESSION['user_id'] = $user->getId(); $_SESSION['username'] = $user->getUsername();
$spaces = $user->getSpaces();
foreach ($spaces as $s)
$_SESSION['spaces'][] = serialize($s);
} else {
$response['status'] = 'error';
$response['message'] = 'Incorrect username and/or password.';
}
} catch (Exception $e) {
// Catch potential exceptions from LoginCredentials or User::login()
// Log the detailed error for server-side debugging
error_log('Login Error: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine());
$response['status'] = 'error';
$response['message'] = 'An internal error occurred. Please try again later.';
// Optionally, for development, you could include $e->getMessage() but not in production.
}
} else {
$response['status'] = 'error';