pretty_dump($_POST),
];
echo json_encode($response);
}
function handleNewThought()
{
$thought = new Thought(Space::retrieveFromDB($_POST['space']), null, unserialize($_SESSION['user']), 3, 2, urldecode($_POST['thought']));
echo json_encode(['log' => 'success']);
}
function handleNewPostHTML()
{
// 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
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);
// 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();
} 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';
$response['message'] = 'Invalid Request: Username and/or password not provided.';
}
echo json_encode($response);
exit;
}