? webfm-og-6.patch
Index: webfm.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webfm/webfm.admin.inc,v
retrieving revision 1.3
diff -u -p -r1.3 webfm.admin.inc
--- webfm.admin.inc	21 Jul 2009 17:45:09 -0000	1.3
+++ webfm.admin.inc	22 Jul 2009 18:48:32 -0000
@@ -100,7 +100,8 @@ function webfm_admin_settings() {
           '#default_value' => variable_get('webfm_file_public', ''),
           '#description' => t('File is downloadable anonymously via webfm_send.')
           );
-
+          
+  // Per role settings
   $roles = user_roles(0,'access webfm');
   $form['roles'] = array('#type' => 'value', '#value' => $roles);
 
@@ -108,7 +109,7 @@ function webfm_admin_settings() {
   webfm_get_extensions_regex(WEBFM_FLUSH);
 
   foreach ($roles as $rid => $role) {
-    if(variable_get("root_dir_".$rid, '') == '') {
+    if (variable_get("root_dir_".$rid, '') == '') {
       drupal_set_message(t('Role root directory not set for @role role', array('@role' => $role)), error);
     }
 
@@ -158,6 +159,41 @@ function webfm_admin_settings() {
             '#description' => t('The maximum size of all files a user can have on the site (in megabytes).')
             );
   }
+  
+  
+  if (module_exists('og')) {
+    // Per group settings
+    $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) {
+      if (variable_get("root_dir_group_".$gid, '') == '') {
+        drupal_set_message(t('Group root directory not set for @group group', array('@group' => $group)), error);
+      }
+      $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',
@@ -277,14 +313,15 @@ function webfm_admin_settings_validate($
   $exceed_max_msg = t('Your PHP settings limit the maximum file size per upload to %size MB.', array('%size' => file_upload_max_size())).'<br/>';
   $more_info = t("Depending on your sever environment, these settings may be changed in the system-wide php.ini file, a php.ini file in your Drupal root directory, in your Drupal site's settings.php file, or in the .htaccess file in your Drupal root directory.");
   $max_upload_size = file_upload_max_size()/(1024*1024);
-  foreach($form_state['values']['roles'] as $rid => $role)  {
+  
+  foreach ($form_state['values']['roles'] as $rid => $role)  {
     $uploadsize = $form_state['values']['webfm_uploadsize_'. $rid];
     $usersize = $form_state['values']['webfm_usersize_'. $rid];
     $role_root_dir_name = $form_state['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)) {
           form_set_error('root_dir_'. $rid, t('The %role root directory name is not valid.', array('%role' => $role)));
@@ -296,11 +333,10 @@ 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)));
       }
     }
-
-    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) {
@@ -311,4 +347,21 @@ function webfm_admin_settings_validate($
      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)));
     }
   }
+  foreach ($form_state['values']['groups'] as $gid => $group)  {
+    $group_root_dir_name = $form_state['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)));
+      }
+    }
+  }
 }
Index: webfm.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webfm/webfm.install,v
retrieving revision 1.14
diff -u -p -r1.14 webfm.install
--- webfm.install	14 Nov 2008 20:59:47 -0000	1.14
+++ webfm.install	22 Jul 2009 18:48:32 -0000
@@ -34,6 +34,13 @@ function webfm_uninstall(){
    variable_del('webfm_uploadsize_'.$rid);
    variable_del('webfm_usersize_'.$rid);
   }
+  if (module_exists('og')) {
+    $groups = og_all_groups_options();
+    foreach ($groups as $gid => $group) {
+     variable_del('root_dir_group_'.$gid);
+    }
+  }
+  variable_get('webfm_og_auto');
   $types = node_get_types();
   foreach ($types as $type) {
     if ($type->type)
Index: webfm.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webfm/webfm.module,v
retrieving revision 1.35
diff -u -p -r1.35 webfm.module
--- webfm.module	22 Jul 2009 03:31:16 -0000	1.35
+++ webfm.module	22 Jul 2009 18:48:33 -0000
@@ -426,6 +426,16 @@ function webfm_nodeapi(&$node, $op, $tea
             webfm_dbinsert_attach($node->nid, $fid, $i++);
         }
       }
+      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':
@@ -539,6 +549,12 @@ function webfm_form_alter(&$form, &$form
   }
 }
 
+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);
+}
+
 function webfm_link_output() {
   if($user->uid == 1 || user_access('administer webfm')) {
     // Settings link - accessible only #1 user or module administrator
@@ -845,7 +861,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
@@ -858,9 +874,13 @@ function webfm_ajax () {
     //no feedback
     exit();
   }
-
-  $webfm_root_path = webfm_get_root_path();
-  if($webfm_root_path == NULL) {
+  $group_directory = '';
+  if ($gid) {
+    $node = node_load($gid);
+    $group_directory = '/'. webfm_get_group_directory($node);
+  }
+  $webfm_root_path = webfm_get_root_path() . $group_directory;
+  if (webfm_get_root_path() == NULL) {
     //WebFM root dir must exist
     webfm_json(array('status' => FALSE, 'err' => t('WebFM root not set')));
     exit();
@@ -873,19 +893,20 @@ function webfm_ajax () {
       case "readtrees":
         $trees = array();
         $err = '';
-        if($webfm_perm == WEBFM_ADMIN) {
+        if ($webfm_perm == WEBFM_ADMIN) {
           //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)) {
-            foreach($webfm_roots as $key => $sub_root) {
+          if (count($webfm_roots)) {
+            foreach ($webfm_roots as $key => $sub_root) {
               //Build webfm directory tree(s) for WEBFM_USER
-              if(!empty($sub_root)) {
+              if (!empty($sub_root)) {
                 $sub_root_path = $root_dir.$sub_root;
-                if(is_dir($sub_root_path)) {
+                if (is_dir($sub_root_path)) {
                   $current = $sub_root;
                   unset($_SESSION['tree_'.$current]);
                   $trees[$key] = webfm_tree($root_dir, $current);
@@ -895,10 +916,31 @@ function webfm_ajax () {
               } else {
                 $err .= t('Root directory not set for @role role ', array('@role' => $webfm_access_roles[$key]));
               }
-            }
-          } else {
+            } 
+          } 
+          else {
             $err = t('No root directory set in WebFM settings for this role');
           }
+          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);
+                  }
+                } 
+              }
+            } 
+            else {
+              $err = t('No root directory set in WebFM settings for this group');
+            }
+          }
         }
 
         if(count($trees)) {
@@ -924,6 +966,14 @@ 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')));
@@ -969,7 +1019,17 @@ function webfm_ajax () {
               }
             }
           }
-
+          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) {
             if(!is_dir($root_dir.$param0)) {
               webfm_json(array('status' => FALSE, 'data' => $root_dir.$param0.t(' path does not exist - refresh required')));
@@ -1794,6 +1854,36 @@ function webfm_get_root_dirs($flush = FA
 }
 
 /**
+ * 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 groups 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'
  */
@@ -1835,6 +1925,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;
 }
 
