| | 725 | /** |
| | 726 | * Remove dot segments from a path |
| | 727 | * |
| | 728 | * @since 3.3.0 |
| | 729 | * |
| | 730 | * @param string $input The string to remove dot segments from |
| | 731 | * @return string |
| | 732 | */ |
| | 733 | function remove_dot_segments( $input ) { |
| | 734 | $output = ''; |
| | 735 | while ( false !== strpos( $input, './' ) || false !== strpos( $input, '/.' ) || '.' === $input || '..' === $input ) { |
| | 736 | // A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise, |
| | 737 | if ( 0 === strpos( $input, '../' ) ) { |
| | 738 | $input = substr( $input, 3 ); |
| | 739 | } elseif ( 0 === strpos( $input, './' ) ) { |
| | 740 | $input = substr( $input, 2 ); |
| | 741 | } |
| | 742 | // B: if the input buffer begins with a prefix of "/./" or "/.", where "." is a complete path segment, then replace that prefix with "/" in the input buffer; otherwise, |
| | 743 | elseif ( 0 === strpos( $input, '/./' ) ) { |
| | 744 | $input = substr_replace( $input, '/', 0, 3 ); |
| | 745 | } elseif ( '/.' === $input ) { |
| | 746 | $input = '/'; |
| | 747 | } |
| | 748 | // C: if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise, |
| | 749 | elseif ( 0 === strpos( $input, '/../' ) ) { |
| | 750 | $input = substr_replace( $input, '/', 0, 4 ); |
| | 751 | $output = substr_replace( $output, '', strrpos( $output, '/' ) ); |
| | 752 | } elseif ( '/..' === $input ) { |
| | 753 | $input = '/'; |
| | 754 | $output = substr_replace( $output, '', strrpos( $output, '/' ) ); |
| | 755 | } |
| | 756 | // D: if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise, |
| | 757 | elseif ( '.' === $input || '..' === $input ) { |
| | 758 | $input = ''; |
| | 759 | } |
| | 760 | // E: move the first path segment in the input buffer to the end of the output buffer, including the initial "/" character (if any) and any subsequent characters up to, but not including, the next "/" character or the end of the input buffer |
| | 761 | elseif ( false !== ( $pos = strpos( $input, '/', 1 ) ) ) { |
| | 762 | $output .= substr( $input, 0, $pos ); |
| | 763 | $input = substr_replace( $input, '', 0, $pos ); |
| | 764 | } else { |
| | 765 | $output .= $input; |
| | 766 | $input = ''; |
| | 767 | } |
| | 768 | } |
| | 769 | return $output . $input; |
| | 770 | } |
| | 771 | |