2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
2017-02-16 11:09:37 -05:00
|
|
|
* Hummingbird Anime List Client
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2017-02-16 11:09:37 -05:00
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2016-10-20 22:09:36 -04:00
|
|
|
* PHP version 7
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
2015-11-16 11:40:01 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2016-08-30 10:01:18 -04:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-02-16 11:09:37 -05:00
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2017-02-16 11:09:37 -05:00
|
|
|
* @version 4.0
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2015-06-16 11:11:35 -04:00
|
|
|
*/
|
2016-10-20 22:09:36 -04:00
|
|
|
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
|
|
|
|
2017-02-22 14:46:35 -05:00
|
|
|
use function Aviat\AnimeClient\_dir;
|
|
|
|
|
2017-03-08 13:46:50 -05:00
|
|
|
use Aviat\AnimeClient\API\JsonAPI;
|
2016-12-20 12:58:37 -05:00
|
|
|
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
|
|
|
|
use Aviat\Ion\View\{HtmlView, HttpView, JsonView};
|
2016-10-20 22:09:36 -04:00
|
|
|
use InvalidArgumentException;
|
2015-09-17 23:11:18 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
2015-09-17 23:11:18 -04:00
|
|
|
* Controller base, defines output methods
|
2015-10-06 10:44:33 -04:00
|
|
|
*
|
|
|
|
* @property Response object $response
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
class Controller {
|
2017-03-08 16:21:01 -05:00
|
|
|
use ControllerTrait;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
2015-06-30 13:03:20 -04:00
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$this->setContainer($container);
|
2016-03-03 16:53:17 -05:00
|
|
|
$auraUrlGenerator = $container->get('aura-router')->getGenerator();
|
2015-09-17 23:11:18 -04:00
|
|
|
$urlGenerator = $container->get('url-generator');
|
2016-04-21 11:14:21 -04:00
|
|
|
$this->cache = $container->get('cache');
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->config = $container->get('config');
|
|
|
|
$this->request = $container->get('request');
|
|
|
|
$this->response = $container->get('response');
|
2017-02-17 08:25:19 -05:00
|
|
|
|
|
|
|
$this->baseData = array_merge((array)$this->baseData, [
|
|
|
|
'url' => $auraUrlGenerator,
|
|
|
|
'urlGenerator' => $urlGenerator,
|
|
|
|
'auth' => $container->get('auth'),
|
|
|
|
'config' => $this->config
|
|
|
|
]);
|
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2016-01-04 10:53:03 -05:00
|
|
|
|
|
|
|
$session = $container->get('session');
|
2017-02-08 15:48:20 -05:00
|
|
|
$this->session = $session->getSegment(SESSION_SEGMENT);
|
2016-01-04 10:53:03 -05:00
|
|
|
|
|
|
|
// Set a 'previous' flash value for better redirects
|
2017-02-16 14:30:06 -05:00
|
|
|
$serverParams = $this->request->getServerParams();
|
|
|
|
if (array_key_exists('HTTP_REFERER', $serverParams))
|
2016-02-17 11:36:37 -05:00
|
|
|
{
|
2017-02-16 14:30:06 -05:00
|
|
|
$this->session->setFlash('previous', $serverParams['HTTP_REFERER']);
|
2016-02-17 11:36:37 -05:00
|
|
|
}
|
2016-01-04 10:53:03 -05:00
|
|
|
|
|
|
|
// Set a message box if available
|
2017-02-15 11:30:16 -05:00
|
|
|
$this->baseData['message'] = $this->session->getFlash('message');
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 12:55:49 -05:00
|
|
|
public function me()
|
|
|
|
{
|
|
|
|
$username = $this->config->get(['kitsu_username']);
|
|
|
|
$model = $this->container->get('kitsu-model');
|
2017-03-08 13:46:50 -05:00
|
|
|
$data = $model->getUserData($username);
|
|
|
|
$included = JsonAPI::lightlyOrganizeIncludes($data['included']);
|
2017-03-08 16:21:01 -05:00
|
|
|
$relationships = JsonAPI::fillRelationshipsFromIncludes($data['data']['relationships'], $included);
|
2017-03-08 13:46:50 -05:00
|
|
|
$this->outputHTML('me', [
|
|
|
|
'title' => 'About' . $this->config->get('whose_list'),
|
|
|
|
'attributes' => $data['data']['attributes'],
|
2017-03-08 16:21:01 -05:00
|
|
|
'relationships' => $relationships,
|
2017-03-08 13:46:50 -05:00
|
|
|
'included' => $included
|
|
|
|
]);
|
2017-03-08 12:55:49 -05:00
|
|
|
}
|
|
|
|
|
2015-11-04 16:36:54 -05:00
|
|
|
/**
|
|
|
|
* Show the login form
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @param string $status
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 22:09:36 -04:00
|
|
|
public function login(string $status = '')
|
2015-11-04 16:36:54 -05:00
|
|
|
{
|
2016-10-20 22:09:36 -04:00
|
|
|
$message = '';
|
2015-11-04 16:36:54 -05:00
|
|
|
|
|
|
|
$view = new HtmlView($this->container);
|
|
|
|
|
2016-10-20 22:09:36 -04:00
|
|
|
if ($status !== '')
|
2015-11-04 16:36:54 -05:00
|
|
|
{
|
2017-02-16 14:30:06 -05:00
|
|
|
$message = $this->showMessage($view, 'error', $status);
|
2015-11-04 16:36:54 -05:00
|
|
|
}
|
|
|
|
|
2016-01-04 10:53:03 -05:00
|
|
|
// Set the redirect url
|
2017-02-16 14:30:06 -05:00
|
|
|
$this->setSessionRedirect();
|
2016-01-04 10:53:03 -05:00
|
|
|
|
2015-11-04 16:36:54 -05:00
|
|
|
$this->outputHTML('login', [
|
|
|
|
'title' => 'Api login',
|
|
|
|
'message' => $message
|
|
|
|
], $view);
|
|
|
|
}
|
|
|
|
|
2015-11-13 11:33:27 -05:00
|
|
|
/**
|
|
|
|
* Attempt login authentication
|
|
|
|
*
|
2015-11-18 10:48:05 -05:00
|
|
|
* @return void
|
2015-11-13 11:33:27 -05:00
|
|
|
*/
|
2016-12-20 12:58:37 -05:00
|
|
|
public function loginAction()
|
2015-11-13 11:33:27 -05:00
|
|
|
{
|
|
|
|
$auth = $this->container->get('auth');
|
2016-02-17 11:36:37 -05:00
|
|
|
$post = $this->request->getParsedBody();
|
|
|
|
if ($auth->authenticate($post['password']))
|
2015-11-13 11:33:27 -05:00
|
|
|
{
|
2017-02-22 14:46:35 -05:00
|
|
|
$this->sessionRedirect();
|
|
|
|
return;
|
2015-11-13 11:33:27 -05:00
|
|
|
}
|
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$this->setFlashMessage('Invalid username or password.');
|
2017-01-12 15:41:20 -05:00
|
|
|
$this->redirect($this->urlGenerator->url('login'), 303);
|
2015-11-13 11:33:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deauthorize the current user
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
$auth = $this->container->get('auth');
|
|
|
|
$auth->logout();
|
|
|
|
|
2017-01-06 21:39:01 -05:00
|
|
|
$this->redirectToDefaultRoute();
|
2015-11-13 11:33:27 -05:00
|
|
|
}
|
2015-06-11 16:44:52 -04:00
|
|
|
}
|
2015-09-15 13:19:29 -04:00
|
|
|
// End of BaseController.php
|