From 26f2807464df28b2ea383fab4d875b8e49fd282b Mon Sep 17 00:00:00 2001
From: lucha <lucha@paranoici.org>
Date: Thu, 26 Aug 2021 10:51:10 +0200
Subject: [PATCH] added upload-mime-type.php plugin

Adds filters on the MIME type checks when uploading files.

Currently it allows to upload text files containing GPG/PGP keys.
It closes issue https://git.autistici.org/noblogs/noblogs-composer/-/issues/30

In the future we might need to add other filters.
---
 upload-mime-types.php | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 upload-mime-types.php

diff --git a/upload-mime-types.php b/upload-mime-types.php
new file mode 100644
index 0000000..e8c8c9b
--- /dev/null
+++ b/upload-mime-types.php
@@ -0,0 +1,31 @@
+<?php
+/*
+ * Plugin Name: A/I - Upload MIME types
+ * Description: Changes the list of allowed MIME types for uploads
+ * Version: 0.1.0
+ * Author: Autistici/Inventati
+ * Author URI: https://autistici.org
+ */
+
+/* Allow application/pgp-keys mime type
+ *
+ * If the file type associated to the filename extension and the one detected by fileinfo do not match
+ * wordpress will set $types['ext'] and $types['type'] to false.
+ * In order to accept the file we need to set these values to something not false.
+*/
+add_filter( 'wp_check_filetype_and_ext', function( $types, $file, $filename, $mimes, $real_mime ){
+    // Check if the file type was detected (by fileinfo) to be application/pgp-keys
+    if ( $real_mime == 'application/pgp-keys'){
+        // get the extention and file type associated to it (most probably 'text/plain')        
+        $wp_filetype = wp_check_filetype( $filename, $mimes );
+
+        // only accept the file if the extension is one of the allowed ones for text/plain and application/pgp-keys
+        if (in_array($wp_filetype['type'], array('text/plain', 'application/pgp-keys'), true)){
+            $types['ext'] = $wp_filetype['ext'];
+            $types['type'] = $wp_filetype['type'];
+        }
+    }
+    return $types;
+}, 1, 5);
+
+?>
-- 
GitLab