PR CI / ledger (pull_request) Successful in 4s
PR CI / flow (pull_request) Successful in 5s
PR CI / security (pull_request) Failing after 42s
PR CI / test (lowest) (pull_request) Successful in 48s
PR CI / test (stable) (pull_request) Successful in 49s
PR CI / ui (pull_request) Successful in 53s
PR CI / pr-ci (pull_request) Failing after 1s
PR CI / release-automerge (pull_request) Skipped
52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Modules\Statistics;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use IriksIT\Manager\Support\Names;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Counts the public pages that are served to a program (see BotRecorder). Only a
|
|
* page that exists and was served: an address that is made up, a redirect and the
|
|
* manager are left out, so the list of pages cannot be filled with nonsense.
|
|
*/
|
|
class CountBots
|
|
{
|
|
public function __construct(private readonly BotRecorder $recorder) {}
|
|
|
|
/**
|
|
* @param Closure(Request): Response $next
|
|
*/
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$response = $next($request);
|
|
|
|
if ($this->counts($request, $response)) {
|
|
try {
|
|
$this->recorder->hit((string) $request->userAgent(), $request->getPathInfo());
|
|
} catch (Throwable $e) {
|
|
// Counting must never be the reason a page is not served.
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
private function counts(Request $request, Response $response): bool
|
|
{
|
|
$name = $request->route()?->getName();
|
|
|
|
return $request->isMethod('GET')
|
|
&& $response->getStatusCode() === 200
|
|
&& is_string($name)
|
|
&& ! str_starts_with($name, Names::routePrefix())
|
|
// The same promise as for visitors: who asks not to be followed is not counted at all.
|
|
&& $request->header('Sec-GPC') !== '1'
|
|
&& $request->header('DNT') !== '1';
|
|
}
|
|
}
|