3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette's instantiation and routes
10 * Copyright © 2012-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2012-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since 0.8.2dev 2014-11-10
37 use Galette\Middleware\Authenticate
;
38 use Galette\Middleware\Language
;
39 use Galette\Middleware\Telemetry
;
40 use Galette\Middleware\TrailingSlash
;
41 use Galette\Middleware\UpdateAndMaintenance
;
42 use RKA\SessionMiddleware
;
43 use Slim\Routing\RouteContext
;
44 use Galette\Core\Galette
;
46 use Slim\Views\TwigMiddleware
;
47 use Slim\Routing\RouteParser
;
48 use Psr\Http\Message\ServerRequestInterface
as Request
;
49 use Psr\Http\Server\RequestHandlerInterface
as RequestHandler
;
51 if (!defined('GLOB_BRACE')) {
52 define('GLOB_BRACE', 0);
55 $time_start = microtime(true);
57 //define galette's root directory
58 if (!defined('GALETTE_ROOT')) {
59 define('GALETTE_ROOT', __DIR__
. '/../');
62 // define relative base path templating can use
63 if (!defined('GALETTE_BASE_PATH')) {
64 define('GALETTE_BASE_PATH', '../');
67 $needs_update = false;
69 require_once GALETTE_ROOT
. 'includes/galette.inc.php';
71 //Galette needs database update!
73 define('GALETTE_THEME', 'themes/default/');
74 $gapp = new \Galette\Core\
LightSlimApp();
76 $gapp = new \Galette\Core\
SlimApp();
78 $app = $gapp->getApp();
79 $app->setBasePath((function () {
80 $scriptDir = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
81 $uri = (string)parse_url('http://a' . $_SERVER['REQUEST_URI'] ??
'', PHP_URL_PATH
);
82 if (stripos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
83 return dirname($_SERVER['SCRIPT_NAME']);
85 if ($scriptDir !== '/' && stripos($uri, $scriptDir) === 0) {
91 //CONFIGURE AND START SESSION
94 if (!defined('GALETTE_TIMEOUT')) {
95 //See https://php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime
96 define('GALETTE_TIMEOUT', 0);
100 //since PREFIX_DB and NAME_DB are required to properly instanciate sessions,
101 // we have to check here if they're assigned
102 if ($installer ||
!defined('PREFIX_DB') ||
!defined('NAME_DB')) {
103 $session_name = 'install_' . str_replace('.', '_', GALETTE_VERSION
);
105 $session_name = PREFIX_DB
. '_' . NAME_DB
. '_' . str_replace('.', '_', GALETTE_VERSION
);
107 $session_name = 'galette_' . $session_name;
108 $session = new SessionMiddleware([
109 'name' => $session_name,
110 'lifetime' => GALETTE_TIMEOUT
116 // Set up dependencies
117 require GALETTE_ROOT
. '/includes/dependencies.php';
118 $app->add($app->getContainer()->get('csrf'));
121 * Authentication middleware
123 $authenticate = new Authenticate($container);
125 require_once GALETTE_ROOT
. 'includes/routes/main.routes.php';
129 new UpdateAndMaintenance(
130 $container->get('i18n'),
131 $container->get(RouteParser
::class),
132 UpdateAndMaintenance
::NEED_UPDATE
140 //FIXME: remove in 1.1.0; routes/groups should call middleware directly
141 $showPublicPages = new \Galette\Middleware\
PublicPages($container);
143 //Maintenance middleware
144 if (Galette
::MODE_MAINT
=== GALETTE_MODE
&& !$container->get('login')->isSuperAdmin()) {
146 new UpdateAndMaintenance(
147 $container->get('i18n'),
148 $container->get(RouteParser
::class),
149 UpdateAndMaintenance
::MAINTENANCE
155 * Change language middleware
157 * Require determineRouteBeforeAppMiddleware to be on.
159 $app->add(Language
::class);
161 //Telemetry update middleware
162 $app->add(Telemetry
::class);
164 require_once GALETTE_ROOT
. 'includes/routes/authentication.routes.php';
165 require_once GALETTE_ROOT
. 'includes/routes/management.routes.php';
166 require_once GALETTE_ROOT
. 'includes/routes/members.routes.php';
167 require_once GALETTE_ROOT
. 'includes/routes/groups.routes.php';
168 require_once GALETTE_ROOT
. 'includes/routes/contributions.routes.php';
169 require_once GALETTE_ROOT
. 'includes/routes/public_pages.routes.php';
170 require_once GALETTE_ROOT
. 'includes/routes/ajax.routes.php';
171 require_once GALETTE_ROOT
. 'includes/routes/plugins.routes.php';
173 // Via this middleware you could access the route and routing results from the resolved route
174 $app->add(function (Request
$request, RequestHandler
$handler) use ($container) {
175 $routeContext = RouteContext
::fromRequest($request);
176 $route = $routeContext->getRoute();
178 // return NotFound for non-existent route
180 throw new \Slim\Exception\
HttpNotFoundException($request);
183 $name = $route->getName();
184 $arguments = $route->getArguments();
186 $view = $container->get(Twig
::class);
187 $view->getEnvironment()->addGlobal('cur_route', $name);
188 $view->getEnvironment()->addGlobal('cur_subroute', array_shift($arguments));
189 // ... do something with the data ...
191 return $handler->handle($request);
194 // Add Routing Middleware - required for ACLs to work
195 $app->addRoutingMiddleware();
198 * Add Error Handling Middleware
200 * @param bool $displayErrorDetails -> Should be set to false in production
201 * @param bool $logErrors -> Parameter is passed to the default ErrorHandler
202 * @param bool $logErrorDetails -> Display error details in error log
203 * which can be replaced by a callable of your choice.
205 * Note: This middleware should be added last. It will not handle any exceptions/errors
206 * for middleware added after it.
208 $errorMiddleware = $app->addErrorMiddleware(
209 Galette
::isDebugEnabled(),
214 $errorHandler = $errorMiddleware->getDefaultErrorHandler();
215 $errorHandler->registerErrorRenderer('text/html', \Galette\Renderers\Html
::class);
218 * Twig-View Middleware
219 * At the end, so it can be used to render errors
221 $app->add(TwigMiddleware
::createFromContainer($app, Twig
::class));
225 if (isset($profiler)) {