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
BEncode/BDecode php 7.4+
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Support

2024-11-30 22:00:03
This seems to be fine. removed stripslashes. Back up your files before trying this. Let me know if its fine. Seems fine for me
BEncode
declare(strict_types=1);

class BEncode
{
   // Sort dictionary keys
   private function makeSorted(array $array): array
   {
       ksort($array);
       return $array;
   }

   // Encode a single entry
   private function encodeEntry($entry, string &$fd): void
   {
       if (is_int($entry) || is_float($entry)) {
           $fd .= "i{$entry}e";
       } elseif (is_string($entry)) {
           $fd .= strlen($entry) . ":{$entry}";
       } else {
           throw new InvalidArgumentException("Unsupported entry type.");
       }
   }

   // Encode a list
   private function encodeList(array $list, string &$fd): void
   {
       $fd .= "l";
       foreach ($list as $item) {
           $this->decideEncode($item, $fd);
       }
       $fd .= "e";
   }

   // Encode a dictionary
   private function encodeDict(array $dict, string &$fd): void
   {
       $fd .= "d";
       $sortedDict = $this->makeSorted($dict);
       foreach ($sortedDict as $key => $value) {
           $this->encodeEntry($key, $fd);
           $this->decideEncode($value, $fd);
       }
       $fd .= "e";
   }

   // Decide what to encode
   private function decideEncode($data, string &$fd): void
   {
       if (is_array($data)) {
           if (array_keys($data) === range(0, count($data) - 1)) {
               $this->encodeList($data, $fd);
           } else {
               $this->encodeDict($data, $fd);
           }
       } else {
           $this->encodeEntry($data, $fd);
       }
   }

   // Public method to encode data
   public function encode($data): string
   {
       $fd = "";
       $this->decideEncode($data, $fd);
       return $fd;
   }
}

// Usage example:
function BEncode($data): string
{
   return (new BEncode())->encode($data);
}


BDecode
declare(strict_types=1);

class BDecode
{
   public function numberDecode(string $wholefile, int $start): array
   {
       $ret = [0];
       $offset = $start;

       $negative = false;
       if ($wholefile[$offset] === '-') {
           $negative = true;
           $offset++;
       }

       if ($wholefile[$offset] === '0') {
           $offset++;
           if ($negative) {
               return [false];
           }
           if ($wholefile[$offset] === ':' || $wholefile[$offset] === 'e') {
               $ret[0] = 0;
               $ret[1] = $offset + 1;
               return $ret;
           }
           return [false];
       }

       while (isset($wholefile[$offset]) && ctype_digit($wholefile[$offset])) {
           $ret[0] = $ret[0] * 10 + (int)$wholefile[$offset];
           $offset++;
       }

       if ($wholefile[$offset] === 'e' || $wholefile[$offset] === ':') {
           $ret[1] = $offset + 1;
           if ($negative) {
               if ($ret[0] === 0) {
                   return [false];
               }
               $ret[0] = -$ret[0];
           }
           return $ret;
       }

       return [false];
   }

   public function decodeEntry(string $wholefile, int $offset = 0): array
   {
       switch ($wholefile[$offset]) {
           case 'd':
               return $this->decodeDict($wholefile, $offset);
           case 'l':
               return $this->decodeList($wholefile, $offset);
           case 'i':
               return $this->numberDecode($wholefile, $offset + 1);
           default:
               $info = $this->numberDecode($wholefile, $offset);
               if ($info[0] === false) {
                   return [false];
               }
               $ret[0] = substr($wholefile, $info[1], $info[0]);
               $ret[1] = $info[1] + strlen($ret[0]);
               return $ret;
       }
   }

   public function decodeList(string $wholefile, int $start): array
   {
       if ($wholefile[$start] !== 'l') {
           return [false];
       }

       $offset = $start + 1;
       $ret = [];

       while ($wholefile[$offset] !== 'e') {
           $value = $this->decodeEntry($wholefile, $offset);
           if ($value[0] === false) {
               return [false];
           }
           $ret[] = $value[0];
           $offset = $value[1];
       }

       return [$ret, $offset + 1];
   }

   public function decodeDict(string $wholefile, int $start = 0): array
   {
       if ($wholefile[$start] !== 'd') {
           return [false];
       }

       $offset = $start + 1;
       $ret = [];

       while ($wholefile[$offset] !== 'e') {
           $keyInfo = $this->decodeEntry($wholefile, $offset);
           if ($keyInfo[0] === false) {
               return [false];
           }

           $key = $keyInfo[0];
           $offset = $keyInfo[1];

           $valueInfo = $this->decodeEntry($wholefile, $offset);
           if ($valueInfo[0] === false) {
               return [false];
           }

           $ret[$key] = $valueInfo[0];
           $offset = $valueInfo[1];
       }

       return [$ret, $offset + 1];
   }
}

// Use this function, e.g., BDecode("d8:announce44:http://www...");
function BDecode(string $wholefile)
{
   $decoder = new BDecode();
   $result = $decoder->decodeEntry($wholefile);
   return $result[0];
}

TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Last edited by MicroMonkey on 2024-12-25 20:48:08
2024-12-22 17:30:02
Thank you very much, it's holding up well so far
and torrent in green, but if I want to upload, it gives blank page and no error?
BDecode.php
Download
BEncode.php
Download

Last edited by spiritblue on 2024-12-22 18:55:46
2024-12-23 18:36:11
Im not sure spirit. So far I have it working fine on php 7.4 and php 8.3.6.  Maybe try put this at the top of your functions.php file and see what the blank page is.
// Enable error reporting for development
ini_set('display_errors', 1); // Display errors on the screen
ini_set('display_startup_errors', 1); // Display errors during PHP startup
error_reporting(E_ALL); // Report all errors, warnings, and notices

TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
2024-12-25 15:43:53
Quite right, I'll check! 👍
I'll get back to you if needed  😉
2024-12-25 20:37:01
also, your BDecode file is not like the one I posted above. Double check that. I made sure to repost my code in the first post incase I had a typo. All code in the first post has been reposted. Check to see if its different.
TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Last edited by MicroMonkey on 2024-12-25 20:50:17
2025-01-01 17:59:42
I found the mistake and it was this line on both files
declare(strict_types=1);


Big thanks !
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: 9
  • Members: 0
  • Guests online: 9
  • Comments: 3
  • Messages: 1,728