2013-05-22 17:36:55 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor;
|
2013-05-22 17:36:55 +02:00
|
|
|
|
|
|
|
use b8\Config;
|
2015-02-12 12:42:09 +01:00
|
|
|
use b8\Exception\HttpException\ForbiddenException;
|
2013-05-22 17:36:55 +02:00
|
|
|
use b8\Http\Request;
|
|
|
|
use b8\Http\Response;
|
|
|
|
use b8\View;
|
|
|
|
|
|
|
|
class Controller extends \b8\Controller
|
|
|
|
{
|
2013-10-08 19:24:20 +02:00
|
|
|
/**
|
|
|
|
* @var \b8\View
|
|
|
|
*/
|
|
|
|
protected $controllerView;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \b8\View
|
|
|
|
*/
|
|
|
|
protected $view;
|
|
|
|
|
2015-01-11 11:44:34 +01:00
|
|
|
/**
|
|
|
|
* @var \b8\View
|
|
|
|
*/
|
|
|
|
public $layout;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Initialise the controller.
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
// Extended by actual controllers.
|
|
|
|
}
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
* @param Request $request
|
|
|
|
* @param Response $response
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
public function __construct(Config $config, Request $request, Response $response)
|
|
|
|
{
|
|
|
|
parent::__construct($config, $request, $response);
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$class = explode('\\', get_class($this));
|
|
|
|
$this->className = substr(array_pop($class), 0, -10);
|
|
|
|
$this->setControllerView();
|
|
|
|
}
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Set the view that this controller should use.
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function setControllerView()
|
|
|
|
{
|
|
|
|
if (View::exists($this->className)) {
|
|
|
|
$this->controllerView = new View($this->className);
|
|
|
|
} else {
|
2014-02-24 16:30:44 +01:00
|
|
|
$this->controllerView = new View\Template('{@content}');
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Set the view that this controller action should use.
|
|
|
|
* @param $action
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function setView($action)
|
|
|
|
{
|
|
|
|
if (View::exists($this->className . '/' . $action)) {
|
|
|
|
$this->view = new View($this->className . '/' . $action);
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle the incoming request.
|
|
|
|
* @param $action
|
|
|
|
* @param $actionParams
|
2016-05-09 08:20:26 +02:00
|
|
|
*
|
|
|
|
* @return Response
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
public function handleAction($action, $actionParams)
|
|
|
|
{
|
|
|
|
$this->setView($action);
|
|
|
|
$response = parent::handleAction($action, $actionParams);
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2015-02-12 13:37:56 +01:00
|
|
|
if ($response instanceof Response) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (is_string($response)) {
|
|
|
|
$this->controllerView->content = $response;
|
|
|
|
} elseif (isset($this->view)) {
|
|
|
|
$this->controllerView->content = $this->view->render();
|
|
|
|
}
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$this->response->setContent($this->controllerView->render());
|
2013-05-22 17:36:55 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
return $this->response;
|
|
|
|
}
|
2014-07-11 16:29:27 +02:00
|
|
|
|
2014-12-02 17:26:55 +01:00
|
|
|
/**
|
|
|
|
* Require that the currently logged in user is an administrator.
|
|
|
|
* @throws ForbiddenException
|
|
|
|
*/
|
2014-07-11 16:29:27 +02:00
|
|
|
protected function requireAdmin()
|
|
|
|
{
|
2014-12-02 17:26:55 +01:00
|
|
|
if (!$this->currentUserIsAdmin()) {
|
2014-07-11 16:29:27 +02:00
|
|
|
throw new ForbiddenException('You do not have permission to do that.');
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 17:26:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the currently logged in user is an administrator.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function currentUserIsAdmin()
|
|
|
|
{
|
2016-07-21 19:02:11 +02:00
|
|
|
return $_SESSION['php-censor-user']->getIsAdmin();
|
2014-12-02 17:26:55 +01:00
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|