source: branches/2.2/jabberit_messenger/jmessenger/inc/class.LdapIM.inc.php @ 3260

Revision 3260, 15.6 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 LdapIM
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 getPhotoUser( $_uid )
394        {
395                $uid    = substr($_uid, 0, strpos($_uid, "@"));
396                $host   = substr($_uid, (strpos($_uid, "@") + 1));
397               
398                if( count($this->hostsJabber) )
399                {
400                        $confHosts      = $this->hostsJabber;   
401
402                        for( $i = 0; $i < count($confHosts); $i++ )
403                        {
404                                if( trim($host) === trim($confHosts[$i]['jabberName']) )
405                                {
406                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
407                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
408                                        $this->ldap_user        = $confHosts[$i]['user'];
409                                        $this->ldap_org         = $confHosts[$i]['org'];
410                                        $this->ldap_pass        = $confHosts[$i]['password'];
411                                        $this->ldap             = $this->ldapCatalog();
412                                }
413                        }
414                       
415                        if( !$this->ldap )
416                                $this->ldapRoot();                                                             
417                }
418                else
419                {
420                        $this->ldapRoot();
421                }
422
423                if( $this->ldap )
424                {
425                        $filter     = "(&(phpgwaccounttype=u)(".$this->attribute."=".$uid."))";
426                        $justthese      = array($this->attribute, "uidNumber", "phpgwAccontVisible", "dn", "jpegPhoto");
427                        $search         = ldap_search( $this->ldap, $this->ldap_context, $filter, $justthese, 0, $this->max_result + 1);
428                        $entry          = ldap_get_entries( $this->ldap, $search );
429                       
430                        for( $i = 0 ; $i < $entry['count']; $i++ )
431                        {
432                                if( $entry[$i]['jpegphoto'][0] && $entry[$i]['phpgwaccountvisible'][0] != '-1' )
433                                {
434                                        $filterPhoto    = "(objectclass=*)";
435                                        $photoLdap              = ldap_read($this->ldap, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
436                                        $firstEntry     = ldap_first_entry($this->ldap, $photoLdap);
437                                        $photo                  = ldap_get_values_len($this->ldap, $firstEntry, "jpegPhoto");
438
439                                        if( $this->ldap )
440                                                ldap_close($this->ldap);
441
442                                        return $photo[0];               
443                                }
444                        }
445                }
446
447                return false;
448        }
449
450        public final function getUsersLdapCatalog( $search, $pLdap = false, $uid = false )
451        {
452                $confHosts      = $this->hostsJabber;
453                $result = array();
454                $return = array();
455                $conn   = "";           
456
457                for( $i = 0; $i < count($confHosts); $i++ )
458                {
459                        if( $pLdap && $pLdap == $confHosts[$i]['serverLdap'] )
460                        {
461                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
462                                $this->ldap_context = $confHosts[$i]['contextLdap'];
463                                $this->ldap_user        = $confHosts[$i]['user'];
464                                $this->ldap_org         = $confHosts[$i]['org'];
465                                $this->ldap_pass        = $confHosts[$i]['password'];
466                                $this->ldap             = $this->ldapCatalog();
467
468                                if( $this->ldap )
469                                {
470                                        $filter         = ( $uid ) ? "(&(phpgwaccounttype=u)(|".$uid.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
471                                        $justthese      = array( $this->attribute ,"uidNumber" ,"cn" ,"mail" ,"phpgwAccountVisible" ,"dn" ,"jpegPhoto" );                                                               
472                                        $searchRoot     = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
473                                        $search1        = @ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
474                                        $entry1         = @ldap_get_entries( $this->ldap, $search1 );
475                                        $result         = $this->resultArray( $entry1, $this->ldap, $this->ldap_org );
476       
477                                        if( count($return) > 0 )
478                                        $return = array_merge($return, $result);
479                                        else
480                                                $return = $result;
481                                }
482                               
483                                if( $this->ldap )
484                                        ldap_close($this->ldap);
485                        }
486                }
487               
488                return $return;
489        }
490
491        public final function getUsersLdapRoot( $search, $uidnumber = false, $ous = false )
492        {
493               
494                $result = array();
495                $this->ldapRoot();
496
497                if( $this->ldap )
498                {
499                        $searchRoot     = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
500                        $filter         = ($uidnumber) ? "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
501                        $justthese      = array( $this->attribute, "uidNumber", "cn", "mail", "phpgwAccountVisible", "dn", "jpegPhoto" );                                                               
502                        $search         = @ldap_search( $this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
503                        $entry          = @ldap_get_entries( $this->ldap, $search );
504                        $result         = $this->resultArray( $entry, $this->ldap );
505                }               
506
507                return $result;
508        }
509       
510        private final function resultArray($pArray, $pConn, $pOrg = false)
511        {
512                $entry  = $pArray;
513                $result = array();
514                $j              = 0;
515               
516                for( $i = 0 ; $i < $entry['count']; $i++ )
517                {
518                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
519                        {
520                                $result[$j]['uidnumber']        = $entry[$i]['uidnumber'][0];                   
521                                $result[$j]['mail']                     = $entry[$i]['mail'][0];
522                                $result[$j]['uid']                      = $entry[$i][$this->attribute][0];
523                                $result[$j]['jid']                      = $entry[$i][$this->attribute][0];
524                               
525                                $ou = explode("dc=", $entry[$i]['dn']);
526                                $ou = explode("ou=",$ou[0]);
527                                $ou = array_pop($ou);
528                                $result[$j]['ou']       = strtoupper(substr($ou,0,strlen($ou)-1));
529                               
530                                if( $pOrg === "*" )
531                                        $result[$j]['ouAll'] = "*";
532                                                                               
533                                if( $entry[$i]['jpegphoto'][0] )
534                                {
535                                        $result[$j]['photo'] = "1";
536                                        $filterPhoto = "(objectclass=*)";
537                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
538                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
539                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
540                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
541                                }
542                                else
543                                        $result[$j]['photo'] = "0";
544
545                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
546                        }
547               
548                        $organization = $this->attr_org;
549                }
550               
551                return $result;
552        }
553}
554
555?>
Note: See TracBrowser for help on using the repository browser.