4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Middleware
;
24 use Galette\Core\I18n
;
25 use Psr\Http\Message\ServerRequestInterface
as Request
;
26 use Psr\Http\Message\ResponseInterface
as Response
;
27 use Psr\Http\Server\RequestHandlerInterface
as RequestHandler
;
28 use Slim\Routing\RouteParser
;
31 * Galette Slim middleware for maintenance and needs update pages display.
33 * Relies on Slim modes. Set 'MAINT' for maintenance mode, and 'NEED_UPDATE' for the need update one.
34 * Maintenance mode page will be displayed if current logged in user is not super admin.
36 * Renders maintenance and needs update pages, as 503 (service not available)
38 * @author Johan Cwiklinski <johan@x-tnd.be>
40 class UpdateAndMaintenance
42 public const MAINTENANCE
= 0;
43 public const NEED_UPDATE
= 1;
58 protected RouteParser
$routeParser;
63 * @param I18n $i18n I18n instance
64 * @param RouteParser $routeParser Route parser
65 * @param callable|int $callback Callable or local constant
67 public function __construct(I18n
$i18n, RouteParser
$routeParser, callable|
int $callback = self
::MAINTENANCE
)
70 $this->routeParser
= $routeParser;
72 if ($callback === self
::MAINTENANCE
) {
73 $this->callback
= array($this, 'maintenancePage');
74 } elseif ($callback === self
::NEED_UPDATE
) {
75 $this->callback
= array($this, 'needsUpdatePage');
77 if (!is_callable($callback)) {
78 throw new \
InvalidArgumentException('argument callback must be callable');
80 $this->callback
= $callback;
86 * Middleware invokable class
88 * @param Request $request PSR7 request
89 * @param RequestHandler $handler PSR7 request handler
93 public function __invoke(Request
$request, RequestHandler
$handler): Response
95 $response = new \Slim\Psr7\
Response();
98 ->withHeader('Content-type', 'text/html')
99 ->getBody()->write(call_user_func($this->callback
, $request));
106 * @param Request $request PSR7 request
107 * @param string $contents HTML page contents
111 private function renderPage(Request
$request, string $contents): string
113 $path = $this->routeParser
->urlFor('slash');
115 //add ending / if missing
119 && substr($path, -1) !== '/'
124 $theme_path = $path . GALETTE_THEME
;
126 $body = "<!DOCTYPE html>
127 <html class=\"public_page\" lang=\"" . $this->i18n
->getAbbrev() . "\">
129 <title>" . _T("Galette needs update!") . "</title>
130 <meta charset=\"UTF-8\"/>
131 <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $theme_path . "ui/semantic.min.css\"/>
132 <link rel=\"shortcut icon\" href=\"" . $theme_path . "images/favicon.png\"/>
134 <body class=\"notup2date pushable\">
135 <div class=\"pusher\">
136 <div id=\"main\" class=\"ui container\">
137 <div class=\"ui basic segment\">
138 <div class=\"ui basic center aligned fitted segment\">
139 <img src=\"" . $theme_path . "images/galette.png\" alt=\"[ Galette ]\"/>
141 <div class=\"ui center aligned message\">" . $contents . "</div>
151 * Displays maintenance page
153 * @param Request $request PSR7 request
157 private function maintenancePage(Request
$request): string
159 $contents = "<div class=\"header\">" . _T("Galette is currently under maintenance!") . "</div>
160 <p>" . _T("The Galette instance you are requesting is currently under maintenance. Please come back later.") . "</p>";
161 return $this->renderPage($request, $contents);
165 * Displays needs update page
167 * @param Request $request PSR7 request
171 private function needsUpdatePage(Request
$request): string
173 $contents = "<h1>" . _T("Galette needs update!") . "</h1>
174 <p>" . _T("Your Galette database is not present, or not up to date.") . "</p>
175 <p><em>" . _T("Please run install or upgrade procedure (check the documentation)") . "</em></p>";
176 return $this->renderPage($request, $contents);