]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Middleware/UpdateAndMaintenance.php
Merge branch 'hotfix/1.0.3'
[galette.git] / galette / lib / Galette / Middleware / UpdateAndMaintenance.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette Slim middleware for maintenance and needs update pages display.
7 *
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.
10 *
11 * PHP version 5
12 *
13 * Copyright © 2015-2023 The Galette Team
14 *
15 * This file is part of Galette (http://galette.tuxfamily.org).
16 *
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.
21 *
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.
26 *
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/>.
29 *
30 * @category Core
31 * @package Galette
32 *
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
38 */
39
40 namespace Galette\Middleware;
41
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\RouteParser;
47
48 /**
49 * Galette's Slim middleware for Update and Maintenance
50 *
51 * Renders maintenance and needs update pages, as 503 (service not available)
52 *
53 * @category Middleware
54 * @name UpdateAndMaintenance
55 * @package Galette
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
61 */
62 class UpdateAndMaintenance
63 {
64 public const MAINTENANCE = 0;
65 public const NEED_UPDATE = 1;
66
67 /**
68 * @var callable
69 */
70 protected $callback;
71
72 /**
73 * @var I18n
74 */
75 protected $i18n;
76
77 /**
78 * @var RouteParser
79 */
80 protected RouteParser $routeParser;
81
82 /**
83 * Constructor
84 *
85 * @param I18n $i18n I18n instance
86 * @param RouteParser $routeParser Route parser
87 * @param callable|int $callback Callable or local constant
88 */
89 public function __construct(I18n $i18n, RouteParser $routeParser, callable|int $callback = self::MAINTENANCE)
90 {
91 $this->i18n = $i18n;
92 $this->routeParser = $routeParser;
93
94 if ($callback === self::MAINTENANCE) {
95 $this->callback = array($this, 'maintenancePage');
96 } elseif ($callback === self::NEED_UPDATE) {
97 $this->callback = array($this, 'needsUpdatePage');
98 } else {
99 if (!is_callable($callback)) {
100 throw new \InvalidArgumentException('argument callback must be callable');
101 } else {
102 $this->callback = $callback;
103 }
104 }
105 }
106
107 /**
108 * Middleware invokable class
109 *
110 * @param Request $request PSR7 request
111 * @param RequestHandler $handler PSR7 request handler
112 *
113 * @return Response
114 */
115 public function __invoke(Request $request, RequestHandler $handler): Response
116 {
117 $response = new \Slim\Psr7\Response();
118 $response
119 ->withStatus(503)
120 ->withHeader('Content-type', 'text/html')
121 ->getBody()->write(call_user_func($this->callback, $request));
122 return $response;
123 }
124
125 /**
126 * Renders the page
127 *
128 * @param Request $request PSR7 request
129 * @param string $contents HTML page contents
130 *
131 * @return string
132 */
133 private function renderPage(Request $request, $contents)
134 {
135 $path = $this->routeParser->urlFor('slash');
136
137 //add ending / if missing
138 if (
139 $path === ''
140 || $path !== '/'
141 && substr($path, -1) !== '/'
142 ) {
143 $path .= '/';
144 }
145
146 $theme_path = $path . GALETTE_THEME;
147
148 $body = "<!DOCTYPE html>
149 <html class=\"public_page\" lang=\"" . $this->i18n->getAbbrev() . "\">
150 <head>
151 <title>" . _T("Galette needs update!") . "</title>
152 <meta charset=\"UTF-8\"/>
153 <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $theme_path . "ui/semantic.min.css\"/>
154 <link rel=\"shortcut icon\" href=\"" . $theme_path . "images/favicon.png\"/>
155 </head>
156 <body class=\"notup2date pushable\">
157 <div class=\"pusher\">
158 <div id=\"main\" class=\"ui container\">
159 <div class=\"ui basic segment\">
160 <div class=\"ui basic center aligned fitted segment\">
161 <img src=\"" . $theme_path . "images/galette.png\" alt=\"[ Galette ]\"/>
162 </div>
163 <div class=\"ui center aligned message\">" . $contents . "</div>
164 </div>
165 </div>
166 </div>
167 </body>
168 </html>";
169 return $body;
170 }
171
172 /**
173 * Displays maintenance page
174 *
175 * @param Request $request PSR7 request
176 *
177 * @return string
178 */
179 private function maintenancePage(Request $request)
180 {
181 $contents = "<div class=\"header\">" . _T("Galette is currently under maintenance!") . "</div>
182 <p>" . _T("The Galette instance you are requesting is currently under maintenance. Please come back later.") . "</p>";
183 return $this->renderPage($request, $contents);
184 }
185
186 /**
187 * Displays needs update page
188 *
189 * @param Request $request PSR7 request
190 *
191 * @return string
192 */
193 private function needsUpdatePage(Request $request)
194 {
195 $contents = "<h1>" . _T("Galette needs update!") . "</h1>
196 <p>" . _T("Your Galette database is not present, or not up to date.") . "</p>
197 <p><em>" . _T("Please run install or upgrade procedure (check the documentation)") . "</em></p>";
198 return $this->renderPage($request, $contents);
199 }
200 }