2013-05-03 17:02:53 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
2014-05-12 18:26:17 +02:00
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
2013-05-16 03:57:02 +02:00
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
2014-05-12 18:26:17 +02:00
|
|
|
* @link https://www.phptesting.org/
|
2013-05-16 03:16:56 +02:00
|
|
|
*/
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor;
|
2013-05-16 03:57:02 +02:00
|
|
|
|
|
|
|
use b8;
|
2014-05-09 13:04:42 +02:00
|
|
|
use b8\Exception\HttpException;
|
2014-02-24 16:30:44 +01:00
|
|
|
use b8\Http\Response;
|
2013-05-22 17:36:55 +02:00
|
|
|
use b8\Http\Response\RedirectResponse;
|
|
|
|
use b8\View;
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
|
|
|
* PHPCI Front Controller
|
2013-05-16 03:57:02 +02:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
2013-05-16 03:16:56 +02:00
|
|
|
*/
|
2013-05-03 17:02:53 +02:00
|
|
|
class Application extends b8\Application
|
|
|
|
{
|
2015-01-11 11:44:34 +01:00
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Controller
|
2015-01-11 11:44:34 +01:00
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Initialise PHPCI - Handles session verification, routing, etc.
|
|
|
|
*/
|
2014-02-24 16:30:44 +01:00
|
|
|
public function init()
|
2013-05-16 03:57:02 +02:00
|
|
|
{
|
2014-02-24 16:30:44 +01:00
|
|
|
$request =& $this->request;
|
2016-04-20 17:39:48 +02:00
|
|
|
$route = '/:controller/:action';
|
|
|
|
$opts = ['controller' => 'Home', 'action' => 'index'];
|
2013-05-16 03:57:02 +02:00
|
|
|
|
2014-04-16 13:37:32 +02:00
|
|
|
// Inlined as a closure to fix "using $this when not in object context" on 5.3
|
|
|
|
$validateSession = function () {
|
2016-07-21 19:02:11 +02:00
|
|
|
if (!empty($_SESSION['php-censor-user-id'])) {
|
|
|
|
$user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['php-censor-user-id']);
|
2014-04-16 13:37:32 +02:00
|
|
|
|
|
|
|
if ($user) {
|
2016-07-21 19:02:11 +02:00
|
|
|
$_SESSION['php-censor-user'] = $user;
|
2014-04-16 13:37:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-21 19:02:11 +02:00
|
|
|
unset($_SESSION['php-censor-user-id']);
|
2014-04-16 13:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$skipAuth = [$this, 'shouldSkipAuth'];
|
2014-07-15 11:28:16 +02:00
|
|
|
|
2014-04-16 13:37:32 +02:00
|
|
|
// Handler for the route we're about to register, checks for a valid session where necessary:
|
2014-07-15 11:28:16 +02:00
|
|
|
$routeHandler = function (&$route, Response &$response) use (&$request, $validateSession, $skipAuth) {
|
2016-04-20 17:39:48 +02:00
|
|
|
$skipValidation = in_array($route['controller'], ['session', 'webhook', 'build-status']);
|
2013-06-04 20:49:26 +02:00
|
|
|
|
2015-02-12 15:11:58 +01:00
|
|
|
if (!$skipValidation && !$validateSession() && (!is_callable($skipAuth) || !$skipAuth())) {
|
2014-02-24 16:30:44 +01:00
|
|
|
if ($request->isAjax()) {
|
|
|
|
$response->setResponseCode(401);
|
|
|
|
$response->setContent('');
|
|
|
|
} else {
|
2016-07-21 19:02:11 +02:00
|
|
|
$_SESSION['php-censor-login-redirect'] = substr($request->getPath(), 1);
|
2014-02-24 16:30:44 +01:00
|
|
|
$response = new RedirectResponse($response);
|
2016-07-21 17:20:34 +02:00
|
|
|
$response->setHeader('Location', APP_URL . 'session/login');
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
2013-10-08 09:23:07 +02:00
|
|
|
|
2014-02-24 16:30:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-16 03:57:02 +02:00
|
|
|
|
2014-02-24 16:30:44 +01:00
|
|
|
return true;
|
2014-04-16 13:37:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$this->router->clearRoutes();
|
|
|
|
$this->router->register($route, $opts, $routeHandler);
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
2014-12-08 12:25:33 +01:00
|
|
|
|
2014-02-24 16:30:44 +01:00
|
|
|
/**
|
2014-12-07 17:51:00 +01:00
|
|
|
* Handle an incoming web request.
|
|
|
|
*
|
2016-05-09 08:20:26 +02:00
|
|
|
* @return Response
|
2014-12-07 17:51:00 +01:00
|
|
|
*/
|
2014-02-24 16:30:44 +01:00
|
|
|
public function handleRequest()
|
|
|
|
{
|
2014-05-09 13:04:42 +02:00
|
|
|
try {
|
|
|
|
$this->response = parent::handleRequest();
|
2014-05-09 13:10:48 +02:00
|
|
|
} catch (HttpException $ex) {
|
|
|
|
$this->config->set('page_title', 'Error');
|
|
|
|
|
|
|
|
$view = new View('exception');
|
|
|
|
$view->exception = $ex;
|
|
|
|
|
|
|
|
$this->response->setResponseCode($ex->getErrorCode());
|
|
|
|
$this->response->setContent($view->render());
|
2014-05-09 13:04:42 +02:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$this->config->set('page_title', 'Error');
|
|
|
|
|
|
|
|
$view = new View('exception');
|
|
|
|
$view->exception = $ex;
|
2014-05-09 13:10:48 +02:00
|
|
|
|
|
|
|
$this->response->setResponseCode(500);
|
2014-05-09 13:04:42 +02:00
|
|
|
$this->response->setContent($view->render());
|
|
|
|
}
|
2013-10-08 09:23:07 +02:00
|
|
|
|
2015-01-11 11:44:34 +01:00
|
|
|
if ($this->response->hasLayout() && $this->controller->layout) {
|
2014-12-02 17:26:55 +01:00
|
|
|
$this->setLayoutVariables($this->controller->layout);
|
2014-04-24 17:25:24 +02:00
|
|
|
|
2014-12-02 17:26:55 +01:00
|
|
|
$this->controller->layout->content = $this->response->getContent();
|
|
|
|
$this->response->setContent($this->controller->layout->render());
|
2013-05-22 17:36:55 +02:00
|
|
|
}
|
2013-06-04 20:49:26 +02:00
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
return $this->response;
|
2013-05-16 03:57:02 +02:00
|
|
|
}
|
2014-12-02 17:26:55 +01:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Loads a particular controller, and injects our layout view into it.
|
|
|
|
* @param $class
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-12-02 17:26:55 +01:00
|
|
|
protected function loadController($class)
|
|
|
|
{
|
2016-04-20 17:39:48 +02:00
|
|
|
$controller = parent::loadController($class);
|
|
|
|
$controller->layout = new View('layout');
|
2016-07-19 13:05:02 +02:00
|
|
|
$controller->layout->title = 'PHP Censor';
|
2016-04-20 17:39:48 +02:00
|
|
|
$controller->layout->breadcrumb = [];
|
2014-12-02 17:26:55 +01:00
|
|
|
|
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Injects variables into the layout before rendering it.
|
|
|
|
* @param View $layout
|
|
|
|
*/
|
2014-12-02 17:26:55 +01:00
|
|
|
protected function setLayoutVariables(View &$layout)
|
|
|
|
{
|
2016-04-20 17:39:48 +02:00
|
|
|
$groups = [];
|
2015-10-08 21:22:43 +02:00
|
|
|
$groupStore = b8\Store\Factory::getStore('ProjectGroup');
|
2016-04-20 17:39:48 +02:00
|
|
|
$groupList = $groupStore->getWhere([], 100, 0, [], ['title' => 'ASC']);
|
2015-10-08 17:33:01 +02:00
|
|
|
|
|
|
|
foreach ($groupList['items'] as $group) {
|
2016-04-20 17:39:48 +02:00
|
|
|
$thisGroup = ['title' => $group->getTitle()];
|
2015-10-08 17:33:01 +02:00
|
|
|
$projects = b8\Store\Factory::getStore('Project')->getByGroupId($group->getId());
|
|
|
|
$thisGroup['projects'] = $projects['items'];
|
|
|
|
$groups[] = $thisGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
$layout->groups = $groups;
|
2014-12-02 17:26:55 +01:00
|
|
|
}
|
2014-12-22 16:48:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether we should skip auth (because it is disabled)
|
2017-01-07 14:54:33 +01:00
|
|
|
*
|
2014-12-22 16:48:35 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldSkipAuth()
|
|
|
|
{
|
|
|
|
$config = b8\Config::getInstance();
|
2016-07-21 19:02:11 +02:00
|
|
|
$state = (bool)$config->get('php-censor.authentication_settings.state', false);
|
|
|
|
$userId = $config->get('php-censor.authentication_settings.user_id', 0);
|
2014-12-22 16:48:35 +01:00
|
|
|
|
|
|
|
if (false !== $state && 0 != (int)$userId) {
|
|
|
|
$user = b8\Store\Factory::getStore('User')
|
|
|
|
->getByPrimaryKey($userId);
|
|
|
|
|
|
|
|
if ($user) {
|
2016-07-21 19:02:11 +02:00
|
|
|
$_SESSION['php-censor-user'] = $user;
|
2014-12-22 16:48:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-16 03:57:02 +02:00
|
|
|
}
|