]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Middleware/UpdateAndMaintenance.php
d436c1c602c61c839112647b6e552dcc49ad6cd2
[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-2020 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-2020 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
46 /**
47 * Galette's Slim middleware for Update and Maintenance
48 *
49 * Renders maintainance and needs update pages, as 503 (service not available)
50 *
51 * @category Middleware
52 * @name UpdateAndMaintenance
53 * @package Galette
54 * @author Johan Cwiklinski <johan@x-tnd.be>
55 * @copyright 2015-2020 The Galette Team
56 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
57 * @link http://galette.tuxfamily.org
58 * @since Available since 0.9dev - 2015-10-31
59 */
60 class UpdateAndMaintenance
61 {
62 const MAINTENANCE = 0;
63 const NEED_UPDATE = 1;
64
65 /**
66 * @var callable
67 */
68 protected $callback;
69
70 /**
71 * @var I18n
72 */
73 protected $i18n;
74
75 /**
76 * Constructor
77 *
78 * @param I18n $i18n I18n instance
79 * @param callable|int $callback Callable or local constant
80 */
81 public function __construct(I18n $i18n, $callback = self::MAINTENANCE)
82 {
83 $this->i18n = $i18n;
84
85 if ($callback === self::MAINTENANCE) {
86 $this->callback = array($this, 'maintenancePage');
87 } elseif ($callback === self::NEED_UPDATE) {
88 $this->callback = array($this, 'needsUpdatePage');
89 } else {
90 if (!is_callable($callback)) {
91 throw new \InvalidArgumentException('argument callback must be callable');
92 } else {
93 $this->callback = $callback;
94 }
95 }
96 }
97
98 /**
99 * Middleware invokable class
100 *
101 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
102 * @param \Psr\Http\Message\ResponseInterface $response PSR7 response
103 * @param callable $next Next middleware
104 *
105 * @return \Psr\Http\Message\ResponseInterface
106 */
107 public function __invoke(Request $request, Response $response, $next): Response
108 {
109 $response
110 ->withStatus(503)
111 ->withHeader('Content-type', 'text/html')
112 ->getBody()->write(call_user_func($this->callback, $request));
113 return $response;
114 }
115
116 /**
117 * Renders the page
118 *
119 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
120 * @param string $contents HTML page contents
121 *
122 * @return string
123 */
124 private function renderPage(Request $request, $contents)
125 {
126 $path = str_replace(
127 'index.php',
128 '',
129 $request->getUri()->getBasePath()
130 );
131
132 //add ending / if missing
133 if (
134 $path === ''
135 || $path !== '/'
136 && substr($path, -1) !== '/'
137 ) {
138 $path .= '/';
139 }
140
141 $css_path = $path . GALETTE_THEME;
142
143 $body = "<!DOCTYPE html>
144 <html lang=\"" . $this->i18n->getAbbrev() . "\">
145 <head>
146 <title>" . _T("Galette needs update!") . "</title>
147 <meta charset=\"UTF-8\"/>
148 <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $css_path . "galette.css\"/>
149 </head>
150 <body class=\"notup2date\">
151 <p class=\"center\">
152 <img src=\"" . $css_path . "images/galette.png\" alt=\"\"/>
153 </p>
154 <div id=\"errorbox\">" . $contents . "</div>
155 </body>
156 </html>";
157 return $body;
158 }
159
160 /**
161 * Displays maintenance page
162 *
163 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
164 *
165 * @return string
166 */
167 private function maintenancePage(Request $request)
168 {
169 $contents = "<h1>" . _T("Galette is currently under maintenance!") . "</h1>
170 <p>" . _T("The Galette instance you are requesting is currently under maintenance. Please come back later.") . "</p>";
171 return $this->renderPage($request, $contents);
172 }
173
174 /**
175 * Displays needs update page
176 *
177 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
178 *
179 * @return string
180 */
181 private function needsUpdatePage(Request $request)
182 {
183 $contents = "<h1>" . _T("Galette needs update!") . "</h1>
184 <p>" . _T("Your Galette database is not present, or not up to date.") . "</p>
185 <p><em>" . _T("Please run install or upgrade procedure (check the documentation)") . "</em></p>";
186 return $this->renderPage($request, $contents);
187 }
188 }