]> git.agnieray.net Git - galette.git/blob - galette/includes/dependencies.php
845993692e76f17c6f674a2da58de94405ae4a74
[galette.git] / galette / includes / dependencies.php
1 <?php
2
3 /**
4 * Dependency injection configuration
5 *
6 * PHP version 5
7 *
8 * Copyright © 2003-2018 The Galette Team
9 *
10 * This file is part of Galette (http://galette.tuxfamily.org).
11 *
12 * Galette is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Galette is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * @category Functions
26 * @package Galette
27 *
28 * @author Frédéric Jacquot <unknown@unknow.com>
29 * @author Georges Khaznadar (password encryption, images) <unknown@unknow.com>
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2003-2018 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 */
35
36 use Psr\Container\ContainerInterface;
37 use Galette\Entity\PdfModel;
38 use Slim\Event\SlimEventManager;
39 use Slim\Views\SmartyPlugins;
40
41 $container = $app->getContainer();
42
43 // -----------------------------------------------------------------------------
44 // Error handling
45 // -----------------------------------------------------------------------------
46
47 $container->set('errorHandler', function ($c) {
48 return new Galette\Handlers\Error($c->get('view'), true);
49 });
50
51 $container->set('phpErrorHandler', function ($c) {
52 return new Galette\Handlers\PhpError($c->get('view'), true);
53 });
54
55 $container->set('notFoundHandler', function ($c) {
56 return new Galette\Handlers\NotFound($c->get('view'));
57 });
58
59 // -----------------------------------------------------------------------------
60 // Service providers
61 // -----------------------------------------------------------------------------
62
63 // Register Smarty View helper
64 //TODO: old way - to drop
65 $container->set(
66 'view',
67 DI\get('Slim\Views\Smarty')
68 );
69 $container->set('Slim\Views\Smarty', function (ContainerInterface $c) {
70 $view = new \Slim\Views\Smarty(
71 rtrim(GALETTE_ROOT . GALETTE_TPL_SUBDIR, DIRECTORY_SEPARATOR),
72 [
73 'cacheDir' => rtrim(GALETTE_CACHE_DIR, DIRECTORY_SEPARATOR),
74 'compileDir' => rtrim(GALETTE_COMPILE_DIR, DIRECTORY_SEPARATOR),
75 'pluginsDir' => [
76 GALETTE_ROOT . 'includes/smarty_plugins'
77 ]
78 ]
79 );
80
81 // Add Slim specific plugins
82 $basepath = str_replace(
83 'index.php',
84 '',
85 $c->get('request')->getUri()->getBasePath()
86 );
87 $smartyPlugins = new SmartyPlugins($c->get('router'), $basepath);
88 $view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
89 $view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
90
91 $smarty = $view->getSmarty();
92 $smarty->inheritance_merge_compiled_includes = false;
93
94 $smarty->assign('flash', $c->get('flash'));
95
96 $smarty->assign('login', $c->get('login'));
97 $smarty->assign('logo', $c->get('logo'));
98 $smarty->assign('tpl', $smarty);
99 $smarty->assign('headers', $c->get('plugins')->getTplHeaders());
100 $smarty->assign('plugin_actions', $c->get('plugins')->getTplAdhActions());
101 $smarty->assign(
102 'plugin_batch_actions',
103 $c->get('plugins')->getTplAdhBatchActions()
104 );
105 $smarty->assign(
106 'plugin_detailled_actions',
107 $c->get('plugins')->getTplAdhDetailledActions()
108 );
109 $smarty->assign('scripts_dir', 'js/');
110 $smarty->assign('jquery_dir', 'js/jquery/');
111 $smarty->assign('jquery_markitup_version', JQUERY_MARKITUP_VERSION);
112 $smarty->assign('PAGENAME', basename($_SERVER['SCRIPT_NAME']));
113 $smarty->assign('galette_base_path', './');
114 $smarty->assign('GALETTE_VERSION', GALETTE_VERSION);
115 $smarty->assign('GALETTE_MODE', GALETTE_MODE);
116 $smarty->assign('GALETTE_DISPLAY_ERRORS', GALETTE_DISPLAY_ERRORS);
117 $smarty->assign('_CURRENT_THEME_PATH', _CURRENT_THEME_PATH);
118
119 /*if ($this->parserConfigDir) {
120 $instance->setConfigDir($this->parserConfigDir);
121 }*/
122
123 $smarty->assign('template_subdir', GALETTE_THEME);
124 foreach ($c->get('plugins')->getTplAssignments() as $k => $v) {
125 $smarty->assign($k, $v);
126 }
127 /** galette_lang should be removed and languages used instead */
128 $smarty->assign('galette_lang', $c->get('i18n')->getAbbrev());
129 $smarty->assign('galette_lang_name', $c->get('i18n')->getName());
130 $smarty->assign('languages', $c->get('i18n')->getList());
131 $smarty->assign('i18n', $c->get('i18n'));
132 $smarty->assign('plugins', $c->get('plugins'));
133 $smarty->assign('preferences', $c->get('preferences'));
134 $smarty->assign('pref_slogan', $c->get('preferences')->pref_slogan);
135 $smarty->assign('pref_theme', $c->get('preferences')->pref_theme);
136 $smarty->assign('pref_statut', $c->get('preferences')->pref_statut);
137 $smarty->assign(
138 'pref_editor_enabled',
139 $c->get('preferences')->pref_editor_enabled
140 );
141 $smarty->assign('pref_mail_method', $c->get('preferences')->pref_mail_method);
142 $smarty->assign('existing_mailing', $c->get('session')->mailing !== null);
143 $smarty->assign('contentcls', null);
144 $smarty->assign('additionnal_html_class', null);
145 $smarty->assign('error_detected', null);
146 $smarty->assign('warning_detected', null);
147 $smarty->assign('success_detected', null);
148 $smarty->assign('require_tree', null);
149 $smarty->assign('html_editor', null);
150 $smarty->assign('require_charts', null);
151 $smarty->assign('require_mass', null);
152 $smarty->assign('autocomplete', null);
153 if ($c->get('login')->isAdmin() && $c->get('preferences')->pref_telemetry_date) {
154 $now = new \DateTime();
155 $sent = new \DateTime($c->get('preferences')->pref_telemetry_date);
156 $sent->add(new \DateInterval('P1Y')); // ask to resend telemetry after one year
157 if ($now > $sent && !$_COOKIE['renew_telemetry']) {
158 $smarty->assign('renew_telemetry', true);
159 }
160 }
161
162 foreach ($c->get('plugins')->getModules() as $module_id => $module) {
163 $smarty->addTemplateDir(
164 $module['root'] . '/templates/' . $c->get('preferences')->pref_theme,
165 $module['route']
166 );
167 }
168 return $view;
169 });
170
171 // Flash messages
172 //TODO: old way - to drop
173 $container->set(
174 'flash',
175 DI\get('Slim\Flash\Messages')
176 );
177 $container->set('Slim\Flash\Messages', DI\autowire());
178
179 //TODO: old way - to drop
180 $container->set(
181 'plugins',
182 \DI\get(Galette\Core\Plugins::class)
183 );
184
185 $container->set(Galette\Core\Plugins::class, function (ContainerInterface $c) use ($plugins) {
186 $i18n = $c->get('i18n');
187 $plugins->loadModules($c->get('preferences'), GALETTE_PLUGINS_PATH, $i18n->getLongID());
188 return $plugins;
189 });
190
191 //TODO: old way - to drop
192 $container->set(
193 'i18n',
194 \DI\get('Galette\Core\I18n')
195 );
196
197 $container->set('Galette\Core\I18n', function (ContainerInterface $c) {
198 $i18n = $c->get('session')->i18n;
199 if (!$i18n || !$i18n->getId() || isset($_GET['ui_pref_lang']) && $_GET['ui_pref_lang']) {
200 $i18n = new Galette\Core\I18n($_GET['ui_pref_lang'] ?? false);
201 $c->get('session')->i18n = $i18n;
202 }
203 return $i18n;
204 });
205
206 $container->set('l10n', function (ContainerInterface $c) {
207 $l10n = new Galette\Core\L10n(
208 $c->get('zdb'),
209 $c->get('i18n')
210 );
211 return $l10n;
212 });
213
214 //TODO: old way - to drop
215 $container->set(
216 'zdb',
217 DI\get('Galette\Core\Db')
218 );
219 $container->set('Galette\Core\Db', DI\autowire());
220
221 //TODO: old way - to drop
222 $container->set(
223 'preferences',
224 DI\get('Galette\Core\Preferences')
225 );
226 $container->set('Galette\Core\Preferences', DI\autowire());
227
228 //TODO: old way - to drop
229 $container->set(
230 'login',
231 DI\get('Galette\Core\Login')
232 );
233 $container->set('Galette\Core\Login', function (ContainerInterface $c) {
234 $login = $c->get('session')->login;
235 if (!$login) {
236 $login = new Galette\Core\Login(
237 $c->get('zdb'),
238 $c->get('i18n')
239 );
240 }
241 return $login;
242 });
243
244 /*$container->set('session', function (ContainerInterface $c) {
245 $session = new \RKA\Session();
246 return $session;
247 });*/
248
249 //TODO: old way - to drop
250 $container->set(
251 'logo',
252 DI\get('Galette\Core\Logo')
253 );
254 $container->set('Galette\Core\Logo', DI\autowire());
255
256 //TODO: old way - to drop
257 $container->set(
258 'print_logo',
259 DI\get('Galette\Core\PrintLogo')
260 );
261 $container->set('Galette\Core\PrintLogo', DI\autowire());
262
263 //TODO: old way - to drop
264 $container->set(
265 'history',
266 DI\get('Galette\Core\History')
267 );
268 $container->set('Galette\Core\History', \DI\autowire());
269
270 $container->set('acls', function (ContainerInterface $c) {
271 include_once GALETTE_ROOT . 'includes/core_acls.php';
272 $acls = $core_acls;
273
274 foreach ($c->get('plugins')->getModules() as $plugin) {
275 $acls[$plugin['route'] . 'Info'] = 'member';
276 }
277
278 //use + to be sure core ACLs are not overrided by plugins ones.
279 $acls = $acls + $c->get('plugins')->getAcls();
280
281 //load user defined ACLs
282 if (file_exists(GALETTE_CONFIG_PATH . 'local_acls.inc.php')) {
283 //use array_merge here, we want $local_acls to override core ones.
284 $acls = array_merge($acls, $local_acls);
285 }
286
287 return $acls;
288 });
289
290 $container->set('texts_fields', function (ContainerInterface $c) {
291 include_once GALETTE_ROOT . 'includes/fields_defs/texts_fields.php';
292 return $texts_fields;
293 });
294
295 $container->set('members_fields', function (ContainerInterface $c) {
296 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
297 return $members_fields;
298 });
299
300 $container->set('members_form_fields', function (ContainerInterface $c) {
301 $fields = $c->get('members_fields');
302 foreach ($fields as $k => $field) {
303 if ($field['position'] == -1) {
304 unset($fields[$k]);
305 }
306 }
307 return $fields;
308 });
309
310 $container->set('members_fields_cats', function (ContainerInterface $c) {
311 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
312 return $members_fields_cats;
313 });
314
315 // -----------------------------------------------------------------------------
316 // Service factories
317 // -----------------------------------------------------------------------------
318
319 // monolog
320 $container->set('logger', function (ContainerInterface $c) {
321 $settings = $c->get('settings');
322 $logger = new \Monolog\Logger($settings['logger']['name']);
323 $logger->pushProcessor(new \Monolog\Processor\UidProcessor());
324 $logger->pushHandler(new \Monolog\Handler\StreamHandler($settings['logger']['path'], $settings['logger']['level']));
325 return $logger;
326 });
327
328 //TODO: old way - to drop
329 $container->set(
330 'fields_config',
331 DI\get('Galette\Entity\FieldsConfig')
332 );
333 $container->set('Galette\Entity\FieldsConfig', function (ContainerInterface $c) {
334 $fc = new Galette\Entity\FieldsConfig(
335 $c->get('zdb'),
336 Galette\Entity\Adherent::TABLE,
337 $c->get('members_fields'),
338 $c->get('members_fields_cats')
339 );
340 return $fc;
341 });
342
343 //TODO: old way - to drop
344 $container->set(
345 'lists_config',
346 DI\get('Galette\Entity\ListsConfig')
347 );
348 $container->set('Galette\Entity\ListsConfig', function (ContainerInterface $c) {
349 $fc = new Galette\Entity\ListsConfig(
350 $c->get('zdb'),
351 Galette\Entity\Adherent::TABLE,
352 $c->get('members_fields'),
353 $c->get('members_fields_cats')
354 );
355 return $fc;
356 });
357
358 $container->set('cache', function (ContainerInterface $c) {
359 $adapter = null;
360 if (function_exists('wincache_ucache_add')) {
361 //since APCu is not known to work on windows
362 $adapter = 'wincache';
363 } elseif (function_exists('apcu_fetch')) {
364 $adapter = 'apcu';
365 }
366 if ($adapter !== null) {
367 $uuid = $c->get('galette.mode') !== 'INSTALL' ? $c->get('preferences')->pref_instance_uuid : '_install';
368 $cache = Laminas\Cache\StorageFactory::factory([
369 'adapter' => $adapter,
370 'options' => [
371 'namespace' => str_replace(
372 ['%version', '%uuid'],
373 [GALETTE_VERSION, $uuid],
374 'galette_%version_%uuid'
375 )
376 ]
377 ]);
378 return $cache;
379 }
380 return null;
381 });
382
383 //TODO: old way - to drop
384 $container->set(
385 'translator',
386 DI\get('Galette\Core\Translator')
387 );
388 $container->set('Galette\Core\Translator', function (ContainerInterface $c) {
389 $translator = new Galette\Core\Translator();
390
391 $domains = ['galette'];
392 foreach ($domains as $domain) {
393 //load translation file for domain
394 $translator->addTranslationFilePattern(
395 'gettext',
396 GALETTE_ROOT . '/lang/',
397 '/%s/LC_MESSAGES/' . $domain . '.mo',
398 $domain
399 );
400
401 //check if a local lang file exists and load it
402 $translator->addTranslationFilePattern(
403 'phparray',
404 GALETTE_ROOT . '/lang/',
405 $domain . '_%s_local_lang.php',
406 $domain
407 );
408 }
409
410 $translator->setLocale($c->get('i18n')->getLongID());
411 if (
412 !$c->has('galette.mode')
413 || $c->get('galette.mode') !== 'INSTALL'
414 && $c->get('galette.mode') !== 'NEED_UPDATE'
415 ) {
416 $translator->setCache($c->get('cache'));
417 }
418 return $translator;
419 });
420
421 // Add Event manager to dependency.
422 $container->set(
423 'event_manager',
424 DI\create('Slim\Event\SlimEventManager')
425 ->method(
426 'useListenerProvider',
427 DI\get('Galette\Events\MemberListener')
428 )
429 ->method(
430 'useListenerProvider',
431 DI\get('Galette\Events\ContribListener')
432 )
433 );
434
435 $container->set(
436 'csrf',
437 function (ContainerInterface $c) {
438 $storage = null;
439 $guard = new \Slim\Csrf\Guard(
440 'csrf',
441 $storage,
442 null,
443 200,
444 16,
445 true
446 );
447
448 $guard->setFailureCallable(function ($request, $response, $next) {
449 Analog::log(
450 'CSRF check has failed',
451 Analog::CRITICAL
452 );
453 throw new \RuntimeException(_T('Failed CSRF check!'));
454 });
455
456 return $guard;
457 }
458 );
459
460 //For bad existing globals can be used...
461 global $translator, $i18n;
462 if (
463 !$container->has('galette.mode')
464 || $container->get('galette.mode') !== 'INSTALL'
465 && $container->get('galette.mode') !== 'NEED_UPDATE'
466 ) {
467 global $zdb, $preferences, $login, $hist, $l10n, $emitter;
468 $zdb = $container->get('zdb');
469 $preferences = $container->get('preferences');
470 $login = $container->get('login');
471 $hist = $container->get('history');
472 $l10n = $container->get('l10n');
473 $emitter = $container->get('event_manager');
474 $router = $container->get('router');
475 }
476 $i18n = $container->get('i18n');
477 $translator = $container->get('translator');
478 require_once GALETTE_ROOT . 'includes/i18n.inc.php';