<?php
/**
 * @package Seedbonus
 * @version TorrentTrader v3 MicroMonkey
 * @modified  DjLive
 * */

require_once("backend/functions.php");
dbconn();
loggedinonly();
function format_seedtime($seconds) {
    if ($seconds <= 0) return "---";
    $m = floor($seconds / 2592000); $seconds %= 2592000;
    $w = floor($seconds / 604800);  $seconds %= 604800;
    $d = floor($seconds / 86400);   $seconds %= 86400;
    $h = floor($seconds / 3600);    $seconds %= 3600;
    $i = floor($seconds / 60);

    $res = array();
    if ($m > 0) $res[] = $m . "M";
    if ($w > 0) $res[] = $w . "W";
    if ($d > 0) $res[] = $d . "D";
    if ($h > 0 && $m == 0) $res[] = $h . "h"; 
    if ($i > 0 && $w == 0 && $m == 0) $res[] = $i . "m";

    return implode(" ", array_slice($res, 0, 3)); 
}

// --- PURCHASE  ---
if (isset($_POST['id']) && is_valid_id($_POST['id'])) {
    $res = SQL_Query_exec("SELECT * FROM bonus WHERE id = " . sqlesc($_POST['id']));
    $row = mysqli_fetch_object($res);
    
    if (!$row || $CURUSER['seedbonus'] < $row->cost) {
        autolink("mybonus.php", "Insufficient Bonus Points!");
    } else {
        $uid = (int)$CURUSER['id'];
        $now = gmtime();

        // --- PRE-PURCHASE VALIDATIONS ---
        
        // 1. Double Bonus (Dildo) Validation - Cooldown & Active Status
        if ($row->type == 'dildo') {
            $check_d = mysqli_fetch_assoc(SQL_Query_exec("SELECT double_bonus, double_bonus_until FROM users WHERE id = $uid"));
            
            // If already active
            if ($check_d['double_bonus'] == 'yes' && strtotime($check_d['double_bonus_until']) > $now) {
                autolink("mybonus.php", "You already have Double Bonus active!");
            }
            // 10-day Cooldown from last activation 
            if ($check_d['double_bonus_until'] && strtotime($check_d['double_bonus_until']) > ($now - (10 * 86400))) {
                autolink("mybonus.php", "You can only purchase this bonus 10 days after your last activation.");
            }
        }

        // 2. Panties Validation
        if ($row->type == 'panties1') {
            $check_p = mysqli_fetch_assoc(SQL_Query_exec("SELECT panties1_until FROM users WHERE id = $uid"));
            if (isset($check_p['panties1_until']) && strtotime($check_p['panties1_until']) > $now) {
                autolink("mybonus.php", "You already have this bonus active!");
            }
        }

        // --- EXECUTE TRANSACTION ---

        // Deduct points
        SQL_Query_exec("UPDATE users SET seedbonus = seedbonus - $row->cost WHERE id = $uid");
        
        switch ($row->type) {
            case 'dildo':
                $days = ($row->value > 0) ? (int)$row->value : 10;
                $until = get_date_time($now + ($days * 86400));
                SQL_Query_exec("UPDATE users SET double_bonus = 'yes', double_bonus_until = '$until' WHERE id = $uid");
                write_log("$CURUSER[username] purchased Double Seedbonus for $days days.");
                break;

            case 'invite':
                SQL_Query_exec("UPDATE users SET invites = invites + $row->value WHERE id = $uid");
                write_log("$CURUSER[username] purchased $row->value invites.");
                break;

            case 'traffic':
                SQL_Query_exec("UPDATE users SET uploaded = uploaded + $row->value WHERE id = $uid");
                write_log("$CURUSER[username] purchased " . mksize($row->value) . " upload credit.");
                break;

            case 'HnR':
                $tid = (int)$_POST["torrentid"];
                if (!$tid) autolink("mybonus.php", "You must enter the Torrent ID!");
                
                $check_hnr = SQL_Query_exec("SELECT id FROM snatched WHERE tid = $tid AND uid = $uid AND hnr = 'yes'");
                if (mysqli_num_rows($check_hnr) == 0) {
                    // Refund points if no HnR found
                    SQL_Query_exec("UPDATE users SET seedbonus = seedbonus + $row->cost WHERE id = $uid");
                    autolink("mybonus.php", "No Hit & Run found for this Torrent ID.");
                }
                
                SQL_Query_exec("UPDATE snatched SET ltime = '345600', hnr = 'no' WHERE tid = $tid AND uid = $uid");
                $modcom = sqlesc(gmdate("d-m-Y H:i") . " - H&R on torrent ID $tid cleared using $row->cost points.\n" . $CURUSER['modcomment']);
                SQL_Query_exec("UPDATE users SET modcomment = $modcom WHERE id = $uid");
                write_log("HnR Cleared: Torrent ID $tid for user $CURUSER[username]");
                break;

            case 'giftupload':
                $friendname = trim($_POST['friendname']);
                $res_f = SQL_Query_exec("SELECT id, username FROM users WHERE username = " . sqlesc($friendname));
                if (mysqli_num_rows($res_f) > 0) {
                    $f = mysqli_fetch_assoc($res_f);
                    SQL_Query_exec("UPDATE users SET uploaded = uploaded + $row->value WHERE id = $f[id]");
                    $msg_pm = sqlesc("You received a gift of " . mksize($row->value) . " upload from $CURUSER[username]!");
                    SQL_Query_exec("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES (0, $f[id], '".get_date_time()."', 'Gift Received', $msg_pm)");
                    write_log("$CURUSER[username] sent " . mksize($row->value) . " upload gift to $f[username]");
                } else {
                    // Refund points if friend not found
                    SQL_Query_exec("UPDATE users SET seedbonus = seedbonus + $row->cost WHERE id = $uid");
                    autolink("mybonus.php", "User not found!");
                }
                break;

            case 'panties1':
                $until = get_date_time($now + (30 * 86400));
                SQL_Query_exec("UPDATE users SET panties1 = 'yes', panties1_until = '$until' WHERE id = $uid");
                write_log("$CURUSER[username] activated Panties1 🩲 for 30 days.");
                break;

            case 'warn': // Remove 1 Warning Level
                SQL_Query_exec("DELETE FROM warnings WHERE userid = $uid LIMIT 1");
                $res_w = SQL_Query_exec("SELECT COUNT(*) FROM warnings WHERE userid = $uid");
                $count_w = mysqli_fetch_row($res_w)[0];
                if ($count_w == 0) SQL_Query_exec("UPDATE users SET warned = 'no' WHERE id = $uid");
                
                $modcom = sqlesc(gmdate("d-m-Y") . " - Removed 1 Warning Level with points.\n" . $CURUSER['modcomment']);
                SQL_Query_exec("UPDATE users SET modcomment = $modcom WHERE id = $uid");
                write_log("$CURUSER[username] removed 1 warning level with points.");
                break;

            case 'freeleech':
                $tid = (int)$_POST["torrentid"];
                if (!$tid) autolink("mybonus.php", "Missing Torrent ID!");
                $fl_until = get_date_time($now + 86400); // 24 hours
                SQL_Query_exec("UPDATE torrents SET user_freeleech_uid = $uid, user_freeleech_until = '$fl_until' WHERE id = $tid");
                write_log("$CURUSER[username] made Torrent ID $tid Freeleech for 24h.");
                break;

            case 'VIP':
                $days = ($row->value > 0) ? (int)$row->value : 30;
                // Accumulate time if already a VIP/Donor
                $current_until = ($CURUSER["donoruntil"] > get_date_time()) ? strtotime($CURUSER["donoruntil"]) : $now;
                $until = get_date_time($current_until + ($days * 86400));
                
                SQL_Query_exec("UPDATE users SET class = 6, donor = 'yes', donoruntil = '$until' WHERE id = $uid");
                write_log("$CURUSER[username] purchased VIP status for $days days.");
                break;

            case 'mystery':
                
                $bonuses = [
                    ['type'=>'traffic','value'=>5368709120,'title'=>'+5 GiB Upload','chance'=>30],
                    ['type'=>'traffic', 'value'=>21474836480, 'title'=>'+20 GiB Upload', 'chance'=>15],
                    ['type'=>'dildo', 'value'=>10, 'title'=>'Double BP 10 days', 'chance'=>15],
                    ['type'=>'download', 'value'=>-10737418240, 'title'=>'-10 GiB Download', 'chance'=>15],
                    ['type'=>'panties', 'value'=>0, 'title'=>'Golden Pantsu 🩲', 'chance'=>15],
                    ['type'=>'VIP', 'value'=>30, 'title'=>'VIP 1 month', 'chance'=>5],
                    ['type'=>'invite', 'value'=>1, 'title'=>'1 Invitation', 'chance'=>5],
                ];

                $totalChance = array_sum(array_column($bonuses, 'chance'));
                $rand = random_int(1, $totalChance);
                $running = 0; $reward = null;
                foreach ($bonuses as $b) {
                    $running += $b['chance'];
                    if ($rand <= $running) { $reward = $b; break; }
                }

                $win_text = "";
                if ($reward['type'] == 'traffic') {
                    SQL_Query_exec("UPDATE users SET uploaded = uploaded + {$reward['value']} WHERE id = $uid");
                    $win_text = "won an upload bonus of " . mksize($reward['value']) . "!";
                } elseif ($reward['type'] == 'invite') {
                    SQL_Query_exec("UPDATE users SET invites = invites + {$reward['value']} WHERE id = $uid");
                    $win_text = "received {$reward['value']} new invitation(s)!";
                } elseif ($reward['type'] == 'download') {
                    $val = abs($reward['value']);
                    SQL_Query_exec("UPDATE users SET downloaded = GREATEST(downloaded - $val, 0) WHERE id = $uid");
                    $win_text = "had " . mksize($val) . " removed from their total download!";
                } elseif ($reward['type'] == 'VIP') {
                    $until = get_date_time($now + (30 * 86400));
                    SQL_Query_exec("UPDATE users SET class = 6, donor = 'yes', donoruntil = '$until' WHERE id = $uid");
                    $win_text = "Congratulations! You won VIP status for 30 days!";
                } elseif ($reward['type'] == 'panties') {
                    $until = get_date_time($now + (30 * 86400));
                    SQL_Query_exec("UPDATE users SET panties1 = 'yes', panties1_until = '$until' WHERE id = $uid");
                    $win_text = "received Golden Pantsu 🩲 for 30 days!";
                } elseif ($reward['type'] == 'dildo') {
                    $until = get_date_time($now + (10 * 86400));
                    SQL_Query_exec("UPDATE users SET double_bonus = 'yes', double_bonus_until = '$until' WHERE id = $uid");
                    $win_text = "won Double Seedbonus for 10 days!";
                }

                write_log("$CURUSER[username] opened a Mystery Box: " . strip_tags($win_text));
                
                
                stdhead("Mystery Box Result");
                begin_frame("Mystery Box Result");
                echo "<div style='text-align:center; padding: 30px; background: #000; border: 1px dashed #444;'>";
                echo "<div style='font-size: 50px;'>🎁</div><br>";
                echo "<span style='font-size:18px; color: #00ff00;'>$CURUSER[username], $win_text</span><br><br>";
                echo "<a href='mybonus.php' style='color: yellow; text-decoration: none;'>[ Back to Shop ]</a>";
                echo "</div>";
                end_frame();
                stdfoot();
                die(); 
                break;
        }
        autolink("mybonus.php", "Purchase successful!");
    }
}


