3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette Slim middleware for maintenance and needs update pages display.
8 * Relies on Slim modes. Set 'MAINT' for maintenance mode, and 'NEED_UPDATE' for the need update one.
9 * Maintenance mode page will be displayed if current logged in user is not super admin.
13 * Copyright © 2015-2023 The Galette Team
15 * This file is part of Galette (http://galette.tuxfamily.org).
17 * Galette is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * Galette is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
33 * @author Johan Cwiklinski <johan@x-tnd.be>
34 * @copyright 2015-2023 The Galette Team
35 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
36 * @link http://galette.tuxfamily.org
37 * @since Available since 0.9dev - 2015-10-31
40 namespace Galette\Middleware
;
42 use Galette\Core\I18n
;
43 use Psr\Http\Message\ServerRequestInterface
as Request
;
44 use Psr\Http\Message\ResponseInterface
as Response
;
45 use Psr\Http\Server\RequestHandlerInterface
as RequestHandler
;
46 use Slim\Routing\RouteContext
;
49 * Galette's Slim middleware for Update and Maintenance
51 * Renders maintenance and needs update pages, as 503 (service not available)
53 * @category Middleware
54 * @name UpdateAndMaintenance
56 * @author Johan Cwiklinski <johan@x-tnd.be>
57 * @copyright 2015-2023 The Galette Team
58 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
59 * @link http://galette.tuxfamily.org
60 * @since Available since 0.9dev - 2015-10-31
62 class UpdateAndMaintenance
64 public const MAINTENANCE
= 0;
65 public const NEED_UPDATE
= 1;
80 * @param I18n $i18n I18n instance
81 * @param callable|int $callback Callable or local constant
83 public function __construct(I18n
$i18n, $callback = self
::MAINTENANCE
)
87 if ($callback === self
::MAINTENANCE
) {
88 $this->callback
= array($this, 'maintenancePage');
89 } elseif ($callback === self
::NEED_UPDATE
) {
90 $this->callback
= array($this, 'needsUpdatePage');
92 if (!is_callable($callback)) {
93 throw new \
InvalidArgumentException('argument callback must be callable');
95 $this->callback
= $callback;
101 * Middleware invokable class
103 * @param Request $request PSR7 request
104 * @param RequestHandler $handler PSR7 request handler
108 public function __invoke(Request
$request, RequestHandler
$handler): Response
110 $response = new \Slim\Psr7\
Response();
113 ->withHeader('Content-type', 'text/html')
114 ->getBody()->write(call_user_func($this->callback
, $request));
121 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
122 * @param string $contents HTML page contents
126 private function renderPage(Request
$request, $contents)
128 $routeContext = RouteContext
::fromRequest($request);
129 $routeParser = $routeContext->getRouteParser();
130 $path = $routeParser->urlFor('slash');
132 //add ending / if missing
136 && substr($path, -1) !== '/'
141 $theme_path = $path . GALETTE_THEME
;
143 $body = "<!DOCTYPE html>
144 <html class=\"public_page\" lang=\"" . $this->i18n
->getAbbrev() . "\">
146 <title>" . _T("Galette needs update!") . "</title>
147 <meta charset=\"UTF-8\"/>
148 <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $theme_path . "../../assets/css/galette-main.bundle.min.css\"/>
149 <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $theme_path . "ui/semantic.min.css\"/>
150 <link rel=\"shortcut icon\" href=\"" . $theme_path . "images/favicon.png\"/>
152 <body class=\"notup2date pushable\">
153 <div class=\"pusher\">
154 <div id=\"main\" class=\"ui container\">
155 <div class=\"ui basic segment\">
156 <div class=\"ui basic center aligned fitted segment\">
157 <img src=\"" . $theme_path . "images/galette.png\" alt=\"[ Galette ]\"/>
159 <div class=\"ui center aligned message\">" . $contents . "</div>
169 * Displays maintenance page
171 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
175 private function maintenancePage(Request
$request)
177 $contents = "<div class=\"header\">" . _T("Galette is currently under maintenance!") . "</div>
178 <p>" . _T("The Galette instance you are requesting is currently under maintenance. Please come back later.") . "</p>";
179 return $this->renderPage($request, $contents);
183 * Displays needs update page
185 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
189 private function needsUpdatePage(Request
$request)
191 $contents = "<h1>" . _T("Galette needs update!") . "</h1>
192 <p>" . _T("Your Galette database is not present, or not up to date.") . "</p>
193 <p><em>" . _T("Please run install or upgrade procedure (check the documentation)") . "</em></p>";
194 return $this->renderPage($request, $contents);