]> git.agnieray.net Git - galette.git/blob - galette/includes/main.inc.php
Merge branch 'hotfix/1.0.3'
[galette.git] / galette / includes / main.inc.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette's instantiation and routes
7 *
8 * PHP version 5
9 *
10 * Copyright © 2012-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Main
28 * @package Galette
29 *
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
35 */
36
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;
45 use Slim\Views\Twig;
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;
50
51 if (!defined('GLOB_BRACE')) {
52 define('GLOB_BRACE', 0);
53 }
54
55 $time_start = microtime(true);
56
57 //define galette's root directory
58 if (!defined('GALETTE_ROOT')) {
59 define('GALETTE_ROOT', __DIR__ . '/../');
60 }
61
62 // define relative base path templating can use
63 if (!defined('GALETTE_BASE_PATH')) {
64 define('GALETTE_BASE_PATH', '../');
65 }
66
67 $needs_update = false;
68 /** @ignore */
69 require_once GALETTE_ROOT . 'includes/galette.inc.php';
70
71 //Galette needs database update!
72 if ($needs_update) {
73 define('GALETTE_THEME', 'themes/default/');
74 $gapp = new \Galette\Core\LightSlimApp();
75 } else {
76 $gapp = new \Galette\Core\SlimApp();
77 }
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']);
84 }
85 if ($scriptDir !== '/' && stripos($uri, $scriptDir) === 0) {
86 return $scriptDir;
87 }
88 return '';
89 })());
90
91 //CONFIGURE AND START SESSION
92
93 //Session duration
94 if (!defined('GALETTE_TIMEOUT')) {
95 //See https://php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime
96 define('GALETTE_TIMEOUT', 0);
97 }
98
99 $session_name = '';
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);
104 } else {
105 $session_name = PREFIX_DB . '_' . NAME_DB . '_' . str_replace('.', '_', GALETTE_VERSION);
106 }
107 $session_name = 'galette_' . $session_name;
108 $session = new SessionMiddleware([
109 'name' => $session_name,
110 'lifetime' => GALETTE_TIMEOUT
111 ]);
112
113 $session->start();
114 $app->add($session);
115
116 // Set up dependencies
117 require GALETTE_ROOT . '/includes/dependencies.php';
118 $app->add($app->getContainer()->get('csrf'));
119
120 /**
121 * Authentication middleware
122 */
123 $authenticate = new Authenticate($container);
124
125 require_once GALETTE_ROOT . 'includes/routes/main.routes.php';
126
127 if ($needs_update) {
128 $app->add(
129 new UpdateAndMaintenance(
130 $container->get('i18n'),
131 $container->get(RouteParser::class),
132 UpdateAndMaintenance::NEED_UPDATE
133 )
134 );
135
136 $app->run();
137 die();
138 }
139
140 //FIXME: remove in 1.1.0; routes/groups should call middleware directly
141 $showPublicPages = new \Galette\Middleware\PublicPages($container);
142
143 //Maintenance middleware
144 if (Galette::MODE_MAINT === GALETTE_MODE && !$container->get('login')->isSuperAdmin()) {
145 $app->add(
146 new UpdateAndMaintenance(
147 $container->get('i18n'),
148 $container->get(RouteParser::class),
149 UpdateAndMaintenance::MAINTENANCE
150 )
151 );
152 }
153
154 /**
155 * Change language middleware
156 *
157 * Require determineRouteBeforeAppMiddleware to be on.
158 */
159 $app->add(Language::class);
160
161 //Telemetry update middleware
162 $app->add(Telemetry::class);
163
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';
172
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();
177
178 // return NotFound for non-existent route
179 if (empty($route)) {
180 throw new \Slim\Exception\HttpNotFoundException($request);
181 }
182
183 $name = $route->getName();
184 $arguments = $route->getArguments();
185
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 ...
190
191 return $handler->handle($request);
192 });
193
194 // Add Routing Middleware - required for ACLs to work
195 $app->addRoutingMiddleware();
196
197 /**
198 * Add Error Handling Middleware
199 *
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.
204 *
205 * Note: This middleware should be added last. It will not handle any exceptions/errors
206 * for middleware added after it.
207 */
208 $errorMiddleware = $app->addErrorMiddleware(
209 Galette::isDebugEnabled(),
210 true,
211 true
212 );
213
214 $errorHandler = $errorMiddleware->getDefaultErrorHandler();
215 $errorHandler->registerErrorRenderer('text/html', \Galette\Renderers\Html::class);
216
217 /**
218 * Twig-View Middleware
219 * At the end, so it can be used to render errors
220 */
221 $app->add(TwigMiddleware::createFromContainer($app, Twig::class));
222
223 $app->run();
224
225 if (isset($profiler)) {
226 $profiler->stop();
227 }