home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / groups.class.inc < prev    next >
Encoding:
Text File  |  2004-03-08  |  9.6 KB  |  349 lines

  1. <?php
  2. /*
  3.    Copyright Intermesh 2003
  4.    Author: Merijn Schering <mschering@intermesh.nl>
  5.    Author: Michael Borko <michael.borko@tgm.ac.at>
  6.    Version: 1.0 Release date: 08 July 2003
  7.    Version: 1.5 Release date: 27 February 2004
  8.  
  9.    This program is free software; you can redistribute it and/or modify it
  10.    under the terms of the GNU General Public License as published by the
  11.    Free Software Foundation; either version 2 of the License, or (at your
  12.    option) any later version.
  13.  */
  14.  
  15. require_once ($GO_CONFIG->class_path.'profiles.class.inc');
  16.  
  17. class groups extends db
  18. {
  19.   var $group_everyone = "2";
  20.   var $group_root = "1";
  21.  
  22.   var $ldap;
  23.   var $ldap_um = false;
  24.   var $grouplist;
  25.   var $is_in_group;
  26.   var $grouplist_index;
  27.  
  28.   function groups()
  29.   {
  30.     global $GO_CONFIG;
  31.  
  32.     $this->db();
  33.  
  34.     if ( $GO_CONFIG->auth_db_type == "ldap" )
  35.     {
  36.       require_once($GO_CONFIG->class_path.'users.class.inc');
  37.       $users = new users();
  38.       $this->ldap = new ldap();
  39.       $this->ldap->connect();
  40.       $this->ldap->bind("uid=".$users->get_user(
  41.     $_SESSION['GO_SESSION']['user_id']),
  42.     $_SESSION['GO_SESSION']['user_auth_id']);
  43.  
  44.       if ( $GO_CONFIG->auth_db_ldap_um )
  45.     $this->ldap_um = true;
  46.     }
  47.   }
  48.  
  49.   function delete_group($group_id)
  50.   {
  51.     $this->query("DELETE FROM users_groups WHERE group_id='$group_id'");
  52.     $this->query("DELETE FROM groups WHERE id='$group_id'");
  53.  
  54.     global $GO_SECURITY;
  55.     $GO_SECURITY->delete_group($group_id);
  56.   }
  57.  
  58.   function clear_group($group_id)
  59.   {
  60.     return $this->query("DELETE FROM users_groups WHERE group_id='$group_id'");
  61.   }
  62.  
  63.   function add_user_to_group($user_id, $group_id)
  64.   {
  65.     if ( $user_id )
  66.       return $this->query("INSERT INTO users_groups (user_id,group_id)".
  67.       " VALUES ($user_id, $group_id)");
  68.     else
  69.       return false;
  70.   }
  71.  
  72.   function delete_user($user_id)
  73.   {
  74.     $sql = "DELETE FROM users_groups WHERE user_id='$user_id'";
  75.     $this->query($sql);
  76.     $sql = "SELECT id FROM groups WHERE user_id='$user_id'";
  77.     $this->query($sql);
  78.     $del = new groups;
  79.     while ($this->next_record())
  80.     {
  81.       $del->delete_group($this->f("id"));
  82.     }
  83.   }
  84.  
  85.   function delete_user_from_group($user_id, $group_id)
  86.   {
  87.     return $this->query("DELETE FROM users_groups WHERE".
  88.     " user_id='$user_id' AND group_id='$group_id'");
  89.   }
  90.  
  91.   function get_group($group_id)
  92.   {
  93.     $this->query("SELECT * FROM groups WHERE id='$group_id'");
  94.  
  95.     if($this->next_record())
  96.       return $this->Record;
  97.     else
  98.       return false;
  99.   }
  100.  
  101.   function update_group($group_id, $name)
  102.   {
  103.     return $this->query("UPDATE groups SET name='$name' WHERE id='$group_id'");
  104.   }
  105.  
  106.   function get_group_by_name($name)
  107.   {
  108.     $name = htmlentities($name);
  109.     $this->query("SELECT * FROM groups WHERE name='$name'");
  110.     if ($this->next_record())
  111.     {
  112.       return $this->Record;
  113.     }else
  114.     {
  115.       return false;
  116.     }
  117.   }
  118.  
  119.   function add_group($user_id, $name)
  120.   {
  121.     $name = htmlentities($name);
  122.     $group_id = $this->nextid("groups");
  123.     if ($group_id > 0)
  124.     {
  125.       $this->query("INSERT INTO groups (id, user_id, name) VALUES".
  126.       " ('$group_id','$user_id','".smart_addslashes($name)."')");
  127.       return $group_id;
  128.     }else
  129.     {
  130.       return false;
  131.     }
  132.   }
  133.  
  134.   function user_owns_group($user_id, $group_id)
  135.   {
  136.     $this->query("SELECT user_id FROM groups WHERE user_id='$user_id' AND".
  137.     " id='$group_id'");
  138.     if ($this->num_rows() > 0)
  139.     {
  140.       return true;
  141.     }else
  142.     {
  143.       return false;
  144.     }
  145.   }
  146.  
  147.   function is_in_group($user_id, $group_id)
  148.   {
  149.     if ( $this->ldap_um )
  150.     {
  151. ## If there are only the new UserIDs we can burst the search
  152.       //require_once($GO_CONFIG->class_path."users.class.inc");
  153.       //$users = new users();
  154.       //$uid = $users->get_user($user_id); $uid = $uid["username"];
  155.       //$this->ldap->search( "(&(gidNumber=$group_id)(memberUid=$uid))",
  156.       //    $GO_CONFIG->auth_db_ldap_basedn );
  157.       //$this->ldap->next_entry();
  158.       //if ( $this->ldap->get_values("gidNumber") )
  159.       //    return true;
  160.  
  161. ## Search for the Group in LDAP
  162.       $this->ldap->search( "(gidNumber=$group_id)",
  163.       $GO_CONFIG->auth_db_ldap_basedn );
  164.       $this->ldap->next_entry();
  165.       $this->is_in_group = $this->ldap->get_values("memberUid");
  166.  
  167. ## Search for the possible different UserIDs
  168.       $this->ldap->search( "(uidNumber=$user_id)",
  169.       $GO_CONFIG->auth_db_ldap_basedn );
  170.       $this->ldap->next_entry();
  171.       $uid_array = $this->ldap->get_values("uid");
  172.  
  173. ## Check if the user is in the group
  174.       if ( $this->is_in_group )
  175.     foreach ( $uid_array as $value )
  176.       if ( @in_array( $value, $this->is_in_group ) )
  177.         return true;
  178.     }
  179.  
  180.     $sql = "SELECT user_id FROM users_groups WHERE".
  181.       " user_id='$user_id' AND group_id='$group_id'";
  182.     $this->query($sql);
  183.  
  184.     if ($this->num_rows() > 0)
  185.       return true;
  186.     else
  187.       return false;
  188.   }
  189.  
  190.   function get_users_in_group($group_id, $sort="name", $direction="ASC")
  191.   {
  192.     global $GO_CONFIG;
  193.  
  194. ## Group_id in LDAP is higher than 999!
  195.     if ( $this->ldap_um )
  196.     {
  197.       $this->ldap->search( "(gidNumber=$group_id)",
  198.       $GO_CONFIG->auth_db_ldap_basedn );
  199.  
  200.       $this->ldap->next_entry();
  201.       //$this->is_in_group = $this->ldap->get_values("uniqueMember");
  202.       $this->is_in_group = $this->ldap->get_values("memberUid");
  203.  
  204.       return $this->is_in_group["count"];
  205.  
  206.     } else {
  207.       if ($sort == 'name' || $sort == 'users.name')
  208.       {
  209.     $sort = 'users.first_name '.$direction.', users.last_name';
  210.       }
  211.       $sql = "SELECT users.id, users.first_name, users.last_name FROM".
  212.     " users LEFT JOIN users_groups ON (users.id = users_groups.user_id)".
  213.     " WHERE users_groups.group_id='$group_id' ORDER BY ".
  214.     $sort." ".$direction;
  215.  
  216.       $this->query($sql);
  217.       return $this->num_rows();
  218.     }
  219.   }
  220.  
  221.   function group_is_visible($user_id, $group_id)
  222.   {
  223.     if ($this->user_owns_group($user_id, $group_id)
  224.     || $this->is_in_group($user_id, $group_id))
  225.       return true;
  226.     else
  227.       return false;
  228.   }
  229.  
  230.   // Gets all groups. Not for user display. Use get_authorised groups.
  231.   ////////////////////////////////////////////////////////////////////////
  232.   function get_all_groups()
  233.   {
  234.     $this->query("SELECT groups.*,users.username FROM groups, users WHERE".
  235.     " groups.user_id = users.id ORDER BY groups.id ASC");
  236.   }
  237.  
  238.   //Users can only see groups when they own it or are in it. Therefore this
  239.   //complicated query.
  240.   //////////////////////////////////////////////////////////////////////
  241.   function get_authorised_groups($user_id)
  242.   {
  243.     if ( $this->ldap_um )
  244.     {
  245. ##Use the groups where the user is a member:
  246.       require_once($GO_CONFIG->class_path.'users.class.inc');
  247.       $users = new users();
  248.       $user = $users->get_user($user_id);
  249.  
  250.       $this->ldap->search( "(|(memberUid=$user)(uid=$user))",
  251.       $GO_CONFIG->auth_db_ldap_basedn );
  252. #$this->ldap->search("(&(ou=*$query*)(mail=*))",
  253. #    "dc=tgm,dc=ac,dc=at");
  254.       $entries = $this->ldap->get_entries();
  255.  
  256.       $profile = new profiles();
  257.  
  258.       for ( $i = 0; $i<$entries["count"]; $i++ )
  259. #        $this->grouplist[$i] = $entries[$i]["cn"];
  260.     $this->grouplist[] = $profile->convert_group_profile_ldap(
  261.         $entries[$i] );
  262.  
  263.     } else {
  264.       $sql = "SELECT groups.* FROM groups, users_groups".
  265.     " WHERE ((groups.user_id='$user_id')".
  266.     " OR (users_groups.user_id='$user_id'".
  267.     " AND users_groups.group_id=groups.id))".
  268.     " GROUP BY groups.id ORDER BY groups.id ASC";
  269.       $this->query($sql);
  270.     }
  271.   }
  272.  
  273.   function next_record()
  274.   {
  275.     if ( $this->ldap_um )
  276.     {
  277.       if ( count( $this->grouplist ) > $this->grouplist_index )
  278.       {
  279.     $this->Record = $this->grouplist[$this->grouplist_index++];
  280.     return $this->Record;
  281.       } else
  282.     return false;
  283.     } else
  284.       return parent::next_record();
  285.   }
  286.  
  287.   function search($query, $field, $user_id, $start=0, $offset=0)
  288.   {
  289.     if ( $this->ldap_um )
  290.     {
  291. ##TODO: DONT SEARCH FOR GROUPS UNDER 1000 !!!
  292.       $query = utf8_encode(substr( $query, 1, strlen( $query ) - 2 ));
  293.       switch( $field )
  294.       {
  295.     case "lehrer":
  296.       if ( $query == "*" ) $filter="(&(cn=lehrer*)".
  297.         "(gidNumber=*)(sn=*))";
  298.       else $filter="(&(cn=lehrer*$query*)".
  299.         "(gidNumber=*)(sn=*))";
  300.     $path="ou=Groups,ou=edu,dc=tgm,dc=ac,dc=at";
  301.     break;
  302.     case "schueler":
  303.       if ( $query == "*" ) $filter="(&(cn=schueler*)".
  304.         "(gidNumber=*)(sn=*))";
  305.       else $filter="(&(cn=schueler*$query*)".
  306.         "(gidNumber=*)(sn=*))";
  307.     $path="ou=Groups,ou=edu,dc=tgm,dc=ac,dc=at";
  308.     break;
  309.     case "admins":
  310.       if ( $query == "*" ) $filter="(&(cn=*)".
  311.         "(gidNumber=*)(sn=*))";
  312.       else $filter="(&(cn=*$query*)".
  313.         "(gidNumber=*)(sn=*))";
  314.     $path="ou=Groups,ou=admin,dc=tgm,dc=ac,dc=at";
  315.     break;
  316.     default:
  317.       }
  318.       $this->ldap->search($filter, $path );
  319.       $this->ldap->sort( "cn" );
  320.       $ldapentries = $this->ldap->num_entries();
  321.       $entries = $this->ldap->get_entries();
  322.  
  323.       $profile = new profiles();
  324.  
  325.       for ( $i=0; $i<$entries["count"]; $i++ )
  326. #        $this->grouplist[] = $entries[$i]["sn"];
  327.     $this->grouplist[] = $profile->convert_group_profile_ldap(
  328.         $entries[$i] );
  329.  
  330.       $this->grouplist_index = 0;
  331.       return count($this->grouplist);
  332.  
  333.     } else {
  334.       $sql = "SELECT users.* FROM users, users_groups INNER ".
  335.     "JOIN acl ON users.acl_id= acl.acl_id WHERE ".
  336.     "((acl.group_id = users_groups.group_id ".
  337.     "AND users_groups.user_id = ".$user_id.") OR (".
  338.     "acl.user_id = ".$user_id." )) AND $field ".
  339.     "LIKE '".smart_addslashes($query)."' ".
  340.     "GROUP BY users.id ORDER BY name ASC";
  341.  
  342.       if ($offset != 0)    $sql .= " LIMIT $start, $offset";
  343.  
  344.       $this->query($sql);
  345.       return $this->num_rows();
  346.     }
  347.   }
  348. }
  349.