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
You can always remove these lines for the autolink function - exceptions will be in your log
in download.php it should look like this
in functions (scrape function)
Then in the parse function (i dont use this but should work)
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));
