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
New Bencode Class
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / M-BiTsy / M-BiTsy Support

2020-11-14 21:25:51
Heres the bencode class i use for pdo/mvc for v3 you just need to include the file

search for bdecode.php & bencode.php replace these for require Newbencode.php
when making these edits where looking for the bdecode & bencode functions the old two classes can be removed
ill update with announce but fullys woks the announce still uses separate functions

create file Newbencode.php place in backend
<?php

/**
* Class Bencode
*
* Convert Map:
*  - Dictionary (starts with d, ends with e, with key sort)
*  - List (starts with l, ends with e)
*  - Integer (starts with i, ends with e)
*  - String (starts with number denoting number of characters followed by : and then the string)
*
* @package Rhilip\Bencode
* @author Rhilip
* @license MIT
*
* @see https://en.wikipedia.org/wiki/Bencode
* @see https://wiki.theory.org/index.php/BitTorrentSpecification
*/
class Newbencode
{
   /**
    * Decode bencoded data from string
    *
    * @param string $data
    * @param int $pos
    * @return mixed
    * @throws Exception
    */
   public static function decode($data, &$pos = 0)
   {
       $start_decode = ($pos === 0);   // If it is the root call ?
       if ($start_decode && (!is_string($data) || strlen($data) == 0)) {
           throw new Exception('Decode Input is not valid String.');
       }

       if ($data[$pos] === 'd') {
           $pos++;
           $return = [];
           while ($data[$pos] !== 'e') {
               $key = self::decode($data, $pos);
               $value = self::decode($data, $pos);
               if ($key === null || $value === null) {
                   break;
               }
               if (!is_string($key)) {
                   throw new Exception('Non string key found in the dictionary.');
               } elseif (array_key_exists($key, $return)) {
                   throw new Exception('Duplicate Dictionary key exist before: ' . $key);
               }
               $return[$key] = $value;
           }
           ksort($return, SORT_STRING);
           $pos++;
       } elseif ($data[$pos] === 'l') {
           $pos++;
           $return = [];
           while ($data[$pos] !== 'e') {
               $value = self::decode($data, $pos);
               $return[] = $value;
           }
           $pos++;
       } elseif ($data[$pos] === 'i') {
           $pos++;
           $digits = strpos($data, 'e', $pos) - $pos;
           $value = substr($data, $pos, $digits);
           $return = self::checkInteger($value);
           $pos += $digits + 1;
       } else {
           $digits = strpos($data, ':', $pos) - $pos;
           $len = self::checkInteger(substr($data, $pos, $digits));
           if ($len < 0) {
               throw new Exception('Cannot have non-digit values for String length');
           }

           $pos += ($digits + 1);
           $return = substr($data, $pos, $len);

           if (strlen($return) != $len) {  // Check for String length is match or not
               throw new ErrorException('String length is not match for: ' . $return . ', want ' . $len);
           }

           $pos += $len;
       }

       if ($start_decode && $pos !== strlen($data)) {
           throw new ErrorException('Could not fully decode bencode string');
       }
       return $return;
   }

   /**
    * This private function help us filter value like `-13` `13` will pass the filter and return it's int value
    * Other value like ``,`-0`, `013`, `-013`, `2.127`, `six` will throw A ParseErrorException
    *
    * @param string $value
    * @return int
    * @throws ParseErrorException
    */
   private static function checkInteger($value)
   {
       $int = (int)$value;
       if ((string)$int !== $value) {
           throw new ErrorException('Invalid integer format or integer overflow: ' . $value);
       }
       return $int;
   }

   /**
    * Encode arbitrary data to bencode string
    *
    * @param mixed $data
    * @return string
    */
   public static function encode($data)
   {
       if (is_array($data)) {
           $return = '';
           $check = -1;
           $list = true;
           foreach ($data as $key => $value) {
               if ($key !== ++$check) {
                   $list = false;
                   break;
               }
           }
           if ($list) {
               $return .= 'l';
               foreach ($data as $value) {
                   $return .= self::encode($value);
               }
           } else {
               $return .= 'd';
               ksort($data, SORT_STRING);
               foreach ($data as $key => $value) {
                   $return .= self::encode((string)$key);
                   $return .= self::encode($value);
               }
           }
           $return .= 'e';
       } elseif (is_integer($data)) {
           $return = 'i' . $data . 'e';
       } else {
           $return = strlen($data) . ':' . $data;
       }
       return $return;
   }

   /**
    * Load data from bencoded file
    *
    * @param string $path
    * @return mixed
    * @throws ParseErrorException
    */
   public static function load($path)
   {
       return self::decode(file_get_contents($path));
   }

   /**
    * Dump data to bencoded file
    *
    * @param string $path
    * @param $data
    * @return mixed
    */
   public static function dump($path, $data)
   {
       return file_put_contents($path, self::encode($data));
   }
}


You can always remove these lines for the autolink function - exceptions will be in your log
throw new ErrorException('Invalid integer format or integer overflow: ' . $value);


