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;
|
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
|
|
|
|
{
|
2013-05-16 17:46:30 +02:00
|
|
|
/**
|
|
|
|
* Handle an incoming web request.
|
|
|
|
*/
|
2013-05-16 03:57:02 +02:00
|
|
|
public function handleRequest()
|
|
|
|
{
|
2013-05-22 17:36:55 +02:00
|
|
|
$this->initRequest();
|
2013-05-16 03:57:02 +02:00
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
// Validate the user's session unless it is a login/logout action or a web hook:
|
|
|
|
$sessionAction = ($this->controllerName == 'Session' && in_array($this->action, array('login', 'logout')));
|
2013-07-29 18:34:21 +02:00
|
|
|
$externalAction = in_array($this->controllerName, array('Bitbucket', 'Github', 'Gitlab', 'BuildStatus'));
|
2013-05-22 17:36:55 +02:00
|
|
|
$skipValidation = ($externalAction || $sessionAction);
|
2013-06-04 20:49:26 +02:00
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
if($skipValidation || $this->validateSession()) {
|
|
|
|
parent::handleRequest();
|
2013-05-16 03:57:02 +02:00
|
|
|
}
|
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
if (View::exists('layout') && $this->response->hasLayout()) {
|
|
|
|
$view = new View('layout');
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2013-05-16 17:46:30 +02:00
|
|
|
/**
|
|
|
|
* Validate whether or not the remote user has a valid session:
|
|
|
|
*/
|
2013-05-16 03:57:02 +02:00
|
|
|
protected function validateSession()
|
|
|
|
{
|
|
|
|
if (!empty($_SESSION['user_id'])) {
|
|
|
|
$user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['user_id']);
|
|
|
|
|
|
|
|
if ($user) {
|
2013-05-22 17:36:55 +02:00
|
|
|
$_SESSION['user'] = $user;
|
|
|
|
return true;
|
2013-05-16 03:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($_SESSION['user_id']);
|
|
|
|
}
|
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
if ($this->request->isAjax()) {
|
|
|
|
$this->response->setResponseCode(401);
|
2013-05-22 17:46:59 +02:00
|
|
|
$this->response->setContent('');
|
2013-05-22 17:36:55 +02:00
|
|
|
} else {
|
|
|
|
$this->response = new RedirectResponse($this->response);
|
2013-07-30 19:45:27 +02:00
|
|
|
$this->response->setHeader('Location', PHPCI_URL.'session/login');
|
2013-05-22 17:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-05-16 03:57:02 +02:00
|
|
|
}
|
|
|
|
}
|