Here's a solutiuon for pulling the group membership information out of a
PostgreSQL array. It also shows your user ID and checks to see if you
are a super user.
<?php
require_once("session.inc.php");
require_once("HTML.inc.php");
require_once("menu.inc.php");
HTML_header("Get Groups");
showMenu($menuGroup);
$conn = pg_connect("dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD
host=$PGHOST")
or die("<b>It didn't connect.</b>");
// Get the DB user ID for the current user.
$currentID = getUserID($conn, $PGUSER);
// Get a list of DB groups of which the current user is a member.
$memberOf = getGroupList($conn, $currentID);
// Check for super user status.
$super = checkSuper($conn, $PGUSER);
pg_close($conn);
// Show the results.
echo "User ID: <b>$currentID</b><br><br>";
echo "Member of: <b>$memberOf</b><br><br>";
if ($super == "true")
echo "<b>You are a super user!</b><br><br>";
/*************************/
// Get the DB user ID for the current user.
function getUserID($conn, $usename) {
$result = pg_exec($conn, "SELECT usesysid FROM pg_user WHERE
usename='$usename'");
$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
return $row[usesysid];
}
// Create a list of DB groups of which the current user is a member.
function getGroupList ($conn, $currentID) {
$result = pg_exec($conn, "SELECT groname, grolist FROM pg_group");
$limit = pg_numrows($result);
for ($pass=0; $pass < $limit; ++$pass)
{
$row = pg_fetch_array($result, $pass, PGSQL_ASSOC);
$dirty = $row[grolist];
// Remove { and } from string and create a
comma-seperated list.
$cleaned = str_replace("}{", ",", $dirty);
$cleaned = str_replace("}", "", $cleaned);
$cleaned = str_replace("{", "", $cleaned);
$arrayName = explode(",", $cleaned);
while (list($index, $content) = each($arrayName))
{
if ($content == $currentID)
$memberOf .= "$row[groname] ";
//$memberOf[] .= "$row[groname] ";
}
}
//asort($memberOf);
return $memberOf;
}
// Check for super user status.
function checkSuper ($conn, $usename) {
$result = pg_exec($conn, "SELECT usesuper FROM pg_user WHERE
usename='$usename'");
$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
if ($row[usesuper] == "t")
$answer = "true";
else
$answer = "false";
return $answer;
}
HTML_footer();
?>