4241 | | foreach ( $all_headers as $field => $regex ) { |
4242 | | preg_match( '/^[\s\/*#]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field}); |
4243 | | if ( !empty( ${$field} ) ) |
4244 | | ${$field} = _cleanup_header_comment( ${$field}[1] ); |
4245 | | else |
4246 | | ${$field} = ''; |
| 4240 | // fill return array with header values |
| 4241 | foreach ( $all_headers as $key => $name ) { |
| 4242 | if ( isset( $file_headers[$name] ) ) |
| 4243 | $return[$key] = _cleanup_header_comment( $file_headers[$name] ); |
4251 | | return $file_data; |
| 4249 | /** |
| 4250 | * Retrieve headers from file |
| 4251 | * |
| 4252 | * Get all of the file's file headers regardless of their name. |
| 4253 | * |
| 4254 | * @date 2010-06-02 |
| 4255 | * @see http://codex.wordpress.org/File_Header |
| 4256 | * @see http://core.trac.wordpress.org/attachment/ticket/13699 |
| 4257 | * |
| 4258 | * @param string $file Path to the file |
| 4259 | * @return array all file header values keyed with their name |
| 4260 | */ |
| 4261 | function get_file_header( $file ) { |
| 4262 | $data = ''; |
| 4263 | $header = array (); |
| 4264 | |
| 4265 | // read the first 8 192 bytes (8 kiB) from file, normalized line-endings |
| 4266 | if ( $fp = fopen( $file, 'r' ) ) { |
| 4267 | $data = fread( $fp, 8192 ); |
| 4268 | fclose( $fp ); |
| 4269 | $data = str_replace( array ( "\r\n", "\r" ), "\n", $data ); // normalize line-endings |
| 4270 | } |
| 4271 | |
| 4272 | $pstart = '^[ \t\/*#]*'; |
| 4273 | |
| 4274 | // single header, scrict as documented |
| 4275 | // in http://codex.wordpress.org/File_Header#File_Header_Specification |
| 4276 | $pheader = '([a-z]{3,12}(?: [a-z]{3,12}){0,2}):[ ]?(.+)'; |
| 4277 | |
| 4278 | // instead, single header, more broad: |
| 4279 | $pheader = '([a-z]+(?: [a-z]+)+):[ \t]*(.+)'; |
| 4280 | |
| 4281 | // extract headers block from data |
| 4282 | $matches = array (); |
| 4283 | $result = preg_match( "/(({$pstart}({$pheader})\$)+)/mi", $data, $matches ); |
| 4284 | $block = $result ? trim( $matches[0] ) : ''; |
| 4285 | |
| 4286 | // extract headers line by line |
| 4287 | $lines = explode( "\n", $block ); |
| 4288 | foreach ( $lines as $line ) { |
| 4289 | $matches = array (); |
| 4290 | $result = preg_match( "/{$pstart}{$pheader}\$/i", $line, $matches ); |
| 4291 | if ( $result ) { |
| 4292 | list ( , $name, $value ) = $matches; |
| 4293 | $header[$name] = $value; |
| 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | return $header; |