diff --git a/api/list.php b/api/list.php index 43c154b..86159e8 100644 --- a/api/list.php +++ b/api/list.php @@ -1,30 +1,66 @@ 'not_logged_in' + ]); + exit; + } -$lang = $_GET['lang'] ?? ''; -$list = $_GET['list'] ?? ''; + $user = $_SESSION['user']; -$file = listPath($user, $lang, $list); + $lang = $_GET['lang'] ?? ''; + $list = $_GET['list'] ?? ''; -$data = JsonDB::read($file); + if ($lang === '' || $list === '') { + echo json_encode([ + 'error' => 'missing_params' + ]); + exit; + } + + $file = listPath($user, $lang, $list); + + if (!file_exists($file)) { + echo json_encode([ + 'error' => 'file_not_found', + 'file' => $file + ]); + exit; + } + + $data = JsonDB::read($file); + + if (!is_array($data) || !isset($data['words'])) { + echo json_encode([ + 'error' => 'invalid_data_structure' + ]); + exit; + } -if (!isset($data['words'])) { echo json_encode([ - 'error' => 'list_not_found' + 'ok' => true, + 'metadata' => $data['metadata'] ?? null, + 'words' => $data['words'] ]); - exit; -} -echo json_encode([ - 'metadata' => $data['metadata'] ?? null, - 'words' => $data['words'] -]); \ No newline at end of file +} catch (Throwable $e) { + + http_response_code(500); + + echo json_encode([ + 'error' => 'server_exception', + 'message' => $e->getMessage() + ]); +} \ No newline at end of file