From 00fdc2d08f0776ae9c94f31cd997695a31af804d Mon Sep 17 00:00:00 2001 From: xiaomlove <1939737565@qq.com> Date: Wed, 15 Apr 2026 03:27:30 +0700 Subject: [PATCH] support pg --- app/Models/Faq.php | 9 +++++++ app/Models/RegImage.php | 13 ++++++++++ app/Models/Torrent.php | 24 ++++++++++++++++++- app/Repositories/DashboardRepository.php | 3 ++- .../Captcha/Drivers/ImageCaptchaDriver.php | 16 +++++-------- include/functions.php | 13 +++++++--- nexus/Database/NexusDB.php | 24 +++++++++++++++++-- nexus/Field/Field.php | 9 ++++++- nexus/Install/Install.php | 18 +------------- public/faq.php | 10 ++++---- public/forums.php | 2 +- public/staff.php | 2 +- public/takeupload.php | 2 +- public/topten.php | 21 ++++++++++------ public/userdetails.php | 16 +++++++------ resources/lang/en/dashboard.php | 2 +- resources/lang/zh_CN/dashboard.php | 2 +- resources/lang/zh_TW/dashboard.php | 2 +- 18 files changed, 129 insertions(+), 59 deletions(-) create mode 100644 app/Models/Faq.php create mode 100644 app/Models/RegImage.php diff --git a/app/Models/Faq.php b/app/Models/Faq.php new file mode 100644 index 00000000..ab23aad8 --- /dev/null +++ b/app/Models/Faq.php @@ -0,0 +1,9 @@ + ['text' => 'Windows-vy'], ]; + public function scopeWhereInfoHash($query, string $binaryHash) + { + if (NexusDB::isPgsql()) { + return $query->whereRaw( + "info_hash = decode(?, 'hex')", + [bin2hex($binaryHash)] + ); + } elseif (NexusDB::isMysql()) { + return $query->where('info_hash', $binaryHash); + } + throw new \RuntimeException("Not supported database"); + } + public function getPickInfoAttribute() { $info = self::$pickTypes[$this->picktype] ?? null; @@ -521,8 +535,16 @@ class Torrent extends NexusModel public function tags(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { + $idsString = TagRepository::getOrderByFieldIdString(); + if (NexusDB::isPgsql()) { + $orderByRaw = "array_position(ARRAY[$idsString]::int[], tags.id)"; + } else if (NexusDB::isMysql()) { + $orderByRaw = "FIELD(tags.id, $idsString)"; + } else { + throw new \RuntimeException("Unsupported database"); + } return $this->belongsToMany(Tag::class, 'torrent_tags', 'torrent_id', 'tag_id') - ->orderByRaw(sprintf("field(`tags`.`id`,%s)", TagRepository::getOrderByFieldIdString())); + ->orderByRaw($orderByRaw); } public function reward_logs(): \Illuminate\Database\Eloquent\Relations\HasMany diff --git a/app/Repositories/DashboardRepository.php b/app/Repositories/DashboardRepository.php index af69e9b6..3fbaa60b 100644 --- a/app/Repositories/DashboardRepository.php +++ b/app/Repositories/DashboardRepository.php @@ -47,10 +47,11 @@ class DashboardRepository extends BaseRepository 'value' => PHP_VERSION, ]; $name = 'mysql_version'; + $databaseInfo = NexusDB::getDatabaseVersionInfo(); $result[$name] = [ 'name' => $name, 'text' => nexus_trans("dashboard.system_info.$name"), - 'value' => NexusDB::select('select version() as info')[0]['info'], + 'value' => sprintf("%s: %s", $databaseInfo['dbType'], $databaseInfo['version']), ]; // $name = 'os'; // $result[$name] = [ diff --git a/app/Services/Captcha/Drivers/ImageCaptchaDriver.php b/app/Services/Captcha/Drivers/ImageCaptchaDriver.php index 05adf6bb..127544be 100644 --- a/app/Services/Captcha/Drivers/ImageCaptchaDriver.php +++ b/app/Services/Captcha/Drivers/ImageCaptchaDriver.php @@ -2,6 +2,7 @@ namespace App\Services\Captcha\Drivers; +use App\Models\RegImage; use App\Services\Captcha\CaptchaDriverInterface; use App\Services\Captcha\Exceptions\CaptchaValidationException; @@ -67,16 +68,11 @@ class ImageCaptchaDriver implements CaptchaDriverInterface $random = random_str(); $imagehash = md5($random); $dateline = time(); - - $sql = sprintf( - "INSERT INTO `regimages` (`imagehash`, `imagestring`, `dateline`) VALUES ('%s', '%s', '%s')", - mysql_real_escape_string($imagehash), - mysql_real_escape_string($random), - mysql_real_escape_string((string) $dateline) - ); - - sql_query($sql); - + RegImage::query()->insert([ + 'imagehash' => $imagehash, + 'dateline' => $dateline, + 'imagestring' => $random, + ]); return $imagehash; } diff --git a/include/functions.php b/include/functions.php index 9623956d..c6680524 100644 --- a/include/functions.php +++ b/include/functions.php @@ -3303,7 +3303,7 @@ function pager($rpp, $count, $href, $opts = array(), $pagename = "page") { $start = $page * $rpp; $add_key_shortcut = key_shortcut($page,$pages-1); - return array($pagertop, $pagerbottom, "LIMIT $start,$rpp", $start, $rpp, $page); + return array($pagertop, $pagerbottom, "limit $rpp offset $start", $start, $rpp, $page); } function commenttable($rows, $type, $parent_id, $review = false) @@ -6146,7 +6146,7 @@ function calculate_seed_bonus($uid, $torrentIdArr = null): array $idStr = implode(',', \Illuminate\Support\Arr::wrap($torrentIdArr)); $sql = "select torrents.id, torrents.added, torrents.size, torrents.seeders, 'NO_PEER_ID' as peerID, '' as last_action, '' as ip from torrents WHERE id in ($idStr) and size >= $minSize"; } else { - $sql = "select torrents.id, torrents.added, torrents.size, torrents.seeders, peers.id as peerID, peers.last_action, peers.ip from torrents LEFT JOIN peers ON peers.torrent = torrents.id WHERE peers.userid = $uid AND peers.seeder ='yes' and torrents.size > $minSize group by peers.torrent, peers.peer_id"; + $sql = "select torrents.id, torrents.added, torrents.size, torrents.seeders, peers.id as peerID, peers.last_action, peers.ip from torrents LEFT JOIN peers ON peers.torrent = torrents.id WHERE peers.userid = $uid AND peers.seeder ='yes' and torrents.size > $minSize group by torrents.id, peers.id"; } $tagGrouped = []; $torrentResult = \Nexus\Database\NexusDB::select($sql); @@ -6161,7 +6161,14 @@ function calculate_seed_bonus($uid, $torrentIdArr = null): array $officialAdditionalFactor = \App\Models\Setting::get('bonus.official_addition'); $zeroBonusTag = \App\Models\Setting::get('bonus.zero_bonus_tag'); $zeroBonusFactor = \App\Models\Setting::get('bonus.zero_bonus_factor'); - $userMedalResult = \Nexus\Database\NexusDB::select("select round(sum(bonus_addition_factor), 5) as factor from medals where id in (select medal_id from user_medals where uid = $uid and (expire_at is null or expire_at > '$nowStr') and (bonus_addition_expire_at is null or bonus_addition_expire_at > '$nowStr'))"); + if (\Nexus\Database\NexusDB::isMysql()) { + $factorField = "round(sum(bonus_addition_factor), 5)"; + } elseif (\Nexus\Database\NexusDB::isPgsql()) { + $factorField = "round(sum(bonus_addition_factor)::numeric, 5)"; + } else { + throw new \RuntimeException("Not supported database"); + } + $userMedalResult = \Nexus\Database\NexusDB::select("select $factorField as factor from medals where id in (select medal_id from user_medals where uid = $uid and (expire_at is null or expire_at > '$nowStr') and (bonus_addition_expire_at is null or bonus_addition_expire_at > '$nowStr'))"); $medalAdditionalFactor = floatval($userMedalResult[0]['factor'] ?? 0); do_log("$logPrefix, sql: $sql, count: " . count($torrentResult) . ", officialTag: $officialTag, officialAdditionalFactor: $officialAdditionalFactor, zeroBonusTag: $zeroBonusTag, zeroBonusFactor: $zeroBonusFactor, medalAdditionalFactor: $medalAdditionalFactor"); $last_action = ""; diff --git a/nexus/Database/NexusDB.php b/nexus/Database/NexusDB.php index 3f23df0d..86f38930 100644 --- a/nexus/Database/NexusDB.php +++ b/nexus/Database/NexusDB.php @@ -164,9 +164,9 @@ class NexusDB if (!IN_NEXUS) { return DB::table($table)->insertGetId($data); } - $fields = array_map(function ($value) {return "`$value`";}, array_keys($data)); + $fields = array_map(function ($value) {return "$value";}, array_keys($data)); $values = array_map(function ($value) {return sqlesc($value);}, array_values($data)); - $sql = sprintf("insert into `%s` (%s) values (%s)", $table, implode(', ', $fields), implode(', ', $values)); + $sql = sprintf("insert into %s (%s) values (%s)", $table, implode(', ', $fields), implode(', ', $values)); sql_query($sql); return mysql_insert_id(); } @@ -486,4 +486,24 @@ class NexusDB return $indexesNames; } + public static function getDatabaseVersionInfo(): array + { + if (self::isMysql()) { + $sql = 'select version() as v'; + $result = NexusDB::select($sql); + $version = $result[0]['v']; + $minVersion = '5.7.8'; + } else if (self::isPgsql()) { + $sql = 'SHOW server_version;'; + $result = NexusDB::select($sql); + $version = $result[0]['server_version']; + $minVersion = '16.0'; + } else { + throw new \RuntimeException('Not supported database.'); + } + $dbType = self::getConnectionName(); + $match = version_compare($version, $minVersion, '>='); + return compact('version', 'match', 'minVersion', 'dbType'); + } + } diff --git a/nexus/Field/Field.php b/nexus/Field/Field.php index fbf83555..1752713d 100644 --- a/nexus/Field/Field.php +++ b/nexus/Field/Field.php @@ -384,7 +384,14 @@ JS; $torrentIdArr = [$torrentId]; } $torrentIdStr = implode(',', $torrentIdArr); - $res = sql_query("select f.*, v.custom_field_value, v.torrent_id from torrents_custom_field_values v inner join torrents_custom_fields f on v.custom_field_id = f.id inner join searchbox box on box.id = $searchBoxId and find_in_set(f.id, box.custom_fields) where torrent_id in ($torrentIdStr) order by f.priority desc"); + if (NexusDB::isMysql()) { + $customFieldStr = "find_in_set(f.id, box.custom_fields)"; + } elseif (NexusDB::isPgsql()) { + $customFieldStr = "f.id = ANY(string_to_array(box.custom_fields, ',')::int[])"; + } else { + throw new \RuntimeException("Not supported database"); + } + $res = sql_query("select f.*, v.custom_field_value, v.torrent_id from torrents_custom_field_values v inner join torrents_custom_fields f on v.custom_field_id = f.id inner join searchbox box on box.id = $searchBoxId and $customFieldStr where torrent_id in ($torrentIdStr) order by f.priority desc"); $values = []; $result = []; while ($row = mysql_fetch_assoc($res)) { diff --git a/nexus/Install/Install.php b/nexus/Install/Install.php index 1ce04c54..680e8cea 100644 --- a/nexus/Install/Install.php +++ b/nexus/Install/Install.php @@ -733,23 +733,7 @@ class Install public function getDatabaseVersionInfo(): array { - if (NexusDB::isMysql()) { - $sql = 'select version() as v'; - $result = NexusDB::select($sql); - $version = $result[0]['v']; - $minVersion = '5.7.8'; - $dbType = "mysql"; - } else if (NexusDB::isPgsql()) { - $sql = 'SHOW server_version;'; - $result = NexusDB::select($sql); - $version = $result[0]['server_version']; - $minVersion = '16.0'; - $dbType = "pgsql"; - } else { - throw new \RuntimeException('Not supported database.'); - } - $match = version_compare($version, $minVersion, '>='); - return compact('version', 'match', 'minVersion', 'dbType'); + return NexusDB::getDatabaseVersionInfo(); } public function getRedisVersionInfo(): array diff --git a/public/faq.php b/public/faq.php index b39459ff..0846d28b 100644 --- a/public/faq.php +++ b/public/faq.php @@ -22,15 +22,17 @@ $is_rulelang = get_single_value("language","rule_lang","WHERE id = ".sqlesc($lan if (!$is_rulelang){ $lang_id = 6; //English } -$res = sql_query("SELECT `id`, `link_id`, `question`, `flag` FROM `faq` WHERE `type`='categ' AND `lang_id` = ".sqlesc($lang_id)." ORDER BY `order` ASC"); -while ($arr = mysql_fetch_array($res)) { +//$res = sql_query("SELECT id, link_id, question, flag FROM faq WHERE type='categ' AND lang_id = ".sqlesc($lang_id)." ORDER BY order ASC"); +$res = \App\Models\Faq::query()->where('type', 'categ')->where('lang_id', $lang_id)->orderBy('order')->get()->toArray(); + foreach ($res as $arr) { $faq_categ[$arr['link_id']]['title'] = $arr['question']; $faq_categ[$arr['link_id']]['flag'] = $arr['flag']; $faq_categ[$arr['link_id']]['link_id'] = $arr['link_id']; } -$res = sql_query("SELECT `id`, `link_id`, `question`, `answer`, `flag`, `categ` FROM `faq` WHERE `type`='item' AND `lang_id` = ".sqlesc($lang_id)." ORDER BY `order` ASC"); -while ($arr = mysql_fetch_array($res)) { +//$res = sql_query("SELECT id, link_id, question, answer, flag, categ FROM faq WHERE type='item' AND lang_id = ".sqlesc($lang_id)." ORDER BY order ASC"); +$res = \App\Models\Faq::query()->where('type', 'item')->where('lang_id', $lang_id)->get()->toArray(); +foreach ($res as $arr) { $faq_categ[$arr['categ']]['items'][$arr['id']]['question'] = $arr['question']; $faq_categ[$arr['categ']]['items'][$arr['id']]['answer'] = $arr['answer']; $faq_categ[$arr['categ']]['items'][$arr['id']]['flag'] = $arr['flag']; diff --git a/public/forums.php b/public/forums.php index 8b501002..33cb885c 100644 --- a/public/forums.php +++ b/public/forums.php @@ -671,7 +671,7 @@ if ($action == "viewtopic") $pagerbottom = "
".$pagerstr."
".$pager."
| Agent | IPV4 | IPV6 | Port |