in download.php it should look like this
        if ($row["external"] != 'yes' && $CURUSER) { // local torrent so add passkey
           // Bencode
           $dict = Newbencode::decode(file_get_contents($fn));
           $dict['announce'] = sprintf($config["PASSKEYURL"], $CURUSER["passkey"]);
           unset($dict['announce-list']);
           $data = Newbencode::encode($dict);

           header('Content-Disposition: attachment; filename="' . $name . '"');


in functions (scrape function)
        $stats = Newbencode::decode($fp);
       //$stats = BDecode($fp);


Then in the parse function (i dont use this but should work)
                $array = Newbencode::decode($parse);

               if ($array === false) {
                   echo "Parser Error: Error Opening torrent, unable to decode.<br>";
               } else {
                   if (array_key_exists("info", $array) === false) {
                       echo "Parser Error: Error opening torrents.<br>";
                   } else {
                       //Get Announce URL
                       $torrentInfo[0] = $array["announce"];

                       //Get Announce List Array
                       if (isset($array["announce-list"])) {
                           $torrentInfo[6] = $array["announce-list"];
                       }

                       //Read info, store as (infovariable)
                       $info = $array["info"];

                       //Calculates SHA1 Hash
                       $torrentInfo[1] = sha1(Newbencode::encode($info));
2020-11-14 21:39:41
here is how i parse for now just wanted to make it a object - for v3 you must require the file where you need it

create file Parse.php place in backend
<?php

class Parse {

   public function __construct() { }

   private function __clone() { }

   public function torr($filename = "")
   {
       $torrentInfo = array();

       $array = array();

       //check file type is a torrents
       $torrent = explode(".", $filename);
       // $fileend = end($torrent);
       $fileend = strtolower(end($torrent));

       if ($fileend == "torrent") {
           $parse = file_get_contents("$filename");

           if ($parse == false) {
               echo "Parser Error: Error Opening torrents, unable to get contents.<br>";
           }

           if (!isset($parse)) {
               echo "Parser Error: Error Opening torrent. Torrent file not chosen or could not be found.<br>";
           } else {
               $array = Newbencode::decode($parse);

               if ($array === false) {
                   echo "Parser Error: Error Opening torrent, unable to decode.<br>";
               } else {
                   if (array_key_exists("info", $array) === false) {
                       echo "Parser Error: Error opening torrents.<br>";
                   } else {
                       //Get Announce URL
                       $torrentInfo[0] = $array["announce"];

                       //Get Announce List Array
                       if (isset($array["announce-list"])) {
                           $torrentInfo[6] = $array["announce-list"];
                       }

                       //Read info, store as (infovariable)
                       $info = $array["info"];

                       //Calculates SHA1 Hash
                       $torrentInfo[1] = sha1(Newbencode::encode($info));

                       // Calculates date from UNIX Epoch
                       $torrentInfo[2] = date('r', $array["creation date"]);

                       // The name of the torrents is different to the file name
                       $torrentInfo[3] = $info['name'];

                       //Get file list
                       if (isset($info["files"]) && is_array($info["files"])) {
                           //Multi file torrents
                           $filecount = 0;
                           $torrentsize = 0;

                           //Get filenames here
                           $torrentInfo[8] = $info["files"];

                           foreach ($info["files"] as $file) {
                               $filecount = ++$filecount;
                               $torrentsize += $file['length'];
                           }

                           $torrentInfo[4] = $torrentsize;
                           $torrentInfo[5] = $filecount; //Get file count

                       } else {
                           // Single File Torrent
                           $torrentInfo[3] = $info['name'];
                           $torrentInfo[4] = $info['length'];
                           $torrentInfo[5] = 1;
                       }

                       //Get torrents comment
                       if (isset($array['comment'])) {
                           $torrentInfo[7] = $array['comment'];
                       }
                   }
               }
           }
       }
       return $torrentInfo;
       //var_dump($torrentInfo);
   }

}


in torrent-upload.php
/*
               $TorrentInfo = array();
               $TorrentInfo = ParseTorrent("$torrent_dir/$fname");

               $announce = $TorrentInfo[0];
               $infohash = $TorrentInfo[1];
               $creationdate = $TorrentInfo[2];
               $internalname = $TorrentInfo[3];
               $torrentsize = $TorrentInfo[4];
               $filecount = $TorrentInfo[5];
               $annlist = $TorrentInfo[6];
               $comment = $TorrentInfo[7];
               $filelist = $TorrentInfo[8];
*/
               $torInfo = new Parse();
               $tor = $torInfo->torr("$torrent_dir/$fname");

               $announce = $tor[0];
               $infohash = $tor[1];
               $creationdate = $tor[2];
               $internalname = $tor[3];
               $torrentsize = $tor[4];
               $filecount = $tor[5];
               $annlist = $tor[6];
               $comment = $tor[7];
               $filelist = $tor[8];
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: 8
  • Members: 0
  • Guests online: 8
  • Comments: 3
  • Messages: 1,728