Index: webfm.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webfm/webfm.module,v
retrieving revision 1.2.2.40
diff -u -p -r1.2.2.40 webfm.module
--- webfm.module	21 Apr 2008 14:08:12 -0000	1.2.2.40
+++ webfm.module	3 Jun 2008 18:48:45 -0000
@@ -212,7 +212,27 @@ function webfm_admin_settings_validate($
         form_set_error('root_dir_'. $rid, t('The WebFM root directory must be valid for the %role root directory name to be valid.', array('%role' => $role)));
       }
     }
-
+    
+    
+    foreach($form_values['groups'] as $gid => $group)  {
+    
+    $group_root_dir_name = $form_values['root_dir_group_'.$gid];
+
+    if(!empty($group_root_dir_name)) {
+      if($valid_webfm_root) {
+        if(!preg_match('/^[0-9a-zA-Z]/', $group_root_dir_name)) {
+          form_set_error('root_dir_group_'. $gid, t('The leading character of the %group root directory must be alphanumeric.', array('%group' => $group)));
+        } else if(preg_match('[\.]', $group_root_dir_name)) {
+          form_set_error('root_dir_group_'. $gid, t('The %group root directory name is not valid.', array('%group' => $group)));
+        } else {
+          $group_root_dir = $webfm_root_dir."/".$group_root_dir_name;
+          file_check_directory($group_root_dir, FILE_CREATE_DIRECTORY, 'root_dir_group_'.$gid);
+        }
+      } else {
+        form_set_error('root_dir_group_'. $gid, t('The WebFM root directory must be valid for the %group root directory name to be valid.', array('%group' => $group)));
+      }
+    }
+  }
     if(!is_numeric($uploadsize) || ($uploadsize <= 0)) {
       form_set_error('webfm_uploadsize_'. $rid, t('The %role file size limit must be a number and greater than zero.', array('%role' => $role)));
     }
@@ -378,6 +398,41 @@ function webfm_admin_settings() {
             );
   }
 
+
+
+//per group directories
+  if (module_exists('og')) {  
+    $groups = og_all_groups_options();
+    $form['groups'] = array('#type' => 'value', '#value' => $groups);
+    $form['webfm_og_auto'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => variable_get('webfm_og_auto', 0),
+      '#title' => t('Create new group folders'),
+      '#description' => t('Automatically create a new group folder when a group is created.'),
+      );
+    foreach ($groups as $gid => $group) {
+      $form["settings_group_".$gid] =
+        array('#type' => 'fieldset',
+              '#title' => t('Settings for @group group', array('@group' => $group)),
+              '#collapsible' => TRUE,
+              '#collapsed' => TRUE
+              );
+
+      $form["settings_group_".$gid]["root_dir_group_".$gid] =
+        array('#type' => 'textfield',
+              '#title' => t('Group Root directory'),
+              '#default_value' => variable_get("root_dir_group_".$gid, ''),
+              '#maxlength' => '100',
+              '#size' => '70',
+              '#description' => t('Root directory for this group.
+                                  <br />This path is relative to "WebFM Root directory" set above.
+                                  <br />If this directory path is compound then the path must already exist for this
+                                  <br />setting to validate.')
+             );
+
+    }
+  }
+ 
   $form['attach'] =
     array('#type' => 'fieldset',
           '#title' => t('WebFM attachments'),
@@ -585,6 +640,21 @@ function webfm_nodeapi(&$node, $op, $tea
             webfm_dbinsert_attach($node->nid, $fid, $i++);
         }
       }
+      //automatically create new group folder when group is added
+      if (module_exists('og') && variable_get('webfm_og_auto', 0) == 1) {
+        $groups = og_all_groups_options();
+        if ($groups[$node->nid]) {
+          // make the node title into a suitable directory name
+          $group_directory = trim($node->title);
+          $group_directory = drupal_strtolower($group_directory);
+          $group_directory = str_replace(array(' ', '-'), '_', $group_directory);
+          $group_directory = preg_replace('/[^a-z0-9_]/', '', $group_directory);
+          $group_root_dir = file_directory_path() .'/'. variable_get('webfm_root_dir', '') ."/". $group_directory;
+          file_check_directory($group_root_dir, FILE_CREATE_DIRECTORY, 'root_dir_group_'.$node->nid);
+          variable_set('root_dir_group_'. $node->nid, $group_root_dir);
+        }
+      }
+      
       break;
 
     case 'update':
@@ -1010,7 +1080,8 @@ function webfm_ajax () {
           //Build webfm directory tree
           unset($_SESSION['tree_'.$webfm_root_path]);
           $trees[0] = webfm_tree($root_dir, $webfm_root_path);
-        } else {
+        } 
+        else {
           //clear static array of roles with webfm access ($webfm_roots cached only for 'read' op)
           $webfm_roots = webfm_get_root_dirs(TRUE);
           if(count($webfm_roots)) {
@@ -1029,14 +1100,30 @@ function webfm_ajax () {
                 $err .= t(' Root directory not set for @role role ', array('@role' => $webfm_access_roles[$key]));
               }
             }
-          } else {
-            $err = t('No root directory set in WebFM settings for this role');
-          }
-        }
-
-        if(count($trees)) {
-          webfm_json(array('status' => TRUE, 'tree' => $trees, 'current' => $webfm_root_path, 'admin' => $webfm_perm == WEBFM_ADMIN, 'err' => $err));
-        } else {
+          } 
+            $group_trees = array();
+            if (module_exists('og')) {
+              //clear static array of groups with directories ($webfm_roots cached only for 'read' op)
+              $webfm_group_roots = webfm_get_group_root_dirs(TRUE);
+              if(count($webfm_group_roots)) {
+                foreach($webfm_group_roots as $key => $sub_root) {
+                  //Build webfm directory tree(s) for WEBFM_USER
+                  if(!empty($sub_root)) {
+                    $sub_root_path = $root_dir.$sub_root;
+                    if(is_dir($sub_root_path)) {
+                      $current = $sub_root;
+                      unset($_SESSION['tree_'.$current]);
+                      $trees[$key] = webfm_tree($root_dir, $current);
+                    }
+                  } 
+                }
+              } 
+            }
+         }  
+        if (count($trees)) {
+            webfm_json(array('status' => TRUE, 'tree' => $trees, 'current' => $webfm_root_path, 'admin' => $webfm_perm == WEBFM_ADMIN, 'err' => $err));
+        } 
+        else {
           $err .= t(' No trees found.');
           webfm_json(array('status' => FALSE, 'err' => $err));
         }
