From 4051abe71559bed203633fbb43c2b12a3ba9f8c7 Mon Sep 17 00:00:00 2001 From: Ilmari Karonen Date: Fri, 21 Nov 2008 12:52:36 +0000 Subject: [PATCH] Turn wfMkdirParents() into just a thin wrapper around mkdir( $dir, $mode, true ); this won't work in PHP4, but we don't support that, do we? NOTE: the old code used to ignore the umask by explicitly forcing the permissions with chmod(). If this is desired behavior, it can be achieved by temporarily setting the umask to 0, as in: $oldmask = umask( 0 ); $rv = mkdir( $dir, $mode, true ); umask( $oldmask ); return $rv; However, I can't see why we'd want to do this. In the worst case, users with excessively tight umasks can find themselves with unusable directories, but the proper solution to that is to fix the umask rather than to ignore it. In the best case, we've just plugged a security hole the user didn't realize they had (because they assumed their umask would Just Work). --- includes/GlobalFunctions.php | 61 ++++-------------------------------- 1 file changed, 6 insertions(+), 55 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 0542eb97f2..44304409dc 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1928,69 +1928,20 @@ function wfTempDir() { /** * Make directory, and make all parent directories if they don't exist * - * @param string $fullDir Full path to directory to create + * @param string $dir Full path to directory to create * @param int $mode Chmod value to use, default is $wgDirectoryMode * @return bool */ -function wfMkdirParents( $fullDir, $mode = null ) { +function wfMkdirParents( $dir, $mode = null ) { global $wgDirectoryMode; - if( strval( $fullDir ) === '' ) - return true; - if( file_exists( $fullDir ) ) - return true; - // If not defined or isn't an int, set to default - if ( is_null( $mode ) ) { - $mode = $wgDirectoryMode; - } - - - # Go back through the paths to find the first directory that exists - $currentDir = $fullDir; - $createList = array(); - while ( strval( $currentDir ) !== '' && !file_exists( $currentDir ) ) { - # Strip trailing slashes - $currentDir = rtrim( $currentDir, '/\\' ); - - # Add to create list - $createList[] = $currentDir; - - # Find next delimiter searching from the end - $p = max( strrpos( $currentDir, '/' ), strrpos( $currentDir, '\\' ) ); - if ( $p === false ) { - $currentDir = false; - } else { - $currentDir = substr( $currentDir, 0, $p ); - } - } - if ( count( $createList ) == 0 ) { - # Directory specified already exists + if( strval( $dir ) === '' || file_exists( $dir ) ) return true; - } elseif ( $currentDir === false ) { - # Went all the way back to root and it apparently doesn't exist - wfDebugLog( 'mkdir', "Root doesn't exist?\n" ); - return false; - } - # Now go forward creating directories - $createList = array_reverse( $createList ); - # Is the parent directory writable? - if ( $currentDir === '' ) { - $currentDir = '/'; - } - if ( !is_writable( $currentDir ) ) { - wfDebugLog( 'mkdir', "Not writable: $currentDir\n" ); - return false; - } + if ( is_null( $mode ) ) + $mode = $wgDirectoryMode; - foreach ( $createList as $dir ) { - # use chmod to override the umask, as suggested by the PHP manual - if ( !mkdir( $dir, $mode ) || !chmod( $dir, $mode ) ) { - wfDebugLog( 'mkdir', "Unable to create directory $dir\n" ); - return false; - } - } - return true; + return mkdir( $dir, $mode, true ); // PHP5 <3 } /** -- 2.20.1