WordPress.org

Make WordPress Core

Ticket #12637: 12637.diff

File 12637.diff, 2.6 KB (added by chrisbliss18, 5 years ago)

Alters the behavior of unzip_file() to strip out MACOSX directories

  • wp-admin/includes/file.php

    diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php
    index b9557b1..b69fd3f 100644
    a b function download_url( $url ) { 
    487487 
    488488/** 
    489489 * Unzip's a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction. 
    490  * Assumes that WP_Filesystem() has already been called and set up. 
     490 * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present. 
    491491 * 
    492492 * Attempts to increase the PHP Memory limit to 256M before uncompressing, 
    493493 * However, The most memory required shouldn't be much larger than the Archive itself. 
    function _unzip_file_ziparchive($file, $to, $needed_dirs = array() ) { 
    560560                if ( ! $info = $z->statIndex($i) ) 
    561561                        return new WP_Error('stat_failed', __('Could not retrieve file from archive.')); 
    562562 
     563                if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Skip the OS X-created __MACOSX directory 
     564                        continue; 
     565 
    563566                if ( '/' == substr($info['name'], -1) ) // directory 
    564567                        $needed_dirs[] = $to . untrailingslashit($info['name']); 
    565568                else 
    function _unzip_file_ziparchive($file, $to, $needed_dirs = array() ) { 
    583586                if ( '/' == substr($info['name'], -1) ) // directory 
    584587                        continue; 
    585588 
     589                if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files 
     590                        continue; 
     591 
    586592                $contents = $z->getFromIndex($i); 
    587593                if ( false === $contents ) 
    588594                        return new WP_Error('extract_failed', __('Could not extract file from archive.'), $info['name']); 
    function _unzip_file_pclzip($file, $to, $needed_dirs = array()) { 
    622628                return new WP_Error('empty_archive', __('Empty archive.')); 
    623629 
    624630        // Determine any children directories needed (From within the archive) 
    625         foreach ( $archive_files as $file ) 
     631        foreach ( $archive_files as $file ) { 
     632                if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Skip the OS X-created __MACOSX directory 
     633                        continue; 
     634 
    626635                $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname($file['filename']) ); 
     636        } 
    627637 
    628638        $needed_dirs = array_unique($needed_dirs); 
    629639        asort($needed_dirs); 
    function _unzip_file_pclzip($file, $to, $needed_dirs = array()) { 
    640650                if ( $file['folder'] ) 
    641651                        continue; 
    642652 
     653                if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files 
     654                        continue; 
     655 
    643656                if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE) ) 
    644657                        return new WP_Error('copy_failed', __('Could not copy file.'), $to . $file['filename']); 
    645658        }