U[dated Seed bonus to better iterate recent claimed users per row, rewrote a few queries to be more efficient and check more conditions, minor fixes here and there.


<?
if (session_status() == PHP_SESSION_NONE) { session_start(); }
require_once '../site.php';
$db->connect();
site_header();

// Seed Bonus - Reworked - Sep 19, 2020
// Updated/optimized + social/UI additions - Mar 21, 2021

if (!$account->sessionLogin()) {
header(sprintf('Location: %s/siginin/', $CONFIG['base_url']));
   die;
}

$errors = array();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
   
   $data = array();
   $keys = array('id', 'userid', 'torrentid');
   foreach ($keys as $key) {
       if (!array_key_exists($key, $_POST)) {
           $errors []= sprintf('Seed bonus item ID %s not valid.', $key);        
       }
       $data[$key] = $_POST[$key];
   }
   
   $res = $db->query_params('SELECT users.seedbonus, users.perk_avatar, users.username, users.slots, users.last_exchange, users.class, users.user_id, bonus.cost, bonus.descr, bonus.type, bonus.value, bonus.id FROM users, bonus WHERE users.user_id = :user_id AND bonus.id = :post_id', array('user_id' => $account->getId(), 'post_id' => $data['id'])) or die('db error');
   
if (!($row = $res->fetch())) {
if ($row['seedbonus'] < $row['cost'])
$errors []= 'Demand not valid.';
   }
   
   if (array_key_exists('id', $data) && $data['id'] == $row['id']) {
   
   if ($row['type'] == 'invite') {
        $query = $db->query_params("UPDATE users SET invites = invites + :value WHERE user_id = :user_id", array('value' => $row['value'], 'user_id' => $account->getid()));
printf('Your account has been <b>credited</b> for <b>%s</b> Extra Invites!', $row['value']);
}

if ($row['type'] == 'traffic') {
        $query = $db->query_params('UPDATE users SET uploaded = uploaded + :value, last_exchange = :bonus WHERE user_id = :user_id', array('bonus' => $row['value'], 'value' => $row['value'], 'user_id' => $account->getid()));
printf('Your account has been <b>credited</b> for <b>%s</b> bonus Upload Data!', format_size($row['value']));
}

if ($row['type'] == 'slots' && $data['id'] == $row['id']) {
        $query = $db->query_params('UPDATE users SET slots = :slots WHERE user_id = :user_id', array('slots' => $data_row['value'], 'user_id' => $account->getid()));
printf('Your account has been <b>credited</b> for <b>%s</b> Extra Download Slots!', $row['value']);
}
   
if ($row['type'] == 'feature' && $data['id'] == $row['id']) {
        $query = $db->query_params("UPDATE users SET perk_avatar = 'yes' WHERE user_id = :user_id", array('user_id' => $account->getid()));
printf('<span>Your site comments will be <b>featured</b> with a %s', '<img src="/themes/default/images/super_star.png" style="height: 50px; padding: 0;margin-bottom: -10px;position: relative;transform: rotate(-15deg);margin-left: -3px;margin-right: -30px;" /></span>');
}

if ($row['type'] == 'restore') {
        $query = $db->query_params("UPDATE users SET limited = 'no' WHERE user_id = :user_id", array('user_id' => $account->getid()));
$db->query_params("DELETE FROM warnings WHERE userid = :user_id", array('user_id' => $account->getid()));
printf('<span>Your account has been <b>restored</b>, and all warnings removed.</span>');
}

if ($query) {
    $db->query_params("UPDATE users SET seedbonus = seedbonus - :cost WHERE user_id = :user_id", array('cost' => $row['cost'], 'user_id' => $account->getid()));
$db->query_params("INSERT INTO bonus_users (user_id, added, type) VALUES (:user_id, NOW(), :type)", array('user_id' => $account->getId(), 'type' => $row['type']));
    }
   }
}

// Get exchange item list    
$res = $db->query_params('SELECT * FROM bonus ORDER BY type');
$bonusid = $res->fetch();
$rowid = $bonusid['id'];


// Get users seed bonus points - Sep 2, 2020
$points = $db->query_params('SELECT seedbonus, user_id FROM users WHERE user_id = :user_id', array('user_id' => $account->getId()));
$userpoints = $points->fetch();
?>
 
<h1 itemprop="name">
<i class="material-icons" style="color:#7FB335;font-size:45px;vertical-align:bottom;padding-right:10px">eco</i> Seed Bonus
<?=(!empty($userpoints['seedbonus']))
? "<span style=\"font-size:27px;font-weight:400;padding:5px;border-radius:7px;background:#ED2324;text-align:center;color:#FFF;box-shadow: inset 1px 5px 7px -6px #000000;float:right\">" . $userpoints['seedbonus'] . "</span>"
: "<span style=\"font-size:27px;font-weight:400;padding:5px;border-radius:7px;background:#ED2324;text-align:center;color:#FFF;box-shadow: inset 1px 5px 7px -6px #000000;margin-right:15px\">0</span>";
?>
</h1>

<div itemprop="description">Explore options that you can redeem from accumulated <b>points</b> against depending on the share of torrents.</div>  

