| | 719 | * Remove dot segments from a path |
| | 720 | * |
| | 721 | * @access private |
| | 722 | * @param string $input |
| | 723 | * @return string |
| | 724 | */ |
| | 725 | function remove_dot_segments($input) |
| | 726 | { |
| | 727 | $output = ''; |
| | 728 | while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..') |
| | 729 | { |
| | 730 | // A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise, |
| | 731 | if (strpos($input, '../') === 0) |
| | 732 | { |
| | 733 | $input = substr($input, 3); |
| | 734 | } |
| | 735 | elseif (strpos($input, './') === 0) |
| | 736 | { |
| | 737 | $input = substr($input, 2); |
| | 738 | } |
| | 739 | // 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, |
| | 740 | elseif (strpos($input, '/./') === 0) |
| | 741 | { |
| | 742 | $input = substr_replace($input, '/', 0, 3); |
| | 743 | } |
| | 744 | elseif ($input === '/.') |
| | 745 | { |
| | 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 (strpos($input, '/../') === 0) |
| | 750 | { |
| | 751 | $input = substr_replace($input, '/', 0, 4); |
| | 752 | $output = substr_replace($output, '', strrpos($output, '/')); |
| | 753 | } |
| | 754 | elseif ($input === '/..') |
| | 755 | { |
| | 756 | $input = '/'; |
| | 757 | $output = substr_replace($output, '', strrpos($output, '/')); |
| | 758 | } |
| | 759 | // D: if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise, |
| | 760 | elseif ($input === '.' || $input === '..') |
| | 761 | { |
| | 762 | $input = ''; |
| | 763 | } |
| | 764 | // 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 |
| | 765 | elseif (($pos = strpos($input, '/', 1)) !== false) |
| | 766 | { |
| | 767 | $output .= substr($input, 0, $pos); |
| | 768 | $input = substr_replace($input, '', 0, $pos); |
| | 769 | } |
| | 770 | else |
| | 771 | { |
| | 772 | $output .= $input; |
| | 773 | $input = ''; |
| | 774 | } |
| | 775 | } |
| | 776 | return $output . $input; |
| | 777 | } |
| | 778 | |
| | 779 | /** |