103 | | if (!file_exists( $filename ) || is_writeable( $filename ) ) { |
104 | | if (!file_exists( $filename ) ) { |
| 103 | // if we can write to the speified file |
| 104 | if ( ! file_exists( $filename ) || is_writeable( $filename ) ) { |
| 105 | // go ahead and add the markers to the insertion |
| 106 | $insertion = array_merge( array( '# BEGIN ' . $marker ), $insertion, array( '# END ' . $marker ) ); |
| 107 | |
| 108 | // load any existing file into an array for parsing below |
| 109 | if ( ! file_exists( $filename ) ) { |
116 | | foreach ( $markerdata as $n => $markerline ) { |
117 | | if (strpos($markerline, '# BEGIN ' . $marker) !== false) |
| 130 | $added_already = false; |
| 131 | |
| 132 | // start searching the lines for a possible matching insertion |
| 133 | while ( ( $line = array_shift( $markerdata ) ) !== null ) { |
| 134 | // if we have a marker that matches our new marker, then we found what was previously written, |
| 135 | // so stop adding to the output array, and start tracking lines for later comparison |
| 136 | if ( strpos( $line, '# BEGIN ' . $marker ) !== false ) |
125 | | if (strpos($markerline, '# END ' . $marker) !== false) { |
126 | | fwrite( $f, "# BEGIN {$marker}\n" ); |
127 | | if ( is_array( $insertion )) |
128 | | foreach ( $insertion as $insertline ) |
129 | | fwrite( $f, "{$insertline}\n" ); |
130 | | fwrite( $f, "# END {$marker}\n" ); |
| 144 | |
| 145 | // if we are at the ending marker line, then we need to compare |
| 146 | if ( strpos( $line, '# END ' . $marker ) !== false ) { |
| 147 | // clean up the possible list for comparison to the insertion |
| 148 | $possible = array_values( array_filter( array_map( 'trim', $possible ) ) ); |
| 149 | |
| 150 | // if we found out block, then just quit with a success, becuase nothing needs to be done |
| 151 | // otherwise, ignore this block, and keep searching in case there are multiple blocks |
| 152 | // techinically this otherwise should not matter, but good to be safe |
| 153 | if ( ! $added_already && $possible === $sane_insertion ) { |
| 154 | return true; |
| 155 | } else { |
| 156 | $possible = array(); |
| 157 | |
| 158 | // prevent writing two of these blocks |
| 159 | if ( ! $added_already ) { |
| 160 | $output = $output . implode( "\n", $insertion ) . "\n"; |
| 161 | $added_already = true; |
| 162 | } |
| 163 | } |
| 164 | |
136 | | if (!$foundit) { |
137 | | fwrite( $f, "\n# BEGIN {$marker}\n" ); |
138 | | foreach ( $insertion as $insertline ) |
139 | | fwrite( $f, "{$insertline}\n" ); |
140 | | fwrite( $f, "# END {$marker}\n" ); |
141 | | } |
| 171 | |
| 172 | $output = trim( $output ); |
| 173 | $out_len = strlen( $output ); |
| 174 | |
| 175 | // if we made it this far, then it is time for writing, so open the output file for writing |
| 176 | if ( !$f = @fopen( $filename, 'w' ) ) |
| 177 | return false; |
| 178 | |
| 179 | // write the file |
| 180 | if ( $out_len ) |
| 181 | fwrite( $f, $output, $out_len ); |
| 182 | |
| 183 | // close the file |