Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions src/Glpi/Application/View/Extension/FrontEndAssetsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,53 @@ public function configJs(): string
$cfg_glpi += Config::getSafeConfig(true);
}

// Common config keys used by front-end code.
// These will be immediately available while others will be lazy-loaded via a proxy handler.
$common_configs = [
'root_doc', 'url_base', 'planning_begin', 'planning_end', 'planning_work_days', 'toast_location',
'is_demo_dashboards', 'priority_1', 'priority_2', 'priority_3', 'priority_4', 'priority_5', 'priority_6',
'date_format', 'refresh_views',
];
$common_config = array_filter(
$cfg_glpi,
static fn($key) => in_array($key, $common_configs, true),
ARRAY_FILTER_USE_KEY
);
$common_config = json_encode($common_config);

$plugins_path = \array_combine(
Plugin::getPlugins(),
\array_map(fn(string $plugin_key) => "/plugins/{$plugin_key}", Plugin::getPlugins())
\array_map(static fn(string $plugin_key) => "/plugins/{$plugin_key}", Plugin::getPlugins())
);

$script = sprintf('window.CFG_GLPI = %s;', json_encode($cfg_glpi, JSON_PRETTY_PRINT))
. "\n"
. sprintf('window.GLPI_PLUGINS_PATH = %s;', json_encode($plugins_path, JSON_PRETTY_PRINT));
$plugins_path = json_encode($plugins_path);

$script = <<<JS
window.CFG_GLPI = new Proxy($common_config, {
get: (target, prop, receiver) => {
if (prop in target) {
return Reflect.get(target, prop, receiver);
}
// Note there was a request for a config not in the common set to indicate a potential optimization
console.debug("Lazy loading a CFG_GLPI property that was not available by default: " + prop + '. This may be an optimization opportunity.');
// Hydrate the missing config via a synchronous AJAX call
$.ajax({
type: 'GET',
url: CFG_GLPI.root_doc + '/Session/Config/' + prop,
async: false,
dataType: 'json',
success: (value) => {
target[prop] = value;
},
error: () => {
console.warn('Failed to load config property ' + prop);
target[prop] = undefined;
}
});
return Reflect.get(target, prop, receiver);
}
});
window.GLPI_PLUGINS_PATH = $plugins_path;
JS;

return Html::scriptBlock($script);
}
Expand Down
60 changes: 60 additions & 0 deletions src/Glpi/Controller/Session/ConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Controller\Session;

use Config;
use Glpi\Controller\AbstractController;
use Glpi\Exception\Http\NotFoundHttpException;
use Glpi\Http\Firewall;
use Glpi\Security\Attribute\SecurityStrategy;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class ConfigController extends AbstractController
{
#[Route("/Session/Config/{key}", name: "get_config_value")]
#[SecurityStrategy(Firewall::STRATEGY_AUTHENTICATED)]
public function content(Request $request): Response
{
$safe_config = Config::getSafeConfig(true);
$key = $request->get('key');
if (!array_key_exists($key, $safe_config)) {
throw new NotFoundHttpException();
}
return new JsonResponse($safe_config[$key]);
}
}
Loading