![]() 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/-488ba8b9/ |
<?php
/**
* Radio Station System - Helper Functions
*/
require_once __DIR__ . '/../../config/database.php';
/**
* Get radio station by ID
*/
function getRadioStation($station_id) {
$pdo = getDBConnection();
if (!$pdo) return false;
try {
$stmt = $pdo->prepare("SELECT * FROM radio_stations WHERE id = ?");
$stmt->execute([$station_id]);
return $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Get radio station failed: " . $e->getMessage());
return false;
}
}
/**
* Get radio station by API key
*/
function getRadioStationByAPIKey($api_key) {
$pdo = getDBConnection();
if (!$pdo) return false;
try {
$stmt = $pdo->prepare("SELECT * FROM radio_stations WHERE api_key = ? AND api_enabled = 1");
$stmt->execute([$api_key]);
return $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Get radio station by API key failed: " . $e->getMessage());
return false;
}
}
/**
* Authenticate radio station API request
*/
function authenticateRadioAPI() {
$headers = getallheaders();
$auth = $headers['Authorization'] ?? '';
if (!preg_match('/Bearer (.+):(.+)/', $auth, $matches)) {
return false;
}
$api_key = $matches[1];
$api_secret = $matches[2];
$station = getRadioStationByAPIKey($api_key);
if (!$station || !password_verify($api_secret, $station['api_secret'])) {
return false;
}
// Check subscription status
if ($station['subscription_status'] !== 'active') {
return false;
}
// Check play limit
if ($station['current_month_plays'] >= $station['monthly_play_limit']) {
return false;
}
return $station;
}
/**
* Log a radio play
*/
function logRadioPlay($station_id, $track_id, $data = []) {
$pdo = getDBConnection();
if (!$pdo) return false;
try {
$played_at = $data['played_at'] ?? date('Y-m-d H:i:s');
$played_datetime = new DateTime($played_at);
$stmt = $pdo->prepare("
INSERT INTO radio_play_logs (
station_id, track_id, playlist_id,
played_at, duration_played, play_type,
time_of_day, day_of_week, listener_count,
played_by, source, ip_address, user_agent
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$result = $stmt->execute([
$station_id,
$track_id,
$data['playlist_id'] ?? null,
$played_at,
$data['duration_played'] ?? null,
$data['play_type'] ?? 'full',
$played_datetime->format('H:i:s'),
$played_datetime->format('w'),
$data['listener_count'] ?? null,
$data['played_by'] ?? null,
$data['source'] ?? 'api',
$_SERVER['REMOTE_ADDR'] ?? null,
$_SERVER['HTTP_USER_AGENT'] ?? null
]);
if ($result) {
$play_id = $pdo->lastInsertId();
// Update station play count
$pdo->prepare("
UPDATE radio_stations
SET current_month_plays = current_month_plays + 1
WHERE id = ?
")->execute([$station_id]);
// Update track play count
$pdo->prepare("
UPDATE music_tracks
SET radio_play_count = radio_play_count + 1,
radio_last_played = NOW()
WHERE id = ?
")->execute([$track_id]);
// Auto-create license if doesn't exist
ensureRadioLicense($station_id, $track_id);
// Calculate royalty
calculateRadioRoyalty($pdo, $play_id, $station_id, $track_id);
return $play_id;
}
return false;
} catch (PDOException $e) {
error_log("Log radio play failed: " . $e->getMessage());
return false;
}
}
/**
* Ensure radio license exists
*/
function ensureRadioLicense($station_id, $track_id) {
$pdo = getDBConnection();
if (!$pdo) return false;
try {
// Check if license exists
$stmt = $pdo->prepare("
SELECT id FROM radio_licenses
WHERE station_id = ? AND track_id = ? AND status = 'active'
AND (end_date IS NULL OR end_date >= CURDATE())
");
$stmt->execute([$station_id, $track_id]);
if (!$stmt->fetch()) {
// Create subscription license
$stmt = $pdo->prepare("
INSERT INTO radio_licenses (station_id, track_id, license_type, start_date, status)
VALUES (?, ?, 'subscription', CURDATE(), 'active')
");
$stmt->execute([$station_id, $track_id]);
}
return true;
} catch (PDOException $e) {
error_log("Ensure radio license failed: " . $e->getMessage());
return false;
}
}
/**
* Calculate radio royalty
*/
function calculateRadioRoyalty($pdo, $play_log_id, $station_id, $track_id) {
try {
// Get track and artist info
$stmt = $pdo->prepare("
SELECT t.radio_royalty_rate, t.user_id as artist_id
FROM music_tracks t
WHERE t.id = ?
");
$stmt->execute([$track_id]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$track || !$track['artist_id']) return false;
// Get station subscription info
$stmt = $pdo->prepare("
SELECT license_tier, monthly_play_limit
FROM radio_stations
WHERE id = ?
");
$stmt->execute([$station_id]);
$station = $stmt->fetch(PDO::FETCH_ASSOC);
// Calculate royalty (per play rate)
$base_rate = $track['radio_royalty_rate'] ?? 0.01;
$royalty_amount = $base_rate;
$platform_fee = $royalty_amount * 0.30;
$artist_payout = $royalty_amount * 0.70;
// Insert royalty record
$stmt = $pdo->prepare("
INSERT INTO radio_royalties (
play_log_id, station_id, track_id, artist_id,
base_rate, total_amount, platform_fee, artist_payout, payment_status
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pending')
");
$stmt->execute([
$play_log_id,
$station_id,
$track_id,
$track['artist_id'],
$base_rate,
$royalty_amount,
$platform_fee,
$artist_payout
]);
// Update play log
$pdo->prepare("
UPDATE radio_play_logs
SET royalty_calculated = TRUE, royalty_amount = ?
WHERE id = ?
")->execute([$royalty_amount, $play_log_id]);
// Update artist totals
$pdo->prepare("
UPDATE users
SET radio_total_plays = radio_total_plays + 1,
radio_total_royalties = radio_total_royalties + ?
WHERE id = ?
")->execute([$artist_payout, $track['artist_id']]);
return true;
} catch (PDOException $e) {
error_log("Calculate radio royalty failed: " . $e->getMessage());
return false;
}
}
/**
* Get station statistics
*/
function getStationStats($station_id, $days = 30) {
$pdo = getDBConnection();
if (!$pdo) return false;
try {
$stmt = $pdo->prepare("
SELECT
COUNT(*) as total_plays,
COUNT(DISTINCT track_id) as unique_tracks,
COUNT(DISTINCT DATE(played_at)) as active_days,
SUM(duration_played) as total_duration
FROM radio_play_logs
WHERE station_id = ?
AND played_at >= DATE_SUB(NOW(), INTERVAL ? DAY)
");
$stmt->execute([$station_id, $days]);
return $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Get station stats failed: " . $e->getMessage());
return false;
}
}
/**
* Get top tracks for station
*/
function getStationTopTracks($station_id, $limit = 10, $days = 30) {
$pdo = getDBConnection();
if (!$pdo) return [];
try {
$stmt = $pdo->prepare("
SELECT
t.id,
t.title,
t.artist_name,
COUNT(*) as play_count,
MAX(pl.played_at) as last_played
FROM radio_play_logs pl
JOIN music_tracks t ON pl.track_id = t.id
WHERE pl.station_id = ?
AND pl.played_at >= DATE_SUB(NOW(), INTERVAL ? DAY)
GROUP BY t.id
ORDER BY play_count DESC
LIMIT ?
");
$stmt->execute([$station_id, $days, $limit]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Get station top tracks failed: " . $e->getMessage());
return [];
}
}
/**
* Generate API credentials
*/
function generateAPICredentials() {
return [
'api_key' => bin2hex(random_bytes(32)),
'api_secret' => bin2hex(random_bytes(32))
];
}
/**
* Get tier pricing
*/
function getTierPrice($tier) {
$prices = [
'local' => 99,
'regional' => 299,
'national' => 999,
'enterprise' => 0 // Custom pricing
];
return $prices[$tier] ?? 99;
}
/**
* Get tier play limit
*/
function getTierPlayLimit($tier) {
$limits = [
'local' => 500,
'regional' => 2000,
'national' => 999999,
'enterprise' => 999999
];
return $limits[$tier] ?? 500;
}