|
| 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 | +} |
0 commit comments