T.ME/BIBIL_0DAY
CasperSecurity


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/domains/soundstudiopro.com/private_html/utils/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/gositeme/domains/soundstudiopro.com/private_html/utils/artist_notifications.php
<?php
/**
 * Artist Notification Functions
 * Handles notifications and emails to artists when their tracks are purchased
 * and to event organizers when their tickets are sold
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/email.php';
require_once __DIR__ . '/../includes/translations.php';

/**
 * Create a notification for an artist when their track is purchased
 * 
 * @param int $artist_id The artist's user ID
 * @param int $track_id The purchased track ID
 * @param int $buyer_id The buyer's user ID
 * @param float $price_paid The price paid for the track
 * @param int $purchase_id The purchase record ID
 * @return bool Success status
 */
function notifyArtistOfTrackPurchase($artist_id, $track_id, $buyer_id, $price_paid, $purchase_id) {
    try {
        $pdo = getDBConnection();
        if (!$pdo) {
            error_log("notifyArtistOfTrackPurchase: Database connection failed");
            return false;
        }
        
        // Ensure artist_purchase_notifications table exists
        $pdo->exec("
            CREATE TABLE IF NOT EXISTS artist_purchase_notifications (
                id INT AUTO_INCREMENT PRIMARY KEY,
                artist_id INT NOT NULL,
                track_id INT NOT NULL,
                buyer_id INT NOT NULL,
                purchase_id INT NOT NULL,
                price_paid DECIMAL(10,2) NOT NULL,
                is_read TINYINT(1) DEFAULT 0,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (artist_id) REFERENCES users(id) ON DELETE CASCADE,
                FOREIGN KEY (track_id) REFERENCES music_tracks(id) ON DELETE CASCADE,
                FOREIGN KEY (buyer_id) REFERENCES users(id) ON DELETE CASCADE,
                INDEX idx_artist_read (artist_id, is_read),
                INDEX idx_created_at (created_at)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        ");
        
        // Get track information
        $stmt = $pdo->prepare("SELECT title, price FROM music_tracks WHERE id = ?");
        $stmt->execute([$track_id]);
        $track = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$track) {
            error_log("notifyArtistOfTrackPurchase: Track not found. Track ID: $track_id");
            return false;
        }
        
        // Get buyer information
        $stmt = $pdo->prepare("SELECT name, email FROM users WHERE id = ?");
        $stmt->execute([$buyer_id]);
        $buyer = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$buyer) {
            error_log("notifyArtistOfTrackPurchase: Buyer not found. Buyer ID: $buyer_id");
            return false;
        }
        
        // Combine info for email
        $info = [
            'title' => $track['title'],
            'price' => $track['price'],
            'buyer_name' => $buyer['name'],
            'buyer_email' => $buyer['email']
        ];
        
        // Insert notification
        $stmt = $pdo->prepare("
            INSERT INTO artist_purchase_notifications 
            (artist_id, track_id, buyer_id, purchase_id, price_paid, created_at)
            VALUES (?, ?, ?, ?, ?, NOW())
        ");
        $stmt->execute([$artist_id, $track_id, $buyer_id, $purchase_id, $price_paid]);
        
        $notification_id = $pdo->lastInsertId();
        error_log("notifyArtistOfTrackPurchase: Notification created successfully. Notification ID: $notification_id, Artist ID: $artist_id, Track ID: $track_id, Buyer ID: $buyer_id, Price: $price_paid");
        
        // Send email notification to artist
        sendArtistPurchaseEmail($artist_id, $track_id, $buyer_id, $price_paid, $purchase_id, $info);
        
        return true;
    } catch (Exception $e) {
        error_log("notifyArtistOfTrackPurchase error: " . $e->getMessage());
        return false;
    }
}

/**
 * Send email notification to artist about track purchase
 * 
 * @param int $artist_id The artist's user ID
 * @param int $track_id The purchased track ID
 * @param int $buyer_id The buyer's user ID
 * @param float $price_paid The price paid
 * @param int $purchase_id The purchase record ID
 * @param array $info Track and buyer information
 * @return bool Success status
 */
function sendArtistPurchaseEmail($artist_id, $track_id, $buyer_id, $price_paid, $purchase_id, $info) {
    try {
        $pdo = getDBConnection();
        if (!$pdo) {
            return false;
        }
        
        // Get artist information
        $stmt = $pdo->prepare("SELECT name, email FROM users WHERE id = ?");
        $stmt->execute([$artist_id]);
        $artist = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$artist) {
            error_log("sendArtistPurchaseEmail: Artist not found");
            return false;
        }
        
        // Get user language preference (default to English)
        // Try to get from artist's preferences or default to English
        $lang = 'en';
        try {
            if (function_exists('getCurrentLanguage')) {
                $lang = getCurrentLanguage();
            }
            // Also check if artist has a language preference in database
            $lang_stmt = $pdo->prepare("SELECT language_preference FROM users WHERE id = ?");
            $lang_stmt->execute([$artist_id]);
            $user_lang = $lang_stmt->fetchColumn();
            if ($user_lang && in_array($user_lang, ['en', 'fr'])) {
                $lang = $user_lang;
            }
        } catch (Exception $e) {
            // Default to English if language detection fails
            $lang = 'en';
        }
        
        // Generate email content
        $track_title = htmlspecialchars($info['title'] ?? 'Untitled Track');
        $buyer_name = htmlspecialchars($info['buyer_name'] ?? t('notifications.anonymous_buyer'));
        $formatted_price = number_format($price_paid, 2);
        $earnings_url = 'https://soundstudiopro.com/artist_dashboard.php?tab=earnings';
        
        // Email subject
        $subject = $lang === 'fr' 
            ? "🎵 Votre morceau \"{$track_title}\" a été acheté !"
            : "🎵 Your track \"{$track_title}\" was purchased!";
        
        // Email body (HTML)
        if ($lang === 'fr') {
            $html_body = "
            <!DOCTYPE html>
            <html>
            <head>
                <meta charset='UTF-8'>
                <style>
                    body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
                    .container { max-width: 600px; margin: 0 auto; padding: 20px; }
                    .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 8px 8px 0 0; }
                    .content { background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; }
                    .purchase-info { background: white; padding: 20px; border-radius: 8px; margin: 20px 0; border-left: 4px solid #667eea; }
                    .button { display: inline-block; padding: 12px 30px; background: #667eea; color: white; text-decoration: none; border-radius: 6px; margin-top: 20px; }
                    .footer { text-align: center; margin-top: 30px; color: #666; font-size: 12px; }
                </style>
            </head>
            <body>
                <div class='container'>
                    <div class='header'>
                        <h1>🎵 Votre morceau a été acheté !</h1>
                    </div>
                    <div class='content'>
                        <p>Bonjour {$artist['name']},</p>
                        <p>Excellente nouvelle ! Votre morceau <strong>\"{$track_title}\"</strong> vient d'être acheté.</p>
                        
                        <div class='purchase-info'>
                            <h3>Détails de l'achat :</h3>
                            <p><strong>Morceau :</strong> {$track_title}</p>
                            <p><strong>Acheteur :</strong> {$buyer_name}</p>
                            <p><strong>Prix :</strong> \${$formatted_price}</p>
                            <p><strong>Date :</strong> " . date('d/m/Y à H:i') . "</p>
                        </div>
                        
                        <p>Vous pouvez consulter tous vos revenus et ventes dans votre tableau de bord artiste.</p>
                        
                        <a href='{$earnings_url}' class='button'>Voir mes revenus</a>
                        
                        <p style='margin-top: 30px;'>Continuez à créer de la musique incroyable !</p>
                        
                        <div class='footer'>
                            <p>SoundStudioPro - Votre plateforme musicale</p>
                        </div>
                    </div>
                </div>
            </body>
            </html>
            ";
            
            $text_body = "
Bonjour {$artist['name']},

Excellente nouvelle ! Votre morceau \"{$track_title}\" vient d'être acheté.

Détails de l'achat :
- Morceau : {$track_title}
- Acheteur : {$buyer_name}
- Prix : \${$formatted_price}
- Date : " . date('d/m/Y à H:i') . "

Vous pouvez consulter tous vos revenus et ventes dans votre tableau de bord artiste :
{$earnings_url}

Continuez à créer de la musique incroyable !

SoundStudioPro
            ";
        } else {
            $html_body = "
            <!DOCTYPE html>
            <html>
            <head>
                <meta charset='UTF-8'>
                <style>
                    body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
                    .container { max-width: 600px; margin: 0 auto; padding: 20px; }
                    .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 8px 8px 0 0; }
                    .content { background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; }
                    .purchase-info { background: white; padding: 20px; border-radius: 8px; margin: 20px 0; border-left: 4px solid #667eea; }
                    .button { display: inline-block; padding: 12px 30px; background: #667eea; color: white; text-decoration: none; border-radius: 6px; margin-top: 20px; }
                    .footer { text-align: center; margin-top: 30px; color: #666; font-size: 12px; }
                </style>
            </head>
            <body>
                <div class='container'>
                    <div class='header'>
                        <h1>🎵 Your Track Was Purchased!</h1>
                    </div>
                    <div class='content'>
                        <p>Hello {$artist['name']},</p>
                        <p>Great news! Your track <strong>\"{$track_title}\"</strong> was just purchased.</p>
                        
                        <div class='purchase-info'>
                            <h3>Purchase Details:</h3>
                            <p><strong>Track:</strong> {$track_title}</p>
                            <p><strong>Buyer:</strong> {$buyer_name}</p>
                            <p><strong>Price:</strong> \${$formatted_price}</p>
                            <p><strong>Date:</strong> " . date('F j, Y \a\t g:i A') . "</p>
                        </div>
                        
                        <p>You can view all your earnings and sales in your artist dashboard.</p>
                        
                        <a href='{$earnings_url}' class='button'>View My Earnings</a>
                        
                        <p style='margin-top: 30px;'>Keep creating amazing music!</p>
                        
                        <div class='footer'>
                            <p>SoundStudioPro - Your Music Platform</p>
                        </div>
                    </div>
                </div>
            </body>
            </html>
            ";
            
            $text_body = "
Hello {$artist['name']},

Great news! Your track \"{$track_title}\" was just purchased.

Purchase Details:
- Track: {$track_title}
- Buyer: {$buyer_name}
- Price: \${$formatted_price}
- Date: " . date('F j, Y \a\t g:i A') . "

You can view all your earnings and sales in your artist dashboard:
{$earnings_url}

Keep creating amazing music!

SoundStudioPro
            ";
        }
        
        // Send email
        $email_sent = sendEmail(
            $artist['email'],
            $artist['name'],
            $subject,
            $html_body,
            $text_body,
            'artist_purchase_notification',
            $artist_id,
            $purchase_id
        );
        
        if ($email_sent) {
            error_log("Artist purchase email sent successfully to: {$artist['email']}");
        } else {
            error_log("Failed to send artist purchase email to: {$artist['email']}");
        }
        
        return $email_sent;
    } catch (Exception $e) {
        error_log("sendArtistPurchaseEmail error: " . $e->getMessage());
        return false;
    }
}

/**
 * Create a notification for an event organizer when tickets are sold
 * 
 * @param int $organizer_id The event organizer's user ID
 * @param int $event_id The event ID
 * @param int $buyer_id The buyer's user ID
 * @param float $price_paid The price paid per ticket
 * @param int $quantity The quantity of tickets sold
 * @param int $sale_id The event_ticket_sales record ID
 * @return bool Success status
 */
function notifyOrganizerOfTicketSale($organizer_id, $event_id, $buyer_id, $price_paid, $quantity, $sale_id) {
    try {
        $pdo = getDBConnection();
        if (!$pdo) {
            error_log("notifyOrganizerOfTicketSale: Database connection failed");
            return false;
        }
        
        // Ensure event_ticket_notifications table exists
        $pdo->exec("
            CREATE TABLE IF NOT EXISTS event_ticket_notifications (
                id INT AUTO_INCREMENT PRIMARY KEY,
                organizer_id INT NOT NULL,
                event_id INT NOT NULL,
                buyer_id INT NOT NULL,
                sale_id INT NOT NULL,
                price_paid DECIMAL(10,2) NOT NULL,
                quantity INT NOT NULL DEFAULT 1,
                is_read TINYINT(1) DEFAULT 0,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                INDEX idx_organizer_read (organizer_id, is_read),
                INDEX idx_created_at (created_at),
                INDEX idx_organizer_id (organizer_id),
                INDEX idx_event_id (event_id),
                INDEX idx_buyer_id (buyer_id)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        ");
        
        // Get event information
        $stmt = $pdo->prepare("SELECT title FROM events WHERE id = ?");
        $stmt->execute([$event_id]);
        $event = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$event) {
            error_log("notifyOrganizerOfTicketSale: Event not found. Event ID: $event_id");
            return false;
        }
        
        // Get buyer information
        $stmt = $pdo->prepare("SELECT name, email FROM users WHERE id = ?");
        $stmt->execute([$buyer_id]);
        $buyer = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$buyer) {
            error_log("notifyOrganizerOfTicketSale: Buyer not found. Buyer ID: $buyer_id");
            return false;
        }
        
        // Insert notification
        $stmt = $pdo->prepare("
            INSERT INTO event_ticket_notifications 
            (organizer_id, event_id, buyer_id, sale_id, price_paid, quantity, created_at)
            VALUES (?, ?, ?, ?, ?, ?, NOW())
        ");
        $stmt->execute([$organizer_id, $event_id, $buyer_id, $sale_id, $price_paid, $quantity]);
        
        $notification_id = $pdo->lastInsertId();
        error_log("notifyOrganizerOfTicketSale: Notification created successfully. Notification ID: $notification_id, Organizer ID: $organizer_id, Event ID: $event_id, Buyer ID: $buyer_id, Quantity: $quantity, Price: $price_paid");
        
        return true;
    } catch (Exception $e) {
        error_log("notifyOrganizerOfTicketSale error: " . $e->getMessage());
        return false;
    }
}

/**
 * Get unread ticket sale notifications count for an event organizer
 * 
 * @param int $organizer_id The organizer's user ID
 * @return int Count of unread notifications
 */
function getOrganizerTicketNotificationsCount($organizer_id) {
    try {
        $pdo = getDBConnection();
        if (!$pdo) {
            return 0;
        }
        
        // Check if table exists
        $tableCheck = $pdo->query("SHOW TABLES LIKE 'event_ticket_notifications'");
        if ($tableCheck->rowCount() === 0) {
            return 0;
        }
        
        $stmt = $pdo->prepare("
            SELECT COUNT(*) 
            FROM event_ticket_notifications 
            WHERE organizer_id = ? AND is_read = 0
        ");
        $stmt->execute([$organizer_id]);
        return (int)$stmt->fetchColumn();
    } catch (Exception $e) {
        error_log("getOrganizerTicketNotificationsCount error: " . $e->getMessage());
        return 0;
    }
}

/**
 * Get unread purchase notifications count for an artist
 * 
 * @param int $artist_id The artist's user ID
 * @return int Count of unread notifications
 */
function getArtistPurchaseNotificationsCount($artist_id) {
    try {
        $pdo = getDBConnection();
        if (!$pdo) {
            return 0;
        }
        
        // Check if table exists
        $tableCheck = $pdo->query("SHOW TABLES LIKE 'artist_purchase_notifications'");
        if ($tableCheck->rowCount() === 0) {
            return 0;
        }
        
        $stmt = $pdo->prepare("
            SELECT COUNT(*) 
            FROM artist_purchase_notifications 
            WHERE artist_id = ? AND is_read = 0
        ");
        $stmt->execute([$artist_id]);
        return (int)$stmt->fetchColumn();
    } catch (Exception $e) {
        error_log("getArtistPurchaseNotificationsCount error: " . $e->getMessage());
        return 0;
    }
}


CasperSecurity Mini