diff --git a/README.md b/README.md index 50da901..75be912 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,7 @@ - 启用模块 `rewrite` - 为网站根目录设置 `AllowOverride All` - 使用其它 Webserver,可以自行查询如何将 .htaccess 规则转换为你所使用的网站配置并写进你的网站配置当中 -- PHP 版本 8.0 以上 - (旧版可能可以使用,但未经完全测试) +- PHP 版本 8.1 以上 - PHP 模块 `xml` (也可能叫做 `dom`) - PHP 模块 `mbstring` - PHP 模块 `fileinfo` diff --git a/assets/robots.allow b/assets/robots.allow new file mode 100644 index 0000000..e4b09c1 --- /dev/null +++ b/assets/robots.allow @@ -0,0 +1,2 @@ +User-agent: * +Allow:/ diff --git a/assets/robots.deny b/assets/robots.deny new file mode 100644 index 0000000..1f53798 --- /dev/null +++ b/assets/robots.deny @@ -0,0 +1,2 @@ +User-agent: * +Disallow: / diff --git a/composer.json b/composer.json index b4427db..d3ea68d 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } }, "require": { - "php": ">=8.0", + "php": ">=8.1", "ext-xml": "*", "ext-dom": "*", "ext-mbstring": "*", diff --git a/index.php b/index.php index 5abaa4f..c1f71d0 100644 --- a/index.php +++ b/index.php @@ -4,6 +4,7 @@ require "./constant.php"; require "./vendor/autoload.php"; use SukWs\Bookshelf\Data\PageMeta; +use SukWs\Bookshelf\Data\SiteConfig\RobotsPolicy; use SukWs\Bookshelf\Data\SiteMeta; use SukWs\Bookshelf\Utils\PageParse; use SukWs\Bookshelf\Utils\RequestNotExistException; @@ -18,6 +19,24 @@ try { $tmp = substr($req, 0, -1); $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 { // 寻找页面 diff --git a/src/Data/SiteMeta.php b/src/Data/SiteMeta.php index a5425fd..dc5fa67 100644 --- a/src/Data/SiteMeta.php +++ b/src/Data/SiteMeta.php @@ -3,6 +3,7 @@ namespace SukWs\Bookshelf\Data; use Exception; +use SukWs\Bookshelf\Data\SiteConfig\RobotsPolicy; use SukWs\Bookshelf\Element\Bookshelf; class SiteMeta { @@ -88,4 +89,13 @@ class SiteMeta { 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, + }; + } + }