@@ -1058,6 +1145,15 @@ function webfm_ajax () {
              (array_key_exists($root_role, $webfm_roots))) {
             $current = "/".$root;
           }
+          
+          if (module_exists('og')) {
+            $webfm_group_roots = webfm_get_group_root_dirs();
+            $root_group = trim(rawurldecode($_POST["param0"]));
+            if(($root = variable_get("root_dir_group_".$root_group, '')) &&
+               (array_key_exists($root_group, $webfm_group_roots))) {
+              $current = "/".$root;
+            }
+          }   
         }
         if(!isset($current)) {
           webfm_json(array('status' => FALSE, 'data' => t('unknown tree')));
@@ -1102,6 +1198,17 @@ function webfm_ajax () {
                 break;
               }
             }
+            if (module_exists('og')) {
+              $webfm_group_roots = webfm_get_group_root_dirs();
+              foreach($webfm_group_roots as $key => $sub_root) {
+                // The read path must be contained within a legitimate group root dir for this user
+                if($sub_root && webfm_check_path($param0, $sub_root)) {
+                  $perm_flag = TRUE;
+                  break;
+                }
+              }
+            }
+            
           }
 
           if($perm_flag) {
@@ -1905,7 +2012,36 @@ function webfm_get_root_dirs($flush = FA
 
   return $webfm_roots;
 }
+/**
+ * Helper function to get array of group root directories for a user
+ */
+function webfm_get_group_root_dirs($flush = FALSE) {
+  global $user;
+  static $webfm_roots = array();
+  static $webfm_access_groups = array();
+  //add per group directories
+   if($flush)
+      $webfm_access_groups = array();
+
+    // Roles with 'access webfm' perm
+    if(!count($webfm_access_groups)) {     
+      $webfm_roots = array();
+      foreach($user->og_groups as $key => $group) {
+        if ($group['is_active']) {
+          // Groups with directories that user is active in
+          $path = variable_get("root_dir_group_".$key, '');
+          if(!empty($path)) {
+            // Prevent redundant trees for roles with common root dir
+            if(!in_array($path, $webfm_roots)) {
+              $webfm_roots[$key] = "/".$path;
+            }
+          }
+        }   
+      } 
+  }
 
+  return $webfm_roots;
+}
 /**
  * Helper function to determine if a webfm_file record is modifiable by a
  * user with 'access webfm'
@@ -1948,6 +2084,16 @@ function webfm_path_access($path) {
       return TRUE;
     }
   }
+  //alternatively the read path may be in a legitimate group root dir for this user
+  if (module_exists('og')) {
+    $webfm_group_roots = webfm_get_group_root_dirs();
+    foreach($webfm_group_roots as $key => $sub_root) {
+      // The read path must be contained within a legitimate role root dir for this user
+      if(webfm_check_path($path, $root_dir.$sub_root)) {
+        return TRUE;
+      }
+    }
+  }
   return FALSE;
 }
 
