From 5d872e35557bc3d4a9a9cebf3c794c81e1fb31e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B8nstantine=20Kovalensky?= <45331093+kovalensky@users.noreply.github.com> Date: Sat, 4 Nov 2023 09:56:31 +0400 Subject: [PATCH] Fix issues related to file list display and torrent registration (#1028) Bencoding libraries properties changed in the new version --- bt/announce.php | 2 +- library/ajax/view_torrent.php | 10 ++++------ src/Legacy/Torrent.php | 24 +++++++++++++----------- src/Legacy/TorrentFileList.php | 7 ++++++- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/bt/announce.php b/bt/announce.php index 4da30da43..83eb219ee 100644 --- a/bt/announce.php +++ b/bt/announce.php @@ -132,7 +132,7 @@ $peer_hash = hash('xxh128', $passkey . $info_hash_hex . $port); // Events $stopped = ($event === 'stopped'); -// Get the real port to help NAT users +// Get the real port to help some NAT users $port = $_SERVER['REMOTE_PORT']; // Set seeder & complete diff --git a/library/ajax/view_torrent.php b/library/ajax/view_torrent.php index 46ac44b8a..42ed7e971 100644 --- a/library/ajax/view_torrent.php +++ b/library/ajax/view_torrent.php @@ -32,14 +32,12 @@ if (!file_exists($filename) || !$file_contents = file_get_contents($filename)) { } } -if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents)) { +if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { return $lang['TORFILE_INVALID']; } $torrent = new TorrentPier\Legacy\TorrentFileList($tor); -if (($tor['info']['meta version'] ?? null) == 2) { - $tor_filelist = $torrent->fileTreeList($tor['info']['file tree'], $tor['info']['name'] ?? ''); // v2 -} else { - $tor_filelist = $torrent->get_filelist(); // v1 -} + +$tor_filelist = $torrent->get_filelist(); + $this->response['html'] = $tor_filelist; diff --git a/src/Legacy/Torrent.php b/src/Legacy/Torrent.php index d6d770f3e..5f929ba81 100644 --- a/src/Legacy/Torrent.php +++ b/src/Legacy/Torrent.php @@ -316,7 +316,7 @@ class Torrent if (!is_file($filename) || !file_exists($filename)) { self::torrent_error_exit($lang['ERROR_NO_ATTACHMENT']); } - if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents)) { + if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { self::torrent_error_exit($lang['TORFILE_INVALID']); } @@ -348,26 +348,27 @@ class Torrent } // Check if torrent contains info_hash v2 or v1 - $bt_v1 = $bt_v2 = false; - if (($info['meta version'] ?? null) == 2 && is_array($info['file tree'] ?? null)) { - $bt_v2 = true; + if (($info['meta version'] ?? null) == 2 ) { + if (is_array($info['file tree'] ?? null)) { + $bt_v2 = true; + } } if (isset($info['pieces'])) { $bt_v1 = true; } - if ($bb_cfg['tracker']['disabled_v2_torrents'] && $bt_v2 && !$bt_v1) { + if ($bb_cfg['tracker']['disabled_v2_torrents'] && isset($bt_v2) && !isset($bt_v1)) { self::torrent_error_exit('v2-only torrents were disabled, allowed: v1 and hybrids'); } // Getting info_hash v1 - if ($bt_v1) { + if (isset($bt_v1)) { $info_hash = pack('H*', hash('sha1', \Arokettu\Bencode\Bencode::encode($info))); $info_hash_sql = rtrim(DB()->escape($info_hash), ' '); $info_hash_where = "WHERE info_hash = '$info_hash_sql'"; } // Getting info_hash v2 - if ($bt_v2) { + if (isset($bt_v2)) { $v2_hash = hash('sha256', \Arokettu\Bencode\Bencode::encode($info)); $info_hash_v2 = pack('H*', $v2_hash); $info_hash_v2_sql = rtrim(DB()->escape($info_hash_v2), ' '); @@ -386,17 +387,18 @@ class Torrent } $totallen = 0; + if (isset($info['length'])) { $totallen = (float)$info['length']; - } elseif ($bt_v1 && isset($info['files']) && \is_array($info['files'])) { + } elseif (isset($bt_v1) && isset($info['files']) && \is_array($info['files'])) { foreach ($info['files'] as $fn => $f) { // Exclude padding files if (($f['attr'] ?? null) !== 'p') { $totallen += (float)$f['length']; } } - } elseif ($bt_v2) { - $fileTreeSize = function ($array, string $name = '') use (&$fileTreeSize) { + } elseif (isset($bt_v2)) { + $fileTreeSize = function (array $array, string $name = '') use (&$fileTreeSize) { $size = 0; foreach ($array as $key => $value) { @@ -543,7 +545,7 @@ class Torrent $ann_url = $bb_cfg['bt_announce_url']; $file_contents = file_get_contents($filename); - if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents)) { + if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { bb_die($lang['TORFILE_INVALID']); } diff --git a/src/Legacy/TorrentFileList.php b/src/Legacy/TorrentFileList.php index 2f9d91190..682b449df 100644 --- a/src/Legacy/TorrentFileList.php +++ b/src/Legacy/TorrentFileList.php @@ -42,6 +42,11 @@ class TorrentFileList public function get_filelist() { global $html; + if (($this->tor_decoded['info']['meta version'] ?? null) == 2) { + if (is_array($this->tor_decoded['info']['file tree'] ?? null)) { + return $this->fileTreeList($this->tor_decoded['info']['file tree']); //v2 + } + } $this->build_filelist_array(); @@ -132,7 +137,7 @@ class TorrentFileList * @param string $name * @return string */ - public function fileTreeList($array, string $name = ''): string + public function fileTreeList(array $array, string $name = ''): string { $folders = []; $rootFiles = [];