]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/AbstractController.php
Migration to slimv4
[galette.git] / galette / lib / Galette / Controllers / AbstractController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette abstract controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2019-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 Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2019-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 Available since 0.9.4dev - 2019-12-02
35 */
36
37 namespace Galette\Controllers;
38
39 use Psr\Container\ContainerInterface;
40 use Slim\Psr7\Request;
41 use Slim\Psr7\Response;
42 use Slim\Routing\RouteContext;
43 use Slim\Routing\RouteParser;
44
45 /**
46 * Galette abstract controller
47 *
48 * @category Controllers
49 * @name AbstractController
50 * @package Galette
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2019-2023 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.9.4dev - 2019-12-02
56 */
57
58 abstract class AbstractController
59 {
60 private $container;
61 /**
62 * @var \Galette\Core\Db
63 */
64 #[Inject]
65 protected $zdb;
66 /**
67 * @var \Galette\Core\Login
68 */
69 #[Inject]
70 protected $login;
71 /**
72 * @var \Galette\Core\Preferences
73 */
74 #[Inject]
75 protected $preferences;
76 /**
77 * @var \Slim\Views\Twig
78 */
79 protected $view;
80 /**
81 * @var \Galette\Core\Logo
82 */
83 #[Inject]
84 protected $logo;
85 /**
86 * @var \Galette\Core\PrintLogo
87 */
88 #[Inject]
89 protected $print_logo;
90 /**
91 * @var \Galette\Core\Plugins
92 */
93 #[Inject]
94 protected $plugins;
95 /**
96 * @var \Slim\Routing\RouteParser
97 */
98 #[Inject]
99 protected $routeparser;
100 /**
101 * @var \Galette\Core\History
102 */
103 #[Inject]
104 protected $history;
105 /**
106 * @var \Galette\Core\I18n
107 */
108 #[Inject]
109 protected $i18n;
110 /**
111 * @var \Galette\Core\L10n
112 */
113 #[Inject]
114 protected $l10n;
115 /**
116 * Session
117 */
118 #[Inject("session")]
119 protected $session;
120 /**
121 * @var \Slim\Flash\Messages
122 */
123 #[Inject]
124 protected $flash;
125 /**
126 * @var \Galette\Entity\FieldsConfig
127 */
128 #[Inject]
129 protected $fields_config;
130 /**
131 * @var \Galette\Entity\ListsConfig
132 */
133 #[Inject]
134 protected $lists_config;
135 /**
136 * @var array
137 */
138 #[Inject("members_fields")]
139 protected $members_fields;
140 /**
141 * @var array
142 */
143 #[Inject("members_form_fields")]
144 protected $members_form_fields;
145 /**
146 * @var array
147 */
148 #[Inject("members_fields_cats")]
149 protected $members_fields_cats;
150
151 /**
152 * @var \Galette\Handlers\NotFound
153 */
154 /*#[Inject]
155 protected $notFoundHandler;*/
156
157 /**
158 * Constructor
159 *
160 * @param ContainerInterface $container Container instance
161 */
162 public function __construct(ContainerInterface $container)
163 {
164 $this->container = $container;
165 //set various services we need
166 $this->zdb = $container->get('zdb');
167 $this->login = $container->get('login');
168 $this->preferences = $container->get('preferences');
169 $this->view = $container->get(\Slim\Views\Twig::class);
170 $this->logo = $container->get('logo');
171 $this->print_logo = $container->get('print_logo');
172 $this->routeparser = $container->get(RouteParser::class);
173 $this->history = $container->get('history');
174 $this->i18n = $container->get('i18n');
175 $this->l10n = $container->get('l10n');
176 $this->session = $container->get('session');
177 $this->flash = $container->get('flash');
178 $this->fields_config = $container->get('fields_config');
179 $this->lists_config = $container->get('lists_config');
180 /*$this->notFoundHandler = $container->get('notFoundHandler');*/
181 $this->members_fields = $container->get('members_fields');
182 $this->members_form_fields = $container->get('members_form_fields');
183 $this->members_fields_cats = $container->get('members_fields_cats');
184 $this->plugins = $container->get('plugins');
185 }
186
187 /**
188 * Galette redirection workflow
189 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
190 *
191 * @param Request $request PSR Request
192 * @param Response $response PSR Response
193 *
194 * @return Response
195 */
196 protected function galetteRedirect(Request $request, Response $response)
197 {
198 //reinject flash messages so they're not lost
199 $flashes = $this->flash->getMessages();
200 foreach ($flashes as $type => $messages) {
201 foreach ($messages as $message) {
202 $this->container->get('flash')->addMessage($type, $message);
203 }
204 }
205
206 if ($this->login->isLogged()) {
207 $urlRedirect = null;
208 if ($this->session->urlRedirect !== null) {
209 $urlRedirect = $this->getGaletteBaseUrl($request) . $this->session->urlRedirect;
210 $this->session->urlRedirect = null;
211 }
212
213 if ($urlRedirect !== null) {
214 return $response
215 ->withStatus(301)
216 ->withHeader('Location', $urlRedirect);
217 } else {
218 if (
219 $this->login->isSuperAdmin()
220 || $this->login->isAdmin()
221 || $this->login->isStaff()
222 ) {
223 if (
224 !isset($_COOKIE['show_galette_dashboard'])
225 || $_COOKIE['show_galette_dashboard'] == 1
226 ) {
227 return $response
228 ->withStatus(301)
229 ->withHeader('Location', $this->routeparser->urlFor('dashboard'));
230 } else {
231 return $response
232 ->withStatus(301)
233 ->withHeader('Location', $this->routeparser->urlFor('members'));
234 }
235 } else {
236 return $response
237 ->withStatus(301)
238 ->withHeader('Location', $this->routeparser->urlFor('dashboard'));
239 }
240 }
241 } else {
242 return $response
243 ->withStatus(301)
244 ->withHeader('Location', $this->routeparser->urlFor('login'));
245 }
246 }
247
248 /**
249 * Get base URL fixed for proxies
250 *
251 * @param Request $request PSR Request
252 *
253 * @return string
254 */
255 private function getGaletteBaseUrl(Request $request)
256 {
257 $url = preg_replace(
258 [
259 '|index\.php|',
260 '|https?://' . $_SERVER['HTTP_HOST'] . '(:\d+)?' . '|'
261 ],
262 ['', ''],
263 $request->getUri()->getBaseUrl()
264 );
265 if (strlen($url) && substr($url, -1) !== '/') {
266 $url .= '/';
267 }
268 return $url;
269 }
270
271 /**
272 * Get route arguments
273 * php-di bridge pass each variable, not an array of all arguments
274 *
275 * @param Request $request PSR Request
276 *
277 * @return array
278 */
279 protected function getArgs(Request $request): array
280 {
281 $routeContext = RouteContext::fromRequest($request);
282 $route = $routeContext->getRoute();
283 $args = $route->getArguments();
284 return $args;
285 }
286
287 /**
288 * Get a JSON response
289 *
290 * @param Response $response Response instance
291 * @param array $data Data to send
292 * @param int $status HTTP status code
293 *
294 * @return Response
295 */
296 protected function withJson(Response $response, array $data, int $status = 200): Response
297 {
298 $response = $response->withStatus($status);
299 $response = $response->withHeader('Content-Type', 'application/json');
300 $response->getBody()->write(json_encode($data));
301 return $response;
302 }
303 }