--- user.module.4.7.5.orig 2007-01-19 17:18:33.000000000 -0800 +++ user.module 2007-01-19 17:11:00.000000000 -0800 @@ -335,7 +335,7 @@ */ function user_access($string, $account = NULL) { global $user; - static $perm = array(); + static $perm = array(); if (is_null($account)) { $account = $user; @@ -347,22 +347,302 @@ } // To reduce the number of SQL queries, we cache the user's permissions - // in a static variable. - if (!isset($perm[$account->uid])) { - $result = db_query("SELECT DISTINCT(p.perm) FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid IN (%s)", implode(',', array_keys($account->roles))); + // in a static variable. + // + // Modified - cannot cache permissions if they can change according to content + // +// if (!isset($perm[$account->uid])) { +// $result = db_query("SELECT DISTINCT(p.perm) FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid IN (%s)", implode(',', array_keys($account->roles))); + $result = db_query("SELECT DISTINCT(p.perm) FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid IN (%s)", implode(',', user_all_roles($account))); $perm[$account->uid] = ''; - while ($row = db_fetch_object($result)) { - $perm[$account->uid] .= "$row->perm, "; - } - } + while ($row = db_fetch_object($result)) { + $perm[$account->uid] .= "$row->perm, "; + } // end while + +// } // end isset if +// +// Test +// Test to remove duplicates from $perm[$account->uid]; +// + static $tempArray = array(); + static $tempArray0 = array(); + $tempString = ""; + + // + // First covert $perm[$account->uid] to string; + // + $tempString = $perm[$account->uid]; + // + // Next, convert this to an array; + // + $tempArray = split(',', $tempString); + // + // Now, make the array unique + // + $tempArray0 = array_unique($tempArray); + // + // Now copy back to $perm[$account->uid]; + // + $perm[$account->uid] = implode(",", $tempArray0); +// +// end test + +// +// This is for user.module +// +// Another test to see what is written to $perm[$acount->uid] +// +// + if (strpos($perm[$account->uid], $string) !== FALSE) { + $testStatus = "true"; + } else { + $testStatus = "false"; + } + $testDate = format_date(time(), 'custom', 'Y-m-d h:i:s a'); + $testUser = $account->uid; + $testFunction = "user_access"; + $testSubFunction = ""; + $testString = $string; + $testUserName = $account->name; + $testURI = $_SERVER['REQUEST_URI']; + $testAArg0 = arg(0); + $testAArg1 = arg(1); + $testAArg2 = arg(2); + $testAArg3 = arg(3); + $testPerm = $perm[$account->uid]; + if (user_table_exists('user_test')) { + $testQuery = "insert into user_test (testStatus, testDate, testUser, testUserName, testFunction, testSubFunction, testString, testURI, testAArg0, testAArg1, testAArg2, testAArg3, testPerm) values ('" . $testStatus . "','" . $testDate . "','" . $testUser . "','" . $testUserName . "','" . $testFunction . "','" . $testSubFunction . "','" . $testString . "','" . $testURI . "','" . $testAArg0 . "','" . $testAArg1 . "','" . $testAArg2 . "','" . $testAArg3 . "','" . $testPerm . "')"; + db_query($testQuery); + } +// +// end test +// if (isset($perm[$account->uid])) { - return strpos($perm[$account->uid], "$string, ") !== FALSE; +// return strpos($perm[$account->uid], "$string, ") !== FALSE; + return strpos($perm[$account->uid], $string) !== FALSE; } return FALSE; } + +// Modified - Added this function +// This function takes the global $user (or $account) value and returns a list +// of group and non-group roles for this user +// +function user_all_roles($user) { + + // This will be the process to get BOTH the group and non-group roles for a user + + $uid = $user->uid; + $gid = 0; + $nodeID = 0; + + $uri_request_id = $_SERVER['REQUEST_URI']; + $arg = explode("/", $uri_request_id); + + $ogroles = array(); + $x1 = 0; + // + // This will by default add the anonymous user role (no need since we merge user->roles) + // + // $ogroles[0] = 1; + // $x1 = 1; + // + // We get the groupID + // + $group_node = og_get_group_context(); + $gid02 = $group_node->nid; + $gid = $gid02; + if ($gid02 === null) $gid = 0; + + // + // If the above doesn't get the groupID, then we start trying stuff + // + if ($gid == 0) { + + // http://doadance.scbbs.com/drupal03/node/79 + + if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) { + $nodeID = (int)arg(1); + $gid = getgid($nodeID, $uid); + } + + // http://doadance.scbbs.com/drupal03/node/79/edit + // http://doadance.scbbs.com/drupal03/node/79/outline + // http://doadance.scbbs.com/drupal03/node/79/track + + if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'outline' || arg(2) == 'track' ) ) { + $nodeID = (int)arg(1); + $gid = getgid($nodeID, $uid); + } + // 0 1 2 + // http://www.mysite.com/comment/edit/14 + // + if (arg(0) == 'comment' && is_numeric(arg(2)) && arg(1) == 'edit' ) { + $comment = _comment_load(arg(2)); + $nodeID = $comment->nid; + $gid = getgid($nodeID, $uid); + } + // 0 1 2 + // http://www.mysite.com/comment/reply/128#comment_form + // + if (arg(0) == 'comment' && arg(1) == 'reply' ) { + $subsections = explode("#", arg(2)); + $nodeID = (int)$subsections[0]; + $gid = getgid($nodeID, $uid); + } + + // og_term access + // 0 1 2 3 + // http://www.mysite.com/node/add/forum/121 + // + if (arg(0) == 'node' && is_numeric(arg(3)) && arg(1) == 'add' ) { + $nodeID = (int)arg(3); + $gid = getgid($nodeID, $uid); + } + + // Here we get the gid directly + // + // http://doadance.scbbs.com/drupal03/og/users/72 + // http://doadance.scbbs.com/drupal03/og/manage/72 + + if (arg(0) == 'og' && is_numeric(arg(2)) && is_null(arg(3))) { + $gid = (int)arg(2); + } + + // og_calendar + // + // http://doadance.scbbs.com/drupal03/og_calendar/72 + + if (arg(0) == 'og_calendar' && is_numeric(arg(1)) && is_null(arg(2))) { + $gid = (int)arg(1); + } + + // og_forum + // 0 1 2 3 + // http://www.mysite.com/og_forum/39/72?edit[og_groups][]=72 + // http://www.mysite.com/og_forum/37/71?sort=asc&order=Replies&edit[og_groups][0]=71 + + if ($arg[1] == 'og_forum' && strpos($arg[3], 'og_groups') !== false && (!is_null($arg[3])) ) { + $subsections = explode("=", $arg[3]); + foreach ($subsections as $subsection) { + if (is_numeric($subsection)) { + $gid = (int)$subsection; + } + } + } + + // og create nodes + // 0 1 2 3 + // http://sbn.scbbs.com/node/add/forum?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/simplenews?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/webform?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/video?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/page?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/blog?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/event?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/poll?edit[og_groups][]=72 + // http://sbn.scbbs.com/node/add/flexinode-3?edit[og_groups][]=72 + + if ($arg[1] == 'node' && strpos($arg[3], 'og_groups') !== false && (!is_null($arg[3])) ) { + $subsections = explode("=", $arg[3]); + $gid = (int)$subsections[1]; + } + + // og create nodes (custom node/145 aliased to node/addform) + // 0 1 2 3 + // http://demo.schooldistrictlive.com/node/addform?type=forum&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=simplenews&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=webform&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=video&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=page&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=blog&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=event&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=poll&edit[og_groups][]=72 + // http://demo.schooldistrictlive.com/node/addform?type=flexinode-3&edit[og_groups][]=72 + + if ($arg[1] == 'node') { + if (strpos($arg[2], 'addform') !== false ) { + if (strpos($arg[2], 'og_groups') !== false ) { + $subsections = explode("=", $arg[2]); + $gid = (int)$subsections[2]; + } + } + } + + } // end $gid if + // + // Now, using $uid and $gid we find out what roles this user has in this group; + // + // + // And, this is the query I want to use + // + $results = db_query('SELECT role.rid FROM role INNER JOIN og_users_roles ON role.rid = og_users_roles.rid WHERE og_users_roles.uid = ' . $uid . ' AND og_users_roles.gid = ' . $gid ); + + while ( $row = db_fetch_array($results) ) { + $ogroles[$x1] = $row["rid"]; + $x1++; + } + + // + // First, we need to create an array of the $user->role keys + // + // $temp = implode(", ", array_keys($user02->roles)); + + $temp = implode(", ", array_keys($user->roles)); + $c = array(); + $c = explode(", ", $temp); + // + // Next, we add the $user->roles array to the $ogroles array + // + $d = array(); + $d = array_merge($c, $ogroles); + $d = array_unique($d); +// +// If I don't do this, then I won't get the merged results; +// + return $d; + +} // end user_all_roles() + +// Modified - Added this function +// +function getgid($nodeID, $uid) { + + // This will return a $gid based upon the $nodeID and $uid supplied + + $gid = 0; + + // $result = db_query("select node_access.gid from node_access INNER JOIN og_uid ON node_access.gid = og_uid.nid WHERE node_access.nid = $nodeID AND og_uid.uid = $uid"); + $result = db_query("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 node_access.nid = $nodeID AND og_uid.uid = $uid"); + while($t = db_fetch_object($result)) { + $gid = $t->gid; + } + + // If $gid still equals 0 then try searching og_term table (if it exists) + // og forums will typically be listed here + + if ($gid == 0) { + if (user_table_exists('og_term')) { + $result = db_query("select og_term.nid from og_term INNER JOIN og_uid ON og_term.nid = og_uid.nid WHERE og_term.tid = $nodeID AND og_uid.uid = $uid"); + while($t = db_fetch_object($result)) { + $gid = $t->nid; + } + } + } + + return $gid; + +} // end getgid() + +// Modified - Added this function +// +function user_table_exists($table) { + return db_num_rows(db_query("SHOW TABLES LIKE '{" . db_escape_table($table) . "}'")); +} + /** * Checks for usernames blocked by user administration