Skip to content

Commit

Permalink
config: fix correctness issues in reading
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Mar 15, 2024
1 parent 77f47ed commit 54b467a
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,16 @@ private function readData() {

// Include file and merge config
foreach ($configFiles as $file) {
$fileExistsAndIsReadable = file_exists($file) && is_readable($file);
$filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false;
if ($file === $this->configFilePath &&
$filePointer === false) {
// Opening the main config might not be possible, e.g. if the wrong
// permissions are set (likely on a new installation)
unset($CONFIG);

// Invalidate opcache (only if the timestamp changed)
if (function_exists('opcache_invalidate')) {
@opcache_invalidate($file, false);
}

$filePointer = fopen($file, 'r');
if ($filePointer === false) {
// e.g. wrong permissions are set (likely on a new installation)
continue;
}

Expand All @@ -229,8 +233,14 @@ private function readData() {
throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
}

unset($CONFIG);
include $file;
try {
include $file;
} finally {
// Close the file pointer and release the lock
flock($filePointer, LOCK_UN);
fclose($filePointer);
}

if (!defined('PHPUNIT_RUN') && headers_sent()) {
// syntax issues in the config file like leading spaces causing PHP to send output
$errorMessage = sprintf('Config file has leading content, please remove everything before "<?php" in %s', basename($file));
Expand All @@ -242,10 +252,6 @@ private function readData() {
if (isset($CONFIG) && is_array($CONFIG)) {
$this->cache = array_merge($this->cache, $CONFIG);
}

// Close the file pointer and release the lock
flock($filePointer, LOCK_UN);
fclose($filePointer);
}

$this->envCache = getenv();
Expand Down

0 comments on commit 54b467a

Please sign in to comment.