Changelog - Feb 11/22:
- Shorter code
- Fixed hash detection using version array check (confirmed - qBittorrent v4.4.0)
- Fixed incorrect count caused by extra padding data
- Reduced long checks with shorter logic
- Correctly check version using 'meta version' tree item (specification)
- Checks correct version for file list iteration to handle all cases
- Replaced file_get_contents with low-level and binary-safe file reading
- Shorter code
- Fixed hash detection using version array check (confirmed - qBittorrent v4.4.0)
- Fixed incorrect count caused by extra padding data
- Reduced long checks with shorter logic
- Correctly check version using 'meta version' tree item (specification)
- Checks correct version for file list iteration to handle all cases
- Replaced file_get_contents with low-level and binary-safe file reading
<?
function torr(string $filename = ''): mixed
{
// Array storage
$torrentInfo = array();
$array = array();
// Torrent Version
$version = array();
// Torrent filename
$torrent = explode(".", $filename);
$fileend = strtolower(end($torrent));
if ($fileend == "torrent") {
$fp = fopen($filename, 'rb');
$parse = '';
while (!feof($fp)) {
$parse .= fread($fp, 8192);
}
fclose($fp);
// If Parse Lets Read It
if ($parse === false) {
echo "Error Getting Torrent file";
} else {
// Decode File Array
$array = Bencode::decode($parse);
// Push version to Array
array_push($version, (array_key_exists('meta version', $array['info']) ? intval($array['info']['meta version']) : 1));
// If Array Lets Read It
if (empty($array)) {
die('Unable to decode metadata.');
} else {
if (array_key_exists('info', $array) !== true) {
die('Error opening torrent');
} else {
// Get Announce URL
$torrentInfo['announce'] = isset($array["announce"]) ? $array["announce"] : null;
// Get Announce List Array
$torrentInfo["announce-list"] = (isset($array["announce-list"])) ? $array["announce-list"] : null;
// Calculates date from UNIX Epoch - You want to return NULL on this if empty to pass parse validation
$torrentInfo["creation date"] = isset($array["creation date"]) ? date('r', $array["creation date"]) : null;
// Get torrents comment
$torrentInfo['comment'] = (isset($array['comment'])) ? $array['comment'] : null;
// Read info - files
$info = $array["info"];
// The name of the torrents is different to the file name
$torrentInfo['name'] = $info['name'];
// Read the torrent version stored in array previously
$torrentInfo['type'] = $version[0];
// Calculate Size/Count
$filecount = 0;
$torrentsize = 0;
// Calculates Hashes - SHA1/SHA256
$torrentInfo['hash'] = in_array(2, $version) ? hash('sha256', Bencode::encode($info)) : hash('sha1', Bencode::encode($info));
$path = array();
// File List
$struct = in_array(2, $version) ? $info['file tree'] : $info['files'];
if (array_key_exists('file tree', $info)) {
foreach ($struct as $key => $value) {
foreach ($value as $detail) {
$filecount = ++$filecount;
$torrentsize += $detail['length'];
array_merge($path, array(
'length' => $detail['length']
));
}
array_push($path, array(
'length' => $detail['length'],
'path' => array(
sizeof($value) - 1 => $key
)
));
}
$torrentInfo['files'] = $path;
$torrentInfo['length'] = $torrentsize;
$torrentInfo['ttfilecount'] = $filecount;
}
if (array_key_exists('file tree', $info) || array_key_exists('files', $info)) {
if (isset($info['files']) || $filecount > 1) {
// Multi File
if (isset($info['files']) && is_array($info['files'])) {
$torrentInfo['files'] = $info['files'];
foreach ($info['files'] as $file) {
$filecount = ++$filecount;
$torrentsize += $file['length'];
}
$torrentInfo['length'] = $torrentsize;
$torrentInfo['ttfilecount'] = !empty($path) ? count($path) : $filecount;
}
} else {
if ($filecount === 1) {
// Single File - Remove or Use
}
}
}
}
}
}
}
return $torrentInfo;
}
Last edited by STorrents2020 on 2022-02-12 06:20:22
