Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/21/2017 09:30:26 PM (7 years ago)
Author:
r-a-y
Message:

Attachments: Fix issues with uploading a file containing accented characters.

Previously, when uploading a new avatar or cover image with accented
characters, the upload would fail. This is due to an upstream WordPress
bug since WP does not handle sanitizing these types of file names properly.
(See #WP22363, #WP24661, #WP15955 for just a few of the related problems.)

To fix this in BuddyPress, we are putting in a workaround to convert file
names with UTF-8 characters to their ASCII equivalent. First, we try to
use PHP's intl extension if available. If not, we use WP's
remove_accents() function, and lastly if the filename still contains
UTF-8 characters, we also try to use iconv() if available.

This is considered a workaround until WordPress fixes this in core.

Fixes #7484.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-attachment.php

    r11543 r11602  
    262262        }
    263263
     264        // Helper for utf-8 filenames.
     265        add_filter( 'sanitize_file_name', array( $this, 'sanitize_utf8_filename' ) );
     266
    264267        // Upload the attachment.
    265268        $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides, $time );
     269
     270        remove_filter( 'sanitize_file_name', array( $this, 'sanitize_utf8_filename' ) );
    266271
    267272        // Restore WordPress Uploads data.
     
    272277        // Finally return the uploaded file or the error.
    273278        return $this->attachment;
     279    }
     280
     281    /**
     282     * Helper to convert utf-8 characters in filenames to their ASCII equivalent.
     283     *
     284     * @since 2.9.0
     285     *
     286     * @param  string $retval Filename.
     287     * @return string
     288     */
     289    public function sanitize_utf8_filename( $retval ) {
     290        // PHP 5.4+ or with PECL intl 2.0+
     291        if ( function_exists( 'transliterator_transliterate' ) && seems_utf8( $retval ) ) {
     292            $retval = transliterator_transliterate( 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove', $retval );
     293
     294        // Older.
     295        } else {
     296            // Use WP's built-in function to convert accents to their ASCII equivalent.
     297            $retval = remove_accents( $retval );
     298
     299            // Still here? use iconv().
     300            if ( function_exists( 'iconv' ) && seems_utf8( $retval ) ) {
     301                $retval = iconv( 'UTF-8', 'ASCII//TRANSLIT//IGNORE', $retval );
     302            }
     303        }
     304
     305        return $retval;
    274306    }
    275307
Note: See TracChangeset for help on using the changeset viewer.