-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
71 lines (58 loc) · 2.2 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace mauricerenck\DarkVisitors;
use Kirby;
use Kirby\Http\Response;
use Amp;
@include_once __DIR__ . '/vendor/autoload.php';
Kirby::plugin('mauricerenck/darkvisitors', [
'options' => [
'cache' => true,
],
'routes' => [
[
'pattern' => 'robots.txt',
'method' => 'GET',
'action' => function () {
$api = new Api();
$apiEntries = $api->get();
$config = new Config();
$pluginConfig = $config->getUserConfig();
$robotTxt = [];
if (isset($pluginConfig['userConfig'])) {
$robotTxt = array_merge($robotTxt, $pluginConfig['userConfig']);
}
if (isset($pluginConfig['sitemaps'])) {
$robotTxt = array_merge($robotTxt, array_map(fn($url) => "Sitemap: $url", $pluginConfig['sitemaps']));
}
if (!is_null($apiEntries)) {
$robotTxt[] = '';
$robotTxt = array_merge($robotTxt, $apiEntries);
}
return new Response(join("\n", $robotTxt), 'text/plain', 200);
},
],
[
'pattern' => '(:any)',
'method' => 'GET',
'action' => function () {
$request = kirby()->request();
$api = new Api();
if (option('mauricerenck.dark-visitors.analytics', false)) {
$requestPath = $request->path()->toString(true);
$requestMethod = $request->method();
$requestHeaders = $request->headers();
try {
$future = Amp\async(function () use ($api, $requestPath, $requestMethod, $requestHeaders) {
$result = $api->trackAgent($requestPath, $requestMethod, $requestHeaders);
return $result;
});
$future->await();
} catch (\Throwable $e) {
echo 'Error' . $e->getMessage();
}
}
$this->next();
},
],
],
]);