| | 1025 | // Test if a give filesystem path is absolute ('/foo/bar', 'c:\windows') |
|---|
| | 1026 | function 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; |
|---|
| | 1030 | |
|---|
| | 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') |
|---|
| | 1043 | function path_join( $base, $path ) { |
|---|
| | 1044 | if ( path_is_absolute($path) ) |
|---|
| | 1045 | return $path; |
|---|
| | 1046 | |
|---|
| | 1047 | return rtrim($base, '/') . '/' . ltrim($path, '/'); |
|---|
| | 1048 | } |
|---|
| 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 ) ); |
|---|