Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Code Journel
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / V4 / Support & Suggestions

2020-09-05 18:19:00
Here is the code journal. Here I will post with a heading, what i've improved/fixed and will supply code. This makes a great place to reference back, without 20 documents riddled through the hard drive lol

-------

I got tired of looking at avail_slots() how it was so I made better, with the db class of course.


// Max Slots
function avail_slots($user_id, $user_class)
{
   global $db;
   $checkslots  = $db->query_params('SELECT u.class, u.user_id, g.group_id, g.level, g.maxslots FROM groups g LEFT JOIN users u ON u.class = g.group_id WHERE g.group_id = :user_class', array('user_class' => $user_class));
   $row  = $checkslots->fetch();
   if (array_key_exists('warned', $row)) { // does the column exist?
       if ($row['warned'] == 'yes'): $maxslot = 1; else: $maxslot = $row['maxslots']; endif;
       return $maxslot;
   }
}

And the main function to check this. I also cleaned this up and tested on a naked page impersonating my secondary account for accuracy.

$maxslots = $db->query_params("SELECT COUNT(DISTINCT torrent_id) AS total FROM peers WHERE user_id = :user_id AND seeder = 'no'", array('user_id' => $user_row['user_id']));
foreach ($maxslots as $slots)
   $totalslots = $slots['total'];
   $maxslot = avail_slots($user_row['user_id'], $user_row['class']);
   if ($totalslots >= $maxslot) fail("Maximum $totalslots/$maxslot download slots exceeded! Finish current transfers to reclaim slots.");

The secret here is making use of SQL properly and utilizing JOINS when needed. No need to call on 2 queries. I didn't bother doing a join-less query as that just complicates testing.
-------

Pager. Counting total torrent row results in a slim fashion. pager() picks up on count value to generate the 0,1 LIMIT. My JS now reads this variable, and old pager is removed indefinitely.

$countdb = $db->query_params('SELECT COUNT(torrent_id) FROM torrents');
   foreach ($countdb->fetch() as $total) $count = $total;


And we have [color=rgb(226, 80, 65)]$count[/color]

-------

Cleaning up Cleanup lol and adding better checks, removing the silly INT gmtime value and using DATETIME in place.


function autoclean()
{
   global $db, $CONFIG;
   require_once '../cleanup.php';
   $clean_time = $db->query_params("SELECT last_time FROM tasks WHERE task='cleanup'");
   $clean      = $clean_time->fetch();
   $last_time  = $clean['last_time'];
 
   if (empty($clean)) $db->query_params("INSERT INTO tasks (task, last_time) VALUES ('cleanup', now())"); return;
   if ($last_time + $CONFIG['autoclean_interval'] < date()) return;
       $planned_clean = $db->query_params("UPDATE tasks SET last_time = now() WHERE task='cleanup' AND last_time = :last_time", array('now' => $last_time));
   if (!empty($planned_clean)) return;
 
   do_cleanup();
}


-------

Messages. As I've taken a try at a working type, its not built right. So redoing with a message class, using this query I found on stackoverflow

SELECT CONCAT(GREATEST(sender,receiver)," ",LEAST(sender,receiver)) AS thread, MAX(CONCAT(added,"|",msg)) as added, MAX(added) AS last_message, messages.* FROM messages WHERE receiver= 1 GROUP BY added ORDER BY added DESC


Threaded messages. Both IDs between both sender and receiver are there if query run for (receiver = 1) - 1 = admin (very first account)

So thats a good step for messages and gets all one needs.
2020-11-29 21:50:10
Happy Hour - announce.php

The idea behind this, is to turn OFF download slot limits, peer limits and open the "doors" wide open for Saturday & Sunday.

This will allow a full peer burst for the following week, allowing a great boost on the weekly downloads while doing the same burst again the following weekend. This will incorperate a banner in the sidebar that advertises "Happy Hour Access".

Max Slot / Peer limits resume Monday @ 12:00 AM

Full code (PHP 5 - 7+ compatible)


// Happy Hour - No Slot Limits, No peer limits - PHP 5/7+ compatible : Saturday/Sunday - Nov 29, 2020
if (!in_array(date('l'), ['Saturday', 'Sunday'])) {
   $maxslots = $db->query_params("SELECT COUNT(DISTINCT torrent_id) AS total, last_announce FROM peers WHERE user_id = :user_id AND seeder = 'no'", array('user_id' => $user_row['user_id']));
   $current  = $maxslots->fetch();
       if ($current['total'] > $user_row['slots']) {
           $checklast   = $db->query_params('SELECT user, date, message FROM shoutbox WHERE user = :username', array('username' => $user_row['username']));
           $lastmessage = $checklast->fetch();
               if ($lastmessage['date'] < $current['last_announce']) {
                   $event = "[url=$CONFIG[base_url]/user/?id=$user_row[user_id] ] $user_row[username][/url] reached their maximum download slot $current[total]/$user_row[slots]";
                   $reason = "You have reached your max download slots of 4 (out of 3 available). You can visit Seed Bonus to upgrade to more slots.";
                   $db->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array('receiver' => $user_row['username'], 'event' => $event));
                   $db->query_params("INSERT INTO messages (sender, receiver, added, msg, poster, subject) VALUES (0, :user_id, NOW(), :reason, 0, 'Max Download Slots Reached')", array('user_id' => $user_row['user_id'], 'reason' => $reason)); // Sep 23, 2020
               }
               fail("$current[total]/$user_row[slots] download slots exceeded! Finish current transfers to reclaim slots.");
       }
} else {
   $event = "Today is Happy Hour! No slot limits, No peer limits - Full Speeds!";
   $db->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array('receiver' => 'NerbBot', 'event' => $event));
}
2020-11-29 23:03:16
User Passkey IP limit checking


