source: branches/2.2/jabberit_messenger/inc/class.ldap_im.inc.php @ 3266

Revision 3266, 14.0 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #1316 - Correcoes e melhorias na busca dos contatos para o Modulo IM.

  • 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
13if( is_dir('../phpgwapi/inc') )
14        define('PHPGW_API_INC','../phpgwapi/inc');
15else
16        define('PHPGW_API_INC','../../phpgwapi/inc');
17
18require_once( PHPGW_API_INC . '/class.common.inc.php');
19
20class ldap_im
21{
22        private $attribute;
23        private $common;
24        private $hostsJabber;
25        private $ldap;
26        private $ldap_context;
27        private $ldap_user;
28        private $ldap_host;
29        private $ldap_org;
30        private $ldap_pass;
31        private $max_result;
32       
33        public final function __construct()
34        {
35                $this->ldap_host        = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
36                $this->ldap_context     = $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'];
37                $this->ldap_user        = $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'];
38                $this->ldap_pass        = $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'];
39
40                // Hosts Jabber
41                $this->hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
42               
43                // Result Ldap
44                $this->max_result = 15;
45               
46                if ( file_exists('inc/attributeLdap.php') )
47                {
48                        require_once('attributeLdap.php');
49                        $this->attribute = trim($attributeTypeName);
50                }
51                else
52                        $this->attribute = "uid";
53        }
54
55        public final function __destruct()
56        {
57                if( $this->ldap )
58                        @ldap_close($this->ldap);
59        }       
60
61        private final function ldapConn()
62        {
63                $this->common = new common();           
64               
65                $GLOBALS['phpgw_info']['server']['ldap_version3'] = true;
66               
67                if( $this->ldap_user && $this->ldap_pass )
68                        $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_user . "," . $this->ldap_context , $this->ldap_pass, false );
69                else
70                        $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_context , "", false );
71        }
72       
73        private final function ldapRoot()
74        {
75                $this->ldap_host        = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
76                $this->ldap_context     = $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'];
77                $this->ldap_user        = $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'];
78                $this->ldap_pass        = $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'];
79               
80                $this->ldapConn();
81        }
82
83        private final function ldapCatalog()
84        {
85                $version3 = true;
86                $refer  = true;
87
88                if(!function_exists('ldap_connect'))
89                        return false;
90               
91                if(!$conn = ldap_connect($this->ldap_host))
92                        return false;
93
94                if( $version3 )
95                        if( !ldap_set_option($conn,LDAP_OPT_PROTOCOL_VERSION,3) )
96                                $version3 = false;
97
98                ldap_set_option($conn, LDAP_OPT_REFERRALS, $refer);
99
100                // Bind as Admin
101                if($this->ldap_user && $this->ldap_pass && !ldap_bind($conn, $this->ldap_user . "," .$this->ldap_context, $this->ldap_pass))
102                        return false;
103               
104                // Bind as Anonymous
105                if(!$this->ldap_user && !$this->ldap_pass && !@ldap_bind($conn))
106                        return false;
107
108                return $conn;
109        }
110
111        private function getLdapHost()
112        {
113                return  $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
114        }
115
116        public final function getGroupsLdap($pData)
117        {
118                $result_groups = "";
119               
120                if( $pData['serverLdap'] == $this->ldap_host || $pData['serverLdap'] == 'localhost' )
121                {
122                        $this->ldapRoot();
123                }
124                else
125                {
126                        $confHosts      = $this->hostsJabber;
127                       
128                        for($i = 0; $i < count($confHosts); $i++ )
129                        {
130                                if( $pData['serverLdap'] == $confHosts[$i]['serverLdap'] )
131                                {
132                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
133                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
134                                        $this->ldap_user        = $confHosts[$i]['user'];
135                                        $this->ldap_org         = $confHosts[$i]['org'];
136                                        $this->ldap_pass        = $confHosts[$i]['password'];
137                               
138                                        $this->ldap = $this->ldapCatalog();
139                                }
140                        }
141                }               
142
143                if( $this->ldap )       
144                {
145                        if( !$pData['search'] && $pData['ou'] != "-1" )
146                        {
147                                $filter = "(&(phpgwAccountType=g)(objectClass=posixGroup))";
148                                $justthese = array("cn","gidNumber");
149                                $search = ldap_list( $this->ldap, $pData['ou'] , $filter, $justthese );
150                                $entry = ldap_get_entries( $this->ldap, $search );
151                        }
152                       
153                        if( $pData['search'] )
154                        {
155                                $filter = "(&(phpgwAccountType=g)(&(objectClass=posixGroup)(cn=".$pData['search']."*)))";
156                                $justthese = array("cn","gidNumber");
157                                $search = ldap_search( $this->ldap, $this->ldap_context , $filter, $justthese );
158                                $entry = ldap_get_entries( $this->ldap, $search );
159                        }
160                       
161                        if( $entry && $entry['count'] > 0 )
162                        {                                       
163                                array_shift($entry);
164
165                                foreach($entry as $tmp)
166                                        $groups[] = $tmp['cn'][0]."/".$tmp['gidnumber'][0];
167                               
168                                natsort($groups);
169                               
170                                $result_groups = "<ldap>";
171                                foreach($groups as $gtmp)
172                                {
173                                        $tmp = explode("/",$gtmp);     
174                                        $result_groups .= "<org><cn>".$tmp[0]."</cn><gid>".$tmp[1]."</gid></org>";
175                                }
176                                $result_groups .= "</ldap>";
177                        }
178                }
179
180                return $result_groups;
181        }
182
183        public final function getMaxResults()
184        {
185                return $this->max_result;
186        }
187
188        public final function getMembers( $pMembers, $pServers )
189        {
190                $members = $pMembers;
191               
192                foreach( $pServers as $servers => $groups )
193                {
194                        if( $servers == $this->getLdapHost() || $servers == 'localhost')
195                        {
196                                $this->ldapRoot();
197                               
198                                $count = count($members[$servers]);
199                               
200                                for( $i = 0; $i < $count; $i++ )
201                                {
202                                        if ( ! $this->getMemberUid($groups, $members[$servers][$i]['uid'] ) )
203                                                unset( $members[$servers][$i] );
204                                }
205
206                                if( $this->ldap )
207                                        @ldap_close($this->ldap);
208                        }
209                        else
210                        {
211                                $confHosts      = $this->hostsJabber;
212                       
213                                for($i = 0; $i < count($confHosts); $i++ )
214                                {
215                                        if( $this->ldap )
216                                                @ldap_close($this->ldap);
217                                               
218                                        if( trim($servers) === trim($confHosts[$i]['serverLdap']) )
219                                        {
220                                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
221                                                $this->ldap_context = $confHosts[$i]['contextLdap'];
222                                                $this->ldap_user        = $confHosts[$i]['user'];
223                                                $this->ldap_org         = $confHosts[$i]['org'];
224                                                $this->ldap_pass        = $confHosts[$i]['password'];
225                                                $this->ldap             = $this->ldapCatalog();
226
227                                                $count = count($members[$servers]);
228                                               
229                                                for( $i = 0; $i < $count; $i++ )
230                                                {
231                                                        if ( ! $this->getMemberUid($groups, $members[$servers][$i]['uid'] ) )
232                                                                unset( $members[$servers][$i] );
233                                                }
234               
235                                                if( $this->ldap )
236                                                        @ldap_close($this->ldap);
237                                        }
238                                }
239                        }
240                }
241               
242                return $members;
243        }
244
245        private function getMemberUid( $pGidNumber, $pMemberUid )
246        {
247                $filter         = "(&(phpgwAccountType=g)(|{$pGidNumber})(memberuid={$pMemberUid}))";
248                $justthese      = array("memberuid");
249       
250                if( $this->ldap )
251                {
252                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese );
253                        $result = ldap_get_entries($this->ldap,$search);
254                        if( $result["count"] )
255                                return true;
256                }
257
258                return false;
259        }
260
261        public final function getGroupsMemberUid( $pGroup, $pLdap )
262        {
263                if( $pLdap == $this->ldap_host || $pLdap == 'localhost' )
264                {
265                        $this->ldapRoot();
266                       
267                        if( $this->ldap )
268                        {
269                                $filter = "(&(objectclass=posixgroup)(|".$pGroup."))";
270                                if( strpos($pGroup, "gidnumber") === false )
271                                        $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))";
272                                       
273                                $justthese = array("dn","memberuid","gidnumber");
274                                $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
275                                $result = ldap_get_entries($this->ldap,$search);
276                        }
277                }
278                else
279                {
280                        $confHosts      = $this->hostsJabber;
281               
282                        for($i = 0; $i < count($confHosts); $i++ )
283                        {
284                                if( $this->ldap )
285                                        @ldap_close($this->ldap);
286                                       
287                                if( trim($pLdap) === trim($confHosts[$i]['serverLdap']) )
288                                {
289                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
290                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
291                                        $this->ldap_user        = $confHosts[$i]['user'];
292                                        $this->ldap_org         = $confHosts[$i]['org'];
293                                        $this->ldap_pass        = $confHosts[$i]['password'];
294                                        $this->ldap = $this->ldapCatalog();
295                                       
296                                        if( $this->ldap )
297                                        {
298                                                $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))";
299                                                $justthese = array("dn","memberuid","gidnumber");
300                                                $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese);
301                                                $result = ldap_get_entries($this->ldap,$search);
302                                        }
303                                       
304                                }
305                        }
306                }
307
308                if( $result['count'] > 0 )
309                        return $result;
310
311                return false;
312        }
313
314        public final function getOrganizationsLdap($pLdap_host)
315        {
316
317                if( $pLdap_host == $this->ldap_host || $pLdap_host == 'localhost' )
318                {
319                        $this->ldapRoot();
320                }
321                else
322                {
323                        $confHosts      = $this->hostsJabber;
324                       
325                        for($i = 0; $i < count($confHosts); $i++ )
326                        {
327                                if( $pLdap_host == $confHosts[$i]['serverLdap'] )
328                                {
329                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
330                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
331                                        $this->ldap_user        = $confHosts[$i]['user'];
332                                        $this->ldap_org         = $confHosts[$i]['org'];
333                                        $this->ldap_pass        = $confHosts[$i]['password'];
334                               
335                                        $this->ldap = $this->ldapCatalog();
336                                }
337                        }
338                }
339               
340                if( $this->ldap )
341                {
342                        $filter = "(ou=*)";
343                        $justthese = array("dn");
344                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
345                        $info = ldap_get_entries($this->ldap, $search);
346               
347                        for ($i=0; $i<$info["count"]; $i++)
348                        {
349                                $a_sectors[] = $info[$i]['dn'];
350                        }       
351                }
352
353                // Retiro o count do array info e inverto o array para ordenação.
354                foreach ($a_sectors as $context)
355                {
356                        $array_dn = ldap_explode_dn ( $context, 1 );
357                        $array_dn_reverse  = array_reverse ( $array_dn, true );
358                        array_pop ( $array_dn_reverse );
359                        $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
360                }
361               
362                // Ordenação
363                natcasesort($inverted_dn);
364
365                foreach ( $inverted_dn as $dn=>$invert_ufn )
366                {
367            $display = '';
368
369            $array_dn_reverse = explode ( "#", $invert_ufn );
370            $array_dn  = array_reverse ( $array_dn_reverse, true );
371
372            $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1);
373
374            if ($level == 0)
375                    $display .= '+';
376            else
377            {
378                                for( $i = 0; $i < $level; $i++)
379                                        $display .= '---';
380            }
381
382            reset ( $array_dn );
383            $display .= ' ' . (current ( $array_dn ) );
384                       
385                        $dn = trim(strtolower($dn));
386                        $options[$dn] = $display;
387        }
388
389            return $options;
390
391        }
392
393        public final function getUsersLdapCatalog( $search, $pLdap = false, $uid = false )
394        {
395                $confHosts      = $this->hostsJabber;
396                $result = array();
397                $return = array();
398                $conn   = "";           
399
400                for( $i = 0; $i < count($confHosts); $i++ )
401                {
402                        if( $pLdap && $pLdap == $confHosts[$i]['serverLdap'] )
403                        {
404                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
405                                $this->ldap_context = $confHosts[$i]['contextLdap'];
406                                $this->ldap_user        = $confHosts[$i]['user'];
407                                $this->ldap_org         = $confHosts[$i]['org'];
408                                $this->ldap_pass        = $confHosts[$i]['password'];
409                                $this->ldap             = $this->ldapCatalog();
410
411                                if( $this->ldap )
412                                {
413                                        $filter         = ( $uid ) ? "(&(phpgwaccounttype=u)(|".$uid.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
414                                        $justthese      = array( $this->attribute ,"uidNumber" ,"cn" ,"mail" ,"phpgwAccountVisible" ,"dn" ,"jpegPhoto" );                                                               
415                                        $searchRoot     = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
416                                        $search1        = @ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
417                                        $entry1         = @ldap_get_entries( $this->ldap, $search1 );
418                                        $result         = $this->resultArray( $entry1, $this->ldap, $this->ldap_org );
419       
420                                        if( count($return) > 0 )
421                                        $return = array_merge($return, $result);
422                                        else
423                                                $return = $result;
424                                }
425                               
426                                if( $this->ldap )
427                                        ldap_close($this->ldap);
428                        }
429                }
430               
431                return $return;
432        }
433
434        public final function getUsersLdapRoot( $search, $uidnumber = false, $ous = false )
435        {
436                $result = array();
437                $this->ldapRoot();
438
439                if( $this->ldap )
440                {
441                        $searchRoot     = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
442                        $filter         = ($uidnumber) ? "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
443                        $justthese      = array( $this->attribute, "uidNumber", "cn", "mail", "phpgwAccountVisible", "dn", "jpegPhoto" );                                                               
444                        $search         = @ldap_search( $this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
445                        $entry          = @ldap_get_entries( $this->ldap, $search );
446                        $result         = $this->resultArray( $entry, $this->ldap );
447                }               
448
449                return $result;
450        }
451       
452        private final function resultArray($pArray, $pConn, $pOrg = false)
453        {
454                $entry  = $pArray;
455                $result = array();
456                $j              = 0;
457               
458                for( $i = 0 ; $i < $entry['count']; $i++ )
459                {
460                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
461                        {
462                                $result[$j]['uidnumber']        = $entry[$i]['uidnumber'][0];                   
463                                $result[$j]['mail']                     = $entry[$i]['mail'][0];
464                                $result[$j]['uid']                      = $entry[$i][$this->attribute][0];
465                                $result[$j]['jid']                      = $entry[$i][$this->attribute][0];
466                               
467                                $ou = explode("dc=", $entry[$i]['dn']);
468                                $ou = explode("ou=",$ou[0]);
469                                $ou = array_pop($ou);
470                                $result[$j]['ou']       = strtoupper(substr($ou,0,strlen($ou)-1));
471                               
472                                if( $pOrg === "*" )
473                                        $result[$j]['ouAll'] = "*";
474                                                                               
475                                if( $entry[$i]['jpegphoto'][0] )
476                                {
477                                        $result[$j]['photo'] = "1";
478                                        $filterPhoto = "(objectclass=*)";
479                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
480                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
481                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
482                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
483                                }
484                                else
485                                        $result[$j]['photo'] = "0";
486
487                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
488                        }
489               
490                        $organization = $this->attr_org;
491                }
492               
493                return $result;
494        }
495}
496
497?>
Note: See TracBrowser for help on using the repository browser.