Skip to content

Commit feb75d7

Browse files
committed
build: index.php and updater.phar
Signed-off-by: Arthur Schiwon <[email protected]>
1 parent c694f97 commit feb75d7

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

index.php

+38-26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
99

10+
1011
class UpdateException extends \Exception {
1112

1213
/** @param list<string> $data */
@@ -63,18 +64,7 @@ class Updater {
6364
public function __construct(string $baseDir) {
6465
$this->baseDir = $baseDir;
6566

66-
if ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) {
67-
$configFileName = rtrim($dir, '/') . '/config.php';
68-
} else {
69-
$configFileName = $this->baseDir . '/../config/config.php';
70-
}
71-
if (!file_exists($configFileName)) {
72-
throw new \Exception('Could not find config.php. Is this file in the "updater" subfolder of Nextcloud?');
73-
}
74-
75-
/** @var array $CONFIG */
76-
require_once $configFileName;
77-
$this->configValues = $CONFIG;
67+
[$this->configValues] = $this->readConfigFile();
7868

7969
if (php_sapi_name() !== 'cli' && ($this->configValues['upgrade.disable-web'] ?? false)) {
8070
// updater disabled
@@ -116,6 +106,38 @@ public function __construct(string $baseDir) {
116106
$this->buildTime = $buildTime;
117107
}
118108

109+
/**
110+
* @return array{array, string}
111+
*/
112+
private function readConfigFile(): array {
113+
if ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) {
114+
$configFileName = realpath($dir . '/config.php');
115+
} else {
116+
$configFileName = $this->baseDir . '/config/config.php';
117+
}
118+
if (!file_exists($configFileName)) {
119+
throw new \Exception('Could not find config.php (' . $configFileName . '). Is this file in the "updater" subfolder of Nextcloud?');
120+
}
121+
$filePointer = @fopen($configFileName, 'r');
122+
if ($filePointer === false) {
123+
throw new \Exception('Could not open config.php (' . $configFileName . ').');
124+
}
125+
if (!flock($filePointer, LOCK_SH)) {
126+
throw new \Exception('Could not acquire a shared lock on the config file (' . $configFileName . ')');
127+
}
128+
129+
try {
130+
require $configFileName;
131+
} finally {
132+
// Close the file pointer and release the lock
133+
flock($filePointer, LOCK_UN);
134+
fclose($filePointer);
135+
}
136+
137+
/** @var array $CONFIG */
138+
return [$CONFIG,$configFileName];
139+
}
140+
119141
/**
120142
* Returns whether the web updater is disabled
121143
*
@@ -374,27 +396,17 @@ public function checkWritePermissions(): void {
374396
public function setMaintenanceMode(bool $state): void {
375397
$this->silentLog('[info] setMaintenanceMode("' . ($state ? 'true' : 'false') . '")');
376398

377-
if ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) {
378-
$configFileName = rtrim($dir, '/') . '/config.php';
379-
} else {
380-
$configFileName = $this->baseDir . '/../config/config.php';
381-
}
399+
[$CONFIG, $configFileName] = $this->readConfigFile();
382400
$this->silentLog('[info] configFileName ' . $configFileName);
383401

384-
// usually is already tested in the constructor but just to be on the safe side
385-
if (!file_exists($configFileName)) {
386-
throw new \Exception('Could not find config.php.');
387-
}
388-
/** @var array $CONFIG */
389-
require $configFileName;
390402
$CONFIG['maintenance'] = $state;
391403
$content = "<?php\n";
392404
$content .= '$CONFIG = ';
393405
$content .= var_export($CONFIG, true);
394406
$content .= ";\n";
395-
$state = file_put_contents($configFileName, $content);
396-
if ($state === false) {
397-
throw new \Exception('Could not write to config.php');
407+
$writeSuccess = file_put_contents($configFileName, $content, LOCK_EX);
408+
if ($writeSuccess === false) {
409+
throw new \Exception('Could not write to config.php (' . $configFileName . ')');
398410
}
399411
$this->silentLog('[info] end of setMaintenanceMode()');
400412
}

updater.phar

305 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)