2018-02-01 11:49:40 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Controller\Api;
|
2018-02-01 11:49:40 +00:00
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
use App\Acl;
|
2019-09-10 16:40:31 +00:00
|
|
|
use App\Exception\PermissionDeniedException;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2019-02-25 06:43:00 +00:00
|
|
|
use App\Radio\AutoDJ;
|
2018-08-04 22:05:14 +00:00
|
|
|
use App\Radio\Backend\Liquidsoap;
|
2020-11-30 09:55:30 +00:00
|
|
|
use App\Sync\Task\NowPlayingTask;
|
2019-08-01 15:53:16 +00:00
|
|
|
use Monolog\Logger;
|
2018-11-17 15:30:28 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2018-02-01 11:49:40 +00:00
|
|
|
|
|
|
|
class InternalController
|
|
|
|
{
|
2019-08-01 15:53:16 +00:00
|
|
|
public function __construct(
|
2021-04-23 05:24:12 +00:00
|
|
|
protected NowPlayingTask $syncNowPlaying,
|
|
|
|
protected AutoDJ $autodj,
|
|
|
|
protected Logger $logger
|
2019-08-01 15:53:16 +00:00
|
|
|
) {
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
public function authAction(ServerRequest $request, Response $response): ResponseInterface
|
2018-02-01 11:49:40 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$this->checkStationAuth($request);
|
2018-02-06 17:55:58 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$station = $request->getStation();
|
2018-02-01 11:49:40 +00:00
|
|
|
if (!$station->getEnableStreamers()) {
|
2021-01-11 11:08:06 +00:00
|
|
|
$this->logger->error(
|
|
|
|
'Attempted DJ authentication when streamers are disabled on this station.',
|
|
|
|
[
|
|
|
|
'station_id' => $station->getId(),
|
|
|
|
'station_name' => $station->getName(),
|
|
|
|
]
|
|
|
|
);
|
2019-08-01 15:53:16 +00:00
|
|
|
|
2019-08-07 04:33:55 +00:00
|
|
|
$response->getBody()->write('false');
|
|
|
|
return $response;
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$params = $request->getParams();
|
2019-09-12 15:50:30 +00:00
|
|
|
$user = $params['dj-user'] ?? '';
|
|
|
|
$pass = $params['dj-password'] ?? '';
|
2018-02-08 13:45:45 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$adapter = $request->getStationBackend();
|
2018-03-12 00:32:27 +00:00
|
|
|
if ($adapter instanceof Liquidsoap) {
|
2019-08-07 04:33:55 +00:00
|
|
|
$response->getBody()->write($adapter->authenticateStreamer($station, $user, $pass));
|
|
|
|
return $response;
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
2018-08-19 10:40:05 +00:00
|
|
|
|
2019-08-07 04:33:55 +00:00
|
|
|
$response->getBody()->write('false');
|
|
|
|
return $response;
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
protected function checkStationAuth(ServerRequest $request): void
|
2019-09-04 18:00:51 +00:00
|
|
|
{
|
|
|
|
$station = $request->getStation();
|
|
|
|
|
2021-01-11 11:08:06 +00:00
|
|
|
$acl = $request->getAcl();
|
|
|
|
if ($acl->isAllowed(Acl::GLOBAL_VIEW, $station->getId())) {
|
2019-09-04 18:00:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = $request->getParams();
|
|
|
|
$auth_key = $params['api_auth'];
|
|
|
|
if (!$station->validateAdapterApiKey($auth_key)) {
|
2021-01-11 11:08:06 +00:00
|
|
|
$this->logger->error(
|
|
|
|
'Invalid API key supplied for internal API call.',
|
|
|
|
[
|
|
|
|
'station_id' => $station->getId(),
|
|
|
|
'station_name' => $station->getName(),
|
|
|
|
]
|
|
|
|
);
|
2019-09-04 18:00:51 +00:00
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
throw new PermissionDeniedException();
|
2019-09-04 18:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
public function nextsongAction(ServerRequest $request, Response $response): ResponseInterface
|
2018-02-01 11:49:40 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$this->checkStationAuth($request);
|
2018-02-06 17:55:58 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$params = $request->getParams();
|
2019-08-07 04:33:55 +00:00
|
|
|
$as_autodj = isset($params['api_auth']);
|
2018-02-01 11:49:40 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$response->getBody()->write($this->autodj->annotateNextSong($request->getStation(), $as_autodj));
|
2019-08-07 04:33:55 +00:00
|
|
|
return $response;
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
public function djonAction(ServerRequest $request, Response $response): ResponseInterface
|
2018-02-01 11:49:40 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$this->checkStationAuth($request);
|
2018-02-06 17:55:58 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$adapter = $request->getStationBackend();
|
2018-03-12 00:32:27 +00:00
|
|
|
if ($adapter instanceof Liquidsoap) {
|
2019-08-09 15:00:21 +00:00
|
|
|
$station = $request->getStation();
|
2020-01-29 02:23:55 +00:00
|
|
|
$user = $request->getParam('dj-user', '');
|
2019-08-01 15:53:16 +00:00
|
|
|
|
2021-01-11 11:08:06 +00:00
|
|
|
$this->logger->notice(
|
|
|
|
'Received "DJ connected" ping from Liquidsoap.',
|
|
|
|
[
|
|
|
|
'station_id' => $station->getId(),
|
|
|
|
'station_name' => $station->getName(),
|
|
|
|
'dj' => $user,
|
|
|
|
]
|
|
|
|
);
|
2019-08-01 15:53:16 +00:00
|
|
|
|
2020-01-29 02:23:55 +00:00
|
|
|
$response->getBody()->write($adapter->onConnect($station, $user));
|
|
|
|
return $response;
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 04:33:55 +00:00
|
|
|
$response->getBody()->write('received');
|
|
|
|
return $response;
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
public function djoffAction(ServerRequest $request, Response $response): ResponseInterface
|
2018-02-03 21:54:12 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$this->checkStationAuth($request);
|
2018-02-06 17:55:58 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$adapter = $request->getStationBackend();
|
2018-03-12 00:32:27 +00:00
|
|
|
if ($adapter instanceof Liquidsoap) {
|
2019-08-09 15:00:21 +00:00
|
|
|
$station = $request->getStation();
|
2020-01-29 02:23:55 +00:00
|
|
|
$user = $request->getParam('dj-user', '');
|
2019-08-01 15:53:16 +00:00
|
|
|
|
2021-01-11 11:08:06 +00:00
|
|
|
$this->logger->notice(
|
|
|
|
'Received "DJ disconnected" ping from Liquidsoap.',
|
|
|
|
[
|
|
|
|
'station_id' => $station->getId(),
|
|
|
|
'station_name' => $station->getName(),
|
|
|
|
'dj' => $user,
|
|
|
|
]
|
|
|
|
);
|
2019-08-01 15:53:16 +00:00
|
|
|
|
2020-01-29 02:23:55 +00:00
|
|
|
$response->getBody()->write($adapter->onDisconnect($station, $user));
|
|
|
|
return $response;
|
2018-03-12 00:32:27 +00:00
|
|
|
}
|
2018-02-03 21:54:12 +00:00
|
|
|
|
2019-08-07 04:33:55 +00:00
|
|
|
$response->getBody()->write('received');
|
|
|
|
return $response;
|
2018-02-03 21:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
public function feedbackAction(ServerRequest $request, Response $response): ResponseInterface
|
2018-02-03 21:54:12 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$this->checkStationAuth($request);
|
2018-02-06 17:55:58 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$station = $request->getStation();
|
2018-02-03 21:54:12 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
$body = $request->getParams();
|
2019-02-27 19:18:12 +00:00
|
|
|
|
2021-01-11 11:08:06 +00:00
|
|
|
$this->syncNowPlaying->queueStation(
|
|
|
|
$station,
|
|
|
|
[
|
|
|
|
'song_id' => $body['song'] ?? null,
|
|
|
|
'media_id' => $body['media'] ?? null,
|
|
|
|
'playlist_id' => $body['playlist'] ?? null,
|
|
|
|
]
|
|
|
|
);
|
2019-02-25 06:43:00 +00:00
|
|
|
|
2019-08-07 04:33:55 +00:00
|
|
|
$response->getBody()->write('OK');
|
|
|
|
return $response;
|
2018-02-03 21:54:12 +00:00
|
|
|
}
|
2018-07-23 08:07:11 +00:00
|
|
|
}
|