From 1d9dfb5e37114d0a36f5b31fbc9e8b7c7fc76b80 Mon Sep 17 00:00:00 2001 From: Anthony Ryan Date: Mon, 11 Nov 2024 12:19:05 -0500 Subject: [PATCH] Convert recentTrackers->last_used to an array (#2783) Previously, we were storing a string, and if a user created 20 torrents in a row, it would add 20 trailing carriage returns (\r) to the box. We already do the logic of stripping empty lines for other purposes, lets reuse that here. Add de-duplication of last used trackers (to match the list array). Also eliminate $announce_list, because it's never used, and `grep -rn announce_list * | grep global` indicates no other function is using it without it being passed. --- plugins/create/action.php | 34 +++++++++++++++++----------------- plugins/create/init.js | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/plugins/create/action.php b/plugins/create/action.php index 68eeaca02..8c4c064c1 100644 --- a/plugins/create/action.php +++ b/plugins/create/action.php @@ -10,7 +10,7 @@ class recentTrackers public $hash = "rtrackers.dat"; public $modified = false; public $list = array(); - public $last_used = ""; + public $last_used = array(); public $piece_size = 1024; public $start_seeding = false; public $private_torrent = false; @@ -21,6 +21,14 @@ static public function load() $cache = new rCache(); $rt = new recentTrackers(); $cache->get($rt); + + // TODO: Temporary migration from string to array + // Can remove this after November 2026 + if (is_string(($rt->last_used))) + { + $rt->last_used = []; + } + return($rt); } public function store() @@ -51,6 +59,7 @@ public function get() public function strip() { global $recentTrackersMaxCount; + $this->last_used = array_values( array_unique($this->last_used) ); $this->list = array_values( array_unique($this->list) ); $cnt = count($this->list); $arr = array_values($this->list); @@ -131,38 +140,29 @@ static public function getTrackerDomain($announce) { $rt = recentTrackers::load(); $trackers = array(); - $announce_list = ''; if(isset($_REQUEST['trackers'])) { - $rt->last_used = $_REQUEST['trackers']; // remember trackers last used - $arr = explode("\r",$_REQUEST['trackers']); + $arr = explode("\r", $_REQUEST['trackers']); foreach( $arr as $key => $value ) { $value = trim($value); - if(strlen($value)) + if(strlen($value) === 0) { - $trackers[] = $value; - $rt->list[] = $value; - } - else - { - if(count($trackers)>0) - { - $announce_list .= (' -a '.escapeshellarg(implode(',',$trackers))); - $trackers = array(); - } + continue; } + + $trackers[] = $value; + $rt->list[] = $value; } } // remember checkbox and dropdown options + $rt->last_used = $trackers; $rt->piece_size = $_REQUEST['piece_size']; $rt->start_seeding = $_REQUEST['start_seeding']; $rt->private_torrent = $_REQUEST['private']; $rt->hybrid_torrent = $_REQUEST['hybrid']; $rt->store(); - if(count($trackers)>0) - $announce_list .= (' -a '.escapeshellarg(implode(',',$trackers))); $piece_size = 262144; if(isset($_REQUEST['piece_size'])) $piece_size = $_REQUEST['piece_size']*1024; diff --git a/plugins/create/init.js b/plugins/create/init.js index c09a9c358..6ed03a67f 100644 --- a/plugins/create/init.js +++ b/plugins/create/init.js @@ -226,7 +226,7 @@ plugin.onLangLoaded = function() { theDialogManager.setHandler("tcreate", "beforeShow", () => { if (plugin.recentTrackers) { const recent = plugin.recentTrackers; - $("#trackers").val(recent.last_used); + $("#trackers").val(recent.last_used.join("\r\n")); $(`#piece_size option[value=${recent.piece_size || 1024}]`).prop("selected", true); $("#start_seeding").prop("checked", iv(recent.start_seeding)); $("#private").prop("checked", iv(recent.private_torrent));