Skip to content

Commit 8ffa382

Browse files
committed
FIXME test
1 parent 4f1edb3 commit 8ffa382

File tree

210 files changed

+29378
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

210 files changed

+29378
-0
lines changed

src/PHPCensor/Application.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
namespace PHPCensor;
4+
5+
use b8;
6+
use b8\Exception\HttpException;
7+
use b8\Http\Response;
8+
use b8\Http\Response\RedirectResponse;
9+
use b8\View;
10+
11+
/**
12+
* @author Dan Cryer <dan@block8.co.uk>
13+
*/
14+
class Application extends b8\Application
15+
{
16+
/**
17+
* @var \PHPCensor\Controller
18+
*/
19+
protected $controller;
20+
21+
/**
22+
* Initialise Application - Handles session verification, routing, etc.
23+
*/
24+
public function init()
25+
{
26+
$request =& $this->request;
27+
$route = '/:controller/:action';
28+
$opts = ['controller' => 'Home', 'action' => 'index']; //FIXME : Initialise Application - Handles session verification, routing, etc.
29+
30+
// Inlined as a closure to fix "using $this when not in object context" on 5.3
31+
$validateSession = function () {
32+
if (!empty($_SESSION['php-censor-user-id'])) {
33+
$user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['php-censor-user-id']);
34+
35+
if ($user) {
36+
$_SESSION['php-censor-user'] = $user;
37+
return true;
38+
}
39+
40+
unset($_SESSION['php-censor-user-id']);
41+
}
42+
43+
return false;
44+
};
45+
46+
$skipAuth = [$this, 'shouldSkipAuth'];
47+
48+
// Handler for the route we're about to register, checks for a valid session where necessary:
49+
$routeHandler = function (&$route, Response &$response) use (&$request, $validateSession, $skipAuth) {
50+
$skipValidation = in_array($route['controller'], ['session', 'webhook', 'build-status']);
51+
52+
if (!$skipValidation && !$validateSession() && (!is_callable($skipAuth) || !$skipAuth())) {
53+
if ($request->isAjax()) {
54+
$response->setResponseCode(401);
55+
$response->setContent('');
56+
} else {
57+
$_SESSION['php-censor-login-redirect'] = substr($request->getPath(), 1);
58+
$response = new RedirectResponse($response);
59+
$response->setHeader('Location', APP_URL . 'session/login');
60+
}
61+
62+
return false;
63+
}
64+
65+
return true;
66+
};
67+
68+
$this->router->clearRoutes();
69+
$this->router->register($route, $opts, $routeHandler);
70+
}
71+
72+
/**
73+
* Handle an incoming web request.
74+
*
75+
* @return Response
76+
*/
77+
public function handleRequest()
78+
{
79+
try {
80+
$this->response = parent::handleRequest(); // FIX ME : Handle an incoming web request.
81+
} catch (HttpException $ex) {
82+
$this->config->set('page_title', 'Error');
83+
84+
$view = new View('exception');
85+
$view->exception = $ex;
86+
87+
$this->response->setResponseCode($ex->getErrorCode());
88+
$this->response->setContent($view->render());
89+
} catch (\Exception $ex) {
90+
$this->config->set('page_title', 'Error');
91+
92+
$view = new View('exception');
93+
$view->exception = $ex;
94+
95+
$this->response->setResponseCode(500);
96+
$this->response->setContent($view->render());
97+
}
98+
99+
if ($this->response->hasLayout() && $this->controller && $this->controller->layout) {
100+
$this->setLayoutVariables($this->controller->layout);
101+
102+
$this->controller->layout->content = $this->response->getContent();
103+
$this->response->setContent($this->controller->layout->render());
104+
}
105+
106+
return $this->response;
107+
}
108+
109+
/**
110+
* Loads a particular controller, and injects our layout view into it.
111+
*
112+
* @param string $class
113+
*
114+
* @return b8\Controller
115+
*/
116+
protected function loadController($class)
117+
{
118+
$controller = parent::loadController($class);
119+
$controller->layout = new View('layout');
120+
$controller->layout->title = 'PHP Censor';
121+
$controller->layout->breadcrumb = [];
122+
123+
return $controller;
124+
}
125+
126+
/**
127+
* Injects variables into the layout before rendering it.
128+
*
129+
* @param View $layout
130+
*/
131+
protected function setLayoutVariables(View &$layout)
132+
{
133+
$groups = [];
134+
$groupStore = b8\Store\Factory::getStore('ProjectGroup');
135+
$groupList = $groupStore->getWhere([], 100, 0, [], ['title' => 'ASC']);
136+
137+
foreach ($groupList['items'] as $group) {
138+
$thisGroup = ['title' => $group->getTitle()];
139+
$projects = b8\Store\Factory::getStore('Project')->getByGroupId($group->getId(), false);
140+
$thisGroup['projects'] = $projects['items'];
141+
$groups[] = $thisGroup;
142+
}
143+
144+
$archived_projects = b8\Store\Factory::getStore('Project')->getAll(true);
145+
$layout->archived_projects = $archived_projects['items'];
146+
$layout->groups = $groups;
147+
}
148+
149+
/**
150+
* Check whether we should skip auth (because it is disabled)
151+
*
152+
* @return boolean
153+
*/
154+
protected function shouldSkipAuth()
155+
{
156+
$config = b8\Config::getInstance();
157+
$disableAuth = (bool)$config->get('php-censor.security.disable_auth', false);
158+
$defaultUserId = (integer)$config->get('php-censor.security.default_user_id', 1);
159+
160+
if ($disableAuth && $defaultUserId) {
161+
$user = b8\Store\Factory::getStore('User')
162+
->getByPrimaryKey($defaultUserId);
163+
164+
if ($user) {
165+
$_SESSION['php-censor-user'] = $user;
166+
return true;
167+
}
168+
}
169+
170+
return false;
171+
}
172+
}

