ms-files.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Multisite upload handler.
  4. *
  5. * @since 3.0.0
  6. *
  7. * @package WordPress
  8. * @subpackage Multisite
  9. */
  10. define( 'MS_FILES_REQUEST', true );
  11. define( 'SHORTINIT', true );
  12. require_once dirname( __DIR__ ) . '/wp-load.php';
  13. if ( ! is_multisite() ) {
  14. die( 'Multisite support not enabled' );
  15. }
  16. ms_file_constants();
  17. if ( '1' == $current_blog->archived || '1' == $current_blog->spam || '1' == $current_blog->deleted ) {
  18. status_header( 404 );
  19. die( '404 &#8212; File not found.' );
  20. }
  21. $file = rtrim( BLOGUPLOADDIR, '/' ) . '/' . str_replace( '..', '', $_GET['file'] );
  22. if ( ! is_file( $file ) ) {
  23. status_header( 404 );
  24. die( '404 &#8212; File not found.' );
  25. }
  26. $mime = wp_check_filetype( $file );
  27. if ( false === $mime['type'] && function_exists( 'mime_content_type' ) ) {
  28. $mime['type'] = mime_content_type( $file );
  29. }
  30. if ( $mime['type'] ) {
  31. $mimetype = $mime['type'];
  32. } else {
  33. $mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 );
  34. }
  35. header( 'Content-Type: ' . $mimetype ); // Always send this.
  36. if ( false === strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) ) {
  37. header( 'Content-Length: ' . filesize( $file ) );
  38. }
  39. // Optional support for X-Sendfile and X-Accel-Redirect.
  40. if ( WPMU_ACCEL_REDIRECT ) {
  41. header( 'X-Accel-Redirect: ' . str_replace( WP_CONTENT_DIR, '', $file ) );
  42. exit;
  43. } elseif ( WPMU_SENDFILE ) {
  44. header( 'X-Sendfile: ' . $file );
  45. exit;
  46. }
  47. $last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
  48. $etag = '"' . md5( $last_modified ) . '"';
  49. header( "Last-Modified: $last_modified GMT" );
  50. header( 'ETag: ' . $etag );
  51. header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
  52. // Support for conditional GET - use stripslashes() to avoid formatting.php dependency.
  53. $client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
  54. if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
  55. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false;
  56. }
  57. $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
  58. // If string is empty, return 0. If not, attempt to parse into a timestamp.
  59. $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
  60. // Make a timestamp for our most recent modification...
  61. $modified_timestamp = strtotime( $last_modified );
  62. if ( ( $client_last_modified && $client_etag )
  63. ? ( ( $client_modified_timestamp >= $modified_timestamp ) && ( $client_etag == $etag ) )
  64. : ( ( $client_modified_timestamp >= $modified_timestamp ) || ( $client_etag == $etag ) )
  65. ) {
  66. status_header( 304 );
  67. exit;
  68. }
  69. // If we made it this far, just serve the file.
  70. readfile( $file );
  71. flush();