From f584cf5d297ac5a442a238d4b8965efed721f7f8 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Wed, 22 May 2024 14:21:07 +0700 Subject: [PATCH] Minor improvements (#1481) * Minor improvements * Updated * Update functions.php * Updated * Update .gitignore * Updated * Update CHANGELOG.md --- .gitignore | 2 - CHANGELOG.md | 2 +- composer.json | 1 - install/sql/mysql.sql | 3 - library/attach_mod/displaying_torrent.php | 2 +- library/defines.php | 1 + library/includes/cron/cron_run.php | 2 +- .../includes/cron/jobs/update_geolite_db.php | 82 ------------------- library/includes/functions.php | 17 +--- .../templates/default/viewtopic_torrent.tpl | 4 +- tracker.php | 2 +- 11 files changed, 10 insertions(+), 108 deletions(-) delete mode 100644 library/includes/cron/jobs/update_geolite_db.php diff --git a/.gitignore b/.gitignore index 8f425eb05..c5b8f2412 100644 --- a/.gitignore +++ b/.gitignore @@ -33,8 +33,6 @@ $RECYCLE.BIN/ *.lnk *.bat *.ver -*.mmdb -*.mmdb.old ### OSX ### .DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md index 99b0257ff..f12d67fb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - Demo mode: Allow registering torrents by default [\#1440](https://github.com/torrentpier/torrentpier/pull/1440) ([belomaxorka](https://github.com/belomaxorka)) - Temp: Removed showing forum description in `viewforum.php` [\#1465](https://github.com/torrentpier/torrentpier/pull/1465) ([belomaxorka](https://github.com/belomaxorka)) - Code refactoring [\#1441](https://github.com/torrentpier/torrentpier/pull/1441) ([belomaxorka](https://github.com/belomaxorka)) -- Minor improvements [\#1435](https://github.com/torrentpier/torrentpier/pull/1435), [\#1443](https://github.com/torrentpier/torrentpier/pull/1443), [\#1446](https://github.com/torrentpier/torrentpier/pull/1446), [\#1450](https://github.com/torrentpier/torrentpier/pull/1450), [\#1452](https://github.com/torrentpier/torrentpier/pull/1452), [\#1458](https://github.com/torrentpier/torrentpier/pull/1458), [\#1461](https://github.com/torrentpier/torrentpier/pull/1461), [\#1462](https://github.com/torrentpier/torrentpier/pull/1462), [\#1467](https://github.com/torrentpier/torrentpier/pull/1467), [\#1469](https://github.com/torrentpier/torrentpier/pull/1469), [\#1472](https://github.com/torrentpier/torrentpier/pull/1472), [\#1477](https://github.com/torrentpier/torrentpier/pull/1477), [\#1480](https://github.com/torrentpier/torrentpier/pull/1480) ([belomaxorka](https://github.com/belomaxorka)) +- Minor improvements [\#1435](https://github.com/torrentpier/torrentpier/pull/1435), [\#1443](https://github.com/torrentpier/torrentpier/pull/1443), [\#1446](https://github.com/torrentpier/torrentpier/pull/1446), [\#1450](https://github.com/torrentpier/torrentpier/pull/1450), [\#1452](https://github.com/torrentpier/torrentpier/pull/1452), [\#1458](https://github.com/torrentpier/torrentpier/pull/1458), [\#1461](https://github.com/torrentpier/torrentpier/pull/1461), [\#1462](https://github.com/torrentpier/torrentpier/pull/1462), [\#1467](https://github.com/torrentpier/torrentpier/pull/1467), [\#1469](https://github.com/torrentpier/torrentpier/pull/1469), [\#1472](https://github.com/torrentpier/torrentpier/pull/1472), [\#1477](https://github.com/torrentpier/torrentpier/pull/1477), [\#1480](https://github.com/torrentpier/torrentpier/pull/1480), [\#1481](https://github.com/torrentpier/torrentpier/pull/1481) ([belomaxorka](https://github.com/belomaxorka)) - Updated deps [\#1454](https://github.com/torrentpier/torrentpier/pull/1454), [\#1455](https://github.com/torrentpier/torrentpier/pull/1455), [\#1459](https://github.com/torrentpier/torrentpier/pull/1459), [\#1460](https://github.com/torrentpier/torrentpier/pull/1460) ([belomaxorka](https://github.com/belomaxorka)) - New Crowdin updates [\#1444](https://github.com/torrentpier/torrentpier/pull/1444), [\#1447](https://github.com/torrentpier/torrentpier/pull/1447), [\#1453](https://github.com/torrentpier/torrentpier/pull/1453), [\#1468](https://github.com/torrentpier/torrentpier/pull/1468), [\#1473](https://github.com/torrentpier/torrentpier/pull/1473), [\#1476](https://github.com/torrentpier/torrentpier/pull/1476), [\#1479](https://github.com/torrentpier/torrentpier/pull/1479) ([Exileum](https://github.com/Exileum)) diff --git a/composer.json b/composer.json index e8ca6db7e..61f96ee7b 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,6 @@ "claviska/simpleimage": "^4.0", "egulias/email-validator": "^4.0.1", "filp/whoops": "^2.15", - "geoip2/geoip2": "^3.0", "gigablah/sphinxphp": "2.0.8", "google/recaptcha": "^1.3", "jacklul/monolog-telegram": "^3.1", diff --git a/install/sql/mysql.sql b/install/sql/mysql.sql index ed6e7d497..edf03db8e 100644 --- a/install/sql/mysql.sql +++ b/install/sql/mysql.sql @@ -654,9 +654,6 @@ VALUES ('1', 'Attach maintenance', 'attach_maintenance.php', 'daily', '', '05:00 '0', '0', '0'), ('1', 'Demo mode', 'demo_mode.php', 'daily', '', '05:00:00', '255', '', '', '', '1', 'demo_mode_cron', '1', '1', - '0'), - ('1', 'Update GeoLite DB', 'update_geolite_db.php', 'daily', '', '05:00:00', '255', '', '', '', '1', - 'update_geolite_db', '1', '1', '0'); -- ---------------------------- diff --git a/library/attach_mod/displaying_torrent.php b/library/attach_mod/displaying_torrent.php index bfe4d467c..e3a81a40c 100644 --- a/library/attach_mod/displaying_torrent.php +++ b/library/attach_mod/displaying_torrent.php @@ -448,7 +448,7 @@ if ($tor_reged && $tor_info) { 'ROW_BGR' => $row_bgr, 'NAME' => ($peer['update_time']) ? $name : "$name", 'PEER_ID' => isset($peer['peer_id']) ? get_user_torrent_client($peer['peer_id']) : $lang['UNKNOWN'], - 'COUNTRY' => render_flag(getCountryByIP($ip, $port), false), + 'COUNTRY' => render_flag(infoByIP($ip, $port)['countryCode'], false), 'COMPL_PRC' => $compl_perc, 'UP_TOTAL' => ($max_up_id[$x] == $pid) ? "$up_tot" : $up_tot, 'DOWN_TOTAL' => ($max_down_id[$x] == $pid) ? "$down_tot" : $down_tot, diff --git a/library/defines.php b/library/defines.php index 72df2a587..9d4bc464f 100644 --- a/library/defines.php +++ b/library/defines.php @@ -32,6 +32,7 @@ define('TEMPLATES_DIR', BB_PATH . '/styles/templates'); define('APP_NAME', 'TorrentPier'); define('UPDATER_URL', 'https://api.github.com/repos/torrentpier/torrentpier/releases/latest'); define('UPDATER_FILE', INT_DATA_DIR . '/updater.ver'); +define('API_IP_URL', 'https://freeipapi.com/api/json/'); define('CHECK_REQIREMENTS', [ 'status' => true, 'php_min_version' => '8.1.0', diff --git a/library/includes/cron/cron_run.php b/library/includes/cron/cron_run.php index cf8deb0e4..e3ff75649 100644 --- a/library/includes/cron/cron_run.php +++ b/library/includes/cron/cron_run.php @@ -83,7 +83,7 @@ foreach ($cron_jobs as $job) { $msg .= LOG_LF . '------=-------=----------=------=-------=----------'; bb_log($msg . LOG_LF, CRON_LOG_DIR . '/' . CRON_LOG_FILE); - if (is_countable($cron_runtime_log)) { + if (is_array($cron_runtime_log)) { $runtime_log_file = ($job['log_file']) ?: $job['cron_script']; $cron_runtime_log[] = ''; bb_log($cron_runtime_log, CRON_LOG_DIR . '/' . basename($runtime_log_file)); diff --git a/library/includes/cron/jobs/update_geolite_db.php b/library/includes/cron/jobs/update_geolite_db.php deleted file mode 100644 index 6d5e71b56..000000000 --- a/library/includes/cron/jobs/update_geolite_db.php +++ /dev/null @@ -1,82 +0,0 @@ - ['header' => 'User-Agent: ' . APP_NAME]]); -$repo_content = file_get_contents($repo_link, context: $context); - -$json_response = false; -if ($repo_content !== false) { - $json_response = json_decode(utf8_encode($repo_content), true); - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Successfully connected to: " . $repo_link; -} else { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Cannot access to: " . $repo_link; -} - -if (is_array($json_response) && !empty($json_response)) { - $download_link = $json_response['assets'][1]['browser_download_url']; - $file_date = $json_response['name'] ?? ''; - if (!empty($download_link)) { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Link to download is valid: " . $download_link; - $get_file = file_get_contents($download_link); - $get_file_md5 = md5_file($download_link); - if ($get_file !== false) { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- GeoLite file obtained. MD5: $get_file_md5"; - file_put_contents($save_path, $get_file); // Save new GeoLite file! - if (is_file($save_path) && ($get_file_md5 === md5_file($save_path))) { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- GeoLite file successfully saved ($file_date). MD5 hashes are identical"; - if (is_file($old_file_path) && unlink($old_file_path)) { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Old GeoLite file successfully removed (Second step)"; - } - } else { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Reverting all changes..."; - if (is_file($old_file_path)) { - if (rename($old_file_path, $save_path)) { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Successfully reverted"; - } else { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Cannot revert changes, because cannot rename old file"; - } - } else { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Cannot revert changes, old file not found"; - } - } - } else { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- GeoLite file not obtained"; - } - } else { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Cannot find link to download"; - } -} else { - $cron_runtime_log[] = date('Y-m-d H:i:s') . " -- Invalid response from server: " . $json_response; -} diff --git a/library/includes/functions.php b/library/includes/functions.php index 086379d6e..278272906 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -2166,26 +2166,17 @@ function readUpdaterFile(): array|bool } /** - * Show country ISO Code by user IP address + * IP Geolocation API * * @param string $ipAddress * @param int $port * @return mixed|string|null */ -function getCountryByIP(string $ipAddress, int $port = 1111): mixed +function infoByIP(string $ipAddress, int $port = 0): mixed { - global $lang; - if (!$data = CACHE('bb_ip2countries')->get($ipAddress . '_' . $port)) { - $cityDbReader = new \GeoIp2\Database\Reader(INT_DATA_DIR . '/GeoLite2-City.mmdb'); - try { - $record = $cityDbReader->city($ipAddress); - $data = $record->country->isoCode; - } catch (\GeoIp2\Exception\AddressNotFoundException $e) { - $data = $lang['UNKNOWN']; - } catch (\MaxMind\Db\Reader\InvalidDatabaseException $e) { - bb_die($e->getMessage()); - } + $response = file_get_contents(API_IP_URL . $ipAddress); + $data = json_decode($response, true); CACHE('bb_ip2countries')->set($ipAddress . '_' . $port, $data, 1200); } diff --git a/styles/templates/default/viewtopic_torrent.tpl b/styles/templates/default/viewtopic_torrent.tpl index 86a19d0e0..e7f8e8267 100644 --- a/styles/templates/default/viewtopic_torrent.tpl +++ b/styles/templates/default/viewtopic_torrent.tpl @@ -238,9 +238,7 @@ ajax.callback.callseed = function (data) { -
{L_DL_INFO}
-
-
This DL-list includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com.
+
{L_DL_INFO}
diff --git a/tracker.php b/tracker.php index e0b4a567e..3a90ed43e 100644 --- a/tracker.php +++ b/tracker.php @@ -46,7 +46,7 @@ $lastvisit = (!IS_GUEST) ? $userdata['user_lastvisit'] : ''; $search_id = (isset($_GET['search_id']) && is_string($_GET['search_id'])) ? $_GET['search_id'] : ''; $session_id = $userdata['session_id']; -$status = (isset($_POST['status']) && is_countable($_POST['status'])) ? (array)$_POST['status'] : []; +$status = (isset($_POST['status']) && is_array($_POST['status'])) ? $_POST['status'] : []; $cat_forum = $tor_to_show = $search_in_forums_ary = []; $title_match_sql = $title_match_q = $search_in_forums_csv = '';