// Peer Passkey IP limit check
$ipcheck = $db->query_params('SELECT DISTINCT(ip) AS ip FROM peers WHERE user_id = :user_id', array('user_id' => $user_row['user_id'])) or fail('Unable to select peer IP');
$count = 0;
$found = false;
foreach ($ipcheck->fetch() as $row) {
   $count++;
   if ($row['ip'] == $data['ip']) {
       $found = true;
   }
}
if (!$found && $count >= $CONFIG['max_passkey_ips']) fail('You have reached your IP limit for this passkey.');


Config

'max_passkey_ips'  =>  2,
2020-12-16 03:38:05
3 Failed Login Attempts - Email notification & logging

So this became a random idea during the day today, gave it some thought for a few hours and got home and wrote it out in 1 shot. Works great as expected, 2 typos, no bugs lol pleased.
I always use PEAR Mail as its my favourite for a long time, so it stuck ever since.

If account gets failed login 3 times in a row, a email notification is sent to the account owner and count resets back to 0

Reference: $data['username'] is the same as $_POST['username']

// Failed login attempts (3) with Email Notification & Log - Dec 15, 2020
       $checkusers = $db->query_params('SELECT username, email, login_attempts, ip FROM users WHERE username = :username', array('username' => $data['username'])) or die('Unable to retrieve username');
       if (!($checkuser = $checkusers->fetch())) {
           // Query successful, username found
       }else{
           if ($checkuser['login_attempts'] >= 0) {
               $db->query_params('UPDATE users SET login_attempts = login_attempts + 1 WHERE username = :username', array('username' => $checkuser['username'])) or die('Unable to retrieve username');
           }
           if ($checkuser['login_attempts'] >= 3) {
               $db->query_params('UPDATE users SET login_attempts = 0 WHERE username = :username', array('username' => $checkuser['username'])) or die('Unable to retrieve username');

               include('Mail.php');
               $recipients = $checkuser['email'];
               $headers['From'] = $CONFIG['site_email'];
               $headers['To'] = $checkuser['email'];
               $headers['Subject'] = 'Failed Login Attempts';
               $headers['MIME-Version'] = 1;
               $headers['Content-type'] = 'text/html;charset=iso-8859-1';
               // Define SMTP Parameters
               $params['host'] = 'smtp.someservice.com';
               $params['port'] = '25';
               $params['auth'] = ''; // PLAIN for fields filled below, blank for false
               $params['username'] = '';
               $params['password'] = '';

               // SMTP debug - only helps with authentication issues
               $params['debug'] = false;
               // Create the mail object using the Mail::factory method
               $mail_object = Mail::factory('smtp', $params);

               // Send reset email
               $template = sprintf('<html><head>
               <style>body{font-family:arial,sans-serif,helvetica;background-color:rgba(236,236,236,1);margin:0;padding:0}.email-header{background:url("https://domain.com/header.png") left no-repeat rgba(237,35,36,1);width:1000px;height:56px;margin-bottom:20px}.email-body{box-shadow:0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);background:rgba(255,255,255,1);padding:10 10px 50px 10px;font-size:17px;width:500px;margin:0 auto;text-align:center;overflow:auto}.welcome-msg{font-size:45px;text-align:center;padding:0 20px 20px 0}.welcome-msg2{font-size:17px;text-align:center;padding:0 20px 20 0}.email-body .welcome-msg{padding-top:15px}.email-body a{margin-left:-20px;margin-top:10px}.btn-signin{box-shadow:inset 0 1px 0 0 rgba(242,156,147,1);background:linear-gradient(to bottom,rgba(254,26,0,1) 5px,rgba(206,1,0,1) 1000px);background-color:rgba(254,26,0,1);border-radius:6px;border:1px solid rgba(216,53,38,1);display:inline-block;cursor:pointer;color:rgba(255,255,255,1);font-family:Arial;font-size:15px;font-weight:bold;padding:6px 24px;text-decoration:none;text-shadow:0 1px 0 rgba(178,62,53,1)}.btn-signin:hover{background:linear-gradient(to bottom,rgba(206,1,0,1) 5px,rgba(254,26,0,1) 1000px);background-color:rgba(206,1,0,1)}.btn-signin:active{position:relative;top:1px}</style>
               </head>
               <body>
               <div class="email-header">
               </div>
               <div class="welcome-msg">Failed Login Attempts</div>
               <div class="welcome-msg2">Tracker has detected <b> %1$s </b> failed login attempts from IP <b> %2$s </b> attempting to gain access to this account.</div>
               <div class="email-body">
               <div class="welcome-msg">Actions</div>
               <div class="email-content">
               <url here></div>
               </div>
               <div class="welcome-msg"><a href="https://domain.com/recover/" class="btn-signin">Reset Password</a></div>
               </div>
               </body>
               </html>', $checkuser['login_attempts'], $checkuser['ip']);
               $mail_object->send($recipients, $headers, $template);
               write_log("3 failed login attempts. Email sent to  $headers[To]");
           }
}


And email looks like (without logo)
[ATTACH type="full" alt="email-sample.png"]6001[/ATTACH]
email-sample.png
email-sample.png
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 2
  • Members: 0
  • Guests online: 2
  • Comments: 3
  • Messages: 1,728