Index: webfm.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webfm/webfm.module,v
retrieving revision 1.2.2.44
diff -u -p -r1.2.2.44 webfm.module
--- webfm.module	17 Oct 2008 17:14:49 -0000	1.2.2.44
+++ webfm.module	15 Mar 2009 05:39:41 -0000
@@ -207,9 +207,26 @@ function webfm_admin_settings_validate($
       } 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)) {
@@ -374,6 +391,38 @@ 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'),
@@ -474,9 +523,11 @@ function webfm_perm() {
 /**
  * Implementation of hook_menu().
  */
-function webfm_menu($maycache) {
+function webfm_menu($may_cache) {
+  global $user;
+
   $items = array();
-  if ($maycache) {
+  if ($may_cache) {
     $items[] = array(
       'title' => t('Web File Manager'),
       'path' => 'webfm',
@@ -508,6 +559,20 @@ function webfm_menu($maycache) {
       'access' => user_access('administer webfm'),
       'type' => MENU_NORMAL_ITEM);
   }
+  else {
+    if (arg(0) == 'node' && is_numeric(arg(1))) {
+      $node = node_load(arg(1));
+      if(isset($node->type) && in_array($node->nid, array_keys($user->og_groups))) {
+        $items[] = array(
+          'title' => t('Files'),
+          'path' => 'node/'. arg(1) . '/webfm',
+          'callback' => 'webfm_main',
+          'access' => user_access('access webfm'),
+          'type' => MENU_LOCAL_TASK,
+        );
+      }
+    }
+  }
   return $items;
 }
 
@@ -623,6 +688,18 @@ 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 = webfm_get_group_directory($node);
+          $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':
@@ -640,6 +717,12 @@ function webfm_nodeapi(&$node, $op, $tea
   }
 }
 
+function webfm_get_group_directory($node) {
+  $group_directory = drupal_strtolower(trim($node->title));
+  $group_directory = str_replace(array(' ', '-'), '_', $group_directory);
+  return preg_replace('/[^a-z0-9_]/', '', $group_directory);
+}
+
 /**
  * Implementation of hook_form_alter().
  */
@@ -1046,7 +1129,7 @@ function webfm_reload_upload ($url, $con
 /**
  * Ajax post requests
  */
-function webfm_ajax () {
+function webfm_ajax ($gid = NULL) {
   global $user;
 
   //3 possible outcomes - the user is either an admin, user or prohibited
@@ -1060,7 +1143,12 @@ function webfm_ajax () {
     exit();
   }
 
-  $webfm_root_path = webfm_get_root_path();
+  if ($gid) {
+    $node = node_load($gid);
+    $group_directory = '/'. webfm_get_group_directory($node);
+  }
+
+  $webfm_root_path = webfm_get_root_path() . $group_directory;
   if($webfm_root_path == NULL) {
     //WebFM root dir must exist
     webfm_json(array('status' => FALSE, 'err' => t('WebFM root not set')));
@@ -1097,14 +1185,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));
         }
@@ -1124,8 +1228,16 @@ function webfm_ajax () {
           $root_role = trim(rawurldecode($_POST["param0"]));
           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')));
@@ -1170,6 +1282,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) {
@@ -1907,7 +2030,7 @@ function theme_webfm_upload_form($form) 
  * Function to pass base_url, icon directory, debug and cleanurl flags
  */
 function webfm_inline_js($base_url, $clean_url, $uid) {
-  $js = '<script type="text/javascript">function getBaseUrl(){return '.drupal_to_js($base_url).';} function getWebfmIconDir(){return '.drupal_to_js($base_url."/".variable_get('webfm_icon_dir','')).';} function getWebfmCleanUrl(){return '.drupal_to_js($clean_url).'; }function getWebfmIETreeOffset(){return '.drupal_to_js(variable_get('webfm_ie_dd_tree_offset', '')).';}function getWebfmIEListOffset(){return '.drupal_to_js(variable_get('webfm_ie_dd_list_offset', '')).';} function getWebfmUid(){return '.drupal_to_js($uid).';} function getWebfmDateFormat(){return '.drupal_to_js(variable_get('webfm_date_format', WEBFM_DATE_FORMAT_DAY)).';} function getWebfmMetaName(){return '.drupal_to_js(variable_get('webfm_display_title', '')).';}</script>' ;
+  $js = '<script type="text/javascript">function getBaseUrl(){return '.drupal_to_js($base_url).';} function getWebfmIconDir(){return '.drupal_to_js($base_url."/".variable_get('webfm_icon_dir','')).';} function getWebfmCleanUrl(){return '.drupal_to_js($clean_url).'; }function getWebfmIETreeOffset(){return '.drupal_to_js(variable_get('webfm_ie_dd_tree_offset', '')).';}function getWebfmIEListOffset(){return '.drupal_to_js(variable_get('webfm_ie_dd_list_offset', '')).';} function getWebfmUid(){return '.drupal_to_js($uid).';} function getWebfmDateFormat(){return '.drupal_to_js(variable_get('webfm_date_format', WEBFM_DATE_FORMAT_DAY)).';} function getWebfmMetaName(){return '.drupal_to_js(variable_get('webfm_display_title', '')).';} function getArg(n){arg = ['.drupal_to_js(arg(0)).','.drupal_to_js(arg(1)).','.drupal_to_js(arg(2)).']; return arg[n];}</script>' ;
   drupal_set_html_head($js);
   return $js;
 }
@@ -1988,7 +2111,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'
@@ -2031,6 +2182,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;
 }
 
Index: js/webfm.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webfm/js/webfm.js,v
retrieving revision 1.2.2.50
diff -u -p -r1.2.2.50 webfm.js
--- js/webfm.js	16 Oct 2008 20:25:39 -0000	1.2.2.50
+++ js/webfm.js	15 Mar 2009 05:39:46 -0000
@@ -3744,7 +3744,8 @@ Webfm.stopEvent = function(event) {
 //the getModUrl() is used by contrib modules for custom ajax
 Webfm.ajaxUrl = function() {
   var path = getBaseUrl() + "/?q=";
-  return path += (typeof getModUrl == "undefined") ? "webfm_js" : getModUrl();
+  path += (typeof getModUrl == "undefined") ? "webfm_js" : getModUrl();
+  return path += (typeof getArg(1) == "string") ? '/' + getArg(1) : '';
 }
 
 // Empty all child nodes
