'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 = '

New Space

'; $spaces = $user->getAllSpaces(); foreach ($spaces as $s) { $html .= ' '; } $html .= '
'; 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 = '

New Thought

'; $spaces = $user->getAllSpaces(); foreach ($spaces as $s) { $html .= ' '; } $html .= '
Thought Moment Essay
'; 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; }