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