Added IndexNow support 🤖 (#1736)

* Added IndexNow support 🤖

* Update CHANGELOG.md

* Updated

* Update IndexNowHelper.php

* Update board_maintenance.php

* Update board_maintenance.php

* Updated

* Updated

* Update Post.php

* Updated

* Update Post.php

* Update IndexNow.php

* Updated

* Updated

* Update config.php

* Update config.php
This commit is contained in:
Roman Kelesidis 2025-01-06 23:32:58 +07:00 committed by GitHub
commit 7a84cdd5c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 150 additions and 1 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@
### TorrentPier ###
*.log
/*.txt
*.integrity
install.php_*
composer-setup.php

View file

@ -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))

View file

@ -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",

43
composer.lock generated
View file

@ -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",

View file

@ -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?

View file

@ -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');

83
src/IndexNow.php Normal file
View file

@ -0,0 +1,83 @@
<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2024 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
namespace TorrentPier;
use Nemorize\Indexnow\Exceptions\IndexnowException;
/**
* Class IndexNow
* @package TorrentPier
*/
class IndexNow
{
/**
* IndexNow instance
*
* @var \Nemorize\Indexnow\Indexnow
*/
private \Nemorize\Indexnow\Indexnow $indexNow;
/**
* IndexNow Key-file extension
*
* @var string
*/
public static string $keyFileExtension = '.txt';
/**
* Log filename
*
* @var string
*/
private static string $logFile = 'index_now';
/**
* Available hosts
*
* @var array|string[]
*/
public array $hosts = [
'yandex' => '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());
}
}
}

View file

@ -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);