![]() 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/3f21f98d/ |
<?php
session_start();
require_once 'config/database.php';
header('Content-Type: application/json');
// Check if user is logged in for certain actions
$user_id = $_SESSION['user_id'] ?? null;
// Get the request data
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? '';
$track_id = $input['track_id'] ?? null;
$user_id_param = $input['user_id'] ?? null;
$pdo = getDBConnection();
if (!$pdo) {
echo json_encode(['success' => false, 'message' => 'Database connection failed']);
exit;
}
try {
switch ($action) {
case 'like':
if (!$user_id) {
echo json_encode(['success' => false, 'message' => 'Please log in to like tracks']);
exit;
}
// Check if already liked
$stmt = $pdo->prepare("SELECT id FROM track_likes WHERE track_id = ? AND user_id = ?");
$stmt->execute([$track_id, $user_id]);
$existing_like = $stmt->fetch();
if ($existing_like) {
// Unlike
$stmt = $pdo->prepare("DELETE FROM track_likes WHERE track_id = ? AND user_id = ?");
$stmt->execute([$track_id, $user_id]);
echo json_encode(['success' => true, 'action' => 'unliked']);
} else {
// Like
$stmt = $pdo->prepare("INSERT INTO track_likes (track_id, user_id) VALUES (?, ?)");
$stmt->execute([$track_id, $user_id]);
echo json_encode(['success' => true, 'action' => 'liked']);
}
break;
case 'view':
// Track view (can be anonymous)
$ip_address = $_SERVER['REMOTE_ADDR'] ?? '';
$stmt = $pdo->prepare("INSERT INTO track_views (track_id, user_id, ip_address) VALUES (?, ?, ?)");
$stmt->execute([$track_id, $user_id, $ip_address]);
echo json_encode(['success' => true]);
break;
case 'play':
// Track play (can be anonymous)
$stmt = $pdo->prepare("INSERT INTO track_plays (track_id, user_id) VALUES (?, ?)");
$stmt->execute([$track_id, $user_id]);
echo json_encode(['success' => true]);
break;
case 'share':
// Track share (can be anonymous)
$share_type = $input['share_type'] ?? 'social';
$stmt = $pdo->prepare("INSERT INTO track_shares (track_id, user_id, share_type) VALUES (?, ?, ?)");
$stmt->execute([$track_id, $user_id, $share_type]);
echo json_encode(['success' => true]);
break;
case 'follow':
if (!$user_id) {
echo json_encode(['success' => false, 'message' => 'Please log in to follow users']);
exit;
}
if ($user_id == $user_id_param) {
echo json_encode(['success' => false, 'message' => 'You cannot follow yourself']);
exit;
}
// Check if already following
$stmt = $pdo->prepare("SELECT id FROM user_follows WHERE follower_id = ? AND following_id = ?");
$stmt->execute([$user_id, $user_id_param]);
$existing_follow = $stmt->fetch();
if ($existing_follow) {
// Unfollow
$stmt = $pdo->prepare("DELETE FROM user_follows WHERE follower_id = ? AND following_id = ?");
$stmt->execute([$user_id, $user_id_param]);
echo json_encode(['success' => true, 'action' => 'unfollowed']);
} else {
// Follow
$stmt = $pdo->prepare("INSERT INTO user_follows (follower_id, following_id) VALUES (?, ?)");
$stmt->execute([$user_id, $user_id_param]);
echo json_encode(['success' => true, 'action' => 'followed']);
}
break;
case 'comment':
if (!$user_id) {
echo json_encode(['success' => false, 'message' => 'Please log in to comment']);
exit;
}
$comment = $input['comment'] ?? '';
if (empty($comment)) {
echo json_encode(['success' => false, 'message' => 'Comment cannot be empty']);
exit;
}
$stmt = $pdo->prepare("INSERT INTO track_comments (track_id, user_id, comment) VALUES (?, ?, ?)");
$stmt->execute([$track_id, $user_id, $comment]);
echo json_encode(['success' => true, 'message' => 'Comment added successfully']);
break;
case 'get_comments':
$track_id = $_GET['track_id'] ?? null;
if (!$track_id) {
echo json_encode(['success' => false, 'message' => 'Track ID required']);
exit;
}
$stmt = $pdo->prepare("
SELECT tc.*, u.name as user_name, u.profile_image
FROM track_comments tc
JOIN users u ON tc.user_id = u.id
WHERE tc.track_id = ?
ORDER BY tc.created_at DESC
");
$stmt->execute([$track_id]);
$comments = $stmt->fetchAll();
echo json_encode(['success' => true, 'comments' => $comments]);
break;
default:
echo json_encode(['success' => false, 'message' => 'Invalid action']);
break;
}
} catch (Exception $e) {
error_log("Social API error: " . $e->getMessage());
echo json_encode(['success' => false, 'message' => 'An error occurred']);
}
?>