Skip to content

Commit 7e60ec2

Browse files
authored
Clean constants handling (#19684)
* Make the `GLPI_LOG_LVL` constant always defined * Deprecate the GLPI_FORCE_MAIL constant * Make SKIP_UPDATES always defined * Make GLPI_SYSTEM_CRON always defined * Replace GLPI_KEEP_CSRF_TOKEN by method params * Add missing GLPI_USE_CSRF_CHECK to PHPStan config * Use the `GLPI_` prefix for all constants * Handle PLUGINS_DIRECTORIES deprecation
1 parent 9bf3af4 commit 7e60ec2

28 files changed

+88
-75
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ The present file will list all changes made to the project; according to the
236236
- `Software::merge()` method is now private.
237237
- The refusal of the collected emails corresponding to a GLPI notification will now be made based on a default rule.
238238
- The `$store_path` parameter has been removed from the `Dropdown::dropdownIcons()` method.
239+
- The `PLUGINS_DIRECTORIES` constant has been renamed to `GLPI_PLUGINS_DIRECTORIES`.
239240

240241
#### Deprecated
241242
- Usage of the `/marketplace` path for plugins URLs. All plugins URLs should now start with `/plugins`.
242243
- Usage of `GLPI_PLUGINS_PATH` javascript variable.
244+
- Usage of the `GLPI_FORCE_MAIL` constant.
243245
- Usage of `MAIL_SMTPSSL` and `MAIL_SMTPTLS` constants.
244246
- Usage of `name` and `users_id_validate` parameter in `ajax/dropdownValidator.php`.
245247
- Usage of `users_id_validate` parameter in `front/commonitilvalidation.form.php`.
@@ -325,7 +327,7 @@ The present file will list all changes made to the project; according to the
325327
- `Toolbox::stripslashes_deep()`
326328

327329
#### Removed
328-
- `GLPI_USE_CSRF_CHECK`, `GLPI_USE_IDOR_CHECK`, `GLPI_CSRF_EXPIRES`, `GLPI_CSRF_MAX_TOKENS` and `GLPI_IDOR_EXPIRES` constants.
330+
- `GLPI_USE_CSRF_CHECK`, `GLPI_USE_IDOR_CHECK`, `GLPI_KEEP_CSRF_TOKEN`, `GLPI_CSRF_EXPIRES`, `GLPI_CSRF_MAX_TOKENS` and `GLPI_IDOR_EXPIRES` constants.
329331
- `GLPI_DEMO_MODE` constant.
330332
- `GLPI_DUMP_DIR` constant.
331333
- `GLPI_SQL_DEBUG` constant.

phpstan.neon.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,22 @@ parameters:
4848
- GLPI_NETWORK_SERVICES
4949
- GLPI_PICTURE_DIR
5050
- GLPI_PLUGIN_DOC_DIR
51+
- GLPI_PLUGINS_DIRECTORIES
5152
- GLPI_RSS_DIR
5253
- GLPI_SERVERSIDE_URL_ALLOWLIST
5354
- GLPI_SESSION_DIR
55+
- GLPI_SKIP_UPDATES
5456
- GLPI_STRICT_ENV
57+
- GLPI_SYSTEM_CRON
5558
- GLPI_TELEMETRY_URI
5659
- GLPI_TEXT_MAXSIZE
5760
- GLPI_THEMES_DIR
5861
- GLPI_TMP_DIR
5962
- GLPI_UPLOAD_DIR
63+
- GLPI_USE_CSRF_CHECK
6064
- GLPI_USER_AGENT_EXTRA_COMMENTS
6165
- GLPI_VAR_DIR
6266
- GLPI_WEBHOOK_ALLOW_RESPONSE_SAVING
63-
- PLUGINS_DIRECTORIES
6467
- TU_USER
6568
ignoreErrors:
6669
- '~Instantiated class XHProfRuns_Default not found~'

src/CronTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ public static function register($itemtype, $name, $frequency, $options = [])
975975
}
976976
}
977977
if (
978-
defined('GLPI_SYSTEM_CRON')
978+
GLPI_SYSTEM_CRON
979979
&& ($input['allowmode'] & self::MODE_EXTERNAL)
980980
&& !isset($input['mode'])
981981
) {

src/DbUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function getItemTypeForTable($table)
367367
*
368368
* @return string
369369
*/
370-
public function fixItemtypeCase(string $itemtype, $root_dir = GLPI_ROOT, array $plugins_dirs = PLUGINS_DIRECTORIES)
370+
public function fixItemtypeCase(string $itemtype, $root_dir = GLPI_ROOT, array $plugins_dirs = GLPI_PLUGINS_DIRECTORIES)
371371
{
372372
/** @var \Psr\SimpleCache\CacheInterface $GLPI_CACHE */
373373
global $GLPI_CACHE;

src/Glpi/Application/Environment.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function getConstantsOverride(string $root_dir): array
133133
self::TESTING => [
134134
'GLPI_CONFIG_DIR' => $root_dir . '/tests/config',
135135
'GLPI_VAR_DIR' => $root_dir . '/tests/files',
136+
'GLPI_LOG_LVL' => LogLevel::DEBUG,
136137
'GLPI_STRICT_ENV' => true,
137138
'GLPI_SERVERSIDE_URL_ALLOWLIST' => [
138139
// default allowlist entries
@@ -143,30 +144,19 @@ public function getConstantsOverride(string $root_dir): array
143144
// calendar mockups
144145
'/^file:\/\/.*\.ics$/',
145146
],
146-
'PLUGINS_DIRECTORIES' => [
147+
'GLPI_PLUGINS_DIRECTORIES' => [
147148
$root_dir . '/plugins',
148149
$root_dir . '/tests/fixtures/plugins',
149150
],
150151
],
151152
self::DEVELOPMENT => [
153+
'GLPI_LOG_LVL' => LogLevel::DEBUG,
152154
'GLPI_STRICT_ENV' => true,
153155
'GLPI_WEBHOOK_ALLOW_RESPONSE_SAVING' => '1',
154156
],
155157
};
156158
}
157159

158-
public function getLogLevel(): string
159-
{
160-
// Do not report debug, info, and notice messages unless in development/testing env.
161-
// Notices are errors with no functional impact, so we do not want people to report them as issues.
162-
// Suppressing the INFO level will prevent deprecations to be pushed in other environments logs.
163-
return match ($this) {
164-
default => LogLevel::WARNING,
165-
self::TESTING => LogLevel::DEBUG,
166-
self::DEVELOPMENT => LogLevel::DEBUG,
167-
};
168-
}
169-
170160
/**
171161
* Will the files of this environment change ?
172162
* This may affect which cache we decide to set (twig, http cache on the

src/Glpi/Application/SystemConfigurator.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Glpi\Log\ErrorLogHandler;
4040
use Monolog\Logger;
4141
use Psr\Log\LoggerInterface;
42+
use Psr\Log\LogLevel;
4243

4344
final class SystemConfigurator
4445
{
@@ -50,6 +51,9 @@ public function __construct(private string $root_dir, private ?string $env)
5051
$this->setSessionConfiguration();
5152
$this->initLogger();
5253
$this->registerErrorHandler();
54+
55+
// Keep it after `registerErrorHandler()` call to be sure that messages are correctly handled.
56+
$this->checkForObsoleteConstants();
5357
}
5458

5559
public function getLogger(): LoggerInterface
@@ -99,7 +103,7 @@ private function computeConstants(): void
99103

100104
// Where to load plugins.
101105
// Order in this array is important (priority to first found).
102-
'PLUGINS_DIRECTORIES' => [
106+
'GLPI_PLUGINS_DIRECTORIES' => [
103107
'{GLPI_MARKETPLACE_DIR}',
104108
$this->root_dir . '/plugins',
105109
],
@@ -145,12 +149,15 @@ private function computeConstants(): void
145149

146150
// Constants dedicated to developers
147151
'GLPI_DISABLE_ONLY_FULL_GROUP_BY_SQL_MODE' => '1', // '1' to disable ONLY_FULL_GROUP_BY 'sql_mode'
152+
'GLPI_LOG_LVL' => LogLevel::WARNING,
153+
'GLPI_SKIP_UPDATES' => false, // `true` to bypass minor versions DB updates
148154
'GLPI_STRICT_ENV' => false, // `true` to make environment more strict (strict variables in twig templates, etc)
149155

150156
// Other constants
151157
'GLPI_AJAX_DASHBOARD' => '1', // 1 for "multi ajax mode" 0 for "single ajax mode" (see Glpi\Dashboard\Grid::getCards)
152158
'GLPI_CALDAV_IMPORT_STATE' => 0, // external events created from a caldav client will take this state by default (0 = Planning::INFO)
153159
'GLPI_CENTRAL_WARNINGS' => '1', // display (1), or not (0), warnings on GLPI Central page
160+
'GLPI_SYSTEM_CRON' => false, // `true` to use the system cron provided by the downstream package
154161
'GLPI_TEXT_MAXSIZE' => '4000', // character threshold for displaying read more button
155162
'GLPI_WEBHOOK_ALLOW_RESPONSE_SAVING' => '0', // allow (1) or not (0) to save webhook response in database
156163
],
@@ -179,6 +186,11 @@ private function computeConstants(): void
179186
include_once($this->root_dir . '/inc/downstream.php');
180187
}
181188

189+
// Handle deprecated/obsolete constants
190+
if (defined('PLUGINS_DIRECTORIES') && !defined('GLPI_PLUGINS_DIRECTORIES')) {
191+
define('GLPI_PLUGINS_DIRECTORIES', PLUGINS_DIRECTORIES);
192+
}
193+
182194
// Configure environment type if not defined by user.
183195
if (Environment::isSet()) {
184196
Environment::validate();
@@ -269,4 +281,21 @@ private function registerErrorHandler(): void
269281
$errorHandler = new ErrorHandler($this->logger);
270282
$errorHandler::register($errorHandler);
271283
}
284+
285+
private function checkForObsoleteConstants(): void
286+
{
287+
if (defined('GLPI_USE_CSRF_CHECK')) {
288+
trigger_error(
289+
'The `GLPI_USE_CSRF_CHECK` constant is now ignored for security reasons.',
290+
E_USER_WARNING
291+
);
292+
}
293+
294+
if (defined('PLUGINS_DIRECTORIES')) {
295+
trigger_error(
296+
'The `PLUGINS_DIRECTORIES` constant is deprecated. Use the `GLPI_PLUGINS_DIRECTORIES` constant instead.',
297+
E_USER_DEPRECATED
298+
);
299+
}
300+
}
272301
}

src/Glpi/Console/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
252252

253253
if (
254254
$is_db_available
255-
&& defined('SKIP_UPDATES')
255+
&& GLPI_SKIP_UPDATES
256256
&& (!($command instanceof GlpiCommandInterface) || $command->requiresUpToDateDb())
257257
&& !Update::isDbUpToDate()
258258
) {

src/Glpi/Console/CommandLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private function findPluginCommands()
218218
}
219219

220220
$plugins_directories = new AppendIterator();
221-
foreach (PLUGINS_DIRECTORIES as $directory) {
221+
foreach (GLPI_PLUGINS_DIRECTORIES as $directory) {
222222
$directory = str_replace(GLPI_ROOT, $this->rootdir, $directory);
223223
$plugins_directories->append(new DirectoryIterator($directory));
224224
}

src/Glpi/Console/Plugin/InstallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected function getDirectoryChoiceChoices()
185185

186186
// Fetch directory list
187187
$directories = [];
188-
foreach (PLUGINS_DIRECTORIES as $plugins_directory) {
188+
foreach (GLPI_PLUGINS_DIRECTORIES as $plugins_directory) {
189189
$directory_handle = opendir($plugins_directory);
190190
while (false !== ($filename = readdir($directory_handle))) {
191191
if (

src/Glpi/Error/ErrorHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ private function configureErrorReporting(): void
236236
$reporting_level = E_ALL;
237237

238238
// Compute max error level that should be reported
239-
$env_psr_level = Environment::get()->getLogLevel();
240-
$env_report_value = self::PSR_ERROR_LEVEL_VALUES[$env_psr_level];
239+
$env_report_value = self::PSR_ERROR_LEVEL_VALUES[GLPI_LOG_LVL];
241240

242241
foreach (self::ERROR_LEVEL_MAP as $value => $log_level) {
243242
$psr_level_value = self::PSR_ERROR_LEVEL_VALUES[$log_level];

0 commit comments

Comments
 (0)