diff --git a/file_entity.pages.inc b/file_entity.pages.inc index 6b56b553b2d90ac9a7d9394e10e47c4e38d9321a..1de1aa512da9a03cea606a2d86beb866f08ef03e 100644 --- a/file_entity.pages.inc +++ b/file_entity.pages.inc @@ -831,39 +831,34 @@ function file_entity_edit($form, &$form_state, $file) { // Set up replacement file validation. $replacement_options = !empty($form_state['#upload_options']) ? $form_state['#upload_options'] : array(); - // The replacement file must have an extension valid for the original type. - $file_extensions = array(); - $file_type_name = isset($file->type) ? $file->type : file_get_type($file); - if (!empty($replacement_options['file_extensions'])) { - $file_extensions = explode(' ', $replacement_options['file_extensions']); - } - elseif ($file_type_name && ($file_type = file_type_load($file_type_name))) { - $file_extensions = file_type_get_valid_extensions($file_type); - } - - // Set allowed file extensions. - if (!empty($file_extensions)) { - // Set to type based file extensions. - $replacement_options['file_extensions'] = implode(' ', $file_extensions); + // Replacement file must have the same extension as the original file. + $replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION); + + // Replacement file must also be one of the allowed filetypes. + $allowed_filetypes = explode(' ', variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm')); + + if (!empty($replacement_options['file_extensions']) && in_array($replacement_options['file_extensions'], $allowed_filetypes)) { + $form['replace_upload'] = array( + '#type' => 'file', + '#title' => t('Replace file'), + '#description' => t('This file will replace the existing file. This action cannot be undone.'), + '#upload_validators' => file_entity_get_upload_validators($replacement_options), + '#pre_render' => array('file_entity_upload_validators_pre_render'), + ); + $form['replace_keep_original_filename'] = array( + '#type' => 'checkbox', + '#title' => t('Keep original filename'), + '#default_value' => variable_get('file_entity_file_replace_options_keep_original_filename', FALSE), + '#description' => t('Rename the newly uploaded file to the name of the original file. This action cannot be undone.'), + ); } else { - // Fallback to the extension of the current file. - $replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION); + $form['replace_upload'] = array( + '#type' => 'item', + '#title' => t('Replace file'), + '#description' => t('This file cannot be replaced.'), + ); } - - $form['replace_upload'] = array( - '#type' => 'file', - '#title' => t('Replace file'), - '#description' => t('This file will replace the existing file. This action cannot be undone.'), - '#upload_validators' => file_entity_get_upload_validators($replacement_options), - '#pre_render' => array('file_entity_upload_validators_pre_render'), - ); - $form['replace_keep_original_filename'] = array( - '#type' => 'checkbox', - '#title' => t('Keep original filename'), - '#default_value' => variable_get('file_entity_file_replace_options_keep_original_filename', FALSE), - '#description' => t('Rename the newly uploaded file to the name of the original file. This action cannot be undone.'), - ); } $form['preview'] = file_view_file($file, 'preview'); diff --git a/file_entity.test b/file_entity.test index cc5cb24cac379dde990bb63da68a1df225254c1c..bb606d4bb24d1ebea0a7532de217ae08f595595b 100644 --- a/file_entity.test +++ b/file_entity.test @@ -56,7 +56,7 @@ class FileEntityTestHelper extends DrupalWebTestCase { protected function createFileEntity($settings = array()) { // Populate defaults array. $settings += array( - 'filepath' => 'Файл для тестирования ' . $this->randomName(), // Prefix with non-latin characters to ensure that all file-related tests work with international filenames. + 'filepath' => 'Файл для тестирования ' . $this->randomName() . '.txt', // Prefix with non-latin characters to ensure that all file-related tests work with international filenames. 'filemime' => 'text/plain', 'uid' => 1, 'timestamp' => REQUEST_TIME, @@ -1087,6 +1087,16 @@ class FileEntityReplaceTestCase extends FileEntityTestHelper { $this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save')); $this->assertRaw(t('The specified file %file could not be uploaded. Only files with the following extensions are allowed:', array('%file' => $image->filename)), 'File validation works, upload failed correctly.'); + // Restrict allowed document filetypes to exclude plain text files. + variable_set('file_entity_default_allowed_extensions', 'doc docx pdf'); + + // Test that the file can't be replaced if there's a problem with the original file's extension. + $this->drupalGet('file/' . $file->fid . '/edit'); + $this->assertRaw(t('This file cannot be replaced.'), 'File extension filter works, upload prevented correctly.'); + + // Clean up variable. + variable_del('file_entity_default_allowed_extensions'); + // Create a non-local file record. $file2 = new stdClass(); $file2->uri = 'oembed://' . $this->randomName();