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