source: trunk/jabberit_messenger/inc/class.ldap_im.inc.php @ 1160

Revision 1160, 8.5 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #571 - Somente criando os *.jars( applet.jar, filetransfer.jar e xhtml.jar )

  • Property svn:executable set to *
RevLine 
[382]1<?php
[417]2  /***************************************************************************\
3  *  Expresso - Expresso Messenger                                            *
4  *     - Alexandre Correia / Rodrigo Souza                                                               *
5  *     - JETI - http://jeti-im.org/                                                                              *
6  * ------------------------------------------------------------------------- *
7  *  This program is free software; you can redistribute it and/or modify it  *
8  *  under the terms of the GNU General Public License as published by the    *
9  *  Free Software Foundation; either version 2 of the License, or (at your   *
10  *  option) any later version.                                               *
11  \***************************************************************************/
[382]12
13define('PHPGW_INCLUDE_ROOT', '../');
14define('PHPGW_API_INC','../phpgwapi/inc');
15require_once( PHPGW_API_INC . '/class.common.inc.php');
16
17class ldap_im
18{
[946]19        private $attr_org;
20        private $common;
21        private $hostsJabber;
[382]22        private $ldap;
23        private $ldap_context;
[1160]24        private $ldap_user;
[946]25        private $ldap_host;
[951]26        private $ldap_org;
[382]27        private $ldap_pass;
28        private $max_result;
29       
30        public final function __construct()
31        {
[946]32                // Attributes org ldap;
33                $this->attr_org = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['attributes_org_ldap_jabberit']);
34               
35                // Hosts Jabber
36                $this->hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
37               
38                // Result Ldap
39                $this->max_result = 30;
[382]40        }
41
42        public final function __destruct()
43        {
44                if( $this->ldap )
45                        ldap_close($this->ldap);
46        }       
47
[946]48        private final function ldapConn()
[382]49        {
[946]50                $this->common = new common();           
[382]51               
[946]52                $GLOBALS['phpgw_info']['server']['ldap_version3'] = true;
53
[1160]54                $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_user . "," . $this->ldap_context , $this->ldap_pass, false );
[382]55        }
56       
[946]57        private final function ldapRoot()
[551]58        {
[946]59                $this->ldap_host        = (isset($_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_host'];
60                $this->ldap_context     = (isset($_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_context'];
[1160]61                $this->ldap_user        = (isset($_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
[946]62                $this->ldap_pass        = (isset($_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
[551]63
[946]64                $this->ldapConn();
65        }
[551]66
[946]67        private final function ldapCatalog()
68        {
69                $version3 = true;
70                $refer  = true;
71
72                if(!function_exists('ldap_connect'))
73                        return false;
[551]74               
[946]75                if(!$conn = ldap_connect($this->ldap_host))
76                        return false;
77
78                if( $version3 )
79                        if( !ldap_set_option($conn,LDAP_OPT_PROTOCOL_VERSION,3) )
80                                $version3 = false;
81
82                ldap_set_option($conn, LDAP_OPT_REFERRALS, $refer);
83
84                // Bind as Admin
[1160]85                if($this->ldap_user && $this->ldap_pass && !ldap_bind($conn, $this->ldap_user . "," .$this->ldap_context, $this->ldap_pass))
[946]86                        return false;
87               
88                // Bind as Anonymous
[1160]89                if(!$this->ldap_user && !$this->ldap_pass && !@ldap_bind($conn))
[946]90                        return false;
[1160]91
[946]92                return $conn;
93        }
94
95        public final function getGroupsLdap($pOrg)
96        {
97                $this->ldapRoot();
[1160]98                $result_groups = "";
[946]99
[1160]100                if( $this->ldap && $pOrg['ou'] != "-1")
[551]101                {
[946]102                        $organization = 'ou=' . $pOrg['ou'] .",". $this->ldap_context;
103                        $filter = "(&(phpgwAccountType=g)(objectClass=posixGroup))";
104                        $justthese = array("cn","gidNumber");
105                        $search = ldap_list($this->ldap, $organization, $filter, $justthese);
106                        $entry = ldap_get_entries( $this->ldap, $search );
107
108                        if( $entry )
109                        {                                       
110                                $result_groups = "<ldap>";
111                                foreach($entry as $tmp)
112                                        if( $tmp['gidnumber'][0] != "" )
113                                                $result_groups .= "<org><cn>".$tmp['cn'][0]."</cn><gid>".$tmp['gidnumber'][0]."</gid></org>";
114                               
115                                $result_groups .= "</ldap>";                                           
116                        }
[1160]117                       
118                        return $result_groups;
[551]119                }
[1160]120               
[946]121                return $result_groups;
[551]122        }
123
[946]124        public final function getGroupsMemberUid($pGroup)
[697]125        {
[946]126                $this->ldapRoot();
[697]127               
128                if( $this->ldap )
129                {
130                        $filter = "(&(objectclass=posixgroup)(|".$pGroup."))";
131                        $justthese = array("dn","memberuid","gidnumber");
132                        $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese);
133                        $result = ldap_get_entries($this->ldap,$search);
134                       
135                        if( $result['count'] > 0 )
136                                return $result;
137                }               
138               
139                return false;
140        }
141
[946]142        public final function getOrganizationsLdap()
[551]143        {
[946]144                $this->ldapRoot();
[1160]145               
[551]146                if( $this->ldap )
147                {
[946]148                        $filter="ou=*";         
149                        $justthese = array("ou");
150                        $search = ldap_search($this->ldap,$this->ldap_context,$filter,$justthese);                     
151                        $entry = ldap_get_entries($this->ldap, $search);
152                }
[551]153
[946]154                foreach($entry as $tmp)
155                        if($tmp['ou'][0] != "")
156                                $result_org[] = $tmp['ou'][0]; 
[551]157
[946]158                return $result_org;
159        }
[551]160
161
[988]162        public final function getUsersLdapCatalog( $search )
[382]163        {
[946]164                $confHosts      = $this->hostsJabber;
165                $result = array();
166                $return = array();
167                $conn   = "";           
168
169                for($i = 0; $i < count($confHosts); $i++ )
[382]170                {
[946]171                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
172                        $this->ldap_context = $confHosts[$i]['contextLdap'];
[1160]173                        $this->ldap_user        = $confHosts[$i]['user'];
[951]174                        $this->ldap_org         = $confHosts[$i]['org'];
[946]175                        $this->ldap_pass        = $confHosts[$i]['password'];
[951]176                       
[946]177                        $conn = $this->ldapCatalog();
[382]178
[946]179                        if( $conn )
[382]180                        {
[946]181                                $filter = "(&(phpgwaccounttype=u)(".$search ."))";
182                                $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                               
[951]183                                $searchRoot = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
[1160]184                               
[951]185                                $search1 = @ldap_search($conn, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
[946]186                                $entry1 = @ldap_get_entries( $conn, $search1 );
187                                $result = $this->resultArray($entry1, $conn );
[1160]188                               
[946]189                                if( count($return) > 0 )
190                                $return = array_merge($return, $result);
191                                else
192                                        $return = $result;                             
[382]193
[946]194                        unset($result);
[382]195
[946]196                        ldap_close($conn);
[382]197                        }
198                }
[946]199               
200                return $return;
[382]201        }
[946]202
[988]203        public final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
[417]204        {
[946]205                $this->ldapRoot();
206                $result = array();
[519]207
208                if( $this->ldap )
209                {
[946]210                        $searchRoot = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
211                        $filter = "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))";
212                        $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                               
213                        $search = ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
214                        $entry = ldap_get_entries( $this->ldap, $search );
[519]215
[946]216                        $result = $this->resultArray($entry, $this->ldap );
217                }               
[519]218
[946]219                return $result;
[417]220        }
[563]221       
[946]222        private final function resultArray($pArray, $pConn)
[563]223        {
[946]224                $entry = $pArray;
225                $result = array();
226
227                $j = 0;
228                for($i = 0 ; $i < $entry['count']; $i++)
[563]229                {
[946]230                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
231                        {
232                                $result[$j]['uidnumber'] = $entry[$i]['uidnumber'][0];                 
233                                $result[$j]['mail']     =  $entry[$i]['mail'][0];
234                                $result[$j]['uid']      =  $entry[$i]['uid'][0];
235                                $result[$j]['jid']      = $entry[$i]['uid'][0];
236                                $ou = explode("dc=", $entry[$i]['dn']);
237                                $ou = explode("ou=",$ou[0]);
238                                $ou = array_pop($ou);
239                                $result[$j]['ou']       = strtoupper(substr($ou,0,strlen($ou)-1));                                     
240                                if( $entry[$i]['jpegphoto'][0] )
241                                {
242                                        $result[$j]['photo'] = "1";
243                                        $filterPhoto = "(objectclass=*)";
244                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
245                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
246                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
247                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
248                                }
249                                else
250                                        $result[$j]['photo'] = "0";
[563]251
[946]252                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
[563]253                        }
[946]254               
255                        $organization = $this->attr_org;
256       
257                        if(is_array($organization))
258                        {
259                                foreach($organization as $attr)
260                                {
261                                        $tmp = explode(";",$attr);
262                                        if( strtolower(trim($tmp[0])) == strtolower(trim($result[$i]['dn'])) )
263                                        {
264                                                switch(strtolower(trim($tmp[1])))
265                                                {
266                                                        case "mail" :
267                                                                        $uid = $result[$i]['mail'];
268                                                                        $uid = substr($uid,0,strpos($uid,"@"));
269                                                                        $result[$i]['uid'] = $uid;
270                                                                        break;
271       
272                                                        case "description" :
273                                                                        // SERPRO
274                                                                        // parte antes do arroba;
275                                                                        $result[$i]['uid'] = $result[$i]['description'];                                                                               
276                                                                        break;
277                                                }
278                                        }
279                                }
280                        }
[563]281                }
[946]282                return $result;
[563]283        }
[382]284}
285
[519]286?>
Note: See TracBrowser for help on using the repository browser.