2013-05-03 17:02:53 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
2013-05-16 03:57:02 +02:00
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://www.phptesting.org/
|
2013-05-16 03:16:56 +02:00
|
|
|
*/
|
2013-05-03 17:02:53 +02:00
|
|
|
|
|
|
|
namespace PHPCI;
|
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
|
|
|
|
{
|
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;
|
|
|
|
$route = '/:controller/:action';
|
2014-03-03 18:10:33 +01:00
|
|
|
$opts = array('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 () {
|
|
|
|
if (!empty($_SESSION['user_id'])) {
|
|
|
|
$user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['user_id']);
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
$_SESSION['user'] = $user;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($_SESSION['user_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handler for the route we're about to register, checks for a valid session where necessary:
|
|
|
|
$routeHandler = function (&$route, Response &$response) use (&$request, $validateSession) {
|
2014-02-24 16:30:44 +01:00
|
|
|
$skipValidation = in_array($route['controller'], array('session', 'webhook', 'build-status'));
|
2013-06-04 20:49:26 +02:00
|
|
|
|
2014-04-16 13:37:32 +02:00
|
|
|
if (!$skipValidation && !$validateSession()) {
|
2014-02-24 16:30:44 +01:00
|
|
|
if ($request->isAjax()) {
|
|
|
|
$response->setResponseCode(401);
|
|
|
|
$response->setContent('');
|
|
|
|
} else {
|
2014-05-09 12:41:34 +02:00
|
|
|
$_SESSION['login_redirect'] = substr($request->getPath(), 1);
|
2014-02-24 16:30:44 +01:00
|
|
|
$response = new RedirectResponse($response);
|
|
|
|
$response->setHeader('Location', PHPCI_URL.'session/login');
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Handle an incoming web request.
|
|
|
|
*/
|
|
|
|
public function handleRequest()
|
|
|
|
{
|
2014-05-09 13:04:42 +02:00
|
|
|
try {
|
|
|
|
$this->response = parent::handleRequest();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$this->config->set('page_title', 'Error');
|
|
|
|
|
|
|
|
$view = new View('exception');
|
|
|
|
$view->exception = $ex;
|
|
|
|
$this->response->setContent($view->render());
|
|
|
|
}
|
2013-10-08 09:23:07 +02:00
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
if (View::exists('layout') && $this->response->hasLayout()) {
|
|
|
|
$view = new View('layout');
|
2014-04-24 17:25:24 +02:00
|
|
|
$pageTitle = $this->config->get('page_title', null);
|
|
|
|
|
|
|
|
if (!is_null($pageTitle)) {
|
|
|
|
$view->title = $pageTitle;
|
|
|
|
}
|
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
$view->content = $this->response->getContent();
|
|
|
|
$this->response->setContent($view->render());
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|