Permalink
Browse files

MIME-type fixes

  • Loading branch information...
1 parent fdbe363 commit 5e81099de65491aed1d7f15e5a948db6ce1e7572 @rinatkhaziev rinatkhaziev committed Apr 11, 2013
Showing with 22 additions and 101 deletions.
  1. +22 −88 frontend-uploader.php
  2. +0 −13 lib/php/functions.php
View
@@ -97,95 +97,31 @@ function __construct() {
*/
function action_init() {
load_plugin_textdomain( 'frontend-uploader', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
- $this->mime_types();
- add_filter( 'upload_mimes', array( $this, 'return_mime_types' ), 999 );
+ $this->allowed_mime_types = $this->_get_mime_types();
+ add_filter( 'upload_mimes', array( $this, '_get_mime_types' ), 999 );
}
- /**
- * Workaround for allowed mime-types
- *
- * @return allowed mime-types
- */
- function mime_types() {
- // Configuration filter:
- // fu_allowed_mime_types should return array of allowed mime types (see readme)
- $this->allowed_mime_types = apply_filters( 'fu_allowed_mime_types', $this->fix_ie_mime_types( wp_get_mime_types() ) );
- $this->return_mime_types();
- }
-
- /**
- * Check if the file could be uploaded
- *
- * @since 0.5
- *
- * @param string $ext File Extension
- * @param string $type MIME-type
- *
- * @return boolean is the file allowed or not
- */
- function _is_allowed( $ext, $type ) {
- $allowed_type_keys = array();
-
- $settings = $this->settings['enabled_files'];
-
- foreach( $this->allowed_mime_types as $exts => $mime ) {
- $extensions = explode('|', $exts );
- if ( isset( $extensions[0] ) ) {
- array_push( $allowed_type_keys, $extensions[0] );
- }
- }
-
- $conditions = array(
- (bool) ( in_array( $ext, $allowed_type_keys ) ),
- (bool) in_array( $type, $this->allowed_mime_types ) && in_array( $ext, $settings ) ,
- );
-
- // var_dump( $allowed_type_keys );
- // var_dump( $this->allowed_mime_types ); var_dump( $conditions); exit;
-
-
- foreach( $conditions as $condition )
- if ( false === $condition )
- return false;
- //var_dump( $conditions ); exit;
- return true;
- }
-
- function return_mime_types() {
- $registered_mimes = fu_get_mime_types();
-
-
- foreach( $registered_mimes as $ext => $details ) {
- if ( !in_array( $ext, (array) $this->settings['enabled_files'] ) )
+ function _get_mime_types() {
+ // Grab default mime-types
+ $mime_types = wp_get_mime_types();
+ $fu_mime_types = fu_get_mime_types();
+ // Workaround for IE
+ $mime_types['jpg|jpe|jpeg|pjpg'] = 'image/pjpeg';
+ $mime_types['png|xpng'] = 'image/x-png';
+ // Iterate through default extensions
+ foreach( $fu_mime_types as $extension => $details ) {
+ // Skip if it's not in the settings
+ if ( !in_array( $extension, $this->settings['enabled_files'] ) )
continue;
- foreach( $details['mimes'] as $file_mime ) {
- $this->allowed_mime_types["{$ext}|{$ext}_{$file_mime}"] = $file_mime;
+ // Iterate through mime-types for this extension
+ foreach( $details['mimes'] as $ext_mime ) {
+ $mime_types[ $extension . '|' . $extension . sanitize_title_with_dashes( $ext_mime ) ] = $ext_mime;
}
-
}
- // var_dump( $this->settings['enabled_files']);
- //var_dump( $registered_mimes );
- // var_dump( $this->allowed_mime_types ); exit;
- // Disallow PHP files just in case
- $no_pasaran = array( 'application/x-php', 'text/x-php', 'text/php', 'application/php', 'application/x-httpd-php', 'application/x-httpd-php-source' );
- // THEY SHALL NOT PASS
- foreach ( $no_pasaran as $np )
- if ( false !== ( $key = array_search( $np, $this->allowed_mime_types ) ) )
- unset( $this->allowed_mime_types[$key] );
-
- return $this->allowed_mime_types;
- }
- /**
- * Add IE-specific MIME types
- * /props mcnasby
- *
- * @param array $mime_types [description]
- * @return [type] [description]
- */
- function fix_ie_mime_types( $mime_types ) {
- $mime_types['jpg|jpe|jpeg|pjpg'] = 'image/pjpeg';
- $mime_types['png|xpng'] = 'image/x-png';
+
+ // Configuration filter: fu_allowed_mime_types should return array of allowed mime types (see readme)
+ $mime_types = apply_filters( 'fu_allowed_mime_types', $mime_types );
return $mime_types;
}
@@ -237,8 +173,6 @@ function _is_public() {
return ( current_user_can( 'read' ) && 'on' == $this->settings['auto_approve_user_files'] ) || ( 'on' == $this->settings['auto_approve_any_files'] );
}
-
-
/**
* Handle uploading of the files
*
@@ -271,9 +205,9 @@ function _handle_files( $post_id ) {
}
preg_match( '/.(?P<ext>[a-zA-Z0-9]+)$/', $k['name'], $ext_match );
- // Add an error message
- if ( !isset( $ext_match['ext'] ) || ! $this->_is_allowed( $ext_match['ext'], $k['type'] ) ) {
- $errors['fu-disallowed-mime-type'][] = array( 'name' => $k['name'], 'mime' => $k['type'] );
+ // Add an error message if MIME-type is not allowed
+ if ( ! in_array( $k['type'], (array) $this->allowed_mime_types ) ) {
+ $errors['fu-disallowed-mime-type'][] = array( 'name' => $k['name'], 'mime' => $k['type'] );
continue;
}
View
@@ -76,19 +76,6 @@ function fu_get_mime_types() {
'image/vnd.adobe.photoshop',
),
),
- 'txt'=>
- array(
- 'label'=> 'Plain Text File',
- 'mimes'=>
- array(
- 'text/plain',
- 'application/txt',
- 'browser/internal',
- 'text/anytext',
- 'widetext/plain',
- 'widetext/paragraph',
- ),
- ),
'csv'=>
array(
'label'=> 'Comma Separated Values File',

0 comments on commit 5e81099

Please sign in to comment.