WordPress.org

Make WordPress Core

Ticket #5953: absolute-upload-dir-r6957.patch

File absolute-upload-dir-r6957.patch, 2.5 KB (added by tellyworth, 5 years ago)
  • wordpress/wp-includes/functions.php

     
    10221022        return false; 
    10231023} 
    10241024 
     1025// Test if a give filesystem path is absolute ('/foo/bar', 'c:\windows') 
     1026function path_is_absolute( $path ) { 
     1027        // this is definitive if true but fails if $path does not exist or contains a symbolic link 
     1028        if ( realpath($path) == $path ) 
     1029                return true; 
    10251030 
     1031        if ( strlen($path) == 0 || $path{0} == '.' ) 
     1032                return false; 
     1033         
     1034        // windows allows absolute paths like this 
     1035        if ( preg_match('#^[a-zA-Z]:\\\\#', $path) ) 
     1036                return true; 
     1037 
     1038        // a path starting with / or \ is absolute; anything else is relative 
     1039        return (bool) preg_match('#^[/\\\\]#', $path); 
     1040} 
     1041 
     1042// Join two filesystem paths together (e.g. 'give me $path relative to $base') 
     1043function path_join( $base, $path ) { 
     1044        if ( path_is_absolute($path) ) 
     1045                return $path; 
     1046         
     1047        return rtrim($base, '/') . '/' . ltrim($path, '/'); 
     1048} 
     1049 
    10261050// Returns an array containing the current upload directory's path and url, or an error message. 
    10271051function wp_upload_dir( $time = NULL ) { 
    10281052        $siteurl = get_option( 'siteurl' ); 
    10291053        $upload_path = $dir = get_option( 'upload_path' ); 
    10301054         
    1031         if ( $upload_path != realpath( $upload_path ) ) { // not an absolute path 
    1032                 //prepend ABSPATH to $dir and $siteurl to $url if they're not already there 
    1033                 $path = str_replace( ABSPATH, '', trim( $upload_path ) ); 
    1034                 $dir = ABSPATH . $path; 
    1035         } 
     1055        // $dir is absolute, $path is (maybe) relative to ABSPATH 
     1056        $dir = path_join( ABSPATH, $upload_path ); 
     1057        $path = str_replace( ABSPATH, '', trim( $upload_path ) ); 
    10361058 
    10371059        if ( !$url = get_option( 'upload_url_path' ) ) 
    10381060                $url = trailingslashit( $siteurl ) . $path; 
     
    10451067                $url = trailingslashit( $siteurl ) . UPLOADS; 
    10461068        } 
    10471069 
     1070        $subdir = ''; 
    10481071        if ( get_option( 'uploads_use_yearmonth_folders' ) ) { 
    10491072                // Generate the yearly and monthly dirs 
    10501073                if ( !$time ) 
    10511074                        $time = current_time( 'mysql' ); 
    10521075                $y = substr( $time, 0, 4 ); 
    10531076                $m = substr( $time, 5, 2 ); 
    1054                 $dir = $dir . "/$y/$m"; 
    1055                 $url = $url . "/$y/$m"; 
     1077                $subdir = "/$y/$m"; 
    10561078        } 
     1079         
     1080        $dir .= $subdir; 
     1081        $url .= $subdir; 
    10571082 
    10581083        // Make sure we have an uploads dir 
    10591084        if ( ! wp_mkdir_p( $dir ) ) { 
     
    10611086                return array( 'error' => $message ); 
    10621087        } 
    10631088 
    1064                 $uploads = array( 'path' => $dir, 'url' => $url, 'error' => false ); 
     1089        $uploads = array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'error' => false ); 
    10651090        return apply_filters( 'upload_dir', $uploads ); 
    10661091} 
    10671092