Make WordPress Core

Ticket #14928: 14928.3.patch

File 14928.3.patch, 3.1 KB (added by hakre, 14 years ago)

Refactoring Iteration, 2nd

  • wp-includes/functions.php

     
    20152015}
    20162016
    20172017/**
     2018 * Make Directories.
     2019 *
    20182020 * Recursive directory creation based on full path.
     2021 * Attempts to set permissions on newly created folders.
    20192022 *
    2020  * Will attempt to set permissions on folders.
     2023 * As php mkdir() (PHP < 5.0.0) does not support recursive paths, this function mimics
     2024 * the *nix `mkdir -p` / `mkdir --parent` command.
    20212025 *
     2026 * In difference to mkdir, the permissions set on newly created folders
     2027 * is based on the parent folder permissions - not umask.
     2028 *
     2029 * @link http://www.gnu.org/software/coreutils/manual/coreutils.html#mkdir-invocation
     2030 * @link http://php.net/mkdir
    20222031 * @since 2.0.1
    20232032 *
    2024  * @param string $target Full path to attempt to create.
    2025  * @return bool Whether the path was created. True if path already exists.
     2033 * @param string $target Path of a directory to create.
     2034 * @return bool Whether the target path exists (already or just created does not make a difference).
    20262035 */
    20272036function wp_mkdir_p( $target ) {
    2028         // from php.net/mkdir user contributed notes
    2029         $target = str_replace( '//', '/', $target );
     2037        // Normalize $target
     2038        $target = str_replace( '//', '/', $target ); // undocumented, from a removed php.net/mkdir user-note
     2039        $target = rtrim( $target, '/' ); // safe mode fails with a trailing slash under certain PHP versions.
     2040        empty( $target ) && $target = '/'; // empty target is root (posix).
    20302041
    2031         // safe mode fails with a trailing slash under certain PHP versions.
    2032         $target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
    2033         if ( empty($target) )
    2034                 $target = '/';
    2035 
     2042        // Handle existing target.
    20362043        if ( file_exists( $target ) )
    20372044                return @is_dir( $target );
    20382045
    2039         // Attempting to create the directory may clutter up our display.
    2040         if ( @mkdir( $target ) ) {
    2041                 $stat = @stat( dirname( $target ) );
    2042                 $dir_perms = $stat['mode'] & 0007777;  // Get the permission bits.
    2043                 @chmod( $target, $dir_perms );
    2044                 return true;
    2045         } elseif ( is_dir( dirname( $target ) ) ) {
    2046                         return false;
    2047         }
     2046        // Handle target parent directory (recursion).
     2047        $parent     = dirname( $target );
     2048        $has_parent = strlen( $target ) !== strlen( $parent );
     2049        if ( $has_parent && !wp_mkdir_p( $parent ) )
     2050                return false;
    20482051
    2049         // If the above failed, attempt to create the parent node, then try again.
    2050         if ( ( $target != '/' ) && ( wp_mkdir_p( dirname( $target ) ) ) )
    2051                 return wp_mkdir_p( $target );
     2052        // Target directory creation
     2053        if ( !@mkdir( $target ) )
     2054                return false;
    20522055
    2053         return false;
     2056        // Set Permissions on created target, defaults to 0755/FS_CHMOD_DIR and if
     2057        // parent status exists, to the parent's status inode protection mode permissions.
     2058        $target_perms = defined( 'FS_CHMOD_DIR' ) ? FS_CHMOD_DIR : 0755; // see /wp-admin/includes/file.php
     2059        if ( $has_parent && $stat_parent = @stat( $parent ) )
     2060                $target_perms = $stat_parent['mode'] & 0007777; // Get permission bits.
     2061
     2062        @chmod( $target, $target_perms );
     2063
     2064        return true;
    20542065}
    20552066
    20562067/**