From aca756ea67f1c857ec80fa4fc4dca64749bed0c1 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Sun, 11 Aug 2024 14:29:37 +0700 Subject: [PATCH] Improved `filelist.php` (#1586) * Improved filelist.php * Update filelist.tpl * Update filelist.php * Update filelist.php * Update CHANGELOG.md * Update filelist.php * Updated * Update filelist.php * Update filelist.tpl * Updated * Update filelist.php * Update filelist.php * Update filelist.php * Update filelist.php * Updated * Update filelist.tpl * Update filelist.php * Update install.php * Update install.php * Update install.php * Update install.php * Update install.php * Update install.php * Update install.php * Updated * Updated * Updated * Updated * Update filelist.php * Update filelist.tpl * Updated * Update main.php --- CHANGELOG.md | 1 + filelist.php | 165 ++++---------------------- install.php | 78 ++++++++++-- library/language/source/main.php | 7 +- styles/templates/default/filelist.tpl | 43 +++++++ 5 files changed, 140 insertions(+), 154 deletions(-) create mode 100644 styles/templates/default/filelist.tpl diff --git a/CHANGELOG.md b/CHANGELOG.md index e80da16f7..209267f27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - [CLI] TorrentPier installer ☕️ [\#1576](https://github.com/torrentpier/torrentpier/pull/1576), [\#1582](https://github.com/torrentpier/torrentpier/pull/1582), [\#1585](https://github.com/torrentpier/torrentpier/pull/1585) ([belomaxorka](https://github.com/belomaxorka)) - Added some new HTML meta-tags [\#1562](https://github.com/torrentpier/torrentpier/pull/1562) ([belomaxorka](https://github.com/belomaxorka)) - Added showing releaser stats in profile [\#1568](https://github.com/torrentpier/torrentpier/pull/1568) ([belomaxorka](https://github.com/belomaxorka)) +- Improved `filelist.php` [\#1586](https://github.com/torrentpier/torrentpier/pull/1586) ([belomaxorka](https://github.com/belomaxorka)) - Demo mode: Save user language in cookies [\#1584](https://github.com/torrentpier/torrentpier/pull/1584) ([belomaxorka](https://github.com/belomaxorka)) - Fixed `md5()` deprecated in PHP 8.4 [\#1561](https://github.com/torrentpier/torrentpier/pull/1561) ([belomaxorka](https://github.com/belomaxorka)) - Increased `USEREMAIL_MAX_LENGTH` [\#1566](https://github.com/torrentpier/torrentpier/pull/1566) ([belomaxorka](https://github.com/belomaxorka)) diff --git a/filelist.php b/filelist.php index cdf8a1fc7..3b4412928 100644 --- a/filelist.php +++ b/filelist.php @@ -7,6 +7,8 @@ * @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License */ +define('BB_SCRIPT', 'filelist'); + require __DIR__ . '/common.php'; // Start session management @@ -17,7 +19,6 @@ if ($bb_cfg['bt_disable_dht'] && IS_GUEST) { } $topic_id = isset($_GET[POST_TOPIC_URL]) ? (int)$_GET[POST_TOPIC_URL] : 0; - if (!$topic_id) { bb_simple_die($lang['INVALID_TOPIC_ID'], 404); } @@ -28,9 +29,8 @@ $sql = 'SELECT t.attach_id, t.info_hash, t.info_hash_v2, t.size, ad.physical_fil ON t.attach_id = ad.attach_id WHERE t.topic_id = ' . $topic_id . ' LIMIT 1'; -$row = DB()->fetch_row($sql); -if (empty($row['physical_filename'])) { +if (!$row = DB()->fetch_row($sql)) { bb_simple_die($lang['INVALID_TOPIC_ID_DB'], 404); } @@ -44,13 +44,11 @@ $t_files_field = $meta_v2 ? 'getFileTree' : 'getFiles'; $t_hash_field = $meta_v2 ? 'piecesRoot' : 'sha1'; $file_path = get_attachments_dir() . '/' . $row['physical_filename']; - if (!is_file($file_path)) { bb_simple_die($lang['TOR_NOT_FOUND'], 410); } $file_contents = file_get_contents($file_path); - if ($bb_cfg['flist_max_files']) { $filetree_pos = $meta_v2 ? strpos($file_contents, '9:file tree') : false; $files_pos = $meta_v1 ? strpos($file_contents, '5:files', $filetree_pos) : false; @@ -77,149 +75,34 @@ if (IS_GUEST && $torrent->isPrivate()) { } $files = $torrent->$t_version_field()->$t_files_field(); - if ($meta_v1 && $meta_v2) { $files = new \RecursiveIteratorIterator($files); // Flatten the list } -$allFiles = ''; +$files_count = 0; foreach ($files as $file) { - $allFiles .= '' . clean_tor_dirname(implode('/', $file->path)) . '' . humn_size($file->length, 2) . '' . $file->$t_hash_field . ''; + $files_count++; + $row_class = ($files_count % 2) ? 'row1' : 'row2'; + $template->assign_block_vars('filelist', [ + 'ROW_NUMBER' => $files_count, + 'ROW_CLASS' => $row_class, + 'FILE_PATH' => clean_tor_dirname(implode('/', $file->path)), + 'FILE_LENGTH' => humn_size($file->length, 2), + 'FILE_HASH' => $file->$t_hash_field ?? '-' + ]); } -$data = [ - 'name' => !empty($t_name = $torrent->getName()) ? htmlCHR(substr($t_name, 0, 255)) : 'undefined', - 'client' => !empty($creator = $torrent->getCreatedBy()) ? htmlCHR(substr($creator, 0, 20)) : 'unknown client', - 'date' => (!empty($dt = $torrent->getCreationDate()) && is_numeric($creation_date = $dt->getTimestamp())) ? date('d-M-Y H:i (e)', $creation_date) : $lang['UNKNOWN'], - 'size' => humn_size($row['size'], 2), - 'file_count' => iterator_count($files), - 'site_url' => FULL_URL, - 'topic_url' => TOPIC_URL . $topic_id, -]; +$torrent_name = !empty($t_name = $torrent->getName()) ? htmlCHR(str_short($t_name, 200)) : $lang['UNKNOWN']; +$torrent_size = humn_size($row['size'], 2); -header('Cache-Control: public, max-age=3600'); +$template->assign_vars([ + 'PAGE_TITLE' => "$torrent_name (" . $torrent_size . ")", + 'FILES_COUNT' => sprintf($lang['BT_FLIST_FILE_PATH'], declension(iterator_count($files), 'files')), + 'TORRENT_CREATION_DATE' => (!empty($dt = $torrent->getCreationDate()) && is_numeric($creation_date = $dt->getTimestamp())) ? date('d-M-Y H:i (e)', $creation_date) : $lang['UNKNOWN'], + 'TORRENT_CLIENT' => !empty($creator = $torrent->getCreatedBy()) ? htmlCHR(str_short($creator, 20)) : $lang['UNKNOWN'], -echo << - - - - - - - - - - - - - + 'BTMR_NOTICE' => sprintf($lang['BT_FLIST_BTMR_NOTICE'], 'https://github.com/kovalensky/tmrr'), + 'U_TOPIC' => TOPIC_URL . $topic_id, +]); -{$data['name']} ({$data['size']}) | {$bb_cfg['sitename']} - - - -← Back to the topic -
-

Name: {$data['name']} | Date: {$data['date']} | Size: {$data['size']}

-

-

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

-

-
- - - - - - - {$allFiles} -
Path ({$data['file_count']} files)Size - BTMR hash - ? - - BitTorrent Merkle Root is a hash of a file embedded in torrents with BitTorrent v2 support, tracker users can extract, calculate them, also download deduplicated torrents using desktop tools such as - Torrent Merkle Root Reader. - - -
-

Generated by TorrentPier

-
- - -EOF; +print_page('filelist.tpl'); diff --git a/install.php b/install.php index 8e6a165e7..e6223bb69 100644 --- a/install.php +++ b/install.php @@ -14,17 +14,10 @@ if (php_sapi_name() !== 'cli') { die('Please run php ' . basename(__FILE__) . ' in CLI mode'); } -// Check if already installed -if (is_file(BB_ROOT . '.env')) { - out('- TorrentPier already installed', 'error'); - exit; -} - /** * System requirements */ define('CHECK_REQUIREMENTS', [ - 'status' => true, 'php_min_version' => '8.1.0', 'ext_list' => [ 'json', @@ -105,6 +98,27 @@ function runProcess(string $cmd, string $input = null): void proc_close($process); } +/** + * Remove directory recursively + * + * @param string $dir + * @return void + */ +function rmdir_rec(string $dir): void +{ + $it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS); + $files = new RecursiveIteratorIterator($it, + RecursiveIteratorIterator::CHILD_FIRST); + foreach ($files as $file) { + if ($file->isDir()) { + rmdir($file->getPathname()); + } else { + unlink($file->getPathname()); + } + } + rmdir($dir); +} + /** * Setting permissions recursively * @@ -123,11 +137,11 @@ function chmod_r(string $dir, int $dirPermissions, int $filePermissions): void $fullPath = realpath($dir . '/' . $file); if (is_dir($fullPath)) { - out("- Directory: $fullPath"); + // out("- Directory: $fullPath"); chmod($fullPath, $dirPermissions); chmod_r($fullPath, $dirPermissions, $filePermissions); } elseif (is_file($fullPath)) { - out("- File: $fullPath"); + // out("- File: $fullPath"); chmod($fullPath, $filePermissions); } else { out("- Cannot find target path: $fullPath", 'error'); @@ -160,8 +174,48 @@ foreach (CHECK_REQUIREMENTS['ext_list'] as $ext) { } out("- All extensions are installed!\n", 'success'); -// Setting permissions -out("- Setting permissions for folders...", 'info'); +// Check if already installed +if (is_file(BB_ROOT . '.env')) { + out('- TorrentPier already installed', 'warning'); + echo 'Are you sure want to re-install TorrentPier? [y/N]: '; + if (readline() === 'y') { + out("\n- Re-install process started...", 'info'); + // environment + if (is_file(BB_ROOT . '.env')) { + if (unlink(BB_ROOT . '.env')) { + out('- Environment file successfully removed!'); + } else { + out('- Cannot remove environment (.env) file. Delete it manually', 'error'); + exit; + } + } + // composer.phar + if (is_file(BB_ROOT . 'composer.phar')) { + if (unlink(BB_ROOT . 'composer.phar')) { + out("- composer.phar file successfully removed!"); + } else { + out('- Cannot remove composer.phar file. Delete it manually', 'error'); + exit; + } + } + // composer dir + if (is_dir(BB_ROOT . 'vendor')) { + rmdir_rec(BB_ROOT . 'vendor'); + if (!is_dir(BB_ROOT . 'vendor')) { + out("- Composer directory successfully removed!"); + } else { + out('- Cannot remove Composer directory. Delete it manually', 'error'); + exit; + } + } + out("- Re-install process completed!\n", 'success'); + } else { + exit; + } +} + +// Applying permissions +out("- Applying permissions for folders...", 'info'); chmod_r(BB_ROOT . 'data', 0755, 0644); chmod_r(BB_ROOT . 'internal_data', 0755, 0644); chmod_r(BB_ROOT . 'sitemap', 0755, 0644); @@ -194,7 +248,7 @@ if (!is_file(BB_ROOT . 'vendor/autoload.php')) { if (is_file(BB_ROOT . 'composer.phar')) { out('- Installing dependencies...', 'info'); runProcess('php ' . BB_ROOT . 'composer.phar install --no-interaction --no-ansi'); - out("- Completed!\n", 'success'); + out("- Completed! Composer dependencies are installed!\n", 'success'); } else { out('- composer.phar not found', 'error'); exit; diff --git a/library/language/source/main.php b/library/language/source/main.php index 1fc121473..81733a4ce 100644 --- a/library/language/source/main.php +++ b/library/language/source/main.php @@ -1348,6 +1348,10 @@ $lang['BT_REG_FAIL_SAME_HASH'] = 'Another torrent with same info_hash already ['second', 'seconds'], @@ -1837,7 +1842,7 @@ $lang['DL_DL'] = 'DL'; $lang['DL_UL_SPEED'] = 'UL speed'; $lang['DL_DL_SPEED'] = 'DL speed'; $lang['DL_PORT'] = 'Port'; -$lang['DL_CLIENT'] = 'Client'; +$lang['DL_CLIENT'] = 'BitTorrent client'; $lang['DL_FORMULA'] = 'Formula: Uploaded/TorrentSize'; $lang['DL_ULR'] = 'ULR'; $lang['DL_STOPPED'] = 'stopped'; diff --git a/styles/templates/default/filelist.tpl b/styles/templates/default/filelist.tpl new file mode 100644 index 000000000..25f9a9cc1 --- /dev/null +++ b/styles/templates/default/filelist.tpl @@ -0,0 +1,43 @@ +

{PAGE_TITLE}

+
    +
  • {L_BT_FLIST_CREATION_DATE}: {TORRENT_CREATION_DATE}
  • +
  • {L_DL_CLIENT}: {TORRENT_CLIENT}
  • +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
#{FILES_COUNT}{L_SIZE}{L_BT_FLIST_BTMR_HASH}
{filelist.ROW_NUMBER}{filelist.FILE_PATH}{filelist.FILE_LENGTH}{filelist.FILE_HASH}
{BTMR_NOTICE}
+ + +
+
+
{L_TOPIC_RETURN} +
+

{CURRENT_TIME}

+

{S_TIMEZONE}

+
+
+