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	5 Jun 2008 15:36:13 -0000
@@ -198,32 +198,49 @@ function webfm_admin_settings_validate($
     $usersize = $form_values['webfm_usersize_'. $rid];
     $role_root_dir_name = $form_values['root_dir_'.$rid];
 
-    if(!empty($role_root_dir_name)) {
-      if($valid_webfm_root) {
-        if(!preg_match('/^[0-9a-zA-Z]/', $role_root_dir_name)) {
+    if (!empty($role_root_dir_name)) {
+      if ($valid_webfm_root) {
+        if (!preg_match('/^[0-9a-zA-Z]/', $role_root_dir_name)) {
           form_set_error('root_dir_'. $rid, t('The leading character of the %role root directory must be alphanumeric.', array('%role' => $role)));
-        } else if(preg_match('[\.]', $role_root_dir_name)) {
+        } else if (preg_match('[\.]', $role_root_dir_name)) {
           form_set_error('root_dir_'. $rid, t('The %role root directory name is not valid.', array('%role' => $role)));
         } else {
-          $role_root_dir = $webfm_root_dir."/".$role_root_dir_name;
+          $role_root_dir = $webfm_root_dir ."/". $role_root_dir_name;
           file_check_directory($role_root_dir, FILE_CREATE_DIRECTORY, 'root_dir_'.$rid);
         }
       } else {
         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)) {
+    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)));
     }
-    if(!is_numeric($usersize) || ($usersize <= 0)) {
+    if (!is_numeric($usersize) || ($usersize <= 0)) {
       form_set_error('webfm_usersize_'. $rid, t('The %role file size limit must be a number and greater than zero.', array('%role' => $role)));
     }
-    if($uploadsize > $max_upload_size) {
+    if ($uploadsize > $max_upload_size) {
      form_set_error('webfm_uploadsize_'. $rid, $exceed_max_msg . $more_info);
      $more_info = '';
     }
-    if($uploadsize > $usersize) {
+    if ($uploadsize > $usersize) {
      form_set_error('webfm_uploadsize_'. $rid, t('The %role maximum file size per upload is greater than the total file size allowed per user', array('%role' => $role)));
     }
   }
@@ -378,6 +395,40 @@ 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 +636,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_directory);
+        }
+      }
+      
       break;
 
     case 'update':
@@ -1010,7 +1076,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 +1096,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));
         }
@@ -1054,10 +1137,18 @@ function webfm_ajax () {
           //role root must exist and this user must be a member of that role
           $webfm_roots = webfm_get_root_dirs();
           $root_role = trim(rawurldecode($_POST["param0"]));
-          if(($root = variable_get("root_dir_".$root_role, '')) &&
+          if(($root = variable_get("root_dir_". $root_role, '')) &&
              (array_key_exists($root_role, $webfm_roots))) {
-            $current = "/".$root;
+            $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 +1193,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 +2007,35 @@ 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 +2078,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;
 }
 
