2019-08-09 17:00:21 +02:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-09 17:00:21 +02:00
|
|
|
namespace App\Http;
|
|
|
|
|
|
|
|
use App\Acl;
|
2020-07-10 06:15:27 +02:00
|
|
|
use App\Auth;
|
|
|
|
use App\Customization;
|
2019-08-09 17:00:21 +02:00
|
|
|
use App\Entity;
|
2022-01-07 09:26:40 +01:00
|
|
|
use App\Enums\SupportedLocales;
|
2020-02-06 03:35:13 +01:00
|
|
|
use App\Exception;
|
|
|
|
use App\RateLimit;
|
|
|
|
use App\Session;
|
|
|
|
use App\View;
|
|
|
|
use Mezzio\Session\SessionInterface;
|
2021-12-11 04:48:19 +01:00
|
|
|
use RuntimeException;
|
2019-08-09 17:00:21 +02:00
|
|
|
|
2020-06-26 22:22:53 +02:00
|
|
|
final class ServerRequest extends \Slim\Http\ServerRequest
|
2019-08-09 17:00:21 +02:00
|
|
|
{
|
2020-02-06 03:35:13 +01:00
|
|
|
public const ATTR_VIEW = 'app_view';
|
|
|
|
public const ATTR_SESSION = 'app_session';
|
|
|
|
public const ATTR_SESSION_CSRF = 'app_session_csrf';
|
|
|
|
public const ATTR_SESSION_FLASH = 'app_session_flash';
|
|
|
|
public const ATTR_ROUTER = 'app_router';
|
|
|
|
public const ATTR_RATE_LIMIT = 'app_rate_limit';
|
2019-08-09 17:00:21 +02:00
|
|
|
public const ATTR_ACL = 'acl';
|
2021-02-28 03:50:45 +01:00
|
|
|
public const ATTR_LOCALE = 'locale';
|
2020-07-10 06:15:27 +02:00
|
|
|
public const ATTR_CUSTOMIZATION = 'customization';
|
|
|
|
public const ATTR_AUTH = 'auth';
|
2019-08-09 17:00:21 +02:00
|
|
|
public const ATTR_STATION = 'station';
|
|
|
|
public const ATTR_USER = 'user';
|
|
|
|
|
2020-02-06 03:35:13 +01:00
|
|
|
public function getView(): View
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_VIEW, View::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSession(): SessionInterface
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_SESSION, SessionInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCsrf(): Session\Csrf
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_SESSION_CSRF, Session\Csrf::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFlash(): Session\Flash
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_SESSION_FLASH, Session\Flash::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRouter(): RouterInterface
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_ROUTER, RouterInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRateLimit(): RateLimit
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_RATE_LIMIT, RateLimit::class);
|
|
|
|
}
|
|
|
|
|
2022-01-07 09:26:40 +01:00
|
|
|
public function getLocale(): SupportedLocales
|
2021-02-28 03:50:45 +01:00
|
|
|
{
|
2022-01-07 09:26:40 +01:00
|
|
|
return $this->getAttributeOfClass(self::ATTR_LOCALE, SupportedLocales::class);
|
2021-02-28 03:50:45 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 06:15:27 +02:00
|
|
|
public function getCustomization(): Customization
|
2020-02-06 03:35:13 +01:00
|
|
|
{
|
2020-07-10 06:15:27 +02:00
|
|
|
return $this->getAttributeOfClass(self::ATTR_CUSTOMIZATION, Customization::class);
|
|
|
|
}
|
2020-02-06 03:35:13 +01:00
|
|
|
|
2020-07-10 06:15:27 +02:00
|
|
|
public function getAuth(): Auth
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_AUTH, Auth::class);
|
2020-02-06 03:35:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-09 17:00:21 +02:00
|
|
|
public function getAcl(): Acl
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_ACL, Acl::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser(): Entity\User
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_USER, Entity\User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStation(): Entity\Station
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_STATION, Entity\Station::class);
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:35:13 +01:00
|
|
|
/**
|
|
|
|
* @param string $attr
|
|
|
|
* @param string $class_name
|
|
|
|
*
|
2020-04-07 01:27:01 +02:00
|
|
|
* @throws Exception\InvalidRequestAttribute
|
2020-02-06 03:35:13 +01:00
|
|
|
*/
|
2021-06-10 05:22:13 +02:00
|
|
|
private function getAttributeOfClass(string $attr, string $class_name): mixed
|
2020-02-06 03:35:13 +01:00
|
|
|
{
|
|
|
|
$object = $this->serverRequest->getAttribute($attr);
|
2020-04-07 01:15:59 +02:00
|
|
|
|
|
|
|
if (empty($object)) {
|
2021-04-24 00:12:47 +02:00
|
|
|
throw new Exception\InvalidRequestAttribute(
|
|
|
|
sprintf(
|
|
|
|
'Attribute "%s" is required and is empty in this request',
|
|
|
|
$attr
|
|
|
|
)
|
|
|
|
);
|
2020-04-07 01:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!($object instanceof $class_name)) {
|
2021-05-25 06:29:07 +02:00
|
|
|
throw new Exception\InvalidRequestAttribute(
|
|
|
|
sprintf(
|
|
|
|
'Attribute "%s" must be of type "%s".',
|
|
|
|
$attr,
|
|
|
|
$class_name
|
|
|
|
)
|
|
|
|
);
|
2020-02-06 03:35:13 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 01:15:59 +02:00
|
|
|
return $object;
|
2020-02-06 03:35:13 +01:00
|
|
|
}
|
2020-07-10 06:15:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the remote user's IP address as indicated by HTTP headers.
|
|
|
|
*/
|
2021-07-19 07:53:45 +02:00
|
|
|
public function getIp(): string
|
2020-07-10 06:15:27 +02:00
|
|
|
{
|
|
|
|
$params = $this->serverRequest->getServerParams();
|
|
|
|
|
2020-10-02 05:21:05 +02:00
|
|
|
$ip = $params['HTTP_CLIENT_IP']
|
2020-07-10 06:15:27 +02:00
|
|
|
?? $params['HTTP_X_FORWARDED_FOR']
|
|
|
|
?? $params['HTTP_X_FORWARDED']
|
|
|
|
?? $params['HTTP_FORWARDED_FOR']
|
|
|
|
?? $params['HTTP_FORWARDED']
|
|
|
|
?? $params['REMOTE_ADDR']
|
2021-07-19 07:53:45 +02:00
|
|
|
?? null;
|
|
|
|
|
|
|
|
if (null === $ip) {
|
2021-12-11 04:48:19 +01:00
|
|
|
throw new RuntimeException('No IP address attached to this request.');
|
2021-07-19 07:53:45 +02:00
|
|
|
}
|
2020-10-02 05:21:05 +02:00
|
|
|
|
|
|
|
// Handle the IP being separated by commas.
|
|
|
|
$ipParts = explode(',', $ip);
|
|
|
|
$ip = array_shift($ipParts);
|
|
|
|
|
|
|
|
return trim($ip);
|
2020-07-10 06:15:27 +02:00
|
|
|
}
|
2019-08-09 17:00:21 +02:00
|
|
|
}
|