310 lines
10 KiB
PHP
310 lines
10 KiB
PHP
<?php
|
|
include 'functions/init.php';
|
|
|
|
error_log('RECEIVED: ' . pretty_dump($_POST));
|
|
if (isset($_POST['ajaxId'])) {
|
|
switch ($_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 '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;
|
|
}
|
|
|
|
if (!isset($_POST['redirect']))
|
|
header('Location: .');
|
|
else
|
|
header('Location: ' . $_POST['redirect']);
|
|
}
|
|
|
|
function handleDeleteSpace()
|
|
{
|
|
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 modalContent">
|
|
<h3 id="newPostLabel">New Thought</h3>
|
|
<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">
|
|
</div>
|
|
|
|
<div id="newMoment" class="column" style="display: none;">
|
|
<input type="text" name="caption" placeholder="Caption goes here." autofocus id="caption">
|
|
</div>
|
|
|
|
<div id="newEssay" class="column" style="display: none;">
|
|
<input type="text" name="title" placeholder="Title" autofocus id="title">
|
|
<textarea name="markdown" placeholder="#Heading" id="markdown"></textarea>
|
|
<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>
|
|
<div id="newModalNav" class="row space_between">
|
|
<span id="newNavThought" class="link">Thought</span>
|
|
<span id="newNavMoment" class="link">Moment</span>
|
|
<span id="newNavEssay" class="link">Essay</span>
|
|
</div>
|
|
</div>
|
|
';
|
|
|
|
echo json_encode(['html' => $html]);
|
|
}
|
|
|
|
function handle_login()
|
|
{
|
|
header('Content-Type: application/json');
|
|
|
|
$response = [];
|
|
|
|
if (isset($_POST['username']) && isset($_POST['password'])) { // Corrected check
|
|
// Ensure LoginCredentials and User classes are loaded
|
|
// e.g., require_once 'path/to/LoginCredentials.php';
|
|
// require_once 'path/to/User.php';
|
|
|
|
try {
|
|
$creds = new LoginCredentials($_POST['username'], $_POST['password']);
|
|
$user = User::login($creds); // Assuming User::login() can return null or throw an exception
|
|
|
|
if ($user != null) {
|
|
$response['status'] = 'success'; // Use a clear status field
|
|
$response['message'] = 'Login Successful.'; // Renamed 'log' to 'message' for clarity
|
|
$response['redirect'] = 'spaces.php?view=list';
|
|
|
|
// Serialize the user object and store it in the session
|
|
$_SESSION['user'] = serialize($user);
|
|
$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) {
|
|
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.';
|
|
}
|
|
} else {
|
|
$response['status'] = 'error';
|
|
$response['message'] = 'Invalid Request: Username and/or password not provided.';
|
|
}
|
|
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|