noticed the files were not insert into database correctly because there if a difference between the return file/filetree array so now i make a array for v2 but in the old v1 way, if this can be improved please help out.
For those on v3 all i have done is edit the backend/parse.php we still need to check announce but this takes care of uploading v2 torrents to the site
my codes different i use it in a class but its pretty easy to walk through the difference
in the parse function i changed $torrentinfo[?] to have named keys to make it easier to read, if you do this you would need to change the same in import.php & upload.php or leave as numbers which ever.
public function Parse($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 Lets Read It
if (!isset($parse)) {
Redirect::autolink(URLROOT, Lang::T("Error Getting Torrent file"));
} else {
// Decode File Array
$array = Bencode::decode($parse);
// If Array Lets Read It
if ($array === false) {
Redirect::autolink(URLROOT, Lang::T("Unable to decode file."));
} else {
if (array_key_exists("info", $array) === false) {
Redirect::autolink(URLROOT . "/index", Lang::T("Error opening torrent.<br>"));
} else {
//Get Announce URL
$torrentInfo['announce'] = $array["announce"];
//Get Announce List Array
if (isset($array["announce-list"])) {
$torrentInfo["announce-list"] = $array["announce-list"];
}
// Calculates date from UNIX Epoch
$torrentInfo["creation date"] = date('r', $array["creation date"]);
//Get torrents comment
if (isset($array['comment'])) {
$torrentInfo['comment'] = $array['comment'];
}
// Read info - files
$info = $array["info"];
// The name of the torrents is different to the file name
$torrentInfo['name'] = $info['name'];
// Calculate Size/Count
$filecount = 0;
$torrentsize = 0;
// Get file list
if (isset($info["files"]) && is_array($info["files"])) {
// Calculates SHA1 Hash
$torrentInfo['hash'] = hash('sha1', Bencode::encode($info));
// Return Marker To identify as v1
$torrentInfo['type'] = '1';
//Get files here
$torrentInfo['files'] = $info["files"];
foreach ($info["files"] as $file) {
$filecount = ++$filecount;
$torrentsize += $file['length'];
}
$torrentInfo['length'] = $torrentsize;
$torrentInfo['ttfilecount'] = $filecount;
} elseif (isset($info["file tree"]) && is_array($info["file tree"])) {
// Calculates SHA256 Hash
$torrentInfo['hash'] = hash('sha256', Bencode::encode($info));
// Return Marker To identify as v2
$torrentInfo['type'] = '2';
//Get files here
$torrentInfo['files'] = $info["file tree"];
//$torrentInfo['files'] = array("path"=>$info["file tree"], "length"=>$info["file tree"][$torrentInfo['name']][$torrentInfo['length']]);
foreach ($info["file tree"] as $key => $value) {
$test = array_flatten($torrentInfo['files']);
$filecount = ++$filecount;
$torrentsize += $test['length'];
$newfiles[] = [
'path' => array($key),
'length' => $test['length']
];
}
// Replace File Stucture
$torrentInfo['files'] = $newfiles;
$torrentInfo['length'] = $torrentsize;
$torrentInfo['ttfilecount'] = $filecount;
} else {
// Single File Torrent
$torrentInfo['name'] = $info['name'];
$torrentInfo['length'] = $info['length'];
$torrentInfo['ttfilecount'] = 1;
}
}
}
}
}
return $torrentInfo;
//var_dump($torrentInfo);
}
i also used this function so add above parse function on parse.php
//convert multi to single array
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
Last edited by M-jay
on 2022-01-16 07:10:10