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: 49 additions & 0 deletions ajax/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @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/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Exception\Http\NotFoundHttpException;

use function Safe\json_encode;

Session::checkLoginUser();
Session::writeClose();

header("Content-Type: application/json; charset=UTF-8");
$safe_config = Config::getSafeConfig(true);
if (!isset($_GET['key']) || !array_key_exists($_GET['key'], $safe_config)) {
throw new NotFoundHttpException();
}

echo json_encode($safe_config[$_GET['key']]);
50 changes: 45 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,54 @@ 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 + '/ajax/config.php',
async: false,
dataType: 'json',
data: { key: prop },
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