Skip to content

Commit d4bfb58

Browse files
committed
Move CFG_GLPI JS var definition in the HTML header
1 parent 03c873e commit d4bfb58

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

.phpstan-baseline.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,12 +3847,6 @@
38473847
'count' => 1,
38483848
'path' => __DIR__ . '/src/Glpi/Application/View/Extension/DocumentExtension.php',
38493849
];
3850-
$ignoreErrors[] = [
3851-
'message' => '#^Function json_encode is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add \'use function Safe\\\\json_encode;\' at the beginning of the file to use the variant provided by the \'thecodingmachine/safe\' library\\.$#',
3852-
'identifier' => 'theCodingMachineSafe.function',
3853-
'count' => 1,
3854-
'path' => __DIR__ . '/src/Glpi/Application/View/Extension/FrontEndAssetsExtension.php',
3855-
];
38563850
$ignoreErrors[] = [
38573851
'message' => '#^Function parse_url is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add \'use function Safe\\\\parse_url;\' at the beginning of the file to use the variant provided by the \'thecodingmachine/safe\' library\\.$#',
38583852
'identifier' => 'theCodingMachineSafe.function',

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ The present file will list all changes made to the project; according to the
471471
- `Html::displayAccessDeniedPage()`
472472
- `Html::displayAjaxMessageAfterRedirect()`. The JS function is already provided by `js/misc.js`.
473473
- `Html::displayItemNotFoundPage()`
474+
- `Html::getCoreVariablesForJavascript()`
474475
- `Html::jsConfirmCallback()`
475476
- `Html::jsHide()`
476477
- `Html::jsShow()`

install/install.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ function header_html($etape)
7676
'custom_header_tags' => [],
7777
]);
7878

79-
// CFG
80-
echo Html::getCoreVariablesForJavascript();
81-
8279
echo "<body>";
8380
echo "<div id='principal'>";
8481
echo "<div id='bloc'>";

install/update.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ function showSecurityKeyCheckForm()
119119
'custom_header_tags' => [],
120120
]);
121121

122-
// CFG
123-
echo Html::getCoreVariablesForJavascript();
124-
125122
echo "<body>";
126123
echo "<div id='principal'>";
127124
echo "<div id='bloc'>";

src/Glpi/Application/View/Extension/FrontEndAssetsExtension.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
namespace Glpi\Application\View\Extension;
3737

38+
use Config;
3839
use DBmysql;
3940
use Entity;
4041
use Glpi\Application\ImportMapGenerator;
@@ -47,6 +48,8 @@
4748
use Twig\Extension\AbstractExtension;
4849
use Twig\TwigFunction;
4950

51+
use function Safe\json_encode;
52+
5053
/**
5154
* @since 10.0.0
5255
*/
@@ -70,6 +73,7 @@ public function getFunctions(): array
7073
new TwigFunction('css_path', [$this, 'cssPath']),
7174
new TwigFunction('js_path', [$this, 'jsPath']),
7275
new TwigFunction('custom_css', [$this, 'customCss'], ['is_safe' => ['html']]),
76+
new TwigFunction('config_js', [$this, 'configJs'], ['is_safe' => ['html']]),
7377
new TwigFunction('locales_js', [$this, 'localesJs'], ['is_safe' => ['html']]),
7478
new TwigFunction('current_theme', [$this, 'currentTheme']),
7579
new TwigFunction('importmap', [$this, 'importmap'], ['is_safe' => ['html']]),
@@ -270,6 +274,34 @@ public function localesJs(): string
270274
return Html::scriptBlock($script);
271275
}
272276

277+
/**
278+
* Return config (CFG_GLPI) JS code.
279+
*
280+
* @return string
281+
*/
282+
public function configJs(): string
283+
{
284+
/** @var array $CFG_GLPI */
285+
global $CFG_GLPI;
286+
287+
$cfg_glpi = [
288+
'url_base' => $CFG_GLPI['url_base'] ?? '', // may not be defined during the install process
289+
'root_doc' => $CFG_GLPI['root_doc'],
290+
];
291+
if (Session::getLoginUserID(true) !== false) {
292+
// expose full config only for connected users
293+
$cfg_glpi += Config::getSafeConfig(true);
294+
}
295+
296+
$plugins_path = \array_map(fn(string $plugin_key) => "/plugins/{$plugin_key}", Plugin::getPlugins());
297+
298+
$script = sprintf('const CFG_GLPI = %s;', json_encode($cfg_glpi, JSON_PRETTY_PRINT))
299+
. "\n"
300+
. sprintf('const GLPI_PLUGINS_PATH = %s;', json_encode($plugins_path, JSON_PRETTY_PRINT));
301+
302+
return Html::scriptBlock($script);
303+
}
304+
273305
/**
274306
* Generate an import map for JavaScript modules
275307
*

src/Html.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,8 +1666,6 @@ public static function footer()
16661666
}
16671667
$FOOTER_LOADED = true;
16681668

1669-
echo self::getCoreVariablesForJavascript(true);
1670-
16711669
if (isset($CFG_GLPI['notifications_ajax']) && $CFG_GLPI['notifications_ajax'] && !Session::isImpersonateActive()) {
16721670
$options = [
16731671
'interval' => ($CFG_GLPI['notifications_ajax_check_interval'] ?: 5) * 1000,
@@ -5964,9 +5962,6 @@ private static function loadJavascript()
59645962
*/
59655963
global $CFG_GLPI, $PLUGIN_HOOKS;
59665964

5967-
// transfer core variables to javascript side
5968-
echo self::getCoreVariablesForJavascript(true);
5969-
59705965
//load on demand scripts
59715966
if (isset($_SESSION['glpi_js_toload'])) {
59725967
foreach ($_SESSION['glpi_js_toload'] as $key => $script) {

templates/layout/parts/head.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474

7575
{{ importmap() }}
7676

77+
{{ config_js() }}
78+
7779
{% for js_file in js_files %}
7880
<script type="text/javascript" src="{{ js_path(js_file.path, js_file.options ?? []) }}"></script>
7981
{% endfor %}

0 commit comments

Comments
 (0)