--- imagefield/imagefield.module	2007-03-07 14:57:59.000000000 +0800
+++ /home/jaydub/imagefield.module	2007-04-20 12:30:45.000000000 +0800
@@ -220,6 +220,33 @@ function imagefield_widget_settings($op,
         '#description' => 
         t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
       );
+
+      $form['max_filesize'] = array (
+        '#type' => 'textfield', 
+        '#title' => t('Maximum filesize for Images'), 
+        '#default_value' => $widget['max_filesize'] ? $widget['max_filesize'] : 0,
+        '#size' => 6, 
+        '#description' => 
+        t('The maximum allowed image file size expressed in kilobytes (e.g. 75k). Set to 0 for no restriction.')
+      );
+
+      $form['max_number_images'] = array (
+        '#type' => 'textfield', 
+        '#title' => t('Maximum number of images'), 
+        '#default_value' => $widget['max_number_images'] ? $widget['max_number_images'] : 0,
+        '#size' => 4, 
+        '#description' => 
+        t('The maximum number of images allowed. Set to 0 for no restriction.')
+      );
+
+      $form['rewrite_filename'] = array (
+        '#type' => 'checkbox', 
+        '#title' => t('Rewrite the filename with MD5'), 
+        '#default_value' => $widget['rewrite_filename'] ? $widget['rewrite_filename'] : 0,
+        '#description' => 
+        t('To avoid duplicate filenames, check this option to write the filename with MD5.')
+      );
+
       $form['image_path'] = array(
         '#type' => 'textfield', 
         '#title' => t('Image path'), 
@@ -248,7 +275,7 @@ function imagefield_widget_settings($op,
       break;
 
     case 'save':
-      return array('max_resolution', 'image_path', 'custom_alt', 'custom_title', 'teaser_preset', 'body_preset');
+      return array('max_resolution', 'max_filesize', 'max_number_images', 'rewrite_filename', 'image_path', 'custom_alt', 'custom_title', 'teaser_preset', 'body_preset');
   }
 }
 
@@ -398,6 +425,25 @@ function imagefield_widget_prepare_form_
   if ($file = file_check_upload($fieldname . '_upload')) {
     $file = (array)$file;
      
+    if ($field['widget']['max_filesize']) {
+      $valid_image = _imagefield_max_filesize_validate($field, $node_field, $file);
+      if (!$valid_image) {
+        return;
+      }
+    }
+
+    if ($field['widget']['max_number_images']) {
+      $valid_image = _imagefield_max_number_images_validate($field, $node_field);
+      if (!$valid_image) {
+        return;
+      }
+    }
+
+    if ($field['widget']['rewrite_filename']) {
+      $extension = substr($file['filename'],strrpos($file['filename'],'.'));
+      $file['filename'] = md5_file($file['filepath']).$extension;
+    }
+
     if (strpos($file['filemime'],'image') !== FALSE) { 
       $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);
       _imagefield_store_temp_file($file, $field, $node_field);
@@ -405,8 +451,41 @@ function imagefield_widget_prepare_form_
   }
       
   // add all new images saved in the session to the node_field
-  if (is_array($_SESSION['imagefield'][$fieldname])) {
-    foreach ($_SESSION['imagefield'][$fieldname] as $delta => $file) {
+  _imagefield_commit_session($node_field, $field);
+}
+
+function _imagefield_max_number_images_validate($field, &$node_field) {
+  $count = 0;
+  if (is_array($node_field)) {
+    $count += count($node_field);
+  }
+  if (is_array($_SESSION['imagefield'][$field['field_name']])) {
+    $count += count($_SESSION['imagefield'][$field['field_name']]);
+  }
+  if ($count >= $field['widget']['max_number_images']) {
+    form_set_error($field['field_name'], t('You are only allowed to upload up to @maximages images', array('@maximages' => $field['widget']['max_number_images'])));
+    _imagefield_commit_session($node_field, $field);
+    return false;
+  }
+  else {
+    return true;
+  }
+}
+
+function _imagefield_max_filesize_validate($field, &$node_field, $file) {
+  if ($file['filesize'] > ($field['widget']['max_filesize'] * 1024)) {
+    form_set_error($field['field_name'], t('The file you uploaded has a filesize greater than the maximum allowed filesize of @sizekb', array('@size' => $field['widget']['max_filesize'])));
+    _imagefield_commit_session($node_field, $field);
+    return false;
+  }
+  else {
+    return true;
+  }
+}
+
+function _imagefield_commit_session(&$node_field, $field) {
+  if (is_array($_SESSION['imagefield'][$field['field_name']])) {
+    foreach ($_SESSION['imagefield'][$field['field_name']] as $delta => $file) {
       $node_field[] = $file;
     }
   }
