PHP code example of anvildev / craft-trails

1. Go to this page and download the library: Download anvildev/craft-trails 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/ */

    

anvildev / craft-trails example snippets



// config/trails.php
return [
    'anchorType' => 's3',
    's3Bucket' => 'trails-anchors-1714291200',  // your $BUCKET from step 1
    's3Region' => 'eu-central-1',
    's3AccessKeyId' => '$AWS_ACCESS_KEY_ID',     // env-var resolution works
    's3SecretAccessKey' => '$AWS_SECRET_ACCESS_KEY',
];


// config/trails.php
return [
    'anchorType' => 'rfc3161',
    'tsaUrl' => 'https://freetsa.org/tsr',

    // Either: path to a PEM CA bundle the PHP process can read.
    'tsaTrustedCaBundle' => '/etc/trails/tsa-ca-bundle.pem',

    // OR: inline PEM (used only if tsaTrustedCaBundle is empty).
    // 'tsaCaBundlePem' => "-----BEGIN CERTIFICATE-----\nMIID…\n-----END CERTIFICATE-----\n",
];

use anvildev\trails\Trails;

Trails::getInstance()->audit->logCustomEvent(
    eventType: 'myplugin.order_shipped',
    category: 'fulfillment',
    description: 'Order #1234 shipped via UPS',
    metadata: ['orderId' => 1234, 'carrier' => 'UPS', 'tracking' => '...'],
    elementId: 1234,
    elementType: 'myplugin\\elements\\Order',
);

use anvildev\trails\services\AuditService;

try {
    AuditService::assertValidEventType('myplugin.user_synced');
} catch (\InvalidArgumentException $e) {
    // handle invalid format
}

use anvildev\trails\Trails;

if (class_exists(Trails::class) && Trails::getInstance() !== null) {
    Trails::getInstance()->eventBridge->listen(
        MyService::class,
        MyService::EVENT_ORDER_SHIPPED,
        'myplugin.order_shipped',
        function($event) {
            return [
                'description' => "Order #{$event->order->id} shipped",
                'elementId' => $event->order->id,
                'elementType' => get_class($event->order),
                'elementTitle' => $event->order->title,
                'metadata' => ['carrier' => $event->carrier],
            ];
        }
    );
}

$timestamp = $_SERVER['HTTP_X_TRAILS_TIMESTAMP'] ?? '';
$signature = $_SERVER['HTTP_X_TRAILS_SIGNATURE'] ?? '';
$body      = file_get_contents('php://input');
$secret    = getenv('TRAILS_WEBHOOK_SECRET');

$expected = 'sha256=' . hash_hmac('sha256', $timestamp . '.' . $body, $secret);
if (!hash_equals($expected, $signature)) {
    http_response_code(401); exit;
}

// Reject stale timestamps (5 min window)
if (abs(time() - (int) $timestamp) > 300) {
    http_response_code(401); exit;
}

// config/trails.php - confirm the relevant flags are true
'logElements'         => true,
'logAuthentication'   => true,
'logAssets'           => true,
'logConfigChanges'    => true,
'logPermissionChanges'=> true,

// And confirm exclusions aren't shadowing what you expect.
'excludedElementTypes' => [...],
'excludedSections'     => [...],
'excludedFieldHandles' => [...],

return [
    'anchorType' => 's3',
    's3Bucket' => 'trails-anchors',
    's3Region' => 'us-east-1',
    's3AccessKeyId' => 'ddevminio',
    's3SecretAccessKey' => 'ddevminio',
    's3Endpoint' => 'http://minio:10101',
    's3UsePathStyle' => true,
];
bash
   php craft queue/run
   
bash
   php craft trails/integrity/verify --verbose
   
bash
php craft trails/integrity/verify --verbose
bash
php craft trails/retention/stats   # are counts increasing at all?
php craft queue/info               # are jobs stuck?