|
21 | 21 | use OCP\AppFramework\Utility\ITimeFactory; |
22 | 22 | use OCP\BackgroundJob\IJobList; |
23 | 23 | use OCP\Defaults; |
| 24 | +use OCP\Http\Client\IClientService; |
24 | 25 | use OCP\IAppConfig; |
25 | 26 | use OCP\IConfig; |
26 | 27 | use OCP\IGroup; |
27 | 28 | use OCP\IGroupManager; |
28 | 29 | use OCP\IL10N; |
29 | 30 | use OCP\IRequest; |
| 31 | +use OCP\IURLGenerator; |
30 | 32 | use OCP\IUserManager; |
31 | 33 | use OCP\IUserSession; |
32 | 34 | use OCP\L10N\IFactory as IL10NFactory; |
@@ -170,8 +172,7 @@ public function getSystemInfo(bool $allowAllDatabases = false): array { |
170 | 172 | self::protectDataDirectory(); |
171 | 173 |
|
172 | 174 | try { |
173 | | - $util = new \OC_Util(); |
174 | | - $htAccessWorking = $util->isHtaccessWorking(Server::get(IConfig::class)); |
| 175 | + $htAccessWorking = $this->isHtaccessWorking($dataDir); |
175 | 176 | } catch (\OCP\HintException $e) { |
176 | 177 | $errors[] = [ |
177 | 178 | 'error' => $e->getMessage(), |
@@ -212,6 +213,87 @@ public function getSystemInfo(bool $allowAllDatabases = false): array { |
212 | 213 | ]; |
213 | 214 | } |
214 | 215 |
|
| 216 | + public function createHtaccessTestFile(string $dataDir): string|false { |
| 217 | + // php dev server does not support htaccess |
| 218 | + if (php_sapi_name() === 'cli-server') { |
| 219 | + return false; |
| 220 | + } |
| 221 | + |
| 222 | + // testdata |
| 223 | + $fileName = '/htaccesstest.txt'; |
| 224 | + $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.'; |
| 225 | + |
| 226 | + // creating a test file |
| 227 | + $testFile = $dataDir . '/' . $fileName; |
| 228 | + |
| 229 | + if (file_exists($testFile)) {// already running this test, possible recursive call |
| 230 | + return false; |
| 231 | + } |
| 232 | + |
| 233 | + $fp = @fopen($testFile, 'w'); |
| 234 | + if (!$fp) { |
| 235 | + throw new \OCP\HintException('Can\'t create test file to check for working .htaccess file.', |
| 236 | + 'Make sure it is possible for the web server to write to ' . $testFile); |
| 237 | + } |
| 238 | + fwrite($fp, $testContent); |
| 239 | + fclose($fp); |
| 240 | + |
| 241 | + return $testContent; |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Check if the .htaccess file is working |
| 246 | + * |
| 247 | + * @param \OCP\IConfig $config |
| 248 | + * @return bool |
| 249 | + * @throws Exception |
| 250 | + * @throws \OCP\HintException If the test file can't get written. |
| 251 | + */ |
| 252 | + public function isHtaccessWorking(string $dataDir) { |
| 253 | + $config = Server::get(IConfig::class); |
| 254 | + |
| 255 | + if (\OC::$CLI || !$config->getSystemValueBool('check_for_working_htaccess', true)) { |
| 256 | + return true; |
| 257 | + } |
| 258 | + |
| 259 | + $testContent = $this->createHtaccessTestFile($dataDir); |
| 260 | + if ($testContent === false) { |
| 261 | + return false; |
| 262 | + } |
| 263 | + |
| 264 | + $fileName = '/htaccesstest.txt'; |
| 265 | + $testFile = $dataDir . '/' . $fileName; |
| 266 | + |
| 267 | + // accessing the file via http |
| 268 | + $url = Server::get(IURLGenerator::class)->getAbsoluteURL(\OC::$WEBROOT . '/data' . $fileName); |
| 269 | + try { |
| 270 | + $content = Server::get(IClientService::class)->newClient()->get($url)->getBody(); |
| 271 | + } catch (\Exception $e) { |
| 272 | + $content = false; |
| 273 | + } |
| 274 | + |
| 275 | + if (str_starts_with($url, 'https:')) { |
| 276 | + $url = 'http:' . substr($url, 6); |
| 277 | + } else { |
| 278 | + $url = 'https:' . substr($url, 5); |
| 279 | + } |
| 280 | + |
| 281 | + try { |
| 282 | + $fallbackContent = Server::get(IClientService::class)->newClient()->get($url)->getBody(); |
| 283 | + } catch (\Exception $e) { |
| 284 | + $fallbackContent = false; |
| 285 | + } |
| 286 | + |
| 287 | + // cleanup |
| 288 | + @unlink($testFile); |
| 289 | + |
| 290 | + /* |
| 291 | + * If the content is not equal to test content our .htaccess |
| 292 | + * is working as required |
| 293 | + */ |
| 294 | + return $content !== $testContent && $fallbackContent !== $testContent; |
| 295 | + } |
| 296 | + |
215 | 297 | /** |
216 | 298 | * @return array<string|array> errors |
217 | 299 | */ |
|
0 commit comments