2019-08-09 15:00:21 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
namespace App\Http;
|
|
|
|
|
|
|
|
use App\Acl;
|
2020-07-10 04:15:27 +00:00
|
|
|
use App\Auth;
|
|
|
|
use App\Customization;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Entity;
|
2020-02-06 02:35:13 +00:00
|
|
|
use App\Exception;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Radio;
|
2020-02-06 02:35:13 +00:00
|
|
|
use App\RateLimit;
|
|
|
|
use App\Session;
|
|
|
|
use App\View;
|
|
|
|
use Mezzio\Session\SessionInterface;
|
2019-08-09 15:00:21 +00:00
|
|
|
|
2020-06-26 20:22:53 +00:00
|
|
|
final class ServerRequest extends \Slim\Http\ServerRequest
|
2019-08-09 15:00:21 +00:00
|
|
|
{
|
2020-02-06 02:35:13 +00: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 15:00:21 +00:00
|
|
|
public const ATTR_ACL = 'acl';
|
2020-07-10 04:15:27 +00:00
|
|
|
public const ATTR_CUSTOMIZATION = 'customization';
|
|
|
|
public const ATTR_AUTH = 'auth';
|
2019-08-09 15:00:21 +00:00
|
|
|
public const ATTR_STATION = 'station';
|
|
|
|
public const ATTR_STATION_BACKEND = 'station_backend';
|
|
|
|
public const ATTR_STATION_FRONTEND = 'station_frontend';
|
|
|
|
public const ATTR_STATION_REMOTES = 'station_remotes';
|
|
|
|
public const ATTR_USER = 'user';
|
|
|
|
|
2020-02-06 02:35:13 +00: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);
|
|
|
|
}
|
|
|
|
|
2020-07-10 04:15:27 +00:00
|
|
|
public function getCustomization(): Customization
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
2020-07-10 04:15:27 +00:00
|
|
|
return $this->getAttributeOfClass(self::ATTR_CUSTOMIZATION, Customization::class);
|
|
|
|
}
|
2020-02-06 02:35:13 +00:00
|
|
|
|
2020-07-10 04:15:27 +00:00
|
|
|
public function getAuth(): Auth
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_AUTH, Auth::class);
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStationFrontend(): Radio\Frontend\AbstractFrontend
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_STATION_FRONTEND, Radio\Frontend\AbstractFrontend::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStationBackend(): Radio\Backend\AbstractBackend
|
|
|
|
{
|
|
|
|
return $this->getAttributeOfClass(self::ATTR_STATION_BACKEND, Radio\Backend\AbstractBackend::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Radio\Remote\AdapterProxy[]
|
2020-04-06 23:27:01 +00:00
|
|
|
* @throws Exception\InvalidRequestAttribute
|
2019-08-09 15:00:21 +00:00
|
|
|
*/
|
|
|
|
public function getStationRemotes(): array
|
|
|
|
{
|
|
|
|
$remotes = $this->serverRequest->getAttribute(self::ATTR_STATION_REMOTES);
|
|
|
|
|
|
|
|
if (null === $remotes) {
|
2020-04-06 23:27:01 +00:00
|
|
|
throw new Exception\InvalidRequestAttribute(sprintf(
|
|
|
|
'Attribute "%s" was not set.',
|
|
|
|
self::ATTR_STATION_REMOTES
|
|
|
|
));
|
2019-08-09 15:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $remotes;
|
|
|
|
}
|
2020-02-06 02:35:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $attr
|
|
|
|
* @param string $class_name
|
|
|
|
*
|
|
|
|
* @return mixed
|
2020-04-06 23:27:01 +00:00
|
|
|
* @throws Exception\InvalidRequestAttribute
|
2020-02-06 02:35:13 +00:00
|
|
|
*/
|
|
|
|
protected function getAttributeOfClass($attr, $class_name)
|
|
|
|
{
|
|
|
|
$object = $this->serverRequest->getAttribute($attr);
|
2020-04-06 23:15:59 +00:00
|
|
|
|
|
|
|
if (empty($object)) {
|
|
|
|
throw new Exception\InvalidRequestAttribute(sprintf(
|
|
|
|
'Attribute "%s" is required and is empty in this request',
|
|
|
|
$attr
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($object instanceof $class_name)) {
|
2020-04-06 23:27:01 +00:00
|
|
|
throw new Exception\InvalidRequestAttribute(sprintf(
|
2020-04-06 23:15:59 +00:00
|
|
|
'Attribute "%s" must be of type "%s".',
|
|
|
|
$attr,
|
|
|
|
$class_name
|
|
|
|
));
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 23:15:59 +00:00
|
|
|
return $object;
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
2020-07-10 04:15:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the remote user's IP address as indicated by HTTP headers.
|
|
|
|
*/
|
|
|
|
public function getIp(): ?string
|
|
|
|
{
|
|
|
|
$params = $this->serverRequest->getServerParams();
|
|
|
|
|
2020-10-02 03:21:05 +00:00
|
|
|
$ip = $params['HTTP_CLIENT_IP']
|
2020-07-10 04:15:27 +00:00
|
|
|
?? $params['HTTP_X_FORWARDED_FOR']
|
|
|
|
?? $params['HTTP_X_FORWARDED']
|
|
|
|
?? $params['HTTP_FORWARDED_FOR']
|
|
|
|
?? $params['HTTP_FORWARDED']
|
|
|
|
?? $params['REMOTE_ADDR']
|
2020-10-02 03:21:05 +00:00
|
|
|
?? '';
|
|
|
|
|
|
|
|
// Handle the IP being separated by commas.
|
|
|
|
$ipParts = explode(',', $ip);
|
|
|
|
$ip = array_shift($ipParts);
|
|
|
|
|
|
|
|
return trim($ip);
|
2020-07-10 04:15:27 +00:00
|
|
|
}
|
2019-08-09 15:00:21 +00:00
|
|
|
}
|