Clean all directories that match the tmp naming pattern at the end of WCFSetup
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 20 May 2022 09:30:12 +0000 (11:30 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 20 May 2022 09:30:12 +0000 (11:30 +0200)
Since e41dfd007b12baed65ab7679fb679e53bcd2adf5 the temporary directory for
WCFSetup resides in the webroot instead of some system internal temporary
directory. As such temporary files might remain in the webroot and likely will
never be cleaned if the install.php is accessed multiple times before the setup
is completed.

Fix this by deleting all directories that match the pattern for the name of the
temporary directory once the setup is completed successfully.

wcfsetup/install.php
wcfsetup/install/files/lib/system/WCFSetup.class.php

index cefb88a0476903dd143aa830dfc0f51058d5542a..9cdd3802aead26bab9a71b0cdeebdb0a8053d54e 100644 (file)
@@ -619,7 +619,7 @@ class BasicFileUtil {
         * Returns the temp folder for the installation.
         */
        public static function getInstallTempFolder(): string {
-               $dir = __DIR__ . '/WCFSetup-' . TMP_FILE_PREFIX . '/';
+               $dir = INSTALL_SCRIPT_DIR . '/WCFSetup-' . TMP_FILE_PREFIX . '/';
                @mkdir($dir);
                self::makeWritable($dir);
                
index 40819562afcf1ba85dc2fb9e0b9d85971aa0f4f6..28915e0f929be3b79c2c6902e6d8e0bf929988e1 100644 (file)
@@ -1317,21 +1317,33 @@ final class WCFSetup extends WCF
         SessionHandler::getInstance()->update();
 
         // Delete tmp files
-        $iterator = new \RecursiveIteratorIterator(
-            new \RecursiveDirectoryIterator(
-                TMP_DIR . '/',
-                \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS
-            ),
-            \RecursiveIteratorIterator::CHILD_FIRST
-        );
-        foreach ($iterator as $path) {
-            if ($path->isDir()) {
-                \rmdir($path);
-            } else {
-                \unlink($path);
+        foreach (new \DirectoryIterator(\INSTALL_SCRIPT_DIR) as $fileInfo) {
+            if (!$fileInfo->isDir()) {
+                continue;
+            }
+
+            if (!\preg_match('/^WCFSetup-[0-9a-f]{16}$/', $fileInfo->getBasename())) {
+                continue;
+            }
+
+            $tmpDirectory = $fileInfo->getPathname();
+
+            $tmpDirectoryIterator = new \RecursiveIteratorIterator(
+                new \RecursiveDirectoryIterator(
+                    $tmpDirectory,
+                    \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS
+                ),
+                \RecursiveIteratorIterator::CHILD_FIRST
+            );
+            foreach ($tmpDirectoryIterator as $tmpFile) {
+                if ($tmpFile->isDir()) {
+                    \rmdir($tmpFile);
+                } else {
+                    \unlink($tmpFile);
+                }
             }
+            \rmdir($tmpDirectory);
         }
-        \rmdir(TMP_DIR . '/');
 
         return $output;
     }