2018-11-01 22:12:41 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2018-11-01 22:12:41 -04:00
|
|
|
*
|
2022-03-04 15:50:35 -05:00
|
|
|
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
|
2018-11-01 22:12:41 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2022-03-04 15:50:35 -05:00
|
|
|
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
|
2018-11-01 22:12:41 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Controller;
|
|
|
|
|
2023-03-16 13:04:55 -04:00
|
|
|
use Aviat\AnimeClient\API\Kitsu\Model;
|
2023-05-09 12:49:36 -04:00
|
|
|
use Aviat\AnimeClient\API\Kitsu\Transformer\{CharacterTransformer, PersonTransformer};
|
2018-11-01 22:12:41 -04:00
|
|
|
use Aviat\AnimeClient\Controller as BaseController;
|
2020-05-06 13:16:40 -04:00
|
|
|
use Aviat\AnimeClient\Enum\EventType;
|
2023-05-09 12:49:36 -04:00
|
|
|
use Aviat\Ion\Attribute\{DefaultController, Route};
|
2023-03-16 13:04:55 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2020-05-06 13:16:40 -04:00
|
|
|
use Aviat\Ion\Event;
|
2018-11-01 22:12:41 -04:00
|
|
|
use Aviat\Ion\View\HtmlView;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for handling routes that don't fit elsewhere
|
|
|
|
*/
|
2023-03-16 13:04:55 -04:00
|
|
|
#[DefaultController]
|
2022-03-03 17:26:09 -05:00
|
|
|
final class Misc extends BaseController
|
|
|
|
{
|
2023-03-16 13:04:55 -04:00
|
|
|
private Model $model;
|
|
|
|
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
parent::__construct($container);
|
|
|
|
$this->model = $container->get('kitsu-model');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect to the default controller/url from an empty path
|
|
|
|
*/
|
|
|
|
#[Route('index_redirect', '/')]
|
|
|
|
public function index(): void
|
|
|
|
{
|
|
|
|
parent::redirectToDefaultRoute();
|
|
|
|
}
|
|
|
|
|
2018-11-01 22:12:41 -04:00
|
|
|
/**
|
|
|
|
* Purges the API cache
|
|
|
|
*/
|
2023-03-16 13:04:55 -04:00
|
|
|
#[Route('cache_purge', '/cache_purge')]
|
2018-11-09 10:38:35 -05:00
|
|
|
public function clearCache(): void
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
2020-05-06 13:16:40 -04:00
|
|
|
$this->checkAuth();
|
|
|
|
|
|
|
|
Event::emit(EventType::CLEAR_CACHE);
|
|
|
|
|
2018-11-01 22:12:41 -04:00
|
|
|
$this->outputHTML('blank', [
|
2022-03-03 17:26:09 -05:00
|
|
|
'title' => 'Cache cleared',
|
2018-11-01 22:12:41 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the login form
|
|
|
|
*/
|
2023-03-16 13:04:55 -04:00
|
|
|
#[Route('login', '/login')]
|
2018-11-09 10:38:35 -05:00
|
|
|
public function login(string $status = ''): void
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
|
|
|
$message = '';
|
|
|
|
|
|
|
|
$view = new HtmlView($this->container);
|
|
|
|
|
|
|
|
if ($status !== '')
|
|
|
|
{
|
|
|
|
$message = $this->showMessage($view, 'error', $status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the redirect url
|
|
|
|
$this->setSessionRedirect();
|
|
|
|
|
|
|
|
$this->outputHTML('login', [
|
|
|
|
'title' => 'Api login',
|
2022-03-03 17:26:09 -05:00
|
|
|
'message' => $message,
|
2018-11-01 22:12:41 -04:00
|
|
|
], $view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt login authentication
|
|
|
|
*/
|
2023-03-16 13:04:55 -04:00
|
|
|
#[Route('login.post', '/login', Route::POST)]
|
2018-11-09 10:38:35 -05:00
|
|
|
public function loginAction(): void
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$post = (array) $this->request->getParsedBody();
|
2018-11-01 22:12:41 -04:00
|
|
|
|
2020-08-05 21:46:14 -04:00
|
|
|
if ($this->auth->authenticate($post['password']))
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
|
|
|
$this->sessionRedirect();
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2018-11-01 22:12:41 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setFlashMessage('Invalid username or password.');
|
2021-02-11 19:54:22 -05:00
|
|
|
|
|
|
|
$redirectUrl = $this->url->generate('login');
|
|
|
|
$redirectUrl = ($redirectUrl !== FALSE) ? $redirectUrl : '';
|
|
|
|
|
|
|
|
$this->redirect($redirectUrl, 303);
|
2018-11-01 22:12:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deauthorize the current user
|
|
|
|
*/
|
2023-03-16 13:04:55 -04:00
|
|
|
#[Route('logout', '/logout')]
|
2018-11-09 10:38:35 -05:00
|
|
|
public function logout(): void
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
2020-08-05 21:46:14 -04:00
|
|
|
$this->auth->logout();
|
2018-11-01 22:12:41 -04:00
|
|
|
|
|
|
|
$this->redirectToDefaultRoute();
|
|
|
|
}
|
2020-08-05 21:46:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the current user is logged in
|
|
|
|
*/
|
2023-03-16 13:04:55 -04:00
|
|
|
#[Route('heartbeat', '/heartbeat')]
|
2020-08-05 21:46:14 -04:00
|
|
|
public function heartbeat(): void
|
|
|
|
{
|
|
|
|
$this->outputJSON(['hasAuth' => $this->auth->isAuthenticated()], 200);
|
|
|
|
}
|
2023-03-16 13:04:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show information about a character
|
|
|
|
*/
|
|
|
|
#[Route('character', '/character/{slug}')]
|
|
|
|
public function character(string $slug): void
|
|
|
|
{
|
|
|
|
$rawData = $this->model->getCharacter($slug);
|
|
|
|
|
|
|
|
if (( ! array_key_exists('data', $rawData)) || empty($rawData['data']))
|
|
|
|
{
|
|
|
|
$this->notFound(
|
|
|
|
$this->formatTitle(
|
|
|
|
'Characters',
|
|
|
|
'Character not found'
|
|
|
|
),
|
|
|
|
'Character Not Found'
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = (new CharacterTransformer())->transform($rawData)->toArray();
|
|
|
|
|
|
|
|
$this->outputHTML('character/details', [
|
|
|
|
'title' => $this->formatTitle(
|
|
|
|
'Characters',
|
|
|
|
$data['name']
|
|
|
|
),
|
|
|
|
'data' => $data,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show information about a person
|
|
|
|
*/
|
|
|
|
#[Route('person', '/people/{slug}')]
|
|
|
|
public function person(string $slug): void
|
|
|
|
{
|
|
|
|
$rawData = $this->model->getPerson($slug);
|
|
|
|
$data = (new PersonTransformer())->transform($rawData)->toArray();
|
|
|
|
|
|
|
|
if (( ! array_key_exists('data', $rawData)) || empty($rawData['data']))
|
|
|
|
{
|
|
|
|
$this->notFound(
|
|
|
|
$this->formatTitle(
|
|
|
|
'People',
|
|
|
|
'Person not found'
|
|
|
|
),
|
|
|
|
'Person Not Found'
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->outputHTML('person/details', [
|
|
|
|
'title' => $this->formatTitle(
|
|
|
|
'People',
|
|
|
|
$data['name']
|
|
|
|
),
|
|
|
|
'data' => $data,
|
|
|
|
]);
|
|
|
|
}
|
2022-03-03 17:26:09 -05:00
|
|
|
}
|