Skip to content

Commit 04c7ffc

Browse files
committed
Allow robots.txt to be configured
1 parent 6971ae4 commit 04c7ffc

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

etc/config.sample.json

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
"hide_popular_documents": false,
2727
"hide_popular_packets": false
2828
},
29+
"robotstxt": {
30+
"*": [{"Disallow": "/"}]
31+
},
2932
"server_update_job_token": null,
3033
"user_login_disabled": false,
3134
"user_password_bcrypt_cost": 12,

src/controllers/Robotstxt.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace BNETDocs\Controllers;
4+
5+
use \BNETDocs\Models\Robotstxt as RobotstxtModel;
6+
use \CarlBennett\MVC\Libraries\Common;
7+
use \CarlBennett\MVC\Libraries\Controller;
8+
use \CarlBennett\MVC\Libraries\Router;
9+
use \CarlBennett\MVC\Libraries\View;
10+
11+
class Robotstxt extends Controller {
12+
public function &run(Router &$router, View &$view, array &$args) {
13+
$model = new RobotstxtModel();
14+
$model->rules = Common::$config->bnetdocs->robotstxt;
15+
$view->render($model);
16+
$model->_responseCode = 200;
17+
return $model;
18+
}
19+
}

src/main.php

+3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ function main() {
201201
$router->addRoute( // URL: /packet/search
202202
"#^/packet/search/?$#", "Packet\\Search", "Packet\\SearchHtml"
203203
);
204+
$router->addRoute( // URL: /robots.txt
205+
"#^/robots.txt/?$#", "Robotstxt", "Robotstxt"
206+
);
204207
$router->addRoute( // URL: /server/:id.json
205208
"#^/server/(\d+)/?.*\.json$#", "Server\\View", "Server\\ViewJSON"
206209
);

src/models/Robotstxt.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace BNETDocs\Models;
4+
5+
use \CarlBennett\MVC\Libraries\Model;
6+
7+
class Robotstxt extends Model {
8+
9+
public $rules;
10+
11+
}

src/static/robots.txt

-1
This file was deleted.

src/views/Robotstxt.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace BNETDocs\Views;
3+
4+
use \BNETDocs\Models\Robotstxt as RobotstxtModel;
5+
use \CarlBennett\MVC\Libraries\Exceptions\IncorrectModelException;
6+
use \CarlBennett\MVC\Libraries\Model;
7+
use \CarlBennett\MVC\Libraries\View;
8+
9+
class Robotstxt extends View
10+
{
11+
public function getMimeType()
12+
{
13+
return 'text/plain;charset=utf-8';
14+
}
15+
16+
public function render(Model &$model)
17+
{
18+
if (!$model instanceof RobotstxtModel)
19+
{
20+
throw new IncorrectModelException();
21+
}
22+
$model->_responseHeaders['Content-Type'] = $this->getMimeType();
23+
24+
foreach ($model->rules as $useragent => $rules)
25+
{
26+
printf("User-agent: %s\r\n", $useragent);
27+
28+
foreach ($rules as $rule)
29+
{
30+
foreach ($rule as $action => $url)
31+
{
32+
printf("%s: %s\r\n", $action, $url);
33+
}
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)