<center>
<div class="card-action ng-scope">
<!-- Rundown -->
<?
$retrieve = $db->query_params("SELECT (SELECT COUNT(torrent_id) FROM peers WHERE user_id = p.user_id AND seeder = 'yes') AS total, GROUP_CONCAT(p.torrent_id) AS torrent_id FROM peers p WHERE p.user_id = :user_id GROUP BY p.torrent_id DESC LIMIT 12", array('user_id' => $account->getId()));
$seeding = $retrieve->fetchAll();
?>
<div itemprop="description">You are seeding:</div>
<div style="padding:10px 0 10px 0">
<?
// Currently seeding torrents - Mar 21, 2021
if ($seeding) {
foreach ($seeding as $row) {
$query           = $db->query_params("SELECT name, cover1, torrent_id FROM torrents WHERE torrent_id IN ($row[torrent_id])");
$data['avatars'] = $query->fetchAll();
foreach ($data['avatars'] as $title) {
print("<a id=\"tips\" title=\"$title[name]\" href=\"/details/$title[torrent_id]/\"><img itemprop=\"image\" class=\"responsive-img\" src=\"/images/covers/$title[cover1]\" alt=\"$title[name]\" style=\"width: 107px; height: 176px; margin-left: 10px\" border=\"0\" /></a>");
}
}
}else{
print("<div style=\"margin-bottom:4px;padding:5px;background:#D2D2D2;text-align:center;color:#242424;font-size:20px;max-width:300px;border-radius:7px\">No (0 found)</div>");
}
?>
<div style="padding-top:7px">You receive <b><?=$CONFIG['bonuspertime']?></b> points per <b>every 10 minutes</b> the system registers you as a seeder by torrent.</div></div>
</center>

<? while ($row = $res->fetch()): ?>

<article itemprop="itemListElement" itemscope="" itemtype="http://schema.org/Article" class="card">

<form method="post" class="cd-form floating-labels" action="/mybonus.php">
<input type="hidden" name="id" value="<?=intval($row['id'])?>" />
<div class="card-action ng-scope" style="font-size:22px">
<!-- Seeder exchange -->
<span style="font-size:27px;font-weight:400;padding: 7px 5px 5px 10px;border-radius:7px;background:#EC2324;text-align:center;color:#FFF;box-shadow: inset 1px 5px 7px -6px #000000;margin-right:15px">
<?=str_replace('Uploaded', '', html_escape($row['title']))?>
</span>
<?=$row['descr']?>
<span style="float:right;margin-right:-125px">
<button class="btn btn-danger" onclick="this.form.submit();" style="font-size:15px" <?=($userpoints['seedbonus'] < $row['cost'] ? 'disabled' : null)?>>
<?=($userpoints['seedbonus'] < $row['cost']) ? 'Not Enough' : 'Exchange'?>
</button>
</span>
  </div>
  <div class="card-action ng-scope" style="font-size:18px;width:700px">
  <?
  // Get item's recent users - Mar 21, 2021 - Query rewritten to support accurate claims per row/type, June 10, 2021
  $claimed = $db->query_params("SELECT b.title, b.type, bu.user_id, u.username, u.avatar, bu.added FROM bonus_users bu LEFT JOIN users u ON u.user_id = bu.user_id LEFT JOIN bonus b ON b.type = b.type WHERE b.id = :bonus_id AND b.value = u.last_exchange ORDER BY bu.added DESC LIMIT 11", array(
  'bonus_id' => $row['id']
  ));
  foreach ($claimed as $bonususer) {
  if (!file_exists($bonususer['avatar'])) {
  $avatar = 'images/noavatar.png';
  }else{
  $avatar = $bonususer['avatar'];
  }
  printf('<div style="float:left;font-size:22px;font-weight:400;padding:5px 5px 5px 7px;border-radius:7px;background:#D2D2D2;text-align:center;color:#FFF;_box-shadow: inset 1px 5px 7px -6px #000000;margin-right:7px;margin-bottom:15px;margin-top:0px"><a href="/user/?id=%s"><img src="/%s" style="height:29px;width:29px;vertical-align:middle;border-radius:7px" /></a>%s</div>', $bonususer['user_id'], $avatar, str_replace('Uploaded', '', $bonususer['title']));
}
?>
</div>
</form>
</article>

<? endwhile; ?>

<article itemprop="itemListElement" itemscope="" itemtype="http://schema.org/Article" class="card">

<div class="card-action ng-scope">
<!-- Rundown -->
<span>
To get bonus points you need to have the complete torrent which includes all txt/nfo documents, samples, and subtitles. Keep the torrent open in your torrent client and your bonus points will start to increase.
<br><br>
It means, when you download a torrent 100% (completely), share back and every hour you will earn bonus points, (up to a maximum of 15 torrents) you keep sharing back. Just download the torrent 100% and don't close your torrent client, keep sharing with others. Even if no one downloads from you, you will still get bonus points for them.
<br><br>Once you accumulate loads of points you can use them on any of the options listed above. You get points even if there are no leechers on the torrent and you can seed as many torrents as you want. Yet another way to help out your ratio!
<br><br>
Keep Seeding! Plus Enjoy Being with us!
</span>
</div>
 
</article>

<div>You receive <?=$CONFIG['bonuspertime']?> points per <?=floor($CONFIG['autoclean_interval'] / 10)?> minutes the system registers you as a seeder by torrent.</div>

<?
$db->query_params("UPDATE users SET page = 'Seed Bonus', last_access = NOW() WHERE user_id = :user_id", array('user_id' => $account->getId()));
site_footer();