--- E:/TEMP/Drupal/comment_upload/Original/comment_upload.module	Tue Apr 21 20:41:04 2009
+++ E:/TEMP/Drupal/comment_upload/comment_upload.module	Fri May 08 14:34:36 2009
@@ -28,10 +28,59 @@
     'page callback' => 'comment_upload_js',
     'access arguments' => array('upload files to comments'),
   );
+  
+  $items['admin/settings/comment_upload_path'] = array(
+    'title' => 'Comment upload paths',
+    'description' => 'Configure a prefix to apply to the path of uploaded files.',
+    'access arguments' => array('administer site configuration'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('comment_upload_path_admin_settings'),
+  ); 
 
   return $items;
 }
 
+
+/**
+ * Configuration callback for this module.
+ */
+function comment_upload_path_admin_settings() {
+  
+  $form = array();
+
+  # Setting form for comment object
+  $form['comment_upload_path_prefix'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Pattern for the file prefix uploaded to the comment'),
+    '#description' => t('Specify the pattern to prefix to file names uploaded with the comment_upload module.  It will be appended after the site files directory (e.g., files) but before the file name itself.  Do not include a leading or trailing slash.  Spaces will be converted to underscores to avoid file system issues.'),
+    '#default_value' => variable_get('comment_upload_path_prefix'),
+  );
+  /*
+  $form['comment_token_help'] = array(
+    '#title' => t('Replacement patterns for comment object'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
+  );
+  $form['comment_token_help']['help'] = array(
+    '#value' => theme('token_help', 'comment'),
+  ); */
+  
+  $form['user_token_help'] = array(
+    '#title' => t('Replacement patterns for user object'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
+  );
+  $form['user_token_help']['help'] = array(
+    '#value' => theme('token_help', 'user'),
+  );  
+
+  return system_settings_form($form);
+}
+
 /**
  * Implementation of hook_perm().
  */
@@ -482,9 +531,27 @@
     'file_validate_image_resolution' => array($limits['resolution']),
     'file_validate_size' => array($limits['file_size'], $limits['user_size']),
   );
-
+  
+  // Apply path prefix
+  if (variable_get('comment_upload_path_prefix', '') != '') {    
+    // Check comment state
+    if (isset($form['cid']['#value'])) {
+      // Existing comment
+      $upload_dir = file_directory_path().'/'.token_replace(variable_get('comment_upload_path_prefix', ''), 'user', user_load(array('uid' => $form['uid']['#value'])));
+      file_check_directory($upload_dir, FILE_CREATE_DIRECTORY);
+    }
+    else {
+      // New comment - get $uid from global user object
+      $upload_dir = file_directory_path().'/'.token_replace(variable_get('comment_upload_path_prefix', ''), 'user', $GLOBALS['user']);
+      file_check_directory($upload_dir, FILE_CREATE_DIRECTORY);
+    }
+  }
+  else {
+    $upload_dir = file_directory_path();
+  }
+  
   // Save new file uploads.
-  if (user_access('upload files to comments') && ($file = file_save_upload('upload', $validators, file_directory_path()))) {
+  if (user_access('upload files to comments') && ($file = file_save_upload('upload', $validators, $upload_dir))) {
     $file->list = variable_get('upload_list_default', 1);
     $file->description = $file->filename;
     $file->weight = 0;
