Files
laysite/app/Http/Request.php

37 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http;
use App\Config\Environment;
final class Request extends \support\Request
{
public function clientAddress(): string
{
$peer = trim($this->getRemoteIp(), '[]');
$configured = Environment::get('LAYSITE_TRUSTED_PROXIES');
$trusted = match (true) {
$configured === false => self::isIntranetIp($peer),
trim($configured) === '*' => true,
default => in_array($peer, array_map('trim', explode(',', $configured)), true),
};
if (!$trusted) {
return $peer;
}
// Bunny replaces X-Real-IP and forwards other custom headers. Standard Cloudflare origin requests omit it.
foreach (['x-real-ip', 'cf-connecting-ip'] as $name) {
$address = $this->header($name);
if (filter_var($address, FILTER_VALIDATE_IP)) {
return inet_ntop(inet_pton($address));
}
}
$address = $this->getRealIp(safeMode: false);
return filter_var($address, FILTER_VALIDATE_IP) ? inet_ntop(inet_pton($address)) : $peer;
}
}