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

Revision 988, 8.4 KB checked in by niltonneto, 15 years ago (diff)

Ticket #505 - Correção de busca dos usuários da própria base de dados.

  • 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;
24        private $ldap_dn;
[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
54                $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_dn, $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'];
61                $this->ldap_dn          = (isset($_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
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
85                if($this->ldap_dn && $this->ldap_pass && !ldap_bind($conn, $this->ldap_dn, $this->ldap_pass))
86                        return false;
87               
88                // Bind as Anonymous
89                if(!$this->ldap_dn && !$this->ldap_pass && !@ldap_bind($conn))
90                        return false;
91                       
92                return $conn;
93        }
94
95        public final function getGroupsLdap($pOrg)
96        {
97                $this->ldapRoot();
98
99                if( $this->ldap )       
[551]100                {
[946]101                        $organization = 'ou=' . $pOrg['ou'] .",". $this->ldap_context;
102                        $filter = "(&(phpgwAccountType=g)(objectClass=posixGroup))";
103                        $justthese = array("cn","gidNumber");
104                        $search = ldap_list($this->ldap, $organization, $filter, $justthese);
105                        $entry = ldap_get_entries( $this->ldap, $search );
106
107                        if( $entry )
108                        {                                       
109                                $result_groups = "<ldap>";
110                                foreach($entry as $tmp)
111                                        if( $tmp['gidnumber'][0] != "" )
112                                                $result_groups .= "<org><cn>".$tmp['cn'][0]."</cn><gid>".$tmp['gidnumber'][0]."</gid></org>";
113                               
114                                $result_groups .= "</ldap>";                                           
115                        }
[551]116                }
[946]117
118                return $result_groups;
[551]119        }
120
[946]121        public final function getGroupsMemberUid($pGroup)
[697]122        {
[946]123                $this->ldapRoot();
[697]124               
125                if( $this->ldap )
126                {
127                        $filter = "(&(objectclass=posixgroup)(|".$pGroup."))";
128                        $justthese = array("dn","memberuid","gidnumber");
129                        $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese);
130                        $result = ldap_get_entries($this->ldap,$search);
131                       
132                        if( $result['count'] > 0 )
133                                return $result;
134                }               
135               
136                return false;
137        }
138
[946]139        public final function getOrganizationsLdap()
[551]140        {
[946]141                $this->ldapRoot();
142       
[551]143                if( $this->ldap )
144                {
[946]145                        $filter="ou=*";         
146                        $justthese = array("ou");
147                        $search = ldap_search($this->ldap,$this->ldap_context,$filter,$justthese);                     
148                        $entry = ldap_get_entries($this->ldap, $search);
149                }
[551]150
[946]151                foreach($entry as $tmp)
152                        if($tmp['ou'][0] != "")
153                                $result_org[] = $tmp['ou'][0]; 
[551]154
[946]155                return $result_org;
156        }
[551]157
158
[988]159        public final function getUsersLdapCatalog( $search )
[382]160        {
[946]161                $confHosts      = $this->hostsJabber;
162                $result = array();
163                $return = array();
164                $conn   = "";           
165
166                for($i = 0; $i < count($confHosts); $i++ )
[382]167                {
[946]168                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
169                        $this->ldap_context = $confHosts[$i]['contextLdap'];
170                        $this->ldap_dn          = $confHosts[$i]['user'];
[951]171                        $this->ldap_org         = $confHosts[$i]['org'];
[946]172                        $this->ldap_pass        = $confHosts[$i]['password'];
[951]173                       
[946]174                        $conn = $this->ldapCatalog();
[382]175
[946]176                        if( $conn )
[382]177                        {
[946]178                                $filter = "(&(phpgwaccounttype=u)(".$search ."))";
179                                $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                               
[951]180                                $searchRoot = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
181                                $search1 = @ldap_search($conn, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
[946]182                                $entry1 = @ldap_get_entries( $conn, $search1 );
183                                $result = $this->resultArray($entry1, $conn );
[382]184
[946]185                                if( count($return) > 0 )
186                                $return = array_merge($return, $result);
187                                else
188                                        $return = $result;                             
[382]189
[946]190                        unset($result);
[382]191
[946]192                        ldap_close($conn);
[382]193                        }
194                }
[946]195               
196                return $return;
[382]197        }
[946]198
[988]199        public final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
[417]200        {
[946]201                $this->ldapRoot();
202                $result = array();
[519]203
204                if( $this->ldap )
205                {
[946]206                        $searchRoot = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
207                        $filter = "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))";
208                        $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                               
209                        $search = ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
210                        $entry = ldap_get_entries( $this->ldap, $search );
[519]211
[946]212                        $result = $this->resultArray($entry, $this->ldap );
213                }               
[519]214
[946]215                return $result;
[417]216        }
[563]217       
[946]218        private final function resultArray($pArray, $pConn)
[563]219        {
[946]220                $entry = $pArray;
221                $result = array();
222
223                $j = 0;
224                for($i = 0 ; $i < $entry['count']; $i++)
[563]225                {
[946]226                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
227                        {
228                                $result[$j]['uidnumber'] = $entry[$i]['uidnumber'][0];                 
229                                $result[$j]['mail']     =  $entry[$i]['mail'][0];
230                                $result[$j]['uid']      =  $entry[$i]['uid'][0];
231                                $result[$j]['jid']      = $entry[$i]['uid'][0];
232                                $ou = explode("dc=", $entry[$i]['dn']);
233                                $ou = explode("ou=",$ou[0]);
234                                $ou = array_pop($ou);
235                                $result[$j]['ou']       = strtoupper(substr($ou,0,strlen($ou)-1));                                     
236                                if( $entry[$i]['jpegphoto'][0] )
237                                {
238                                        $result[$j]['photo'] = "1";
239                                        $filterPhoto = "(objectclass=*)";
240                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
241                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
242                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
243                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
244                                }
245                                else
246                                        $result[$j]['photo'] = "0";
[563]247
[946]248                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
[563]249                        }
[946]250               
251                        $organization = $this->attr_org;
252       
253                        if(is_array($organization))
254                        {
255                                foreach($organization as $attr)
256                                {
257                                        $tmp = explode(";",$attr);
258                                        if( strtolower(trim($tmp[0])) == strtolower(trim($result[$i]['dn'])) )
259                                        {
260                                                switch(strtolower(trim($tmp[1])))
261                                                {
262                                                        case "mail" :
263                                                                        $uid = $result[$i]['mail'];
264                                                                        $uid = substr($uid,0,strpos($uid,"@"));
265                                                                        $result[$i]['uid'] = $uid;
266                                                                        break;
267       
268                                                        case "description" :
269                                                                        // SERPRO
270                                                                        // parte antes do arroba;
271                                                                        $result[$i]['uid'] = $result[$i]['description'];                                                                               
272                                                                        break;
273                                                }
274                                        }
275                                }
276                        }
[563]277                }
[946]278                return $result;
[563]279        }
[382]280}
281
[519]282?>
Note: See TracBrowser for help on using the repository browser.