Index: field_file.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/flexinode/field_file.inc,v
retrieving revision 1.14
diff -u -r1.14 field_file.inc
--- field_file.inc	27 Jan 2007 15:02:23 -0000	1.14
+++ field_file.inc	28 Jan 2007 14:41:20 -0000
@@ -11,7 +11,7 @@
   $form[$fieldname] = array(
     '#type' => 'file',
     '#title' => t($field->label),
-    '#description' => ($node->$fieldname ? t('"%filename" has been uploaded. If you upload another file, the current file will be replaced.', array('%filename' => $node->$fieldname->filename)) : '') . t($field->description),
+    '#description' => ($node->$fieldname ? t('"%filename" has been uploaded. If you upload another file, the current file will be replaced.', array('%filename' => $node->$fieldname->filename)) : '') . t($field->description). t('The file is limited to %kbKB and a file type of %ext.', array('%ext' => $field->options[1], '%kb' => $field->options[2])),
     '#required' => $field->required,
     '#weight' => $field->weight,
     );
@@ -51,12 +51,24 @@
 
 function flexinode_field_file_validate($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  return file_check_upload($fieldname);
+  if($file = file_check_upload($fieldname)) {
+      $pathinfo = pathinfo($file->filename);
+      if ($field->options[1]!=NULL && (!in_array($pathinfo['extension'], explode(',', $field->options[1]) ))) {
+        form_set_error($fieldname, t('The file has the wrong extension and/or type.'));
+      }
+      else if ($field->options[2]!=NULL && @filesize($file->filepath) > ($field->options[2] * 1000)) {
+        form_set_error($fieldname, t('The uploaded image is too large; the maximum file size is %num kB.', array('%num' => $field->options[4])));
+      }
+  }
+  elseif(!empty($node->$fieldname)) {
+    form_set_error($fieldname, t('The file upload was not successful.'));
+  }
 }
 
 function flexinode_field_file_execute($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  if ($file = file_save_upload($fieldname, variable_get('file_directory_path', NULL))) {
+  $savepath = flexinode_save_path((isset($field->options[3])) ? $field->options[3]:NULL);
+  if ($file = file_save_upload($fieldname, $savepath )) {
     return $file;
   }
   elseif (empty($node->$fieldname)) {
@@ -64,6 +76,38 @@
   }
 }
 
+function flexinode_field_file_config($field, $edit) {
+  $form = array();
+
+  $form['options'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Options'),
+    '#description' => t('Limitations for files to upload.'),
+  );
+  $form['options'][1] = array(
+    '#type' => 'textfield',
+    '#title' => t('Allowed extensions'),
+    '#default_value' => $field->options[1],
+    '#description' => t('Comma separated list of allowed file extensions.'),
+    '#required' => TRUE,
+  );
+  $form['options'][2] = array(
+    '#type' => 'textfield',
+    '#title' => t('Maximum file size'),
+    '#default_value' => $field->options[2],
+    '#description' => t('Maximum file file size, in kB. Leave blank for unlimited'),
+  );
+  $form['options'][3] = array(
+    '#type' => 'textfield',
+    '#title' => 'Save path format',
+    '#default_value' => $field->options[3],
+    '#description' => t('Enter save path format, this will be appended to file_directory_path, mark it up with %username, %day, %dayname, %month, %monthname, %year. Blank if not required. Example: nodefiles/%year/%month/%username. Directories will be created if not present, and original filenames are preserved.'),
+  );
+
+  return $form;
+}
+
+
 function flexinode_field_file_format($field, $node, $brief = 0) {
   $fieldname = 'flexinode_'. $field->field_id;
   $file = is_object($node->$fieldname) ? $node->$fieldname : unserialize($node->$fieldname);
Index: field_image.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/flexinode/field_image.inc,v
retrieving revision 1.23
diff -u -r1.23 field_image.inc
--- field_image.inc	27 Jan 2007 13:43:17 -0000	1.23
+++ field_image.inc	28 Jan 2007 14:13:28 -0000
@@ -17,7 +17,7 @@
   $form[$fieldname] = array(
     '#type' => 'file',
     '#title' => t($field->label),
-    '#description' =>  ($node->$fieldname ? t('"%filename" has been uploaded. If you upload another file, the current file will be replaced.', array('%filename' => $node->$fieldname->filename)) : '') .' '. t($field->description) .' '. t('The file is limited to %kbKB and a resolution of %wxh pixels (width x height).', array('%wxh' => $field->options[1], '%kb' => $field->options[4])),
+    '#description' => ($node->$fieldname ? t('"%filename" has been uploaded. If you upload another file, the current file will be replaced.', array('%filename' => $node->$fieldname->filename)) : '') .' '. t($field->description) .' '. t('The file is limited to %kbKB and a resolution of %wxh pixels (width x height).', array('%wxh' => $field->options[1], '%kb' => $field->options[4])),
     '#required' => $field->required,
     '#weight' => $field->weight,
     );
@@ -36,7 +36,8 @@
 
 function flexinode_field_image_insert($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  $node->$fieldname = file_save_upload($node->$fieldname, $node->$fieldname->filename);
+  $savepath = flexinode_save_path((isset($field->options) ? $field->options[5]:NULL));
+  $node->$fieldname = file_save_upload($node->$fieldname, $savepath);
   if (is_object($node->$fieldname)) {
     $node->$fieldname->smallpath = flexinode_field_image_make_smaller($node->$fieldname->filepath, '_sm', $field->options[2]);
     $node->$fieldname->thumbpath = flexinode_field_image_make_smaller($node->$fieldname->filepath, '_th', $field->options[3]);
@@ -98,7 +99,8 @@
 
 function flexinode_field_image_execute($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  if ($file = file_save_upload($fieldname, variable_get('file_directory_path', NULL))) {
+  $savepath = flexinode_save_path((isset($field->options) ? $field->options[5]:NULL));
+  if ($file = file_save_upload($fieldname, $savepath )) {
     return $file;
   }
   elseif(empty($node->$fieldname)) {
@@ -140,7 +142,7 @@
   $form['options'] = array(
     '#type' => 'fieldset',
     '#title' => t('Options'),
-    '#description' => t('Options for the image upload.'),
+    '#description' => t('Limitations and options for images to upload.'),
     '#tree' => TRUE,
   );
   $form['options'][1] = array(
@@ -171,6 +173,12 @@
     '#description' => t('Maximum picture file size, in kB.'),
     '#required' => TRUE,
     );
+  $form['options'][5] = array(
+    '#type' => 'textfield',
+    '#title' => 'Save path format',
+    '#default_value' => $field->options[5],
+    '#description' => t('Enter save path format, this will be appended to file_directory_path, mark it up with %username, %day, %dayname, %month, %monthname, %year. Blank if not required. Example: nodefiles/%year/%month/%username. Directories will be created if not present, and original filenames are preserved.'),
+  );
 
   return $form;
 }
Index: flexinode.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/flexinode/flexinode.module,v
retrieving revision 1.82
diff -u -r1.82 flexinode.module
--- flexinode.module	23 Jan 2007 20:54:01 -0000	1.82
+++ flexinode.module	28 Jan 2007 14:21:13 -0000
@@ -479,3 +479,53 @@
   $field->options = unserialize($field->options);
   return $field;
 }
+
+function flexinode_save_path($metapath) {
+  $savepath=variable_get('file_directory_path', NULL);
+  if(isset($metapath)) {
+    $path_extention=preg_replace_callback("/%.*?(?=(\W|$))/","_flexinode_save_path_markup",$metapath);
+    // create the subdirectories if path_extention is set
+    foreach(explode("/",$path_extention) as $p) {
+      if($p!="/") {
+        $extpath.="/$p";
+        if(!file_exists($savepath."/".$extpath)) {
+          mkdir($savepath."/".$extpath);
+        }
+      }
+    }
+    $savepath.=$extpath;
+  }
+  return $savepath;
+}
+
+// replaces save path tags with correct values
+function _flexinode_save_path_markup($metatag) {
+  global $user; 
+
+  // add any extra functionality here, such as %workflowstate etc
+  foreach($metatag as $tag) {
+    switch(strtolower($tag)) {
+      case "%day":
+        return date("j");
+        break;
+      case "%dayname":
+        return date("l");
+        break;
+      case "%month":
+        return date("n");
+        break;
+      case "%monthname":
+        return date("F");
+        break;
+      case "%year":
+        return date("Y");
+        break;
+      case "%username":
+        return $user->name;
+        break;
+      case "%userid":
+        return $user->uid;
+        break;
+    }
+  }
+}
