[BUGFIX] Fix permissions and create group in mkdir_deep
authorLoek Hilgersom <loek@netcoop.nl>
Fri, 2 Dec 2011 00:48:45 +0000 (01:48 +0100)
committerTolleiv Nietsch <info@tolleiv.de>
Sun, 22 Jan 2012 11:39:42 +0000 (12:39 +0100)
t3lib_div::mkdir_deep does not properly create nested directories.
This is fixed by using API method t3lib_div::fixPermissions on the
first created directory with recursive flag set.

Change-Id: I951028195047a5777f2c305d8d5bdf780a102fb8
Fixes: #32261
Releases: 4.7, 4.6
Reviewed-on: http://review.typo3.org/7022
Reviewed-by: Loek Hilgersom
Tested-by: Loek Hilgersom
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
t3lib/class.t3lib_div.php

index 242f41f..0661099 100644 (file)
@@ -2948,18 +2948,41 @@ final class t3lib_div {
 
                $fullPath = $directory . $deepDirectory;
                if (!is_dir($fullPath) && strlen($fullPath) > 0) {
-                       @mkdir(
-                               $fullPath,
-                               octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']),
-                               TRUE
-                       );
-                       if (!is_dir($fullPath)) {
-                               throw new \RuntimeException(
-                                       'Could not create directory!',
-                                       1170251400
-                               );
+                       $firstCreatedPath = self::createDirectoryPath($fullPath);
+                       if ($firstCreatedPath !== '') {
+                               self::fixPermissions($firstCreatedPath, TRUE);
+                       }
+               }
+       }
+
+       /**
+        * Creates directories for the specified paths if they do not exist. This
+        * functions sets proper permission mask but does not set proper user and
+        * group.
+        *
+        * @static
+        * @param string $fullDirectoryPath
+        * @return string Path to the the first created directory in the hierarchy
+        * @see t3lib_div::mkdir_deep
+        * @throws \RuntimeException If directory could not be created
+        */
+       protected static function createDirectoryPath($fullDirectoryPath) {
+               $currentPath = $fullDirectoryPath;
+               $firstCreatedPath = '';
+               $permissionMask = octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']);
+               if (!@is_dir($currentPath)) {
+                       do {
+                               $firstCreatedPath = $currentPath;
+                               $separatorPosition = strrpos($currentPath, DIRECTORY_SEPARATOR);
+                               $currentPath = substr($currentPath, 0, $separatorPosition);
+                       } while (!is_dir($currentPath) && $separatorPosition !== FALSE);
+
+                       $result = @mkdir($fullDirectoryPath, $permissionMask, TRUE);
+                       if (!$result) {
+                               throw new \RuntimeException('Could not create directory!', 1170251400);
                        }
                }
+               return $firstCreatedPath;
        }
 
        /**