$uid = (int)$CURUSER['id'];
$q_base = "FROM peers p INNER JOIN torrents t ON p.torrent = t.id WHERE p.userid = $uid AND p.seeder = 'yes'";

function count_s($sql) { 
    $r = SQL_Query_exec($sql); 
    return ($r) ? mysqli_num_rows($r) : 0; 
}

$now = time();
$one_year = $now - 31536000;
$six_months = $now - 15552000;

$st = [
    'dying'    => count_s("SELECT p.id $q_base AND (SELECT COUNT(*) FROM peers WHERE torrent = p.torrent AND seeder = 'yes') <= 1"),
    'legend'   => count_s("SELECT p.id $q_base AND t.added <= FROM_UNIXTIME($one_year)"),
    'old'      => count_s("SELECT p.id $q_base AND t.added <= FROM_UNIXTIME($six_months) AND t.added > FROM_UNIXTIME($one_year)"),
    'huge'     => count_s("SELECT p.id $q_base AND t.size >= 107374182400"),
    'large'    => count_s("SELECT p.id $q_base AND t.size >= 26843545600 AND t.size < 107374182400"),
    'everyday' => count_s("SELECT p.id $q_base AND t.size >= 1 AND t.size < 26843545600"),
    'seed_1y'  => count_s("SELECT s.sid FROM snatched s INNER JOIN peers p ON s.tid = p.torrent WHERE s.uid = $uid AND p.userid = $uid AND p.seeder = 'yes' AND s.ltime >= 31536000"),
    'seed_6m'  => count_s("SELECT s.sid FROM snatched s INNER JOIN peers p ON s.tid = p.torrent WHERE s.uid = $uid AND p.userid = $uid AND p.seeder = 'yes' AND s.ltime >= 15552000 AND s.ltime < 31536000"),
    'seed_3m'  => count_s("SELECT s.sid FROM snatched s INNER JOIN peers p ON s.tid = p.torrent WHERE s.uid = $uid AND p.userid = $uid AND p.seeder = 'yes' AND s.ltime >= 7776000 AND s.ltime < 15552000"),
    'seed_2m'  => count_s("SELECT s.sid FROM snatched s INNER JOIN peers p ON s.tid = p.torrent WHERE s.uid = $uid AND p.userid = $uid AND p.seeder = 'yes' AND s.ltime >= 5184000 AND s.ltime < 7776000"),
    'seed_1m'  => count_s("SELECT s.sid FROM snatched s INNER JOIN peers p ON s.tid = p.torrent WHERE s.uid = $uid AND p.userid = $uid AND p.seeder = 'yes' AND s.ltime >= 2592000 AND s.ltime < 5184000"),
    'connect'  => count_s("SELECT id FROM peers WHERE userid = $uid AND connectable = 'yes' AND seeder = 'yes'"),
];

