]> git.agnieray.net Git - galette.git/blob - galette/includes/routes/plugins.routes.php
Improve coding standards
[galette.git] / galette / includes / routes / plugins.routes.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 use Slim\Psr7\Request;
23 use Slim\Psr7\Response;
24
25 $app->group(
26 '/plugins',
27 function (\Slim\Routing\RouteCollectorProxy $app) use ($authenticate): void {
28 /** @var $container \DI\Container */
29 $container = $app->getContainer();
30 $modules = $container->get('plugins')->getModules();
31
32 //Global route to access plugin resources (CSS, JS, images, ...)
33 $app->get(
34 '/{plugin}/res/{path:.*}',
35 function (Request $request, Response $response, $plugin, $path) use ($container) {
36 $ext = pathinfo($path)['extension'];
37 $auth_ext = [
38 'js' => 'text/javascript',
39 'css' => 'text/css',
40 'png' => 'image/png',
41 'jpg' => 'image/jpg',
42 'jpeg' => 'image/jpg',
43 'gif' => 'image/gif',
44 'svg' => 'image/svg+xml',
45 'map' => 'application/json',
46 'woff' => 'application/font-woff',
47 'woff2' => 'application/font-woff2'
48 ];
49 if (strpos($path, '../') === false && isset($auth_ext[$ext])) {
50 $file = $container->get('plugins')->getFile(
51 $plugin,
52 $path
53 );
54
55 $response = $response->withHeader('Content-type', $auth_ext[$ext]);
56
57 $body = $response->getBody();
58 $body->write(file_get_contents($file));
59 return $response;
60 } else {
61 throw new \RuntimeException(
62 sprintf(
63 'Invalid extension %1$s (%2$s)!',
64 $ext,
65 $path
66 ),
67 404
68 );
69 }
70 }
71 )->setName('plugin_res');
72
73 //Declare configured routes for each plugin
74 foreach ($modules as $module_id => $module) {
75 $container->set('Plugin ' . $module['name'], ['module' => $module, 'module_id' => $module_id]);
76
77 $app->group(
78 '/' . $module['route'],
79 //$module_id may be used in included _routes.php from plugin.
80 function (\Slim\Routing\RouteCollectorProxy $app) use ($module, $module_id, $authenticate, $container): void {
81 //Plugin home: give information
82 $app->get(
83 '',
84 function ($request, $response) use ($module, $container) {
85 $params = [
86 'page_title' => $module['name'],
87 'name' => $module['name'],
88 'version' => $module['version'],
89 'date' => $module['date'],
90 'author' => $module['author']
91 ];
92 if ($container->get('login')->isAdmin()) {
93 $params['module'] = $module;
94 }
95 // display page
96 $container->get(\Slim\Views\Twig::class)->render(
97 $response,
98 'pages/plugin_info.html.twig',
99 $params
100 );
101 return $response;
102 }
103 )->setName($module['route'] . 'Info')->add($authenticate);
104
105 $f = $module['root'] . '/_routes.php';
106 include_once $f;
107 }
108 );
109 }
110 }
111 );