diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index b4122781..5fba34f1 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -31,7 +31,7 @@ class BaseRepository return $perPage; } - protected function handleAnonymous($username, $user, User $authenticator, Torrent $torrent = null) + protected function handleAnonymous($username, $user, User $authenticator, ?Torrent $torrent = null) { if (!$user) { return ""; diff --git a/nexus/Database/NexusDB.php b/nexus/Database/NexusDB.php index 54dfe344..804956b1 100644 --- a/nexus/Database/NexusDB.php +++ b/nexus/Database/NexusDB.php @@ -568,4 +568,26 @@ class NexusDB } } + public static function binaryField(string $field): string + { + if (self::isMysql()) { + return sprintf("%s = :%s", $field, $field); + } elseif (self::isPgsql()) { + return sprintf("%s = decode(:%s, 'hex')", $field, $field); + } else { + throw new \RuntimeException('Not supported database.'); + } + } + + public static function binaryFieldBindValue($value): string + { + if (self::isMysql()) { + return $value; + } elseif (self::isPgsql()) { + return bin2hex($value); + } else { + throw new \RuntimeException('Not supported database.'); + } + } + } diff --git a/public/announce.php b/public/announce.php index bf691872..617a5b11 100644 --- a/public/announce.php +++ b/public/announce.php @@ -173,10 +173,12 @@ elseif ($az['showclienterror'] == 'yes'){ // check torrent based on info_hash $tsField = \Nexus\Database\NexusDB::unixTimestampField('added'); -$checkTorrentSql = "SELECT torrents.id, size, owner, sp_state, seeders, leechers, times_completed, $tsField AS ts, added, banned, hr, approval_status, price, categories.mode FROM torrents left join categories on torrents.category = categories.id WHERE info_hash = decode(:info_hash, 'hex') limit 1"; +$infoHashField = \Nexus\Database\NexusDB::binaryField('info_hash'); +$infoHashFieldBindValue = \Nexus\Database\NexusDB::binaryFieldBindValue($info_hash); +$checkTorrentSql = "SELECT torrents.id, size, owner, sp_state, seeders, leechers, times_completed, $tsField AS ts, added, banned, hr, approval_status, price, categories.mode FROM torrents left join categories on torrents.category = categories.id WHERE $infoHashField limit 1"; if (!$torrent = $Cache->get_value('torrent_hash_'.$info_hash.'_content')){ $res = mysql_prepare($checkTorrentSql); - $res->execute(['info_hash' => bin2hex($info_hash)]); + $res->execute(['info_hash' => $infoHashFieldBindValue]); $torrent = mysql_fetch_array($res); $Cache->cache_value('torrent_hash_'.$info_hash.'_content', $torrent, 350); } @@ -290,12 +292,14 @@ if (isset($event) && $event == "stopped") { } } } -$selfwhere = "torrent = $torrentid AND peer_id = decode(:peer_id, 'hex') AND userid = $userid"; +$peerIdField = \Nexus\Database\NexusDB::binaryField('peer_id'); +$peerIdFieldBindValue = \Nexus\Database\NexusDB::binaryFieldBindValue($peer_id); +$selfwhere = "torrent = $torrentid AND $peerIdField AND userid = $userid"; //no found in the above random selection if (!isset($self)) { $res = mysql_prepare("SELECT $fields FROM peers WHERE $selfwhere LIMIT 1"); - $res->execute(['peer_id' => bin2hex($peer_id)]); + $res->execute(['peer_id' => $peerIdFieldBindValue]); $row = mysql_fetch_assoc($res); if ($row) {