]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/AbstractController.php
Remove 'svn' lines
[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-2020 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-2020 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-2020 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\Smarty
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
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("session")
111 */
112 protected $session;
113 /**
114 * @Inject
115 * @var \Slim\Flash\Messages
116 */
117 protected $flash;
118 /**
119 * @Inject
120 * @var \Galette\Entity\FieldsConfig
121 */
122 protected $fields_config;
123 /**
124 * @Inject
125 * @var \Galette\Entity\ListsConfig
126 */
127 protected $lists_config;
128 /**
129 * @Inject
130 * @var array
131 */
132 protected $members_fields;
133 /**
134 * @Inject
135 * @var array
136 */
137 protected $members_form_fields;
138 /**
139 * @Inject
140 * @var array
141 */
142 protected $members_fields_cats;
143
144 /**
145 * @Inject
146 * @var \Galette\Handlers\NotFound
147 */
148 protected $notFoundHandler;
149
150 /**
151 * Constructor
152 *
153 * @param ContainerInterface $container Container instance
154 */
155 public function __construct(ContainerInterface $container)
156 {
157 $this->container = $container;
158 //set variosu services we need
159 $this->zdb = $container->get('zdb');
160 $this->login = $container->get('login');
161 $this->preferences = $container->get('preferences');
162 $this->view = $container->get('view');
163 $this->logo = $container->get('logo');
164 $this->print_logo = $container->get('print_logo');
165 $this->plugins = $container->get('plugins');
166 $this->router = $container->get('router');
167 $this->history = $container->get('history');
168 $this->i18n = $container->get('i18n');
169 $this->session = $container->get('session');
170 $this->flash = $container->get('flash');
171 $this->fields_config = $container->get('fields_config');
172 $this->lists_config = $container->get('lists_config');
173 $this->notFoundHandler = $container->get('notFoundHandler');
174 $this->members_fields = $container->get('members_fields');
175 $this->members_form_fields = $container->get('members_form_fields');
176 $this->members_fields_cats = $container->get('members_fields_cats');
177 }
178
179 /**
180 * Galette redirection workflow
181 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
182 *
183 * @param Request $request PSR Request
184 * @param Response $response PSR Response
185 * @param array $args Request arguments ['r']
186 *
187 * @return void
188 */
189 protected function galetteRedirect(Request $request, Response $response, array $args = [])
190 {
191 //reinject flash messages so they're not lost
192 $flashes = $this->flash->getMessages();
193 foreach ($flashes as $type => $messages) {
194 $this->container->get('flash')->addMessage($type, $message);
195 }
196
197 if ($this->login->isLogged()) {
198 $urlRedirect = null;
199 if ($this->session->urlRedirect !== null) {
200 $urlRedirect = $this->getGaletteBaseUrl($request) . $this->session->urlRedirect;
201 $this->session->urlRedirect = null;
202 }
203
204 if ($urlRedirect !== null) {
205 return $response
206 ->withStatus(301)
207 ->withHeader('Location', $urlRedirect);
208 } else {
209 if (
210 $this->login->isSuperAdmin()
211 || $this->login->isAdmin()
212 || $this->login->isStaff()
213 ) {
214 if (
215 !isset($_COOKIE['show_galette_dashboard'])
216 || $_COOKIE['show_galette_dashboard'] == 1
217 ) {
218 return $response
219 ->withStatus(301)
220 ->withHeader('Location', $this->router->pathFor('dashboard'));
221 } else {
222 return $response
223 ->withStatus(301)
224 ->withHeader('Location', $this->router->pathFor('members'));
225 }
226 } else {
227 return $response
228 ->withStatus(301)
229 ->withHeader('Location', $this->router->pathFor('dashboard'));
230 }
231 }
232 } else {
233 return $response
234 ->withStatus(301)
235 ->withHeader('Location', $this->router->pathFor('login'));
236 }
237 }
238
239 /**
240 * Get base URL fixed for proxies
241 *
242 * @param Request $request PSR Request
243 *
244 * @return string
245 */
246 private function getGaletteBaseUrl(Request $request)
247 {
248 $url = preg_replace(
249 [
250 '|index\.php|',
251 '|https?://' . $_SERVER['HTTP_HOST'] . '(:\d+)?' . '|'
252 ],
253 ['', ''],
254 $request->getUri()->getBaseUrl()
255 );
256 if (strlen($url) && substr($url, -1) !== '/') {
257 $url .= '/';
258 }
259 return $url;
260 }
261 }