323 lines
9.6 KiB
PHP
323 lines
9.6 KiB
PHP
<?php
|
|
include 'functions/init.php';
|
|
|
|
error_log('RECEIVED: ' . pretty_dump($_POST));
|
|
error_log('ALSO: ' . pretty_dump($_FILES));
|
|
|
|
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 'editPostHTML':
|
|
handleEditPostHTML();
|
|
break;
|
|
case 'deletePost':
|
|
handleDeletePost();
|
|
break;
|
|
case 'deleteSpace':
|
|
handleDeleteSpace();
|
|
break;
|
|
case 'viewEssay':
|
|
handleViewEssay();
|
|
break;
|
|
}
|
|
} else if (isset($_POST['formID'])) {
|
|
switch ($_POST['formID']) {
|
|
case 'spaceNew':
|
|
break;
|
|
case 'signup':
|
|
handleSignup();
|
|
break;
|
|
case 'newPost':
|
|
handleNewPost();
|
|
break;
|
|
case 'editPost':
|
|
handleEditPost();
|
|
break;
|
|
case 'newSpace':
|
|
handleNewSpace();
|
|
break;
|
|
}
|
|
|
|
if (!isset($_POST['redirect']))
|
|
header('Location: .');
|
|
else
|
|
header('Location: ' . $_POST['redirect']);
|
|
}
|
|
|
|
function handleViewEssay()
|
|
{
|
|
if (!isset($_POST['id']))
|
|
return;
|
|
|
|
$essay = Post::retrieveFromDB($_POST['id']);
|
|
if (!is_a($essay, 'Essay'))
|
|
return;
|
|
|
|
return json_encode(['html' => $essay->getHTML()]);
|
|
}
|
|
|
|
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']) || !isset($_SESSION['user']))
|
|
return;
|
|
|
|
// TODO: Better input validation for SQL Inj. and XSS
|
|
switch ($_POST['postType']) {
|
|
case 'Thought':
|
|
if (!isset($_POST['thought']))
|
|
return;
|
|
$id = randomId(1) * -1;
|
|
new Thought($_POST['space'], $id, unserialize($_SESSION['user']), 3, 2, urldecode($_POST['thought']));
|
|
break;
|
|
case 'Moment':
|
|
if (!isset($_FILES['image']) || !isset($_POST['caption']))
|
|
return;
|
|
$id = randomId(1) * -1;
|
|
new Moment($_POST['space'], $id, unserialize($_SESSION['user']), 2, 2, $_FILES['image'], $_POST['caption']);
|
|
break;
|
|
case 'Essay':
|
|
if (!isset($_POST['title']) || !isset($_POST['markdown']) || !isset($_POST['excerpt']))
|
|
return;
|
|
$id = randomId(1) * -1;
|
|
new Essay($_POST['space'], $id, 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 handleEditPost()
|
|
{
|
|
if (!isset($_POST['id']) || !isset($_POST['postType']) || !isset($_POST['space']))
|
|
return;
|
|
|
|
switch ($_POST['postType']) {
|
|
case 1:
|
|
if (!isset($_POST['title']) || !isset($_POST['markdown']) || !isset($_POST['excerpt']) || !isset($_POST['space']))
|
|
return;
|
|
Essay::edit($_POST['id'], $_POST['title'], $_POST['markdown'], $_POST['excerpt'], $_POST['space']);
|
|
break;
|
|
case 2:
|
|
if (!isset($_POST['caption']))
|
|
return;
|
|
Moment::edit($_POST['id'], $_POST['caption'], $_POST['space']);
|
|
break;
|
|
case 3:
|
|
if (!isset($_POST['thought']))
|
|
return;
|
|
Thought::edit($_POST['id'], $_POST['thought'], $_POST['space']);
|
|
break;
|
|
}
|
|
|
|
if (isset($_POST['redirect']))
|
|
header('Location: ' . $_POST['redirect']);
|
|
else
|
|
header('Location: .');
|
|
}
|
|
|
|
function handleEditPostHTML()
|
|
{
|
|
if (!isset($_POST['id']))
|
|
return;
|
|
|
|
echo json_encode(['html' => Post::getEditHTML($_POST['id'])]);
|
|
}
|
|
|
|
function handleNewPostHTML()
|
|
{
|
|
echo json_encode(['html' => Post::getComposeHTML()]);
|
|
}
|
|
|
|
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;
|
|
}
|