source: trunk/jabberit_messenger/jmessenger/inc/class.LdapIM.inc.php @ 7655

Revision 7655, 15.6 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

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