--- uploadpath.module	2008-11-09 00:23:30.000000000 +0100
+++ uploadpath.module.linksunten	2009-03-17 13:31:15.000000000 +0100
@@ -1,5 +1,5 @@
 <?php
-// $Id: uploadpath.module,v 1.1.2.5.2.2 2008/11/08 23:23:30 davidwhthomas Exp $
+// $Id: uploadpath.module,v 1.1.2.5.2.1 2008/09/08 08:20:02 davidwhthomas Exp $
 /**
  * @file
  * Upload Path module; allows admins to specify a sub directory for uploaded files.
@@ -32,23 +32,20 @@ function uploadpath_nodeapi(&$node, $op,
     if( !in_array($node->type, variable_get('uploadpath_excluded_node_types',array())) ){
       if(isset($node->files) && user_access('upload files')) {
         foreach ($node->files as $key => $file) {
-          if(is_object($file)){
-            $file = (array)$file; // if object then type cast as array
-          }
           // Only rewrite the path on new files to be saved
-          if ($file['new'] && !$file['remove']){
+          if (!$file['remove']) {
             //get the token path pattern
             $pattern = variable_get('uploadpath_prefix_'.$node->type, false);
             if(!$pattern){ 
               //default pattern
               $pattern = variable_get('uploadpath_prefix', '[type]/[yyyy]/[mm]');
             }
-            if(variable_get('uploadpath_clean_filenames', false) && $node->title){
+            if (variable_get('uploadpath_clean_filenames', false) && (variable_get('uploadpath_rename_filenames', 1) == 1) && $node->title) {
               //convert filename into meaningful semantic name based on node title or file desc :-)
               //e.g: 'DC100_1.jpg' becomes 'the_quick_brown_fox_a3de.jpg'
               if($file['description'] && strlen($file['description']) > 3){
                 $filename_root = $file['description']; //base filename on file desc
-              }else{
+              } else {
                 $filename_root = $node->title; //base filename on node title
               }
               //drupal_set_message($filename_root);
@@ -66,9 +63,19 @@ function uploadpath_nodeapi(&$node, $op,
               // apply new, prefixed file name by token replacing the path pattern
               $file_path = token_replace($pattern . '/', 'node', $node);
               $file_name = $file_path . $file_name;
-            }else{
-              // apply new, prefixed file name by token replacing the path pattern
-              $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node)) . $file['filename'];
+            } else {
+              if (variable_get('uploadpath_clean_filenames', false) && (variable_get('uploadpath_rename_filenames', 1) == 0)) {
+                //get path info of file
+                $file_info = pathinfo($file['filename']);
+                // create a unique and anonymous file name
+                $file_name = substr(uniqid(rand(), true),0,10). '.'. $file_info['extension'];
+                // apply anon file name by token replacing the path pattern
+                $file_path = token_replace($pattern . '/', 'node', $node);
+                $file_name = $file_path . $file_name;
+              } else {
+                // apply new, prefixed file name by token replacing the path pattern
+                $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node)) . $file['filename'];
+              }
             }
             // SECURITY NOTE:
             // Tokens include user supplied information and could provide an attack vector.
@@ -77,16 +84,27 @@ function uploadpath_nodeapi(&$node, $op,
             // Create the directory if it doesn't exist yet.
             $dirs = explode('/', dirname($file_name));
             $directory = file_directory_path();
+            $destination_dir = $directory;
             while (count($dirs)) {
-              $directory .= '/' . array_shift($dirs);
-              file_check_directory($directory, FILE_CREATE_DIRECTORY);
+              $destination_dir .= '/' . array_shift($dirs);
+              file_check_directory($destination_dir, FILE_CREATE_DIRECTORY);
             }
+
+            // Save relative path of file name
+            $rel_file_name = $file_name;
+
+            // Strip file name from relative path
+            $last_slash = strrpos($file_name, '/');
+            $file_name = substr($rel_file_name, $last_slash + 1, strlen($file_name) - $last_slash - 1);
+
             //move file to new subfolder
-            if (file_move($file['filepath'], $file_name, FILE_EXISTS_RENAME)) {
+            if (file_move($file['filepath'], $rel_file_name, FILE_EXISTS_RENAME)) {
               //update node file array with new path, if needed
-              $node->files[$key]['filepath'] = $file_name; 
+              $node->files[$key]['filepath'] = $directory. '/'. $rel_file_name; 
+              $node->files[$key]['filename'] = $file_name; 
               // update file record in database
-              db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $file['filepath'], $file['fid']);
+              db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $file['filepath'], $key);
+              db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d", $file_name, $key);
             }
             //drupal_set_message('<pre>AFTER:'.print_r($node->files[$key],1).'</pre>');
           }
