| | 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 | /** |
| 74 | | if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) )); |
| 75 | | { |
| 76 | | $state = false; |
| 77 | | foreach ( $markerdata as $markerline ) { |
| 78 | | if (strpos($markerline, '# END ' . $marker) !== false) |
| 79 | | $state = false; |
| 80 | | if ( $state ) |
| 81 | | $result[] = $markerline; |
| 82 | | if (strpos($markerline, '# BEGIN ' . $marker) !== false) |
| 83 | | $state = true; |
| 84 | | } |
| | 111 | $markerdata = @file( $filename, FILE_IGNORE_NEW_LINES ); |
| | 112 | |
| | 113 | if ( ! empty ( $markerdata ) ) { |
| | 114 | // We are only interested in lines contained by marker. |
| | 115 | list( , $result, ) = split_by_markers($markerdata, $marker); |
| 137 | | $pre_lines = $post_lines = $existing_lines = array(); |
| 138 | | $found_marker = $found_end_marker = false; |
| 139 | | foreach ( $lines as $line ) { |
| 140 | | if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) { |
| 141 | | $found_marker = true; |
| 142 | | continue; |
| 143 | | } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) { |
| 144 | | $found_end_marker = true; |
| 145 | | continue; |
| 146 | | } |
| 147 | | if ( ! $found_marker ) { |
| 148 | | $pre_lines[] = $line; |
| 149 | | } elseif ( $found_marker && $found_end_marker ) { |
| 150 | | $post_lines[] = $line; |
| 151 | | } else { |
| 152 | | $existing_lines[] = $line; |
| 153 | | } |
| 154 | | } |
| | 165 | list($pre_lines, $existing_lines, $post_lines) = split_by_markers($lines, $marker); |