### Eclipse Workspace Patch 1.0
#P wordpress-trunk
|
|
|
4158 | 4158 | * @param string $context If specified adds filter hook "extra_<$context>_headers" |
4159 | 4159 | */ |
4160 | 4160 | function get_file_data( $file, $default_headers, $context = '' ) { |
4161 | | // We don't need to write to the file, so just open for reading. |
4162 | | $fp = fopen( $file, 'r' ); |
| 4161 | // retrieve all headers from file |
| 4162 | $file_headers = get_file_header( $file ); |
4163 | 4163 | |
4164 | | // Pull only the first 8kiB of the file in. |
4165 | | $file_data = fread( $fp, 8192 ); |
| 4164 | // build header filter based on default headers and context |
| 4165 | if ( empty ( $context ) ) { |
| 4166 | $extra_headers = array (); |
| 4167 | } else { |
| 4168 | $extra_headers = apply_filters( "extra_{$context}_headers", array () ); |
| 4169 | if ( count( $extra_headers ) && false === ($extra_headers = array_combine( $extra_headers, $extra_headers )) ) |
| 4170 | $extra_headers = array (); // fallback in case of a plugin header name as key problem |
| 4171 | } |
| 4172 | $all_headers = array_merge( $extra_headers, $default_headers); |
4166 | 4173 | |
4167 | | // PHP will close file handle, but we are good citizens. |
4168 | | fclose( $fp ); |
| 4174 | // create return array with empty values |
| 4175 | $return = array_keys( $all_headers ); |
| 4176 | if ( count( $return ) ) |
| 4177 | $return = array_combine( $return, array_fill( 0, count( $return ), '') ); |
4169 | 4178 | |
4170 | | if ( $context != '' ) { |
4171 | | $extra_headers = apply_filters( "extra_$context".'_headers', array() ); |
| 4179 | // fill return array with header values |
| 4180 | foreach ( $all_headers as $key => $name ) { |
| 4181 | if ( isset( $file_headers[$name] ) ) |
| 4182 | $return[$key] = _cleanup_header_comment( $file_headers[$name] ); |
| 4183 | } |
4172 | 4184 | |
4173 | | $extra_headers = array_flip( $extra_headers ); |
4174 | | foreach( $extra_headers as $key=>$value ) { |
4175 | | $extra_headers[$key] = $key; |
4176 | | } |
4177 | | $all_headers = array_merge($extra_headers, $default_headers); |
4178 | | } else { |
4179 | | $all_headers = $default_headers; |
| 4185 | return $return; |
| 4186 | } |
| 4187 | |
| 4188 | /** |
| 4189 | * Retrieve headers from file |
| 4190 | * |
| 4191 | * Get all of the file's file headers regardless of their name. |
| 4192 | * |
| 4193 | * @date 2010-06-02 |
| 4194 | * @see http://codex.wordpress.org/File_Header |
| 4195 | * @see http://core.trac.wordpress.org/attachment/ticket/13699 |
| 4196 | * |
| 4197 | * @param string $file Path to the file |
| 4198 | * @return array all file header values keyed with their name |
| 4199 | */ |
| 4200 | function get_file_header( $file ) { |
| 4201 | $data = ''; |
| 4202 | $header = array (); |
| 4203 | |
| 4204 | // read the first 8 192 bytes (8 kiB) from file |
| 4205 | if ( $fp = fopen( $file, 'r' ) ) { |
| 4206 | $data = fread( $fp, 8192 ); |
| 4207 | fclose( $fp ); |
| 4208 | $data = str_replace( array ( "\r\n", "\r" ), "\n", $data ); // normalize line-endings |
4180 | 4209 | } |
4181 | 4210 | |
| 4211 | $pheader = '([a-z]{3,12}( [a-z]{3,12}){0,2}):[ ]?(.*)'; // single header |
4182 | 4212 | |
4183 | | foreach ( $all_headers as $field => $regex ) { |
4184 | | preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field}); |
4185 | | if ( !empty( ${$field} ) ) |
4186 | | ${$field} = _cleanup_header_comment( ${$field}[1] ); |
4187 | | else |
4188 | | ${$field} = ''; |
| 4213 | // extreact headers block from data |
| 4214 | $matches = array (); |
| 4215 | $result = preg_match( "/((.*({$pheader})\n)+)/i", $data, $matches ); |
| 4216 | $block = $result ? trim( $matches[0] ) : ''; |
| 4217 | |
| 4218 | // extract headers line by line |
| 4219 | $lines = explode( "\n", $block ); |
| 4220 | foreach ( $lines as $line ) { |
| 4221 | $matches = array (); |
| 4222 | $result = preg_match( "/(^|\s){$pheader}\$/i", $line, $matches ); |
| 4223 | if ( $result ) { |
| 4224 | list ( , , $name, , $value ) = $matches; |
| 4225 | $header[$name] = $value; |
| 4226 | } |
4189 | 4227 | } |
4190 | 4228 | |
4191 | | $file_data = compact( array_keys( $all_headers ) ); |
| 4229 | return $header; |
| 4230 | } |
4192 | 4231 | |
4193 | | return $file_data; |
4194 | | } |
4195 | 4232 | /* |
4196 | 4233 | * Used internally to tidy up the search terms |
4197 | 4234 | * |