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;
|
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
use Aura\Router\Exception\RouteNotFound;
|
2020-04-10 20:01:46 -04:00
|
|
|
use Aviat\AnimeClient\API\Anilist\Model as AnilistModel;
|
2018-11-01 22:12:41 -04:00
|
|
|
use Aviat\AnimeClient\Controller as BaseController;
|
2020-04-10 20:01:46 -04:00
|
|
|
use Aviat\AnimeClient\Model\Settings as SettingsModel;
|
2018-11-01 22:12:41 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2019-12-09 14:34:23 -05:00
|
|
|
use Aviat\Ion\Di\Exception\ContainerException;
|
|
|
|
use Aviat\Ion\Di\Exception\NotFoundException;
|
2018-11-01 22:12:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for user settings
|
|
|
|
*/
|
|
|
|
final class Settings extends BaseController {
|
2018-12-21 15:52:34 -05:00
|
|
|
|
2018-11-01 22:12:41 -04:00
|
|
|
/**
|
2020-04-10 20:01:46 -04:00
|
|
|
* @var AnilistModel
|
2018-11-01 22:12:41 -04:00
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
private AnilistModel $anilistModel;
|
2018-11-01 22:12:41 -04:00
|
|
|
|
|
|
|
/**
|
2020-04-10 20:01:46 -04:00
|
|
|
* @var SettingsModel
|
2018-11-01 22:12:41 -04:00
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
private SettingsModel $settingsModel;
|
2018-11-01 22:12:41 -04:00
|
|
|
|
2018-11-08 11:36:42 -05:00
|
|
|
/**
|
|
|
|
* Settings constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2018-11-08 11:36:42 -05:00
|
|
|
*/
|
2018-11-01 22:12:41 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
parent::__construct($container);
|
|
|
|
|
|
|
|
$this->anilistModel = $container->get('anilist-model');
|
|
|
|
$this->settingsModel = $container->get('settings-model');
|
2019-01-29 15:12:31 -05:00
|
|
|
|
|
|
|
// This is a rare controller where every route is private
|
|
|
|
$this->checkAuth();
|
2018-11-01 22:12:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the user settings, if logged in
|
|
|
|
*/
|
2018-11-08 11:36:42 -05:00
|
|
|
public function index(): void
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
|
|
|
$auth = $this->container->get('auth');
|
|
|
|
$form = $this->settingsModel->getSettingsForm();
|
|
|
|
|
|
|
|
$hasAnilistLogin = $this->config->has(['anilist', 'access_token']);
|
|
|
|
|
|
|
|
$this->outputHTML('settings/settings', [
|
|
|
|
'anilistModel' => $this->anilistModel,
|
|
|
|
'auth' => $auth,
|
|
|
|
'form' => $form,
|
|
|
|
'hasAnilistLogin' => $hasAnilistLogin,
|
|
|
|
'config' => $this->config,
|
|
|
|
'title' => $this->config->get('whose_list') . "'s Settings",
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to save the user's settings
|
|
|
|
*
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws RouteNotFound
|
2018-11-01 22:12:41 -04:00
|
|
|
*/
|
2018-11-08 11:36:42 -05:00
|
|
|
public function update(): void
|
2018-11-01 22:12:41 -04:00
|
|
|
{
|
|
|
|
$post = $this->request->getParsedBody();
|
|
|
|
unset($post['settings-tabs']);
|
|
|
|
|
|
|
|
$saved = $this->settingsModel->saveSettingsFile($post);
|
|
|
|
|
2019-01-08 15:52:53 -05:00
|
|
|
$saved
|
|
|
|
? $this->setFlashMessage('Saved config settings.', 'success')
|
|
|
|
: $this->setFlashMessage('Failed to save config file.', 'error');
|
2018-11-01 22:12:41 -04:00
|
|
|
|
|
|
|
$this->redirect($this->url->generate('settings'), 303);
|
|
|
|
}
|
2018-11-02 12:58:19 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect to Anilist to start Oauth flow
|
|
|
|
*/
|
2018-11-08 11:36:42 -05:00
|
|
|
public function anilistRedirect(): void
|
2018-11-02 12:58:19 -04:00
|
|
|
{
|
2018-11-08 11:36:42 -05:00
|
|
|
$query = http_build_query([
|
|
|
|
'client_id' => $this->config->get(['anilist', 'client_id']),
|
|
|
|
'redirect_uri' => $this->urlGenerator->url('/anilist-oauth'),
|
|
|
|
'response_type' => 'code',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$redirectUrl = "https://anilist.co/api/v2/oauth/authorize?{$query}";
|
2018-11-02 12:58:19 -04:00
|
|
|
|
|
|
|
$this->redirect($redirectUrl, 303);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Oauth callback for Anilist API
|
|
|
|
*/
|
2018-11-08 11:36:42 -05:00
|
|
|
public function anilistCallback(): void
|
2018-11-02 12:58:19 -04:00
|
|
|
{
|
|
|
|
$query = $this->request->getQueryParams();
|
|
|
|
$authCode = $query['code'];
|
|
|
|
$uri = $this->urlGenerator->url('/anilist-oauth');
|
|
|
|
|
|
|
|
$authData = $this->anilistModel->authenticate($authCode, $uri);
|
|
|
|
$settings = $this->settingsModel->getSettings();
|
|
|
|
|
|
|
|
if (array_key_exists('error', $authData))
|
|
|
|
{
|
|
|
|
$this->errorPage(400, 'Error Linking Account', $authData['hint']);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the override config file
|
|
|
|
$anilistSettings = [
|
|
|
|
'access_token' => $authData['access_token'],
|
|
|
|
'access_token_expires' => (time() - 10) + $authData['expires_in'],
|
|
|
|
'refresh_token' => $authData['refresh_token'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$newSettings = $settings;
|
|
|
|
$newSettings['anilist'] = array_merge($settings['anilist'], $anilistSettings);
|
|
|
|
|
|
|
|
foreach ($newSettings['config'] as $key => $value)
|
|
|
|
{
|
|
|
|
$newSettings[$key] = $value;
|
|
|
|
}
|
|
|
|
unset($newSettings['config']);
|
|
|
|
|
|
|
|
$saved = $this->settingsModel->saveSettingsFile($newSettings);
|
|
|
|
|
2018-12-21 15:52:34 -05:00
|
|
|
$saved
|
|
|
|
? $this->setFlashMessage('Linked Anilist Account', 'success')
|
|
|
|
: $this->setFlashMessage('Error Linking Anilist Account', 'error');
|
2018-11-02 12:58:19 -04:00
|
|
|
|
|
|
|
$this->redirect($this->url->generate('settings'), 303);
|
|
|
|
}
|
2018-11-01 22:12:41 -04:00
|
|
|
}
|