| 59 | * Split input lines into lines preceding, contained by and following BEGIN and |
| 60 | * END markers (in the .htaccess file). |
| 61 | * |
| 62 | * @param array $lines Input file lines without newline characters. |
| 63 | * @param string $marker |
| 64 | * @return array Preceding, existing and following lines as arrays. |
| 65 | */ |
| 66 | function split_by_markers( $lines, $marker ) { |
| 67 | $start_marker = "# BEGIN {$marker}"; |
| 68 | $end_marker = "# END {$marker}"; |
| 69 | |
| 70 | $pre_lines = $post_lines = $existing_lines = array(); |
| 71 | $found_start_marker = $found_end_marker = false; |
| 72 | |
| 73 | foreach ( $lines as $line ) { |
| 74 | if ( ! $found_start_marker && ( $line === $start_marker ) ) { |
| 75 | $found_start_marker = true; |
| 76 | continue; |
| 77 | } |
| 78 | if ( ! $found_end_marker && ( $line === $end_marker ) ) { |
| 79 | $found_end_marker = true; |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | if ( ! $found_start_marker ) { |
| 84 | $pre_lines[] = $line; |
| 85 | } elseif ( ! $found_end_marker ) { |
| 86 | $existing_lines[] = $line; |
| 87 | } else { |
| 88 | $post_lines[] = $line; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return array( $pre_lines, $existing_lines, $post_lines ); |
| 93 | } |
| 94 | |
| 95 | /** |
76 | | $state = false; |
77 | | foreach ( $markerdata as $markerline ) { |
78 | | if ( false !== strpos( $markerline, '# END ' . $marker ) ) { |
79 | | $state = false; |
80 | | if ( $state ) { |
81 | | $result[] = $markerline; |
82 | | } |
83 | | if ( false !== strpos( $markerline, '# BEGIN ' . $marker ) ) { |
84 | | $state = true; |
85 | | } |
86 | | } |
| 113 | if ( ! empty( $markerdata ) ) { |
| 114 | // We are only interested in lines contained by marker. |
| 115 | list( , $result, ) = split_by_markers( $markerdata, $marker ); |
139 | | $pre_lines = $post_lines = $existing_lines = array(); |
140 | | $found_marker = $found_end_marker = false; |
141 | | foreach ( $lines as $line ) { |
142 | | if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) { |
143 | | $found_marker = true; |
144 | | continue; |
145 | | } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) { |
146 | | $found_end_marker = true; |
147 | | continue; |
148 | | } |
149 | | if ( ! $found_marker ) { |
150 | | $pre_lines[] = $line; |
151 | | } elseif ( $found_marker && $found_end_marker ) { |
152 | | $post_lines[] = $line; |
153 | | } else { |
154 | | $existing_lines[] = $line; |
155 | | } |
156 | | } |
| 165 | list($pre_lines, $existing_lines, $post_lines) = split_by_markers($lines, $marker); |