From f59cfe848d68ff915b806f733065dc00f67655b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B8nstantine=20Kovalensky?= <45331093+kovalensky@users.noreply.github.com> Date: Thu, 9 Nov 2023 09:04:56 +0400 Subject: [PATCH] Some code improvements for file listing (#1068) --- library/includes/file_list_v2.php | 35 ++++++++++++++++++++----------- src/Legacy/Torrent.php | 6 ++++-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/library/includes/file_list_v2.php b/library/includes/file_list_v2.php index 4ece4f1fc..23ec87eb7 100644 --- a/library/includes/file_list_v2.php +++ b/library/includes/file_list_v2.php @@ -4,6 +4,8 @@ if (!defined('BB_ROOT')) { die(basename(__FILE__)); } +$user->session_start(); + $topic_id = (int)$_GET['t']; $sql = 'SELECT t.attach_id, t.info_hash_v2, ad.physical_filename @@ -17,30 +19,39 @@ $row = DB()->fetch_row($sql); if (empty($row) || empty($row['physical_filename'])) { http_response_code(404); - die('Valid topic id is missing'); + die($lang['TOPIC_POST_NOT_EXIST']); } if (empty($row['info_hash_v2'])) { http_response_code(404); - die('Currently v2 torrents support file list displaying'); + die('Currently torrents with BitTorrent v2 support are enabled for file listing'); } -$file_contents = file_get_contents(get_attachments_dir() . '/' . $row['physical_filename']); +$file_path = get_attachments_dir() . '/' . $row['physical_filename']; + +if(!is_file($file_path)){ + die($lang['INVALID_ATTACH_ID']); +} + +$file_contents = file_get_contents($file_path); if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { - return $lang['TORFILE_INVALID']; + die($lang['TORFILE_INVALID']); } $torrent = new TorrentPier\Legacy\TorrentFileList($tor); $file_list = $torrent->fileTreeTable($tor['info']['file tree']); -$date = ''; -$name = htmlCHR($tor['info']['name'] ?? ''); -$client = htmlCHR(substr($tor['created by'] ?? 'unknown client', 0, 20)); +$data = [ + 'date' => '', + 'name' => htmlCHR($tor['info']['name'] ?? ''), + 'client' => htmlCHR(substr($tor['created by'] ?? 'unknown client', 0, 20)), + 'size' => humn_size($file_list['size']) +]; + if (isset($tor['creation date']) && is_numeric($tor['creation date'])) { - $date = date("d M Y | G:i:s T", $tor['creation date']); + $data['date'] = date("d M Y | G:i:s T", $tor['creation date']); } -$size = humn_size($file_list['size']); echo " @@ -48,7 +59,7 @@ echo " -File list — $name ($size) +File list — {$data['name']} ({$data['size']})
-

Document name: $name | Date: ($date) | Size: $size +

Document name: {$data['name']} | Date: ({$data['date']}) | Size: {$data['size']}

-

Created by: $client

+

Created by: {$data['client']}


"; diff --git a/src/Legacy/Torrent.php b/src/Legacy/Torrent.php index f750727f6..7259d5b46 100644 --- a/src/Legacy/Torrent.php +++ b/src/Legacy/Torrent.php @@ -311,11 +311,13 @@ class Torrent self::torrent_auth_check($forum_id, $torrent['poster_id']); $filename = get_attachments_dir() . '/' . $torrent['physical_filename']; - $file_contents = file_get_contents($filename); - if (!is_file($filename) || !file_exists($filename)) { + if (!is_file($filename)) { self::torrent_error_exit($lang['ERROR_NO_ATTACHMENT']); } + + $file_contents = file_get_contents($filename); + if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { self::torrent_error_exit($lang['TORFILE_INVALID']); }
PathSizeHash ?