1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2024-12-05 09:26:52 +08:00

添加 site.robots 参数支持了 robots.txt

- 默认为 Allow All
This commit is contained in:
A.C.Sukazyo Eyre 2023-01-25 13:48:03 +08:00
parent 6debb55e06
commit 1a00273578
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
6 changed files with 35 additions and 3 deletions

View File

@ -22,8 +22,7 @@
- 启用模块 `rewrite` - 启用模块 `rewrite`
- 为网站根目录设置 `AllowOverride All` - 为网站根目录设置 `AllowOverride All`
- 使用其它 Webserver可以自行查询如何将 .htaccess 规则转换为你所使用的网站配置并写进你的网站配置当中 - 使用其它 Webserver可以自行查询如何将 .htaccess 规则转换为你所使用的网站配置并写进你的网站配置当中
- PHP 版本 8.0 以上 - PHP 版本 8.1 以上
(旧版可能可以使用,但未经完全测试)
- PHP 模块 `xml` (也可能叫做 `dom`) - PHP 模块 `xml` (也可能叫做 `dom`)
- PHP 模块 `mbstring` - PHP 模块 `mbstring`
- PHP 模块 `fileinfo` - PHP 模块 `fileinfo`

2
assets/robots.allow Normal file
View File

@ -0,0 +1,2 @@
User-agent: *
Allow:/

2
assets/robots.deny Normal file
View File

@ -0,0 +1,2 @@
User-agent: *
Disallow: /

View File

@ -17,7 +17,7 @@
} }
}, },
"require": { "require": {
"php": ">=8.0", "php": ">=8.1",
"ext-xml": "*", "ext-xml": "*",
"ext-dom": "*", "ext-dom": "*",
"ext-mbstring": "*", "ext-mbstring": "*",

View File

@ -4,6 +4,7 @@ require "./constant.php";
require "./vendor/autoload.php"; require "./vendor/autoload.php";
use SukWs\Bookshelf\Data\PageMeta; use SukWs\Bookshelf\Data\PageMeta;
use SukWs\Bookshelf\Data\SiteConfig\RobotsPolicy;
use SukWs\Bookshelf\Data\SiteMeta; use SukWs\Bookshelf\Data\SiteMeta;
use SukWs\Bookshelf\Utils\PageParse; use SukWs\Bookshelf\Utils\PageParse;
use SukWs\Bookshelf\Utils\RequestNotExistException; use SukWs\Bookshelf\Utils\RequestNotExistException;
@ -18,6 +19,24 @@ try {
$tmp = substr($req, 0, -1); $tmp = substr($req, 0, -1);
$uri = explode("/", $req, 2); $uri = explode("/", $req, 2);
// 为 robots.txt 进行特别支持
if (sizeof($uri) == 1 && $uri[0] == "robots.txt") {
$policy = SiteMeta::getRobotsPolicy();
switch ($policy) {
case RobotsPolicy::allow:
exit(file_get_contents("./assets/robots.allow"));
case RobotsPolicy::deny:
exit(file_get_contents("./assets/robots.deny"));
case RobotsPolicy::file:
exit(file_get_contents("./data/robots.txt"));
case RobotsPolicy::raw:
exit(SiteMeta::getConfigurationLevelShelf("site.robots"));
}
}
try { try {
// 寻找页面 // 寻找页面

View File

@ -3,6 +3,7 @@
namespace SukWs\Bookshelf\Data; namespace SukWs\Bookshelf\Data;
use Exception; use Exception;
use SukWs\Bookshelf\Data\SiteConfig\RobotsPolicy;
use SukWs\Bookshelf\Element\Bookshelf; use SukWs\Bookshelf\Element\Bookshelf;
class SiteMeta { class SiteMeta {
@ -88,4 +89,13 @@ class SiteMeta {
return self::$BOOKSHELF->getConfiguration($key); return self::$BOOKSHELF->getConfiguration($key);
} }
public static function getRobotsPolicy (): RobotsPolicy {
return match (self::getConfigurationLevelShelf("site.robots")) {
"allow", null => RobotsPolicy::allow,
"deny" => RobotsPolicy::deny,
"custom", "file" => RobotsPolicy::file,
default => RobotsPolicy::raw,
};
}
} }