forked from laysense/laysense.cn
Fix CMS login and restore PAGEPAN feature icons
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
final class Request extends \support\Request
|
||||
{
|
||||
private const LOOPBACK = ['127.0.0.1', '::1'];
|
||||
|
||||
public function clientAddress(): string
|
||||
{
|
||||
$peer = $this->getRemoteIp();
|
||||
$forwarded = $this->header('x-real-ip', '');
|
||||
if (!$this->fromTrustedProxy() || !filter_var($forwarded, FILTER_VALIDATE_IP)) {
|
||||
return $peer;
|
||||
}
|
||||
|
||||
return inet_ntop(inet_pton($forwarded));
|
||||
}
|
||||
|
||||
protected function setSidCookie(string $sessionName, string $sid, array $cookieParams): void
|
||||
{
|
||||
$proto = $this->fromTrustedProxy() ? $this->header('x-forwarded-proto', '') : '';
|
||||
if ($proto === 'https') {
|
||||
$cookieParams['secure'] = true;
|
||||
} elseif (getenv('LAYSENSE_SECURE_COOKIES') === false
|
||||
&& in_array($this->getRemoteIp(), self::LOOPBACK, true)
|
||||
&& in_array(strtolower((string) $this->host(true)), ['localhost', '127.0.0.1', '[::1]', '::1'], true)
|
||||
&& in_array($proto, ['', 'http'], true)) {
|
||||
$cookieParams['secure'] = false;
|
||||
}
|
||||
parent::setSidCookie($sessionName, $sid, $cookieParams);
|
||||
}
|
||||
|
||||
private function fromTrustedProxy(): bool
|
||||
{
|
||||
$configured = getenv('LAYSENSE_TRUSTED_PROXIES');
|
||||
$trusted = $configured === false ? self::LOOPBACK : array_map('trim', explode(',', $configured));
|
||||
|
||||
return in_array($this->getRemoteIp(), $trusted, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user