src/PHPCensor/BuildFactory.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace PHPCensor;
4+
5+
use b8\Store\Factory;
6+
use PHPCensor\Model\Build;
7+
8+
/**
9+
* BuildFactory - Takes in a generic "Build" and returns a type-specific build model.
10+
*
11+
* @author Dan Cryer <dan@block8.co.uk>
12+
*/
13+
class BuildFactory
14+
{
15+
/**
16+
* @param $buildId
17+
*
18+
* @throws \Exception
19+
*
20+
* @return Build
21+
*/
22+
public static function getBuildById($buildId)
23+
{
24+
$build = Factory::getStore('Build')->getById($buildId);
25+
26+
if (empty($build)) {
27+
throw new \Exception('Build ID ' . $buildId . ' does not exist.');
28+
}
29+
30+
return self::getBuild($build);
31+
}
32+
33+
/**
34+
* Takes a generic build and returns a type-specific build model.
35+
*
36+
* @param Build $build The build from which to get a more specific build type.
37+
*
38+
* @return Build
39+
*/
40+
public static function getBuild(Build $build)
41+
{
42+
$project = $build->getProject();
43+
44+
if (!empty($project)) {
45+
switch ($project->getType()) {
46+
case 'remote':
47+
$type = 'RemoteGitBuild';
48+
break;
49+
case 'local':
50+
$type = 'LocalBuild';
51+
break;
52+
case 'github':
53+
$type = 'GithubBuild';
54+
break;
55+
case 'bitbucket':
56+
$type = 'BitbucketBuild';
57+
break;
58+
case 'bitbuckethg':
59+
$type = 'BitbucketHgBuild';
60+
break;
61+
case 'gitlab':
62+
$type = 'GitlabBuild';
63+
break;
64+
case 'hg':
65+
$type = 'MercurialBuild';
66+
break;
67+
case 'svn':
68+
$type = 'SubversionBuild';
69+
break;
70+
case 'gogs':
71+
$type = 'GogsBuild';
72+
break;
73+
default:
74+
return $build;
75+
}
76+
77+
$class = '\\PHPCensor\\Model\\Build\\' . $type;
78+
$build = new $class($build->getDataArray());
79+
}
80+
81+
return $build;
82+
}
83+
}

0 commit comments

Comments
 (0)