Make WordPress Core

Ticket #13699: 13699.step1-r2.patch

File 13699.step1-r2.patch, 3.4 KB (added by hakre, 15 years ago)
  • wp-includes/functions.php

    ### Eclipse Workspace Patch 1.0
    #P wordpress
     
    42164216 * @param string $context If specified adds filter hook "extra_<$context>_headers"
    42174217 */
    42184218function get_file_data( $file, $default_headers, $context = '' ) {
    4219         // We don't need to write to the file, so just open for reading.
    4220         $fp = fopen( $file, 'r' );
     4219        // retrieve all headers from file
     4220        $file_headers  = get_file_header( $file );
    42214221
    4222         // Pull only the first 8kiB of the file in.
    4223         $file_data = fread( $fp, 8192 );
    4224 
    4225         // PHP will close file handle, but we are good citizens.
    4226         fclose( $fp );
    4227 
     4222        // build header filter based on default headers and context
    42284223        if ( $context != '' ) {
    4229                 $extra_headers = apply_filters( "extra_{$context}".'_headers', array() );
     4224                $extra_headers = apply_filters( "extra_{$context}_headers", array() );
    42304225
    42314226                $extra_headers = array_flip( $extra_headers );
    42324227                foreach( $extra_headers as $key=>$value ) {
     
    42374232                $all_headers = $default_headers;
    42384233        }
    42394234
     4235        // create return array with empty values
     4236        $return = array_keys( $all_headers );
     4237        if ( count( $return ) )
     4238                $return = array_combine( $return, array_fill( 0, count( $return ), '' ) );
    42404239
    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] );
    42474244        }
    42484245
    4249         $file_data = compact( array_keys( $all_headers ) );
     4246        return $return;
     4247}
    42504248
    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 */
     4261function 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;
    42524298}
    42534299
    42544300/*