--- filemanager.module.orig	2005-10-28 18:10:12.000000000 -0700
+++ filemanager.module	2005-11-09 09:22:18.000000000 -0800
@@ -146,6 +146,11 @@
  *   Fileobject on success, otherwise FALSE
  */
 function filemanager_add_file($area, $path, $filename, $mimetype = 'application/unknown', $remove = TRUE, $private = FALSE, $file = FALSE) {
+  // CRG - If this area forces privacy override it here
+  //  - See also filemanager_settings
+  if(variable_get('filemanager_force_private_' . $area, 0)) {
+    $private = TRUE;
+  }
 
   $size = filesize($path);
 
@@ -228,39 +233,113 @@
 
 /**
  * Renames an existing file in the filestore
+ * 
  * @param $file
  *   file or fid to rename
  * @param $name
- *   new name for the file
+ *   new name for the file, use NULL or blank to retain old name
  * @return
- *   the renamed file object on success or false on failure
+ *  the renamed file object on success or false on failure.
  */
 function filemanager_rename($file, $name) {
     $file = filemanager_get_file_info($file);
+
+    // Exit immediately if the rename does nothing
+    if (! $file || $name == $file->filename) {
+      return $file;
+    }
+
+    // Begin rename operation
     $oldworking = filemanager_create_path($file, true);
     $oldactive = filemanager_create_path($file, false);
     $lock = _filemanager_lock();
+
     $file->filename = $name;
+
+    $updated = _filemanager_update_file($file, $oldworking, $oldactive);
+
+    if ($file != false) {
+        db_query("UPDATE {file} SET filename = '%s', directory = '%d' WHERE fid=%d", $file->filename, $file->directory, $file->fid);
+    }
+    _filemanager_unlock($lock);
+    return $file;
+}
+
+/**
+ * Moves the file from public to private or vice versa
+ * 
+ * @param $file
+ *   file or fid to modify
+ * @param $private
+ *  new private state for the file
+ * @return
+ *  the modified file object on success or false on failure.
+ */
+function filemanager_set_private($file, $private) {
+    $file = filemanager_get_file_info($file);
+
+    // The  private column is a char, adjust the flag to match
+    if ($private) {
+        $private = '1';
+    }
+    else {
+        $private = '';
+    }
+
+    // Exit immediately if file is already in the right state
+    if (! $file || $file->private == $private) {
+      return $file;
+    }
+
+    // Begin set_private operation
+    $oldworking = filemanager_create_path($file, true);
+    $oldactive = filemanager_create_path($file, false);
+    $lock = _filemanager_lock();
+
+    $file->private = $private;
+
+    $updated = _filemanager_update_file($file, $oldworking, $oldactive);
+
+    if ($file != false) {
+        db_query("UPDATE {file} SET private='%s', directory = '%d' WHERE fid=%d", $file->private, $file->directory, $file->fid);
+    }
+    _filemanager_unlock($lock);
+    return $file;
+}
+
+/**
+ * helper function for filemanager_rename and filemanager_set_private
+ * does the actual file moving from $oldactive and $oldworking to the 
+ * values set in the $file object
+ * 
+ * @param $file
+ *   file or fid containing new name/directory/private info
+ * @param $oldworking
+ *  the path to the current working file
+ * @param $oldactive
+ *  the path to the current active file
+ * @return
+ *  true on success and false on failure
+ */
+
+function _filemanager_update_file(&$file, $oldworking, $oldactive) {
+    // Using the new file object find/create an appropiate area for this file
     $file = _filemanager_find_directory($file);
     $newworking = filemanager_create_path($file, true);
     $newactive = filemanager_create_path($file, false);
     if (file_exists($oldworking)) {
         if (!file_move($oldworking, $newworking, FILE_EXISTS_ERROR)) {
             drupal_set_message("file exists: {$file->filename}", 'error');
-            $file = false;
+            return false;
         }
     }
-    if ($file != false && file_exists($oldactive)) {
+    if (file_exists($oldactive)) {
         if (!file_move($oldactive, $newactive, FILE_EXISTS_ERROR)) {
             drupal_set_message("file exists: {$file->filename}", 'error');
-            $file = false;
+            return false;
         }
     }
-    if ($file != false) {
-        db_query("UPDATE {file} SET filename='%s', directory=%d WHERE fid=%d", $file->filename, $file->directory, $file->fid);
-    }
-    _filemanager_unlock($lock);
-    return $file;
+    return true;
 }
 
 /**
@@ -503,30 +584,31 @@
         $headers = module_invoke($module, 'filemanager_download', $file);
 
         if ($headers === FALSE) {
-          drupal_access_denied();
+          return drupal_access_denied();
         }
 
         elseif ($headers === TRUE) {
-          file_transfer($filepath, array('Content-Type: '. $file->mimetype, 'Content-Length: '. filesize($filepath), 'Content-Disposition: filename=' . $file->filename));
+          return file_transfer($filepath, array('Content-Type: '. $file->mimetype, 'Content-Length: '. filesize($filepath), 'Content-Disposition: filename=' . $file->filename));
         }
 
         elseif (is_array($headers)) {
-          file_transfer($filepath, $headers);
+          return file_transfer($filepath, $headers);
         }
       }
 
       // Since no modules responded check to see if this is a general area file
       // and allow download if so.
-      if ($file->area == 'general') {
-        file_transfer($filepath, array('Content-Type: '. $file->mimetype, 'Content-Length: '. filesize($filepath), 'Content-Disposition: filename=' . $file->filename));
+      //  - CRG: If privacy was forced then also allow it (duplicates prior functionality)
+      if ($file->area == 'general' || variable_get('filemanager_force_private_' . $file->area, 0)) {
+        return file_transfer($filepath, array('Content-Type: '. $file->mimetype, 'Content-Length: '. filesize($filepath), 'Content-Disposition: filename=' . $file->filename));
       }
     }
 
-    drupal_not_found();
+    return drupal_not_found();
   }
   else {
     // It's a public file so no auth check is required.
-    file_transfer($filepath, array('Content-Type: '. $file->mimetype, 'Content-Length: '. filesize($filepath), 'Content-Disposition: filename=' . $file->filename));
+    return file_transfer($filepath, array('Content-Type: '. $file->mimetype, 'Content-Length: '. filesize($filepath), 'Content-Disposition: filename=' . $file->filename));
   }
 }
 
@@ -615,9 +698,16 @@
 
   $output .= form_textfield(t('Maximum size limit'), 'filemanager_max_size', variable_get('filemanager_max_size', '400'), 6, 10, t('Maximum amount of disk space that can be consumed by all files. Enter in megabytes.'));
 
-  $header = array('Area','Description','Max size (Mb)');
+  $output .= "<p>". t("Forcing a module to use private areas will: Force files to be streamed (no direct access), filemanger's private directory will be used, module needs to implement filemanager_download hook (default allows all access).") . "</p>";
+  $header = array('Area','Description','Max size (Mb)', 'Force Private');
   foreach(filemanager_area_list() as $area) {
-    $rows[] = array($area['name'], $area['description'], form_textfield(null, 'filemanager_area_limit_' . $area['area'], variable_get('filemanager_area_limit_' . $area['area'], '-1'), 6, 10));
+    //$rows[] = array($area['name'], $area['description'], form_textfield(null, 'filemanager_area_limit_' . $area['area'], variable_get('filemanager_area_limit_' . $area['area'], '-1'), 6, 10));
+    $rows[] = array(
+      $area['name'],
+      $area['description'],
+      form_textfield(null, 'filemanager_area_limit_'    . $area['area'],  variable_get('filemanager_area_limit_' . $area['area'], '-1'), 6, 10),
+      form_checkbox(null, 'filemanager_force_private_'  . $area['area'],  1, variable_get('filemanager_force_private_' . $area['area'], '0')), 
+    );
   }
   $output .= form_item(t('File areas'),  theme_table($header, $rows));
   return $output;
