--- og_user_roles.module.5.x-2.0.orig	2007-08-02 13:04:37.000000000 -0700
+++ og_user_roles.module	2007-08-02 13:06:13.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_user_roles.module,v 1.1.2.4 2007/08/01 00:52:28 somebodysysop Exp $
+// $Id: og_user_roles.module,v 1.1.2.3 2007/07/08 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -189,6 +189,18 @@ function og_user_roles_admin_settings() 
       '#description' => t('Select the group type you wish to use for subgroup creation.'),
     );
   }
+
+  $form['og_user_roles_test'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Test/Debug.'),
+    '#collapsible' => TRUE,
+  );
+  $form['og_user_roles_test']['og_user_roles_test_default'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Output debug data to og_user_test table?'),
+    '#default_value' => variable_get('og_user_roles_test_default', 0),
+    '#description' => t('Every time user_access() is called, it will call og_user_all_roles() which adds group roles to $user->roles.  Do you wish to see the output from this function? (Note that this feature is for testing/debug purposes, and could create a very large output file.  This feature also requires that the table og_user_test already exist. Details here: http://drupal.org/node/164038)'),
+  );
 
   return system_settings_form($form);
 }
@@ -586,6 +598,10 @@ function views_handler_filter_group() {
  */
 function og_user_roles_init() {
   global $user;
+  // Let's try getting og roles here, just to see what happens
+    $roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
+    $user->roles = $roles;
+
   // Looking for this format: http://www.scbbs.com/node/add/link?gids[]=29
   // We only need to process this if the user is logged in
 
@@ -1108,6 +1124,16 @@ function og_user_roles_all_roles($user) 
         $gid = intval(current($_REQUEST['gids']));
       }
     }
+    
+	// File upload: http://www.scbbs.com/node/135/225#comment-225
+	if (arg(0) == 'upload' && arg(1) == 'js') {
+      if ($_SESSION['og_last']) $gid = $_SESSION['og_last']->nid;
+	}
+    // File download
+	if (arg(0) == 'system' && arg(1) == 'files') {
+      if ($_SESSION['og_last']) $gid = $_SESSION['og_last']->nid;
+	}
+
   } // end $gid if
 
   //
@@ -1146,9 +1172,11 @@ function og_user_roles_all_roles($user) 
     $d = $c;
   }
 
-  // Write the test data
-  //og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $query);
-
+  // Write the test data: http://drupal.org/node/164038
+  if (variable_get('og_user_roles_test_default', 0) == 1) {
+    og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $query);
+  }
+
   // $d is either the merged results, or just $user->roles;
 
   return $d;
@@ -1163,6 +1191,10 @@ function og_user_roles_getgid($nid, $uid
   while ($t = db_fetch_object($result)) {
     $gid = $t->gid;
   }
+
+  /* Sample:
+   * SELECT node_access.gid from node_access INNER JOIN og_uid ON node_access.gid = og_uid.nid WHERE node_access.realm = 'og_subscriber' AND og_uid.uid = 2 AND (node_access.nid = 194 OR node_access.gid = 194)
+   */
 
   // If $gid still equals 0 then try searching og_term table (if it exists)
   // og forums will typically be listed here
@@ -1200,14 +1232,41 @@ function og_user_roles_write_test($user,
   $test_aarg1 = arg(1);
   $test_aarg2 = arg(2);
   $test_aarg3 = arg(3);
-  $test_perm = 'Roles Returned: ('. implode(',', $d) .') <br />db_query = '. $query;
+//  $test_perm = 'Roles Returned: ('. implode(',', $d) .') <br />db_query = '. $query;
+  $test_perm = 'Roles Returned: ('. implode(',', $d) .')';
 
-  if (og_user_roles_user_table_exists('{user_test}')) {
-    $test_query = "INSERT INTO user_test (test_status, test_date, test_user, test_username, test_group, test_function, test_subfunction, test_string, test_uri, test_aarg0, test_aarg1, test_aarg2, test_aarg3, test_perm) values ('". $test_status ."','". $test_date ."','". $test_user ."','". $test_username ."','". $test_group ."','". $test_function ."','". $test_subfunction ."','". $test_string ."','". $test_uri ."','". $test_aarg0 ."','". $test_aarg1 ."','". $test_aarg2 ."','". $test_aarg3 ."','". $test_perm ."')";
+  if (og_user_roles_user_table_exists('{og_user_test}')) {
+    $test_query = "INSERT INTO og_user_test (testStatus, testDate, testUser, testUserName, testGroup, testFunction, testSubFunction, testString, testURI, testAArg0, testAArg1, testAArg2, testAArg3, testPerm) values ('". $test_status ."','". $test_date ."','". $test_user ."','". $test_username ."','". $test_group ."','". $test_function ."','". $test_subfunction ."','". $test_string ."','". $test_uri ."','". $test_aarg0 ."','". $test_aarg1 ."','". $test_aarg2 ."','". $test_aarg3 ."','". $test_perm ."')";
     db_query($test_query);
   }
 }
-
+
+/**
+ * Create test table.
+ */
+function og_user_roles_create_test() {
+
+ $query = "
+    CREATE TABLE og_user_test (
+      testDate varchar(25),
+      testUser varchar(5),
+      testUserName varchar(40),
+      testGroup varchar(5),
+      testFunction varchar(60),
+      testSubFunction varchar(80),
+      testString varchar(60),
+      testStatus varchar(10),
+      testURI varchar(200),
+      testAArg0 varchar(40),
+      testAArg1 varchar(40),
+      testAArg2 varchar(40),
+      testAArg3 varchar(40),
+      testPerm long varchar)
+    ";
+
+    db_query($test_query);
+}
+
 /**
  * Called by og_user_roles_nodeapi('access')
  * To check og access permissions
