2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Base Controller
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
namespace AnimeClient\Base;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Base class for controllers, defines output methods
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
class Controller {
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The global configuration object
|
|
|
|
* @var object $config
|
|
|
|
*/
|
2015-06-09 18:18:53 -04:00
|
|
|
protected $config;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
/**
|
|
|
|
* Request object
|
|
|
|
* @var object $request
|
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Response object
|
|
|
|
* @var object $response
|
|
|
|
*/
|
|
|
|
protected $response;
|
|
|
|
|
2015-06-26 16:39:10 -04:00
|
|
|
/**
|
|
|
|
* The api model for the current controller
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common data to be sent to views
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-06-30 13:03:20 -04:00
|
|
|
protected $base_data = [
|
|
|
|
'url_type' => 'anime',
|
|
|
|
'other_type' => 'manga',
|
|
|
|
'nav_routes' => []
|
|
|
|
];
|
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-09-14 10:54:50 -04:00
|
|
|
* @param Config $config
|
2015-06-30 13:03:20 -04:00
|
|
|
* @param array $web
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
public function __construct(Container $container)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->config = $container->get('config');
|
|
|
|
$this->base_data['config'] = $this->config;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->request = $container->get('request');
|
|
|
|
$this->response = $container->get('response');
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2015-06-24 16:01:35 -04:00
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->output();
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Get a class member
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
$allowed = ['request', 'response', 'config'];
|
|
|
|
|
|
|
|
if (in_array($key, $allowed))
|
|
|
|
{
|
|
|
|
return $this->$key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
/**
|
2015-06-24 16:01:35 -04:00
|
|
|
* Get the string output of a partial template
|
2015-05-22 12:36:26 -04:00
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-05-22 12:36:26 -04:00
|
|
|
* @param string $template
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param array|object $data
|
2015-06-24 16:01:35 -04:00
|
|
|
* @return string
|
2015-05-22 12:36:26 -04:00
|
|
|
*/
|
2015-06-24 16:01:35 -04:00
|
|
|
public function load_partial($template, $data=[])
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-06-24 16:01:35 -04:00
|
|
|
if (isset($this->base_data))
|
|
|
|
{
|
|
|
|
$data = array_merge($this->base_data, $data);
|
|
|
|
}
|
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
global $router, $defaultHandler;
|
2015-05-22 12:36:26 -04:00
|
|
|
$route = $router->get_route();
|
|
|
|
$data['route_path'] = ($route) ? $router->get_route()->path : "";
|
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
$defaultHandler->addDataTable('Template Data', $data);
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$template_path = _dir(SRC_DIR, 'views', "{$template}.php");
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
if ( ! is_file($template_path))
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
throw new \InvalidArgumentException("Invalid template : {$path}");
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
extract($data);
|
2015-09-14 10:54:50 -04:00
|
|
|
include _dir(SRC_DIR, 'views', 'header.php');
|
2015-06-16 11:11:35 -04:00
|
|
|
include $template_path;
|
2015-09-14 10:54:50 -04:00
|
|
|
include _dir(SRC_DIR, 'views', 'footer.php');
|
2015-05-22 12:36:26 -04:00
|
|
|
$buffer = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
return $buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output a template to HTML, using the provided data
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-24 16:01:35 -04:00
|
|
|
* @param string $template
|
|
|
|
* @param array|object $data
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function outputHTML($template, $data=[])
|
|
|
|
{
|
|
|
|
$buffer = $this->load_partial($template, $data);
|
|
|
|
|
|
|
|
$this->response->content->setType('text/html');
|
|
|
|
$this->response->content->set($buffer);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output json with the proper content type
|
|
|
|
*
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param mixed $data
|
2015-05-22 12:36:26 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function outputJSON($data)
|
|
|
|
{
|
|
|
|
if ( ! is_string($data))
|
|
|
|
{
|
|
|
|
$data = json_encode($data);
|
|
|
|
}
|
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
$this->response->content->setType('application/json');
|
|
|
|
$this->response->content->set($data);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect to the selected page
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param string $url
|
|
|
|
* @param int $code
|
2015-09-14 10:54:50 -04:00
|
|
|
* @param string $type
|
2015-06-16 11:11:35 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function redirect($url, $code, $type="anime")
|
|
|
|
{
|
2015-07-02 14:04:04 -04:00
|
|
|
$url = $this->config->full_url($url, $type);
|
2015-06-16 11:11:35 -04:00
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
$this->response->redirect->to($url, $code);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a message box to the page
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-24 16:01:35 -04:00
|
|
|
* @param string $type
|
|
|
|
* @param string $message
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function show_message($type, $message)
|
|
|
|
{
|
|
|
|
return $this->load_partial('message', [
|
|
|
|
'stat_class' => $type,
|
|
|
|
'message' => $message
|
|
|
|
]);
|
2015-06-16 11:11:35 -04:00
|
|
|
}
|
2015-06-17 08:50:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the api session
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-17 08:50:01 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
session_destroy();
|
2015-07-02 14:04:04 -04:00
|
|
|
$this->response->redirect->seeOther($this->config->full_url(''));
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the login form
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-24 16:01:35 -04:00
|
|
|
* @param string $status
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function login($status="")
|
|
|
|
{
|
|
|
|
$message = "";
|
|
|
|
|
|
|
|
if ($status != "")
|
|
|
|
{
|
|
|
|
$message = $this->show_message('error', $status);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->outputHTML('login', [
|
|
|
|
'title' => 'Api login',
|
|
|
|
'message' => $message
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to log in with the api
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function login_action()
|
|
|
|
{
|
2015-06-26 16:39:10 -04:00
|
|
|
if (
|
|
|
|
$this->model->authenticate(
|
|
|
|
$this->config->hummingbird_username,
|
|
|
|
$this->request->post->get('password')
|
|
|
|
)
|
|
|
|
)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2015-07-02 14:04:04 -04:00
|
|
|
$this->response->redirect->afterPost($this->config->full_url('', $this->base_data['url_type']));
|
2015-06-24 16:01:35 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->login("Invalid username or password.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the appropriate response
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-24 16:01:35 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function output()
|
|
|
|
{
|
|
|
|
// send status
|
2015-06-24 20:57:20 -04:00
|
|
|
@header($this->response->status->get(), true, $this->response->status->getCode());
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
// headers
|
|
|
|
foreach($this->response->headers->get() as $label => $value)
|
|
|
|
{
|
2015-06-24 20:57:20 -04:00
|
|
|
@header("{$label}: {$value}");
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// cookies
|
|
|
|
foreach($this->response->cookies->get() as $name => $cookie)
|
|
|
|
{
|
2015-06-26 16:39:10 -04:00
|
|
|
@setcookie(
|
|
|
|
$name,
|
|
|
|
$cookie['value'],
|
|
|
|
$cookie['expire'],
|
|
|
|
$cookie['path'],
|
|
|
|
$cookie['domain'],
|
|
|
|
$cookie['secure'],
|
|
|
|
$cookie['httponly']
|
|
|
|
);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// send the actual response
|
|
|
|
echo $this->response->content->get();
|
2015-06-17 08:50:01 -04:00
|
|
|
}
|
2015-06-11 16:44:52 -04:00
|
|
|
}
|
2015-09-14 10:54:50 -04:00
|
|
|
// End of BaseController.php
|