diff --git a/.gitignore b/.gitignore index 55dca57d3..fd7fa4bb3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ ### TorrentPier ### *.log +/*.txt *.integrity install.php_* composer-setup.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c85b8b7bf..955993149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ **Merged pull requests:** - Release 2.4.5 🍧️ ([belomaxorka](https://github.com/belomaxorka)) +- Added `IndexNow` protocol support 🤖 [\#1736](https://github.com/torrentpier/torrentpier/pull/1736) ([belomaxorka](https://github.com/belomaxorka)) - Added `TorrentPier instance hash` generation [\#1726](https://github.com/torrentpier/torrentpier/pull/1726) ([belomaxorka](https://github.com/belomaxorka)) - Added `m4a` extension support in M3U playback [\#1724](https://github.com/torrentpier/torrentpier/pull/1724) ([belomaxorka](https://github.com/belomaxorka)) - Created `VersionHelper.php` [\#1731](https://github.com/torrentpier/torrentpier/pull/1731) ([belomaxorka](https://github.com/belomaxorka)) diff --git a/composer.json b/composer.json index cfb4e6611..2a4d1d02b 100644 --- a/composer.json +++ b/composer.json @@ -56,6 +56,7 @@ "egulias/email-validator": "^4.0.1", "filp/whoops": "^2.15", "z4kn4fein/php-semver": "^v3.0.0", + "nemorize/indexnow": "^0.0.1", "gigablah/sphinxphp": "2.0.8", "google/recaptcha": "^1.3", "jacklul/monolog-telegram": "^3.1", diff --git a/composer.lock b/composer.lock index 50756a23c..dfb1c103b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a3dd6c29793f928936aec555bba8ca5c", + "content-hash": "9024100c87e72c5ee811638607aa15f5", "packages": [ { "name": "arokettu/bencode", @@ -1987,6 +1987,47 @@ ], "time": "2024-12-05T17:15:07+00:00" }, + { + "name": "nemorize/indexnow", + "version": "0.0.1", + "source": { + "type": "git", + "url": "https://github.com/nemorize/php-indexnow.git", + "reference": "7602a8ae1de0cf1dd11692a7627f10111c55731a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nemorize/php-indexnow/zipball/7602a8ae1de0cf1dd11692a7627f10111c55731a", + "reference": "7602a8ae1de0cf1dd11692a7627f10111c55731a", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^7.7", + "php": ">=8.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Nemorize\\Indexnow\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ji Yong, Kim", + "email": "nemo@qroffle.com" + } + ], + "description": "PHP library for submitting website URLs using IndexNow to search engines.", + "support": { + "issues": "https://github.com/nemorize/php-indexnow/issues", + "source": "https://github.com/nemorize/php-indexnow/tree/0.0.1" + }, + "time": "2023-07-31T17:08:12+00:00" + }, { "name": "nikic/iter", "version": "v2.4.1", diff --git a/library/config.php b/library/config.php index b1d7f0bb8..136102ea7 100644 --- a/library/config.php +++ b/library/config.php @@ -127,6 +127,12 @@ $bb_cfg['torr_server'] = [ 'disable_for_guest' => true ]; +// IndexNow settings +$bb_cfg['indexnow_settings'] = [ + 'enabled' => false, + 'host' => 'bing', // Available: yandex, bing, seznam, naver +]; + // FAQ url help link $bb_cfg['how_to_download_url_help'] = 'viewtopic.php?t=1'; // How to download? $bb_cfg['what_is_torrent_url_help'] = 'viewtopic.php?t=2'; // What is a torrent? diff --git a/library/includes/cron/jobs/board_maintenance.php b/library/includes/cron/jobs/board_maintenance.php index 8e898cf33..c59eeae38 100644 --- a/library/includes/cron/jobs/board_maintenance.php +++ b/library/includes/cron/jobs/board_maintenance.php @@ -64,6 +64,15 @@ if (empty($bb_cfg['tp_instance_hash']) || ($bb_cfg['tp_instance_hash'] !== hash( bb_update_config(['tp_instance_hash' => hash('xxh128', FULL_URL)]); } +// Generate IndexNow key +if ($bb_cfg['indexnow_settings']['enabled'] && !is_file(BB_ROOT . $bb_cfg['indexnow_key'] . \TorrentPier\IndexNow::$keyFileExtension)) { + $randomIndexNowKey = empty($bb_cfg['indexnow_key']) ? make_rand_str(rand(64, 128)) : $bb_cfg['indexnow_key']; + if ($bb_cfg['indexnow_key'] !== $randomIndexNowKey) { + bb_update_config(['indexnow_key' => $randomIndexNowKey]); + } + file_write($randomIndexNowKey, (BB_ROOT . $randomIndexNowKey . \TorrentPier\IndexNow::$keyFileExtension)); +} + // Check for updates $datastore->update('check_updates'); diff --git a/src/IndexNow.php b/src/IndexNow.php new file mode 100644 index 000000000..1964176a2 --- /dev/null +++ b/src/IndexNow.php @@ -0,0 +1,83 @@ + 'yandex.com', + 'bing' => 'bing.com', + 'seznam' => 'search.seznam.cz', + 'naver' => 'searchadvisor.naver.com' + ]; + + public function __construct() + { + global $bb_cfg; + + $this->indexNow = new \Nemorize\Indexnow\Indexnow(); + $this->indexNow->setKey($bb_cfg['indexnow_key']); + if (in_array($bb_cfg['indexnow_settings']['host'], array_keys($this->hosts))) { + $this->indexNow->setHost($this->hosts[$bb_cfg['indexnow_settings']['host']]); + } else { + bb_log("IndexNow (ERROR): Invalid host: {$bb_cfg['indexnow_settings']['host']}\n", self::$logFile); + bb_die('Invalid host: ' . $bb_cfg['indexnow_settings']['host']); + } + $this->indexNow->setKeyLocation(FULL_URL . $bb_cfg['indexnow_key'] . self::$keyFileExtension); + } + + /** + * Submit page to IndexNow + * + * @param string $url + * @return void + */ + public function submit(string $url): void + { + try { + $this->indexNow->submit($url); + } catch (IndexnowException $e) { + bb_log("IndexNow (ERROR) [$url]: Message: {$e->getMessage()}\n", self::$logFile); + bb_die($e->getMessage()); + } + } +} diff --git a/src/Legacy/Post.php b/src/Legacy/Post.php index fe3aadd40..19e8d1963 100644 --- a/src/Legacy/Post.php +++ b/src/Legacy/Post.php @@ -10,6 +10,7 @@ namespace TorrentPier\Legacy; use TorrentPier\Emailer; +use TorrentPier\IndexNow; use TorrentPier\Legacy\Admin\Common; use TorrentPier\Validate; @@ -220,6 +221,12 @@ class Post } } + // Send IndexNow + if ($bb_cfg['indexnow_settings']['enabled'] && ($mode === 'newtopic' || $mode === 'editpost')) { + $indexNow = new IndexNow(); + $indexNow->submit(FULL_URL . POST_URL . "$post_id#$post_id"); + } + meta_refresh(POST_URL . "$post_id#$post_id"); set_die_append_msg($forum_id, $topic_id);