| 78 | | $foundit = false; |
| 79 | | if ( $markerdata ) { |
| 80 | | $state = true; |
| 81 | | foreach ( $markerdata as $n => $markerline ) { |
| 82 | | if (strpos($markerline, '# BEGIN ' . $marker) !== false) |
| 83 | | $state = false; |
| 84 | | if ( $state ) { |
| 85 | | if ( $n + 1 < count( $markerdata ) ) |
| 86 | | fwrite( $f, "{$markerline}\n" ); |
| 87 | | else |
| 88 | | fwrite( $f, "{$markerline}" ); |
| 89 | | } |
| 90 | | if (strpos($markerline, '# END ' . $marker) !== false) { |
| 91 | | fwrite( $f, "# BEGIN {$marker}\n" ); |
| 92 | | if ( is_array( $insertion )) |
| 93 | | foreach ( $insertion as $insertline ) |
| 94 | | fwrite( $f, "{$insertline}\n" ); |
| 95 | | fwrite( $f, "# END {$marker}\n" ); |
| 96 | | $state = true; |
| 97 | | $foundit = true; |
| 98 | | } |
| 99 | | } |
| | 80 | if ( !is_writable($filename) ) |
| | 81 | return false; |
| | 82 | |
| | 83 | $marker_begin = "# BEGIN $marker"; |
| | 84 | $marker_end = "# END $marker"; |
| | 85 | $buffer = array(); |
| | 86 | $foundit = array(); |
| | 87 | $state = true; |
| | 88 | foreach ($lines as $line) { |
| | 89 | if ( false !== strpos( $line, $marker_begin ) ) |
| | 90 | $state = false; |
| | 91 | if ( $state ) |
| | 92 | $buffer[] = $line; |
| | 93 | if ( false !== strpos( $line, $marker_end ) ) { |
| | 94 | $foundit[] = count( $buffer ); |
| | 95 | $buffer[] = ''; # placeholder |
| | 103 | // backwards compat: non array insertions are ignored if markers were found |
| | 104 | // @todo this should be streamlined |
| | 105 | if (!is_array($insertion)) |
| | 106 | $insertion = array(); |
| | 107 | } |
| | 108 | |
| | 109 | array_unshift($insertion, $marker_begin ); |
| | 110 | $insertion[] = $marker_end; |
| | 111 | $insertion = implode( "\n", $insertion ); |
| | 112 | |
| | 113 | foreach( $foundit as $index ) |
| | 114 | $buffer[$index] = $insertion; |
| | 115 | $buffer = implode( "\n", $buffer); |
| | 116 | |
| | 117 | if ( false === @file_put_contents( $filename, $buffer) ) |