You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.1 KiB
PHP
76 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Middleware;
|
|
|
|
use App\Services\SiteAnalyticsRecorder;
|
|
use App\Services\SiteSecurityEventService;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
class SiteAnalytics
|
|
{
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
|
|
{
|
|
$trackPageView = SiteAnalyticsRecorder::isTrackableRequest($request);
|
|
$response = $next($request, $response);
|
|
|
|
$classification = null;
|
|
try {
|
|
$classification = SiteSecurityEventService::classify($request, $response);
|
|
} catch (\Throwable $exception) {
|
|
}
|
|
|
|
$recordPageView = $trackPageView && (int) $response->getStatusCode() === 200;
|
|
if ($classification === null && !$recordPageView) {
|
|
return $response;
|
|
}
|
|
|
|
try {
|
|
$identity = SiteAnalyticsRecorder::resolveIdentity($request);
|
|
} catch (\Throwable $exception) {
|
|
return $response;
|
|
}
|
|
|
|
$userContext = [];
|
|
try {
|
|
$userContext = SiteAnalyticsRecorder::resolveUserContext($request);
|
|
} catch (\Throwable $exception) {
|
|
}
|
|
|
|
if ($classification !== null) {
|
|
try {
|
|
$classification = SiteSecurityEventService::classify($request, $response, $userContext);
|
|
SiteSecurityEventService::record(
|
|
$request,
|
|
$response,
|
|
$identity,
|
|
$userContext,
|
|
$classification
|
|
);
|
|
} catch (\Throwable $exception) {
|
|
}
|
|
}
|
|
|
|
if (!$recordPageView) {
|
|
return $response;
|
|
}
|
|
|
|
try {
|
|
SiteAnalyticsRecorder::recordPageView(
|
|
$request,
|
|
$identity,
|
|
$response->getStatusCode(),
|
|
$userContext
|
|
);
|
|
|
|
return SiteAnalyticsRecorder::withIdentityCookies(
|
|
$response,
|
|
$request,
|
|
$identity
|
|
);
|
|
} catch (\Throwable $exception) {
|
|
return $response;
|
|
}
|
|
}
|
|
}
|