home *** CD-ROM | disk | FTP | other *** search
- <?php
- /*
- Copyright Intermesh 2003
- Author: Merijn Schering <mschering@intermesh.nl>
- Author: Michael Borko <michael.borko@tgm.ac.at>
- Version: 1.0 Release date: 08 July 2003
- Version: 1.5 Release date: 27 February 2004
-
- This program is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; either version 2 of the License, or (at your
- option) any later version.
- */
-
- require_once ($GO_CONFIG->class_path.'profiles.class.inc');
-
- class groups extends db
- {
- var $group_everyone = "2";
- var $group_root = "1";
-
- var $ldap;
- var $ldap_um = false;
- var $grouplist;
- var $is_in_group;
- var $grouplist_index;
-
- function groups()
- {
- global $GO_CONFIG;
-
- $this->db();
-
- if ( $GO_CONFIG->auth_db_type == "ldap" )
- {
- require_once($GO_CONFIG->class_path.'users.class.inc');
- $users = new users();
- $this->ldap = new ldap();
- $this->ldap->connect();
- $this->ldap->bind("uid=".$users->get_user(
- $_SESSION['GO_SESSION']['user_id']),
- $_SESSION['GO_SESSION']['user_auth_id']);
-
- if ( $GO_CONFIG->auth_db_ldap_um )
- $this->ldap_um = true;
- }
- }
-
- function delete_group($group_id)
- {
- $this->query("DELETE FROM users_groups WHERE group_id='$group_id'");
- $this->query("DELETE FROM groups WHERE id='$group_id'");
-
- global $GO_SECURITY;
- $GO_SECURITY->delete_group($group_id);
- }
-
- function clear_group($group_id)
- {
- return $this->query("DELETE FROM users_groups WHERE group_id='$group_id'");
- }
-
- function add_user_to_group($user_id, $group_id)
- {
- if ( $user_id )
- return $this->query("INSERT INTO users_groups (user_id,group_id)".
- " VALUES ($user_id, $group_id)");
- else
- return false;
- }
-
- function delete_user($user_id)
- {
- $sql = "DELETE FROM users_groups WHERE user_id='$user_id'";
- $this->query($sql);
- $sql = "SELECT id FROM groups WHERE user_id='$user_id'";
- $this->query($sql);
- $del = new groups;
- while ($this->next_record())
- {
- $del->delete_group($this->f("id"));
- }
- }
-
- function delete_user_from_group($user_id, $group_id)
- {
- return $this->query("DELETE FROM users_groups WHERE".
- " user_id='$user_id' AND group_id='$group_id'");
- }
-
- function get_group($group_id)
- {
- $this->query("SELECT * FROM groups WHERE id='$group_id'");
-
- if($this->next_record())
- return $this->Record;
- else
- return false;
- }
-
- function update_group($group_id, $name)
- {
- return $this->query("UPDATE groups SET name='$name' WHERE id='$group_id'");
- }
-
- function get_group_by_name($name)
- {
- $name = htmlentities($name);
- $this->query("SELECT * FROM groups WHERE name='$name'");
- if ($this->next_record())
- {
- return $this->Record;
- }else
- {
- return false;
- }
- }
-
- function add_group($user_id, $name)
- {
- $name = htmlentities($name);
- $group_id = $this->nextid("groups");
- if ($group_id > 0)
- {
- $this->query("INSERT INTO groups (id, user_id, name) VALUES".
- " ('$group_id','$user_id','".smart_addslashes($name)."')");
- return $group_id;
- }else
- {
- return false;
- }
- }
-
- function user_owns_group($user_id, $group_id)
- {
- $this->query("SELECT user_id FROM groups WHERE user_id='$user_id' AND".
- " id='$group_id'");
- if ($this->num_rows() > 0)
- {
- return true;
- }else
- {
- return false;
- }
- }
-
- function is_in_group($user_id, $group_id)
- {
- if ( $this->ldap_um )
- {
- ## If there are only the new UserIDs we can burst the search
- //require_once($GO_CONFIG->class_path."users.class.inc");
- //$users = new users();
- //$uid = $users->get_user($user_id); $uid = $uid["username"];
- //$this->ldap->search( "(&(gidNumber=$group_id)(memberUid=$uid))",
- // $GO_CONFIG->auth_db_ldap_basedn );
- //$this->ldap->next_entry();
- //if ( $this->ldap->get_values("gidNumber") )
- // return true;
-
- ## Search for the Group in LDAP
- $this->ldap->search( "(gidNumber=$group_id)",
- $GO_CONFIG->auth_db_ldap_basedn );
- $this->ldap->next_entry();
- $this->is_in_group = $this->ldap->get_values("memberUid");
-
- ## Search for the possible different UserIDs
- $this->ldap->search( "(uidNumber=$user_id)",
- $GO_CONFIG->auth_db_ldap_basedn );
- $this->ldap->next_entry();
- $uid_array = $this->ldap->get_values("uid");
-
- ## Check if the user is in the group
- if ( $this->is_in_group )
- foreach ( $uid_array as $value )
- if ( @in_array( $value, $this->is_in_group ) )
- return true;
- }
-
- $sql = "SELECT user_id FROM users_groups WHERE".
- " user_id='$user_id' AND group_id='$group_id'";
- $this->query($sql);
-
- if ($this->num_rows() > 0)
- return true;
- else
- return false;
- }
-
- function get_users_in_group($group_id, $sort="name", $direction="ASC")
- {
- global $GO_CONFIG;
-
- ## Group_id in LDAP is higher than 999!
- if ( $this->ldap_um )
- {
- $this->ldap->search( "(gidNumber=$group_id)",
- $GO_CONFIG->auth_db_ldap_basedn );
-
- $this->ldap->next_entry();
- //$this->is_in_group = $this->ldap->get_values("uniqueMember");
- $this->is_in_group = $this->ldap->get_values("memberUid");
-
- return $this->is_in_group["count"];
-
- } else {
- if ($sort == 'name' || $sort == 'users.name')
- {
- $sort = 'users.first_name '.$direction.', users.last_name';
- }
- $sql = "SELECT users.id, users.first_name, users.last_name FROM".
- " users LEFT JOIN users_groups ON (users.id = users_groups.user_id)".
- " WHERE users_groups.group_id='$group_id' ORDER BY ".
- $sort." ".$direction;
-
- $this->query($sql);
- return $this->num_rows();
- }
- }
-
- function group_is_visible($user_id, $group_id)
- {
- if ($this->user_owns_group($user_id, $group_id)
- || $this->is_in_group($user_id, $group_id))
- return true;
- else
- return false;
- }
-
- // Gets all groups. Not for user display. Use get_authorised groups.
- ////////////////////////////////////////////////////////////////////////
- function get_all_groups()
- {
- $this->query("SELECT groups.*,users.username FROM groups, users WHERE".
- " groups.user_id = users.id ORDER BY groups.id ASC");
- }
-
- //Users can only see groups when they own it or are in it. Therefore this
- //complicated query.
- //////////////////////////////////////////////////////////////////////
- function get_authorised_groups($user_id)
- {
- if ( $this->ldap_um )
- {
- ##Use the groups where the user is a member:
- require_once($GO_CONFIG->class_path.'users.class.inc');
- $users = new users();
- $user = $users->get_user($user_id);
-
- $this->ldap->search( "(|(memberUid=$user)(uid=$user))",
- $GO_CONFIG->auth_db_ldap_basedn );
- #$this->ldap->search("(&(ou=*$query*)(mail=*))",
- # "dc=tgm,dc=ac,dc=at");
- $entries = $this->ldap->get_entries();
-
- $profile = new profiles();
-
- for ( $i = 0; $i<$entries["count"]; $i++ )
- # $this->grouplist[$i] = $entries[$i]["cn"];
- $this->grouplist[] = $profile->convert_group_profile_ldap(
- $entries[$i] );
-
- } else {
- $sql = "SELECT groups.* FROM groups, users_groups".
- " WHERE ((groups.user_id='$user_id')".
- " OR (users_groups.user_id='$user_id'".
- " AND users_groups.group_id=groups.id))".
- " GROUP BY groups.id ORDER BY groups.id ASC";
- $this->query($sql);
- }
- }
-
- function next_record()
- {
- if ( $this->ldap_um )
- {
- if ( count( $this->grouplist ) > $this->grouplist_index )
- {
- $this->Record = $this->grouplist[$this->grouplist_index++];
- return $this->Record;
- } else
- return false;
- } else
- return parent::next_record();
- }
-
- function search($query, $field, $user_id, $start=0, $offset=0)
- {
- if ( $this->ldap_um )
- {
- ##TODO: DONT SEARCH FOR GROUPS UNDER 1000 !!!
- $query = utf8_encode(substr( $query, 1, strlen( $query ) - 2 ));
- switch( $field )
- {
- case "lehrer":
- if ( $query == "*" ) $filter="(&(cn=lehrer*)".
- "(gidNumber=*)(sn=*))";
- else $filter="(&(cn=lehrer*$query*)".
- "(gidNumber=*)(sn=*))";
- $path="ou=Groups,ou=edu,dc=tgm,dc=ac,dc=at";
- break;
- case "schueler":
- if ( $query == "*" ) $filter="(&(cn=schueler*)".
- "(gidNumber=*)(sn=*))";
- else $filter="(&(cn=schueler*$query*)".
- "(gidNumber=*)(sn=*))";
- $path="ou=Groups,ou=edu,dc=tgm,dc=ac,dc=at";
- break;
- case "admins":
- if ( $query == "*" ) $filter="(&(cn=*)".
- "(gidNumber=*)(sn=*))";
- else $filter="(&(cn=*$query*)".
- "(gidNumber=*)(sn=*))";
- $path="ou=Groups,ou=admin,dc=tgm,dc=ac,dc=at";
- break;
- default:
- }
- $this->ldap->search($filter, $path );
- $this->ldap->sort( "cn" );
- $ldapentries = $this->ldap->num_entries();
- $entries = $this->ldap->get_entries();
-
- $profile = new profiles();
-
- for ( $i=0; $i<$entries["count"]; $i++ )
- # $this->grouplist[] = $entries[$i]["sn"];
- $this->grouplist[] = $profile->convert_group_profile_ldap(
- $entries[$i] );
-
- $this->grouplist_index = 0;
- return count($this->grouplist);
-
- } else {
- $sql = "SELECT users.* FROM users, users_groups INNER ".
- "JOIN acl ON users.acl_id= acl.acl_id WHERE ".
- "((acl.group_id = users_groups.group_id ".
- "AND users_groups.user_id = ".$user_id.") OR (".
- "acl.user_id = ".$user_id." )) AND $field ".
- "LIKE '".smart_addslashes($query)."' ".
- "GROUP BY users.id ORDER BY name ASC";
-
- if ($offset != 0) $sql .= " LIMIT $start, $offset";
-
- $this->query($sql);
- return $this->num_rows();
- }
- }
- }
-