![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/-7159fd/ |
<?php
/**
* Log a radio play
* POST /api/radio/v1/plays
*/
$data = json_decode(file_get_contents('php://input'), true);
// Validate required fields
if (!isset($data['track_id']) || !isset($data['played_at'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing required fields: track_id, played_at']);
exit;
}
$track_id = (int)$data['track_id'];
$played_at_raw = $data['played_at'];
// Normalize and validate played_at
try {
$played_dt = new DateTime($played_at_raw);
// Normalize to seconds for consistent idempotency checks
$played_at = $played_dt->format('Y-m-d H:i:s');
} catch (Exception $e) {
http_response_code(400);
echo json_encode(['error' => 'Invalid played_at format, expected a valid datetime']);
exit;
}
// Validate track exists
$pdo = getDBConnection();
$stmt = $pdo->prepare("SELECT id, radio_enabled FROM music_tracks WHERE id = ?");
$stmt->execute([$track_id]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$track) {
http_response_code(404);
echo json_encode(['error' => 'Track not found']);
exit;
}
if (!$track['radio_enabled']) {
http_response_code(403);
echo json_encode(['error' => 'Track is not available for radio play']);
exit;
}
// Basic rate limiting per station: prevent abuse bursts
// Example: max 300 logged plays per station per minute
$rateStmt = $pdo->prepare("
SELECT COUNT(*) AS c
FROM radio_play_logs
WHERE station_id = ?
AND played_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE)
");
$rateStmt->execute([$station['id']]);
$rateRow = $rateStmt->fetch(PDO::FETCH_ASSOC);
if ($rateRow && (int)$rateRow['c'] > 300) {
http_response_code(429);
echo json_encode([
'error' => 'Rate limit exceeded for this station. Please slow down logging plays.',
'limit_per_minute' => 300
]);
exit;
}
// Idempotency: avoid double‑logging the exact same play
$dupStmt = $pdo->prepare("
SELECT id
FROM radio_play_logs
WHERE station_id = ?
AND track_id = ?
AND played_at = ?
LIMIT 1
");
$dupStmt->execute([$station['id'], $track_id, $played_at]);
$existingPlay = $dupStmt->fetch(PDO::FETCH_ASSOC);
if ($existingPlay) {
// Return existing play as a successful, idempotent response
$stationFresh = getRadioStation($station['id']);
echo json_encode([
'success' => true,
'play_id' => (int)$existingPlay['id'],
'logged' => false,
'duplicate' => true,
'message' => 'Play already logged for this station/track/timestamp.',
'monthly_plays_remaining' => $stationFresh
? ($stationFresh['monthly_play_limit'] - $stationFresh['current_month_plays'])
: null
]);
exit;
}
// Log the play
$play_id = logRadioPlay($station['id'], $track_id, [
'played_at' => $played_at,
'duration_played' => $data['duration_played'] ?? null,
'play_type' => $data['play_type'] ?? 'full',
'playlist_id' => $data['playlist_id'] ?? null,
'listener_count' => $data['listener_count'] ?? null,
'source' => 'api'
]);
if ($play_id) {
// Get updated station info
$station = getRadioStation($station['id']);
echo json_encode([
'success' => true,
'play_id' => $play_id,
'logged' => true,
'duplicate' => false,
'monthly_plays_remaining' => $station['monthly_play_limit'] - $station['current_month_plays']
]);
} else {
http_response_code(500);
echo json_encode(['error' => 'Failed to log play']);
}