bpiwowar / papercite

Bibtex plugin for wordpress
http://www.bpiwowar.net/software/papercite/
GNU General Public License v2.0
42 stars 47 forks source link

MIME type for .bib should be 'text/plain'? #144

Open brian-mit opened 5 years ago

brian-mit commented 5 years ago

I'm using the Media Library to store my bibtex files and after upgrading Wordpress core from 4.9.9 to 5.1.1 and the papercite plugin from 0.5.15 to 0.5.18, I'm having the following issues:

  1. I can no longer upload .bib files and get the error message 'This file type is not permitted for security reasons.' I was able to get around that using the upload_mimes filter, but I had to use 'text/plain' as the mime type for .bib files as 'application/x-bibtex' didn't work. This probably isn't your problem, I'm just including it as an FYI.
  2. After uploading my bibtex file to the Media Library, it doesn't seem to be found and processed correctly. I think it has to do with its mime type. As a test, I changed line 418 in papercite.php from $bibFile = $this->getDataFile("$biburi", "bib", "bib", "application/x-bibtex", $options); to $bibFile = $this->getDataFile("$biburi", "bib", "bib", "text/plain", $options); and things seem to work.

I'm not sure, but it looks like line 2558 in wp-includes/functions.php might be where the mime type of a file is found:

$finfo     = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );

I check my bibtex file using the following command:

$ php -r '$finfo = finfo_open(FILEINFO_MIME_TYPE); echo finfo_file($finfo, "myBibtexFile.bib");'

and got the response 'text/plain'.

If I create the directory wp-content/papercite-data/bib and put my bibtex files in there, things seem to work, so I can do that as a workaround. Being able to use the Media Library is more convenient though.

digfish commented 5 years ago

Thanks for letting us know about this issue. It appears the "problem" is around from Wordpress 5.0.1 onwards, and was propagated to 4.9.9 also. according to the Wordpress tracker. I found a github gist wirth a workaround here and adapted it for the bibtex case and added at the papercite-wp-plugin.php (I'm using version 0.5.20, which was pushed by me, @digfish). The code is below

/**
 * by digfish (09 Apr 2019)
 * Restore .bib upload functionality in Media Library for WordPress 4.9.9 and up
 * adapted from https://gist.github.com/rmpel/e1e2452ca06ab621fe061e0fde7ae150
 */
add_filter('wp_check_filetype_and_ext', function($values, $file, $filename, $mimes) {
    if ( extension_loaded( 'fileinfo' ) ) {
        // with the php-extension, a bib file is issues type text/plain so we fix that back to
        // application/x-bibtex by trusting the file extension.
        $finfo     = finfo_open( FILEINFO_MIME_TYPE );
        $real_mime = finfo_file( $finfo, $file );
        finfo_close( $finfo );
        if ( $real_mime === 'text/plain' && preg_match( '/\.(bib)$/i', $filename ) ) {
            $values['ext']  = 'bib';
            $values['type'] = 'application/x-bibtex';
        }
    } else {
        // without the php- extension, we probably don't have the issue at all, but just to be sure...
        if ( preg_match( '/\.(bib)$/i', $filename ) ) {
            $values['ext']  = 'bib';
            $values['type'] = 'application/x-bibtex';
        }
    }
    return $values;
}, PHP_INT_MAX, 4);

You can add it to your theme's functions.php until a new commit with the fix is pushed.

Other threads that I stumbled upon regarding this issue in case anyone comes to this topic by Google results:

digfish commented 5 years ago

Pushed the last commit with the fix (v. 0.5.22)

brian-mit commented 5 years ago

Thanks, that seems to work for me. One note in case anyone else finds this: I needed to re-upload my bibtex files. After adding the above code to functions.php, the bibtex files that were currently in my media library still did not seem to be found. After re-uploading them though, they were.