
// Detect if item is within a directory (v1)
foreach ($torrentInfo['files'] as $file)
{
if (strstr(implode('/', $file['path']), '/')) {
echo "I am in a folder";
}
}
if (isset($info['files']) || $filecount > 1)
{
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;
}
}
<?
function torr( ? string $filename = '') : mixed
{
$torrentInfo = array();
$array = array();
$version = array();
$torrent = explode(".", $filename);
$fileend = strtolower(end($torrent));
if ($fileend == "torrent")
{
$parse = file_get_contents("$filename");
if ($parse === false)
{
echo "Error Getting Torrent file";
}
else
{
// Decode File Array
$array = Bencode::decode($parse, bigInt:
BigInt::INTERNAL);
// 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
{
$path = array();
$info = $array["info"];
$torrentInfo['name'] = $info['name'];
$torrentInfo['announce'] = isset($array["announce"]) ? $array["announce"] : null;
$torrentInfo["announce-list"] = (isset($array["announce-list"])) ? $array["announce-list"] : null;
$torrentInfo["creation date"] = isset($array["creation date"]) ? date('r', $array["creation date"]) : null;
$torrentInfo['info_hash'] = in_array(2, $version) ? hash('sha256', Bencode::encode($info)) : hash('sha1', Bencode::encode($info));
$torrentInfo['version'] = $version[0];
if (isset($info['file tree']) && isset($info['pieces']))
{
$torrentInfo['info_hash v1'] = hash('sha1', Bencode::encode($info));
$torrentInfo['info_hash v2'] = hash('sha256', Bencode::encode($info));
$torrentInfo['version'] = 'Hybrid';
unset($torrentInfo['info_hash']);
$torrentInfo['info_hash'] = $torrentInfo['info_hash v1'];
}
$tracker = !empty($array['announce']) ? array(
$array['announce']
) : null;
foreach ((new Scrape())->scrape($torrentInfo['info_hash'], $tracker) as $value)
{
$torrentInfo['seeders'] = intval($value['seeders']);
$torrentInfo['leechers'] = intval($value['leechers']);
$torrentInfo['completed'] = intval($value['completed']);
}
if ((new Scrape())->has_errors())
{
$torrentInfo['scrape error'] = (new Scrape())->get_errors();
}
$torrentInfo['files'] = $path;
if (in_array(2, $version))
{
unset($torrentInfo['files']);
array_push($path, $info);
$torrentInfo['file tree'] = $info['file tree'];
}
$filecount = 0;
$torrentsize = 0;
$torrentInfo['length'] = $torrentsize;
$torrentInfo['ttfilecount'] = $filecount;
$torrentInfo['private'] = array_key_exists('private', $info) ? 'Yes' : 'No';
$torrentInfo['signed'] = array_key_exists('signatures', $array) ? 'Yes' : 'No';
if (array_key_exists('signatures', $array))
{
foreach ($array['signatures'] as $value => $key)
{
$torrentInfo['authority'] = $value;
foreach ($key as $certinfo)
{
$filter = array(
'' => '',
'' => '',
'' => '',
'' => '',
'' => '',
'' => '',
'' => '',
'U' => '',
'6' => '',
'/' => ' ',
'0x06' => '',
'' => '',
'' => '',
'1' => '',
'8' => '',
'0' => '',
'' => '',
'2' => '',
'4' => '',
'9' => '',
'Z' => '',
'3' => '',
'b' => ''
);
$alloc = strtr($certinfo, $filter);
$torrentInfo['certificate'] = explode(' ', trim(explode("\n", $alloc) [3]) , 3) [2];
$split = explode("\n", $alloc) [3];
$sep = preg_split('/[\s,]+/', $split, 3);
$torrentInfo['issuer'] = implode(', ', array(
$sep[0],
$sep[1]
));
break;
}
}
}
if (array_key_exists('file tree', $info) || array_key_exists('files', $info))
{
// Calc file counts
if (isset($info['files']) || $filecount > 1)
{
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;
}
}
}
}
}
}
}
return print_r($torrentInfo);
}
// Calculates Hashes - SHA1/SHA256
$torrentInfo['info_hash'] = in_array(2, $version) ? hash('sha256', Bencode::encode($info)) : hash('sha1', Bencode::encode($info));
$torrentInfo['version'] = $version[0];
if (isset($info['file tree']) && isset($info['pieces'])) {
$torrentInfo['info_hash v1'] = hash('sha1', Bencode::encode($info));
$torrentInfo['info_hash v2'] = hash('sha256', Bencode::encode($info));
$torrentInfo['version'] = 'Hybrid';
unset($torrentInfo['info_hash']);
}
// Open torrent file for reading
$parse = file_get_contents('in.torrent');
$array = Bencode::decode($parse);
// BEP_35 - Torrent Signing
if (!isset($array['signatures'])) {
$certFile = file_get_contents('certificate.pem');
$keyFile = file_get_contents('private.key');
$result = openssl_x509_parse($certFile);
$keyPassphrase = 'Your_Private_Key_Passphrase';
$privatekey = openssl_get_privatekey($keyFile, $keyPassphrase);
openssl_sign($parse, $signature, $privatekey);
$signatures = array(
'signatures' => array(
$result['subject']['CN'] => array(
'certificate' => null, // <--- we need to now deal with this
'info' => 'Optional',
'signature' => $signature
)
)
);
$array = array_merge($array, $signatures);
array_merge($result, $array);
file_put_contents('out.torrent', Bencode::encode($array));
} else {
echo 'torrent is already signed.';
}
// Test
echo '<pre>';
print_r($array);
echo '</pre>';
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")
{
$parse = file_get_contents("$filename");
// If Parse Lets Read It
if ($parse === false)
{
echo "Error Getting Torrent file";
}
else
{
// Decode File Array
$array = Bencode::decode($parse, bigInt:
BigInt::INTERNAL);
// 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
{
$path = array();
$data['announce'] = isset($array["announce"]) ? $array["announce"] : null;
// 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['version'] = $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));
$torrentInfo['info_hash v1'] = hash('sha1', Bencode::encode($info));
$torrentInfo['info_hash v2'] = hash('sha256', Bencode::encode($info));
// Scrape
$tracker = !empty($array['announce']) ? array(
$array['announce']
) : null;
$hash = array(
hash('sha1', Bencode::encode($info))
);
foreach ((new Scrape())->scrape($hash, $tracker) as $value)
{
$torrentInfo['seeders'] = intval($value['seeders']);
$torrentInfo['leechers'] = intval($value['leechers']);
$torrentInfo['completed'] = intval($value['completed']);
}
if ((new Scrape())->has_errors())
{
$torrentInfo['scrape error'] = (new Scrape())->get_errors();
}
// 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;
}
$torrentInfo['private'] = array_key_exists('private', $info) ? 'Yes' : 'No';
$torrentInfo['signed'] = array_key_exists('signatures', $array) ? 'Yes' : 'No';
if (array_key_exists('signatures', $array))
{
foreach ($array['signatures'] as $value => $key)
{
$torrentInfo['authority'] = $value;
foreach ($key as $certinfo)
{
$filter = array(
'' => '',
'' => '',
'' => '',
'' => '',
'' => '',
'' => '',
'' => '',
'U' => '',
'6' => '',
'/' => ' ',
'0x06' => '',
'' => '',
'' => '',
'1' => '',
'8' => '',
'0' => '',
'' => '',
'2' => '',
'4' => '',
'9' => '',
'Z' => '',
'3' => '',
'b' => ''
);
$alloc = strtr($certinfo, $filter);
$torrentInfo['certificate'] = explode(' ', trim(explode("\n", $alloc) [3]) , 3) [2];
$split = explode("\n", $alloc) [3];
$sep = preg_split('/[\s,]+/', $split, 3);
$torrentInfo['issuer'] = implode(', ', array(
$sep[0],
$sep[1]
));
break;
}
}
}
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 print_r($torrentInfo);
}
require 'Scraper.php';