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

Revision 7673, 15.8 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • 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            $confHosts_count = count($confHosts);
128                        for($i = 0; $i < $confHosts_count; ++$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                $confHosts_count = count($confHosts);
214                                for($i = 0; $i < $confHosts_count; ++$i )
215                                {
216                                        if( $this->ldap )
217                                                @ldap_close($this->ldap);
218                                               
219                                        if( trim($servers) === trim($confHosts[$i]['serverLdap']) )
220                                        {
221                                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
222                                                $this->ldap_context = $confHosts[$i]['contextLdap'];
223                                                $this->ldap_user        = $confHosts[$i]['user'];
224                                                $this->ldap_org         = $confHosts[$i]['org'];
225                                                $this->ldap_pass        = $confHosts[$i]['password'];
226                                                $this->ldap             = $this->ldapCatalog();
227
228                                                $count = count($members[$servers]);
229                                               
230                                                for( $i = 0; $i < $count; $i++ )
231                                                {
232                                                        if ( ! $this->getMemberUid($groups, $members[$servers][$i]['uid'] ) )
233                                                                unset( $members[$servers][$i] );
234                                                }
235               
236                                                if( $this->ldap )
237                                                        @ldap_close($this->ldap);
238                                        }
239                                }
240                        }
241                }
242               
243                return $members;
244        }
245
246        private function getMemberUid( $pGidNumber, $pMemberUid )
247        {
248                $filter         = "(&(phpgwAccountType=g)(|{$pGidNumber})(memberuid={$pMemberUid}))";
249                $justthese      = array("memberuid");
250       
251                if( $this->ldap )
252                {
253                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese );
254                        $result = ldap_get_entries($this->ldap,$search);
255                        if( $result["count"] )
256                                return true;
257                }
258
259                return false;
260        }
261
262        public final function getGroupsMemberUid( $pGroup, $pLdap )
263        {
264                if( $pLdap == $this->ldap_host || $pLdap == 'localhost' )
265                {
266                        $this->ldapRoot();
267                       
268                        if( $this->ldap )
269                        {
270                                $filter = "(&(objectclass=posixgroup)(|".$pGroup."))";
271                                if( strpos($pGroup, "gidnumber") === false )
272                                        $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))";
273                                       
274                                $justthese = array("dn","memberuid","gidnumber");
275                                $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
276                                $result = ldap_get_entries($this->ldap,$search);
277                        }
278                }
279                else
280                {
281                        $confHosts      = $this->hostsJabber;
282
283            $confHosts_count = count($confHosts);
284                        for($i = 0; $i < $confHosts_count; ++$i )
285                        {
286                                if( $this->ldap )
287                                        @ldap_close($this->ldap);
288                                       
289                                if( trim($pLdap) === trim($confHosts[$i]['serverLdap']) )
290                                {
291                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
292                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
293                                        $this->ldap_user        = $confHosts[$i]['user'];
294                                        $this->ldap_org         = $confHosts[$i]['org'];
295                                        $this->ldap_pass        = $confHosts[$i]['password'];
296                                        $this->ldap = $this->ldapCatalog();
297                                       
298                                        if( $this->ldap )
299                                        {
300                                                $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))";
301                                                $justthese = array("dn","memberuid","gidnumber");
302                                                $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese);
303                                                $result = ldap_get_entries($this->ldap,$search);
304                                        }
305                                       
306                                }
307                        }
308                }
309
310                if( $result['count'] > 0 )
311                        return $result;
312
313                return false;
314        }
315
316        public final function getOrganizationsLdap($pLdap_host)
317        {
318
319                if( $pLdap_host == $this->ldap_host || $pLdap_host == 'localhost' )
320                {
321                        $this->ldapRoot();
322                }
323                else
324                {
325                        $confHosts      = $this->hostsJabber;
326
327            $confHosts_count = count($confHosts);
328                        for($i = 0; $i < $confHosts_count; ++$i )
329                        {
330                                if( $pLdap_host == $confHosts[$i]['serverLdap'] )
331                                {
332                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
333                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
334                                        $this->ldap_user        = $confHosts[$i]['user'];
335                                        $this->ldap_org         = $confHosts[$i]['org'];
336                                        $this->ldap_pass        = $confHosts[$i]['password'];
337                               
338                                        $this->ldap = $this->ldapCatalog();
339                                }
340                        }
341                }
342               
343                if( $this->ldap )
344                {
345                        $filter = "(objectClass=organizationalUnit)";
346                        $justthese = array("dn");
347                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
348                        $info = ldap_get_entries($this->ldap, $search);
349               
350                        for ($i=0; $i<$info["count"]; ++$i)
351                        {
352                                $a_sectors[] = $info[$i]['dn'];
353                        }       
354                }
355
356                // Retiro o count do array info e inverto o array para ordenação.
357                foreach ($a_sectors as $context)
358                {
359                        $array_dn = ldap_explode_dn ( $context, 1 );
360                        $array_dn_reverse  = array_reverse ( $array_dn, true );
361                        array_pop ( $array_dn_reverse );
362                        $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
363                }
364               
365                // Ordenação
366                natcasesort($inverted_dn);
367
368                foreach ( $inverted_dn as $dn=>$invert_ufn )
369                {
370            $display = '';
371
372            $array_dn_reverse = explode ( "#", $invert_ufn );
373            $array_dn  = array_reverse ( $array_dn_reverse, true );
374
375            $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1);
376
377            if ($level == 0)
378                    $display .= '+';
379            else
380            {
381                                for( $i = 0; $i < $level; ++$i)
382                                        $display .= '---';
383            }
384
385            reset ( $array_dn );
386            $display .= ' ' . (current ( $array_dn ) );
387                       
388                        $dn = trim(strtolower($dn));
389                        $options[$dn] = $display;
390        }
391
392            return $options;
393
394        }
395
396        public final function getPhotoUser( $_uid )
397        {
398                $uid    = substr($_uid, 0, strpos($_uid, "@"));
399                $host   = substr($_uid, (strpos($_uid, "@") + 1));
400               
401                if( count($this->hostsJabber) )
402                {
403                        $confHosts      = $this->hostsJabber;   
404
405            $confHosts_count = count($confHosts);
406                        for( $i = 0; $i < $confHosts_count; ++$i )
407                        {
408                                if( trim($host) === trim($confHosts[$i]['jabberName']) )
409                                {
410                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
411                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
412                                        $this->ldap_user        = $confHosts[$i]['user'];
413                                        $this->ldap_org         = $confHosts[$i]['org'];
414                                        $this->ldap_pass        = $confHosts[$i]['password'];
415                                        $this->ldap             = $this->ldapCatalog();
416                                }
417                        }
418                       
419                        if( !$this->ldap )
420                                $this->ldapRoot();                                                             
421                }
422                else
423                {
424                        $this->ldapRoot();
425                }
426
427                if( $this->ldap )
428                {
429                        $filter     = "(&(phpgwaccounttype=u)(".$this->attribute."=".$uid."))";
430                        $justthese      = array($this->attribute, "uidNumber", "phpgwAccontVisible", "dn", "jpegPhoto");
431                        $search         = ldap_search( $this->ldap, $this->ldap_context, $filter, $justthese, 0, $this->max_result + 1);
432                        $entry          = ldap_get_entries( $this->ldap, $search );
433                       
434                        for( $i = 0 ; $i < $entry['count']; ++$i )
435                        {
436                                if( $entry[$i]['jpegphoto'][0] && $entry[$i]['phpgwaccountvisible'][0] != '-1' )
437                                {
438                                        $filterPhoto    = "(objectclass=*)";
439                                        $photoLdap              = ldap_read($this->ldap, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
440                                        $firstEntry     = ldap_first_entry($this->ldap, $photoLdap);
441                                        $photo                  = ldap_get_values_len($this->ldap, $firstEntry, "jpegPhoto");
442
443                                        if( $this->ldap )
444                                                ldap_close($this->ldap);
445
446                                        return $photo[0];               
447                                }
448                        }
449                }
450
451                return false;
452        }
453
454        public final function getUsersLdapCatalog( $search, $pLdap = false, $uid = false )
455        {
456                $confHosts      = $this->hostsJabber;
457                $result = array();
458                $return = array();
459                $conn   = "";
460
461        $confHosts_count = count($confHosts);
462                for( $i = 0; $i < $confHosts_count; ++$i )
463                {
464                        if( $pLdap && $pLdap == $confHosts[$i]['serverLdap'] )
465                        {
466                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
467                                $this->ldap_context = $confHosts[$i]['contextLdap'];
468                                $this->ldap_user        = $confHosts[$i]['user'];
469                                $this->ldap_org         = $confHosts[$i]['org'];
470                                $this->ldap_pass        = $confHosts[$i]['password'];
471                                $this->ldap             = $this->ldapCatalog();
472
473                                if( $this->ldap )
474                                {
475                                        $filter         = ( $uid ) ? "(&(phpgwaccounttype=u)(|".$uid.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
476                                        $justthese      = array( $this->attribute ,"uidNumber" ,"cn" ,"mail" ,"phpgwAccountVisible" ,"dn" ,"jpegPhoto" );                                                               
477                                        $searchRoot     = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
478                                        $search1        = @ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
479                                        $entry1         = @ldap_get_entries( $this->ldap, $search1 );
480                                        $result         = $this->resultArray( $entry1, $this->ldap, $this->ldap_org );
481       
482                                        if( count($return) > 0 )
483                                        $return = array_merge($return, $result);
484                                        else
485                                                $return = $result;
486                                }
487                               
488                                if( $this->ldap )
489                                        ldap_close($this->ldap);
490                        }
491                }
492               
493                return $return;
494        }
495
496        public final function getUsersLdapRoot( $search, $uidnumber = false, $ous = false )
497        {
498               
499                $result = array();
500                $this->ldapRoot();
501
502                if( $this->ldap )
503                {
504                        $searchRoot     = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
505                        $filter         = ($uidnumber) ? "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
506                        $justthese      = array( $this->attribute, "uidNumber", "cn", "mail", "phpgwAccountVisible", "dn", "jpegPhoto" );                                                               
507                        $search         = @ldap_search( $this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
508                        $entry          = @ldap_get_entries( $this->ldap, $search );
509                        $result         = $this->resultArray( $entry, $this->ldap );
510                }               
511
512                return $result;
513        }
514       
515        private final function resultArray($pArray, $pConn, $pOrg = false)
516        {
517                $entry  = $pArray;
518                $result = array();
519                $j              = 0;
520               
521                for( $i = 0 ; $i < $entry['count']; ++$i )
522                {
523                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
524                        {
525                                $result[$j]['uidnumber']        = $entry[$i]['uidnumber'][0];                   
526                                $result[$j]['mail']                     = $entry[$i]['mail'][0];
527                                $result[$j]['uid']                      = $entry[$i][$this->attribute][0];
528                                $result[$j]['jid']                      = $entry[$i][$this->attribute][0];
529                               
530                                $ou = explode("dc=", $entry[$i]['dn']);
531                                $ou = explode("ou=",$ou[0]);
532                                $ou = array_pop($ou);
533                                $result[$j]['ou']       = strtoupper(substr($ou,0,strlen($ou)-1));
534                               
535                                if( $pOrg === "*" )
536                                        $result[$j]['ouAll'] = "*";
537                                                                               
538                                if( $entry[$i]['jpegphoto'][0] )
539                                {
540                                        $result[$j]['photo'] = "1";
541                                        $filterPhoto = "(objectclass=*)";
542                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
543                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
544                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
545                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
546                                }
547                                else
548                                        $result[$j]['photo'] = "0";
549
550                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
551                        }
552               
553                        $organization = $this->attr_org;
554                }
555               
556                return $result;
557        }
558}
559
560?>
Note: See TracBrowser for help on using the repository browser.