From: joshuaruesweg Date: Wed, 30 Mar 2022 12:55:03 +0000 (+0200) Subject: Make FileDeletePIP safe for ci file systems X-Git-Tag: 5.5.0_Alpha_1~33^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=49c488354f3fd5b02a851a48d912d6aad3601161;p=GitHub%2FWoltLab%2FWCF.git Make FileDeletePIP safe for ci file systems --- diff --git a/wcfsetup/install/files/lib/system/package/plugin/AbstractFileDeletePackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/AbstractFileDeletePackageInstallationPlugin.class.php index 678e18ee1f..20cf5bdde6 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/AbstractFileDeletePackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/AbstractFileDeletePackageInstallationPlugin.class.php @@ -109,9 +109,8 @@ abstract class AbstractFileDeletePackageInstallationPlugin extends AbstractXMLPa } $filePath = $this->getFilePath($file, $application); - if (\file_exists($filePath)) { - \unlink($filePath); - } + + $this->safeDeleteFile($filePath); } } @@ -130,6 +129,49 @@ abstract class AbstractFileDeletePackageInstallationPlugin extends AbstractXMLPa WCF::getDB()->commitTransaction(); } + private static function isFilesystemCaseSensitive(): bool + { + static $isFilesystemCaseSensitive = null; + + if ($isFilesystemCaseSensitive === null) { + $testFilePath = __FILE__; + + $invertedCase = \strstr( + $testFilePath, + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + ); + + $isFilesystemCaseSensitive = !\file_exists($invertedCase); + } + + return $isFilesystemCaseSensitive; + } + + private function safeDeleteFile(string $filePath): void + { + if (!\file_exists($filePath)) { + return; + } + + if (self::isFilesystemCaseSensitive()) { + \unlink($filePath); + + return; + } + + // If the filesystem is case insensitive, we must check, whether the casing of the file + // matches the casing of the file, which we want to delete. Therefore, we must iterate + // through the whole dir to find the potential file. + $pathInfo = \pathinfo($filePath); + foreach (\glob($pathInfo['dirname'] . '/*') as $file) { + if (\basename($file) === $pathInfo['basename']) { + \unlink($filePath); + break; + } + } + } + /** * @inheritDoc */