Index: imagefield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.module,v
retrieving revision 1.30.2.15
diff -u -r1.30.2.15 imagefield.module
--- imagefield.module	19 Apr 2007 23:53:11 -0000	1.30.2.15
+++ imagefield.module	20 Apr 2007 00:11:23 -0000
@@ -216,6 +216,12 @@
         '#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))), 
         '#after_build' => array('imagefield_form_check_directory'),
       );
+      $form['allowed_types'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Allowed types'),
+        '#default_value' => $widget['allowed_types'] ? $widget['allowed_types'] : 'gif,png,jpeg',
+        '#description' => t('Only allow images of certain mime types to be uploaded. Enter a comma seperated list of acceptable mime types (such as <em>gif,png,jpeg</em>), or leave blank to allow all images.'),
+      );
       $form['custom_alt'] = array(
         '#type' => 'checkbox',
         '#title' => t('Enable custom alternate text'),
@@ -234,7 +240,7 @@
       break;
 
     case 'save':
-      return array('max_resolution', 'image_path', 'custom_alt', 'custom_title', 'teaser_preset', 'body_preset');
+      return array('max_resolution', 'image_path', 'allowed_types', 'custom_alt', 'custom_title', 'teaser_preset', 'body_preset');
   }
 }
 
@@ -351,7 +357,12 @@
   // Attach new files 
   if ($file = file_check_upload($fieldname . '_upload')) {
     $file = (array)$file;
-    if (strpos($file['filemime'],'image') !== FALSE) { 
+
+    // Validation must happen immediately after the image is uploaded so we
+    // can discard the file if it turns out not to be a valid mime type
+    $valid_image = _imagefield_widget_upload_validate($node, $field, $node_field, $file);
+
+    if ($valid_image) { 
       $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);
       
       // Create the filepath for the image preview
@@ -379,6 +390,19 @@
       $file_id = count($node_field) + count($_SESSION['imagefield'][$fieldname]);
       $_SESSION['imagefield'][$fieldname][$file_id] = $file;
     }
+    else {
+      // Delete the invalid file
+      file_delete($file['filepath']);
+
+      // If a single field and a valid file is in the session, mark existing image for deletion
+      if (!$field['multiple']) {
+        if (count($_SESSION['imagefield'][$fieldname]) && count($node_field)) {
+          foreach ($node_field as $delta => $session_file) {
+            $node_field[$delta]['flags']['delete'] = TRUE;
+          }
+        }
+      }
+    }
   }
   
   // Load files from preview state. before committing actions.
@@ -509,11 +533,43 @@
   $fieldname = $field['field_name'];
   if ($field['required']) {
     if (!count($node_field)) {
-      form_set_error($fieldname, $field['widget']['label'] .' is required.');
+      form_set_error($fieldname .'_upload', t('%field is required.', array('%field' => $field['widget']['label'])));
     }
   }
 }
 
+function _imagefield_widget_upload_validate($node, $field, $node_field, $file) {
+  $fieldname = $field['field_name'];
+
+  $extensions = explode(',', str_replace(' ', '', $field['widget']['allowed_types']));
+  $allowed_types = array();
+  foreach ($extensions as $extension) {
+    if ($extension == 'jpg' || $extension == 'jpeg') {
+      $allowed_types[] = 'image/jpeg';
+      $allowed_types[] = 'image/pjpeg'; // IE uploads jpegs as pjpeg
+    }
+    else {
+      $allowed_types[] = 'image/'.$extension;
+    }
+  }
+
+  if (!empty($field['widget']['allowed_types']) && !in_array($file['filemime'], $allowed_types)) {
+    $types_string = implode(', ', $extensions);
+    $last_comma = strrpos($types_string, ',');
+    if ($last_comma) {
+      $types_string = substr($types_string, 0, $last_comma) .' or '. substr($types_string, $last_comma + 2);
+    }
+    form_set_error($fieldname .'_upload', t('Only @types format images may be uploaded.', array('@types' => $types_string)));
+    return false;
+  }
+  elseif (!strstr($file['filemime'], 'image/')) {
+    form_set_error($fieldname .'_upload', t('Only images may be uploaded.'));
+    return false;
+  }
+
+  return true;
+}
+
 /**
  * Implementation of hook_field_formatter_info().
  */
