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
|
|
|
|
*
|
2020-12-10 17:06:50 -05:00
|
|
|
* PHP version 7.4+
|
2018-11-01 22:12:41 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
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
|
2018-11-01 22:12:41 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Controller;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\Controller as BaseController;
|
2020-05-06 13:16:40 -04:00
|
|
|
use Aviat\AnimeClient\Enum\EventType;
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
final class Misc extends BaseController {
|
|
|
|
/**
|
|
|
|
* Purges the API cache
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
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', [
|
|
|
|
'title' => 'Cache cleared'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the login form
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @return void
|
|
|
|
*/
|
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',
|
|
|
|
'message' => $message
|
|
|
|
], $view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt login authentication
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function loginAction(): void
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
|
|
|
$post = $this->request->getParsedBody();
|
|
|
|
|
2020-08-05 21:46:14 -04:00
|
|
|
if ($this->auth->authenticate($post['password']))
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
|
|
|
$this->sessionRedirect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setFlashMessage('Invalid username or password.');
|
|
|
|
$this->redirect($this->url->generate('login'), 303);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deauthorize the current user
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
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
|
|
|
|
*/
|
|
|
|
public function heartbeat(): void
|
|
|
|
{
|
|
|
|
$this->outputJSON(['hasAuth' => $this->auth->isAuthenticated()], 200);
|
|
|
|
}
|
2018-11-01 22:12:41 -04:00
|
|
|
}
|