- create_ticket_api.php: remove the wrong CREATE TABLE stub that broke a fresh DB; generate collision-safe ticket_ids so a genuine id collision isn't misreported as a duplicate and a hw alert dropped; stop leaking raw DB errors; correct a reopen comment that falsely claimed refreshed sensor data - manage_recurring.php: fix next-run so create/edit no longer skips the current period (monthly day-of-month this month, daily today if time not passed, correct ISO weekday, month-length clamp); only recompute on schedule changes to avoid double-fire - export_tickets.php, audit_log.php: neutralize CSV formula injection - revoke_api_key.php, generate_api_key.php: correct HTTP status codes and stop the catch clobbering specific 4xx codes - health.php: stop leaking PHP version / extension names / paths to unauthenticated callers - watch_ticket.php: define $data before use - manage_templates/recurring/custom_fields: add audit logging for CRUD; add recurring_ticket + custom_field to the audit entity whitelist Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
132 lines
4.2 KiB
PHP
132 lines
4.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Watch / Unwatch Ticket API
|
|
*
|
|
* GET ?ticket_id=N → returns { watching: bool, watcher_count: int }
|
|
* POST { ticket_id, action: 'watch'|'unwatch' } → toggles watcher row
|
|
*/
|
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true) ?? [];
|
|
|
|
$ticketId = isset($_GET['ticket_id'])
|
|
? (int)$_GET['ticket_id']
|
|
: (int)($data['ticket_id'] ?? 0);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$ticketId = (int)($data['ticket_id'] ?? 0);
|
|
$action = $data['action'] ?? '';
|
|
|
|
if ($ticketId <= 0 || !in_array($action, ['watch', 'unwatch'], true)) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Invalid parameters']);
|
|
exit;
|
|
}
|
|
|
|
$ticketModel = new TicketModel($conn);
|
|
$ticket = $ticketModel->getTicketById($ticketId);
|
|
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'error' => 'Ticket not found']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'watch') {
|
|
$stmt = $conn->prepare(
|
|
"INSERT IGNORE INTO ticket_watchers (ticket_id, user_id) VALUES (?, ?)"
|
|
);
|
|
$stmt->bind_param("ii", $ticketId, $userId);
|
|
$stmt->execute();
|
|
$stmt->close();
|
|
} else {
|
|
$stmt = $conn->prepare(
|
|
"DELETE FROM ticket_watchers WHERE ticket_id = ? AND user_id = ?"
|
|
);
|
|
$stmt->bind_param("ii", $ticketId, $userId);
|
|
$stmt->execute();
|
|
$stmt->close();
|
|
}
|
|
|
|
// Return updated state
|
|
$countStmt = $conn->prepare(
|
|
"SELECT COUNT(*) as cnt FROM ticket_watchers WHERE ticket_id = ?"
|
|
);
|
|
$countStmt->bind_param("i", $ticketId);
|
|
$countStmt->execute();
|
|
$count = (int)$countStmt->get_result()->fetch_assoc()['cnt'];
|
|
$countStmt->close();
|
|
|
|
apiRespond([
|
|
'success' => true,
|
|
'watching' => $action === 'watch',
|
|
'watcher_count' => $count,
|
|
]);
|
|
}
|
|
|
|
// GET — return current watch state for this user
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
|
exit;
|
|
}
|
|
|
|
if ($ticketId <= 0) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'ticket_id required']);
|
|
exit;
|
|
}
|
|
|
|
// Enforce ticket visibility before returning watch state / watcher names, so a
|
|
// restricted ticket's watcher list and count aren't disclosed (the POST path
|
|
// already checks this).
|
|
$ticketModel = new TicketModel($conn);
|
|
$ticket = $ticketModel->getTicketById($ticketId);
|
|
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'error' => 'Ticket not found']);
|
|
exit;
|
|
}
|
|
|
|
$watchingStmt = $conn->prepare(
|
|
"SELECT COUNT(*) as cnt FROM ticket_watchers WHERE ticket_id = ? AND user_id = ?"
|
|
);
|
|
$watchingStmt->bind_param("ii", $ticketId, $userId);
|
|
$watchingStmt->execute();
|
|
$watching = (bool)$watchingStmt->get_result()->fetch_assoc()['cnt'];
|
|
$watchingStmt->close();
|
|
|
|
// Fetch watcher list (up to 6) with display names for avatar group
|
|
$watchersStmt = $conn->prepare(
|
|
"SELECT u.user_id, COALESCE(u.display_name, u.username) AS display_name
|
|
FROM ticket_watchers tw
|
|
JOIN users u ON tw.user_id = u.user_id
|
|
WHERE tw.ticket_id = ?
|
|
ORDER BY tw.created_at ASC
|
|
LIMIT 6"
|
|
);
|
|
$watchersStmt->bind_param("i", $ticketId);
|
|
$watchersStmt->execute();
|
|
$watchersResult = $watchersStmt->get_result();
|
|
$watchers = [];
|
|
while ($row = $watchersResult->fetch_assoc()) {
|
|
$watchers[] = ['user_id' => (int)$row['user_id'], 'display_name' => $row['display_name']];
|
|
}
|
|
$watchersStmt->close();
|
|
|
|
// True watcher count (the list above is capped at 6 for the avatar group)
|
|
$countStmt = $conn->prepare("SELECT COUNT(*) AS cnt FROM ticket_watchers WHERE ticket_id = ?");
|
|
$countStmt->bind_param("i", $ticketId);
|
|
$countStmt->execute();
|
|
$count = (int)$countStmt->get_result()->fetch_assoc()['cnt'];
|
|
$countStmt->close();
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'watching' => $watching,
|
|
'watcher_count' => $count,
|
|
'watchers' => $watchers,
|
|
]);
|