Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #42560, comment 18


Ignore:
Timestamp:
02/23/2021 07:34:58 AM (4 years ago)
Author:
dd32
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #42560, comment 18

    initial v1  
    88        defined( 'SHORTINIT' ) && SHORTINIT &&
    99        ( ! defined( 'WPMU_ACCEL_REDIRECT' ) || ! WPMU_ACCEL_REDIRECT ) &&
    10         ( ! defined( 'WPMU_SENDFILE' ) || ! WPMU_SENDFILE ) &&
    1110        'ms-files.php' === basename( $_SERVER['SCRIPT_FILENAME'] )
    1211) {
     
    1615        // If this is a range request, once Multisite is loaded, override ms-files.php.
    1716        isset( $_SERVER['HTTP_RANGE'] ) && add_action( 'ms_loaded', function() {
    18                 $upload = wp_upload_dir();
    19                 $file   = path_join( $upload['basedir'], $_GET['file'] );
    20                 $size   = file_exists( $file ) ? filesize( $file ) : 0;
     17                $file = rtrim( BLOGUPLOADDIR, '/' ) . '/' . str_replace( '..', '', $_GET['file'] );
     18                $size = is_file( $file ) ? filesize( $file ) : 0;
    2119
    2220                // Bail if it doesn't exist, or is empty.
     
    4745                } else {
    4846                        header( 'Content-Type: image/' . substr( $file, strrpos( $file, '.' ) + 1 ) );
     47                }
     48
     49                $last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
     50                $etag          = '"' . md5( $last_modified ) . '"';
     51                header( "Last-Modified: $last_modified GMT" );
     52                header( 'ETag: ' . $etag );
     53                header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
     54
     55                // Support for conditional GET - use stripslashes() to avoid formatting.php dependency.
     56                $client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
     57
     58                if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
     59                        $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false;
     60                }
     61
     62                $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
     63                // If string is empty, return 0. If not, attempt to parse into a timestamp.
     64                $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
     65
     66                // Make a timestamp for our most recent modification...
     67                $modified_timestamp = strtotime( $last_modified );
     68
     69                if ( ( $client_last_modified && $client_etag )
     70                        ? ( ( $client_modified_timestamp >= $modified_timestamp ) && ( $client_etag == $etag ) )
     71                        : ( ( $client_modified_timestamp >= $modified_timestamp ) || ( $client_etag == $etag ) )
     72                        ) {
     73                        status_header( 304 );
     74                        exit;
    4975                }
    5076