-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselect_library.php
More file actions
42 lines (34 loc) · 1.14 KB
/
select_library.php
File metadata and controls
42 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
if (empty($_SESSION['user_data']['userid']) || empty($_SESSION['authorized'])) {
http_response_code(401);
echo json_encode(['error' => 'Authentication required']);
exit;
}
$payload = json_decode(file_get_contents('php://input') ?: '[]', true);
$chatId = isset($payload['chat_id']) ? trim((string)$payload['chat_id']) : '';
$libraryId = isset($payload['library_id']) ? (int)$payload['library_id'] : 0;
if ($chatId === '' || $libraryId <= 0) {
http_response_code(400);
echo json_encode(['error' => 'chat_id and library_id are required']);
exit;
}
$ok = select_library_for_chat($_SESSION['user_data']['userid'], $chatId, $libraryId);
if (!$ok) {
http_response_code(404);
echo json_encode(['error' => 'Unable to select library for this chat']);
exit;
}
echo json_encode([
'ok' => true,
'chat_id' => $chatId,
'library_id' => $libraryId,
'enabled' => true,
]);