1. Go to this page and download the library: Download audunru/reporting-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Spatie\Csp\Directive;
use Spatie\Csp\Policies\Basic;
class MyPolicy extends Basic
{
public function configure(): void
{
parent::configure();
$this->add(Directive::REPORT_TO, 'default');
}
}
use audunru\ReportingApi\Contracts\ReportEvent;
use audunru\ReportingApi\Events\CspViolationReceived;
use audunru\ReportingApi\Listeners\LogCspViolation;
use audunru\ReportingApi\Listeners\LogReport;
use Illuminate\Support\Facades\Event;
public function boot(): void
{
Event::listen(CspViolationReceived::class, LogCspViolation::class);
Event::listen(ReportEvent::class, LogReport::class);
}
// app/Listeners/MyCspViolationListener.php
namespace App\Listeners;
use audunru\ReportingApi\DTOs\CspViolationReport;
use audunru\ReportingApi\Listeners\LogCspViolation;
class MyCspViolationListener extends LogCspViolation
{
private const EXTENSION_SCHEMES = [
'chrome-extension://',
'moz-extension://',
'safari-extension://',
];
protected function shouldExclude(CspViolationReport $report): bool
{
$blocked = $report->body->blockedURL ?? '';
foreach (self::EXTENSION_SCHEMES as $scheme) {
if (str_starts_with($blocked, $scheme)) {
return true;
}
}
return false;
}
}
// app/Listeners/MyReportListener.php
namespace App\Listeners;
use audunru\ReportingApi\DTOs\Report;
use audunru\ReportingApi\Listeners\LogReport;
class MyReportListener extends LogReport
{
protected function shouldExclude(Report $report): bool
{
return $report->type === 'csp-violation'; // handled separately
}
}
use audunru\ReportingApi\Contracts\ReportEvent;
use audunru\ReportingApi\Events\CspViolationReceived;
use App\Listeners\MyCspViolationListener;
use App\Listeners\MyReportListener;
use Illuminate\Support\Facades\Event;
public function boot(): void
{
Event::listen(CspViolationReceived::class, MyCspViolationListener::class);
Event::listen(ReportEvent::class, MyReportListener::class);
}