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 *
Line 
1<?php
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  \***************************************************************************/
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{
19        private $attr_org;
20        private $common;
21        private $hostsJabber;
22        private $ldap;
23        private $ldap_context;
24        private $ldap_dn;
25        private $ldap_host;
26        private $ldap_org;
27        private $ldap_pass;
28        private $max_result;
29       
30        public final function __construct()
31        {
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;
40        }
41
42        public final function __destruct()
43        {
44                if( $this->ldap )
45                        ldap_close($this->ldap);
46        }       
47
48        private final function ldapConn()
49        {
50                $this->common = new common();           
51               
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 );
55        }
56       
57        private final function ldapRoot()
58        {
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'];
63
64                $this->ldapConn();
65        }
66
67        private final function ldapCatalog()
68        {
69                $version3 = true;
70                $refer  = true;
71
72                if(!function_exists('ldap_connect'))
73                        return false;
74               
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 )       
100                {
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                        }
116                }
117
118                return $result_groups;
119        }
120
121        public final function getGroupsMemberUid($pGroup)
122        {
123                $this->ldapRoot();
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
139        public final function getOrganizationsLdap()
140        {
141                $this->ldapRoot();
142       
143                if( $this->ldap )
144                {
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                }
150
151                foreach($entry as $tmp)
152                        if($tmp['ou'][0] != "")
153                                $result_org[] = $tmp['ou'][0]; 
154
155                return $result_org;
156        }
157
158
159        public final function getUsersLdapCatalog( $search )
160        {
161                $confHosts      = $this->hostsJabber;
162                $result = array();
163                $return = array();
164                $conn   = "";           
165
166                for($i = 0; $i < count($confHosts); $i++ )
167                {
168                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
169                        $this->ldap_context = $confHosts[$i]['contextLdap'];
170                        $this->ldap_dn          = $confHosts[$i]['user'];
171                        $this->ldap_org         = $confHosts[$i]['org'];
172                        $this->ldap_pass        = $confHosts[$i]['password'];
173                       
174                        $conn = $this->ldapCatalog();
175
176                        if( $conn )
177                        {
178                                $filter = "(&(phpgwaccounttype=u)(".$search ."))";
179                                $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                               
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);
182                                $entry1 = @ldap_get_entries( $conn, $search1 );
183                                $result = $this->resultArray($entry1, $conn );
184
185                                if( count($return) > 0 )
186                                $return = array_merge($return, $result);
187                                else
188                                        $return = $result;                             
189
190                        unset($result);
191
192                        ldap_close($conn);
193                        }
194                }
195               
196                return $return;
197        }
198
199        public final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
200        {
201                $this->ldapRoot();
202                $result = array();
203
204                if( $this->ldap )
205                {
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 );
211
212                        $result = $this->resultArray($entry, $this->ldap );
213                }               
214
215                return $result;
216        }
217       
218        private final function resultArray($pArray, $pConn)
219        {
220                $entry = $pArray;
221                $result = array();
222
223                $j = 0;
224                for($i = 0 ; $i < $entry['count']; $i++)
225                {
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";
247
248                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
249                        }
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                        }
277                }
278                return $result;
279        }
280}
281
282?>
Note: See TracBrowser for help on using the repository browser.