// Calcul Hourly Points (PPH)
$pph = ($st['dying'] * 2.0) + 
       ($st['legend'] * 1.5) + 
       ($st['old'] * 1.0) + 
       ($st['huge'] * 0.75) + 
       ($st['large'] * 0.5) + 
       ($st['everyday'] * 0.25) + 
       ($st['seed_1y'] * 2.0) + 
       ($st['seed_6m'] * 1.0) + 
       ($st['seed_3m'] * 0.75) + 
       ($st['seed_2m'] * 0.5) + 
       ($st['seed_1m'] * 0.25) + 
       ($st['connect'] * 0.02);
$is_double = ($CURUSER['double_bonus'] == 'yes' && strtotime($CURUSER['double_bonus_until']) > time());

if ($is_double) {
    $pph = $pph * 2; 
}
stdhead("Seedbonus");
echo "<style>
    .shop-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; padding: 10px; }
    .shop-box { background: #1c1c1c; border: 1px solid #333; padding: 15px; min-height: 220px; display: flex; flex-direction: column; justify-content: space-between; position: relative; }
    .shop-header { border-bottom: 1px solid #333; padding-bottom: 10px; margin-bottom: 15px; text-align: center; font-weight: bold; color: #eee; font-size: 13px; padding-right: 40px; }
    .shop-art { position: absolute; top: 10px; right: 10px; width: 35px; height: 35px; object-fit: contain; }
    .shop-content { font-size: 11px; color: #bbb; line-height: 1.6; flex-grow: 1; }
    .shop-arrow { color: #fff; font-weight: bold; margin-right: 5px; }
    .shop-input { width: 100%; background: #000; border: 1px solid #444; color: #fff; padding: 5px; margin-top: 8px; font-size: 11px; box-sizing: border-box; }
    .buy-button { width: 100%; background: linear-gradient(to bottom, #ffffff 0%, #bbbbbb 100%); color: #000; border: none; padding: 7px; font-weight: bold; cursor: pointer; border-radius: 4px; font-size: 12px; margin-top: 10px; }
    .buy-button:disabled { background: #444; color: #777; cursor: not-allowed; }
</style>";

// ---  EARNINGS & YOUR POINTS ---
echo "<div style='display: flex; gap: 10px; margin-bottom: 20px;'>";
    echo "<div style='flex: 3; background: rgba(0,0,0,0.8); border: 1px solid #153a15; padding: 10px;'>";
        echo "<h3 style='color: #44bb44; text-align:center;'>Current Earnings</h3>";
        echo "<table width='100%' class='table_table' style='font-size: 11px;'>";
        echo "<tr><th class='table_head'>Category</th><th class='table_head'>Description</th><th class='table_head'>Per torrent/h</th><th class='table_head'>Count</th></tr>";
        
        $earns = [
            ["Dying Torrent", "Last seeder remaining!", "+ 2.0", $st['dying']],
            ["Legendary Torrent", "Older than 12 months", "+ 1.5", $st['legend']],
            ["Old Torrent", "Older than 6 months", "+ 1.0", $st['old']],
            ["Huge Torrent", "Size >= 100 GiB", "+ 0.75", $st['huge']],
            ["Large Torrent", "Size 25-100 GiB", "+ 0.5", $st['large']],
            ["Everyday Torrent", "Size 1-25 GiB", "+ 0.25", $st['everyday']],
            ["Legendary Seeder", "Seed Time >= 1 year", "+ 2.0", $st['seed_1y']],
            ["MVP Seeder", "Seed Time 6 months - 1 year", "+ 1.0", $st['seed_6m']],
            ["Committed Seeder", "Seed Time 3 - 6 months", "+ 0.75", $st['seed_3m']],
            ["Team Player Seeder", "Seed Time 2 - 3 months", "+ 0.5", $st['seed_2m']],
            ["Participant Seeder", "Seed Time 1 - 2 months", "+ 0.25", $st['seed_1m']],
            ["Connectable", "Open port / 50h seed", "+ 0.02", $st['connect']],

["New Upload", "Uploading a new torrent", "+ 15.0", "-"],
            ["Fill Request", "Filling a request", "+ 10.0", "-"],
            ["Post Comment", "Comment on torrent", "+ 3.0", "-"],
            ["Say Thanks", "Saying thanks", "+ 2.0", "-"],
            ["Rate Torrent", "Rating a torrent", "+ 2.0", "-"],
            ["Forum Post", "Making a post", "+ 1.0", "-"]
           
        ];

        foreach ($earns as $e) {
            echo "<tr><td class='table_col1'>$e[0]</td><td class='table_col2'>$e[1]</td><td class='table_col1' align='center' style='color:#00ff00;'>$e[2]</td><td class='table_col2' align='center'>$e[3]</td></tr>";
        }
        echo "</table></div>";

    echo "<div style='flex: 1;'>";
        echo "<div style='background: #000; border: 1px solid #153a15; padding: 15px; text-align: center; margin-bottom: 10px;'>";
            echo "<span style='color: #44bb44;'>Your Points</span><br><b style='font-size: 20px; color:yellow;'>".number_format($CURUSER['seedbonus'], 2)."</b></div>";
        echo "<div style='background: #000; border: 1px solid #153a15; padding: 10px; font-size: 11px;'>";
            echo "<h4 style='color: #44bb44; margin: 0 0 5px 0;'>Point Forecast</h4>";
            echo "Second: <span style='float:right; color:#00ff00;'>".number_format($pph/3600, 2)."</span><br>";
            echo "Minute: <span style='float:right;'>".number_format($pph/60, 2)."</span><br>";
            echo "Hour: <span style='float:right; color:#00ff00;'>".number_format($pph, 2)."</span><br>";
            echo "Day: <span style='float:right;'>".number_format($pph*24, 2)."</span><br>";
            echo "Week: <span style='float:right;'>".number_format($pph*168, 2)."</span><br>";
            echo "Month: <span style='float:right;'>".number_format($pph*720, 2)."</span><br>";
            echo "Year: <span style='float:right; color:yellow;'>".number_format($pph*8760, 2)."</span></div></div>";
echo "</div>";

begin_frame("Active Seeding Torrents");

$now_ts = time();
$one_year = $now_ts - 31536000;
$six_months = $now_ts - 15552000;

// --- Pager ---
$count_res = SQL_Query_exec("SELECT COUNT(*) FROM peers WHERE userid = '$uid' AND seeder = 'yes'");
$count_row = mysqli_fetch_row($count_res);
$count = $count_row[0];
$perpage = 15; 
list($pagertop, $pagerbottom, $limit) = pager($perpage, $count, "mybonus.php?");
// ------------------------

$res_p = SQL_Query_exec("SELECT 
                            p.started, p.torrent, p.connectable,
                            t.name, t.size, t.added, t.category, t.seeders, t.leechers,
                            s.ltime, 
                            c.name AS catname
                        FROM peers p 
                        LEFT JOIN torrents t ON p.torrent = t.id 
                        LEFT JOIN snatched s ON (p.torrent = s.tid AND p.userid = s.uid)
                        LEFT JOIN categories c ON t.category = c.id
                        WHERE p.userid = '$uid' AND p.seeder = 'yes' 
                        ORDER BY p.started DESC $limit");

echo "<style>
    .seeding-table { background: #000; border-collapse: collapse; width: 100%; color: #eee; font-family: 'Segoe UI', sans-serif; border: 1px solid #111; margin-bottom: 10px; }
    .seeding-table th { background: #080808; color: #00ff00; padding: 12px 8px; border-bottom: 2px solid #111; font-size: 10px; text-transform: uppercase; text-align: left; }
    .seeding-table td { padding: 10px 8px; border-bottom: 1px solid #111; font-size: 11px; }
    .t-link { color: #fff; font-weight: bold; text-decoration: none; }
    .t-link:hover { color: #00ff00; }
    .text-green { color: #00ff00; font-weight: bold; }
    .text-dim { color: #666; }
    .conn-icon { font-family: 'FontAwesome'; font-style: normal; font-size: 14px; }
</style>";

if ($count > $perpage) echo "<center style='margin-bottom:10px;'>$pagertop</center>";

echo "<table class='seeding-table'>";
echo "<thead>
        <tr>
            <th>Type</th>
            <th>Name</th>
            <th style='text-align:center;'>Size</th>
            <th style='text-align:center;'>S/L</th>
            <th style='text-align:center;'>Conn</th>
            <th style='text-align:center;'>Seedtime</th>
            <th style='text-align:center;'>Age</th>
            <th>Hourly</th>
            <th>Daily</th>
            <th>Weekly</th>
            <th>Monthly</th>
        </tr>
      </thead>
      <tbody>";

if ($res_p && mysqli_num_rows($res_p) > 0) {
    while ($row = mysqli_fetch_assoc($res_p)) {
        if (empty($row['name'])) continue;
        
        

        $h = 0;
        $added_ts = strtotime($row['added']);
        if ($added_ts <= $one_year) $h += 1.5; elseif ($added_ts <= $six_months) $h += 1.0;
        if ($row['size'] >= 107374182400) $h += 0.75; elseif ($row['size'] >= 26843545600) $h += 0.50; elseif ($row['size'] >= 1073741824) $h += 0.25;
        if ($row['connectable'] == 'yes') $h += 0.05;
        if ($row['seeders'] <= 1) $h += 2.0;

        $session_start = strtotime($row['started']);
        $session_duration = ($session_start > 0) ? ($now_ts - $session_start) : 0;
        $total_seed = (int)$row['ltime'] + $session_duration;
        $age = $now_ts - $added_ts;

        if ($row['connectable'] == 'yes') {
            $conn_display = "<i class='fa fa-rss' style='color: #00ff00; text-shadow: 0 0 5px #00ff00;' title='Connectable'></i>";
        } else {
            $conn_display = "<i class='fa fa-rss' style='color: #ff0000; text-shadow: 0 0 5px #ff0000;' title='Not Connectable'></i>";
        }
$is_double = ($CURUSER['double_bonus'] == 'yes' && strtotime($CURUSER['double_bonus_until']) > time());

if ($is_double) {
    $h = $h * 2; 
}

        echo "<tr>
                <td class='text-dim'>".htmlspecialchars($row['catname'])."</td>
                <td><a class='t-link' href='torrents-details.php?id=$row[torrent]'>".htmlspecialchars(substr($row['name'], 0, 70))."</a></td>
                <td align='center' class='text-dim'>".number_format($row['size'] / 1073741824, 2)." GiB</td>
                <td align='center' style='font-size:10px;'>
                    <span style='color:#00ff00;'>$row[seeders]</span> / <span style='color:#ff0000;'>$row[leechers]</span>
                </td>
                <td align='center'>$conn_display</td>
                <td align='center' class='text-green'>".format_seedtime($total_seed)."</td>
                <td align='center' class='text-dim'>".format_seedtime($age)."</td>
                <td class='text-green'>".number_format($h, 2)."</td>
                <td>".number_format($h * 24, 2)."</td>
                <td>".number_format($h * 168, 2)."</td>
                <td>".number_format($h * 720, 2)."</td>
              </tr>";
    }
} else {
    echo "<tr><td colspan='11' align='center'>You are not seeding any torrents at this moment.</td></tr>";
}
echo "</tbody></table>";

if ($count > $perpage) echo "<center>$pagerbottom</center>";

end_frame();

// --- EXCHANGE ---
$res_shop = SQL_Query_exec("SELECT * FROM bonus ORDER BY id ASC");
begin_frame("Bonus Exchange (Shop)");
?>
  <table border="0" cellpadding="3" class="table_table" width="100%">
  <tr>
      <th class="table_head">Option</th>
      <th class="table_head">What is it?</th>
      <th class="table_head">Points</th>
      <th class="table_head">Exchange</th>
  </tr>
<?php while ($row = mysqli_fetch_object($res_shop)): ?>
<form method="post" action="mybonus.php">
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<tr>
    <td class="table_col1">
        <b><?php echo htmlspecialchars($row->title); ?></b><br>
        <?php if ($row->type == 'giftupload'): ?>
             <input type="text" name="friendname" placeholder="Enter friend's username" required style="width:200px; margin-top:5px;">
        <?php endif; ?>
        <?php if ($row->type == 'freeleech' || $row->type == 'HnR'): ?>
            <input type="text" name="torrentid" placeholder="Enter Torrent ID" required style="width:150px; margin-top:5px;">
        <?php endif; ?>
    </td>
    <td class="table_col2"><?php echo htmlspecialchars($row->descr); ?></td>
    <td class="table_col1" align="center"><b style="color:yellow;"><?php echo number_format($row->cost, 0); ?></b></td>
    <td class="table_col2" align="center">
        <input type="submit" value="Buy" <?php echo ($CURUSER['seedbonus'] < $row->cost ? 'disabled="disabled"' : ''); ?> style="cursor:pointer;" />
    </td>
</tr>
</form>
<?php endwhile; ?>
  </table>
<?php
end_frame();
stdfoot();
?>