source: trunk/jabberit_messenger/inc/class.ldap_im.inc.php @ 7673

Revision 7673, 14.4 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  *     - 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            $confHosts_count = count($confHosts);
129                        for($i = 0; $i < $confHosts_count; ++$i )
130                        {
131                                if( $pData['serverLdap'] == $confHosts[$i]['serverLdap'] )
132                                {
133                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
134                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
135                                        $this->ldap_user        = $confHosts[$i]['user'];
136                                        $this->ldap_org         = $confHosts[$i]['org'];
137                                        $this->ldap_pass        = $confHosts[$i]['password'];
138                               
139                                        $this->ldap = $this->ldapCatalog();
140                                }
141                        }
142                }               
143
144                if( $this->ldap )       
145                {
146                        if( !$pData['search'] && $pData['ou'] != "-1" )
147                        {
148                                $filter = "(&(phpgwAccountType=g)(objectClass=posixGroup))";
149                                $justthese = array("cn","gidNumber");
150                                $search = ldap_list( $this->ldap, $pData['ou'] , $filter, $justthese );
151                                $entry = ldap_get_entries( $this->ldap, $search );
152                        }
153                       
154                        if( $pData['search'] )
155                        {
156                                $filter = "(&(phpgwAccountType=g)(&(objectClass=posixGroup)(cn=".$pData['search']."*)))";
157                                $justthese = array("cn","gidNumber");
158                                $search = ldap_search( $this->ldap, $this->ldap_context , $filter, $justthese );
159                                $entry = ldap_get_entries( $this->ldap, $search );
160                        }
161                       
162                        if( $entry && $entry['count'] > 0 )
163                        {                                       
164                                array_shift($entry);
165
166                                foreach($entry as $tmp)
167                                        $groups[] = $tmp['cn'][0]."/".$tmp['gidnumber'][0];
168                               
169                                natsort($groups);
170                               
171                                $result_groups = "<ldap>";
172                                foreach($groups as $gtmp)
173                                {
174                                        $tmp = explode("/",$gtmp);     
175                                        $result_groups .= "<org><cn>".$tmp[0]."</cn><gid>".$tmp[1]."</gid></org>";
176                                }
177                                $result_groups .= "</ldap>";
178                        }
179                }
180
181                return $result_groups;
182        }
183
184        public final function getMaxResults()
185        {
186                return $this->max_result;
187        }
188
189        public final function getMembers( $pMembers, $pServers )
190        {
191                $members = $pMembers;
192               
193                foreach( $pServers as $servers => $groups )
194                {
195                        if( $servers == $this->getLdapHost() || $servers == 'localhost')
196                        {
197                                $this->ldapRoot();
198                               
199                                $count = count($members[$servers]);
200                               
201                                for( $i = 0; $i < $count; ++$i )
202                                {
203                                        if ( ! $this->getMemberUid($groups, $members[$servers][$i]['uid'] ) )
204                                                unset( $members[$servers][$i] );
205                                }
206
207                                if( $this->ldap )
208                                        @ldap_close($this->ldap);
209                        }
210                        else
211                        {
212                                $confHosts      = $this->hostsJabber;
213
214                $confHosts_count = count($confHosts);
215                                for($i = 0; $i < $confHosts_count; ++$i )
216                                {
217                                        if( $this->ldap )
218                                                @ldap_close($this->ldap);
219                                               
220                                        if( trim($servers) === trim($confHosts[$i]['serverLdap']) )
221                                        {
222                                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
223                                                $this->ldap_context = $confHosts[$i]['contextLdap'];
224                                                $this->ldap_user        = $confHosts[$i]['user'];
225                                                $this->ldap_org         = $confHosts[$i]['org'];
226                                                $this->ldap_pass        = $confHosts[$i]['password'];
227                                                $this->ldap             = $this->ldapCatalog();
228
229                                                $count = count($members[$servers]);
230                                               
231                                                for( $i = 0; $i < $count; ++$i )
232                                                {
233                                                        if ( ! $this->getMemberUid($groups, $members[$servers][$i]['uid'] ) )
234                                                                unset( $members[$servers][$i] );
235                                                }
236               
237                                                if( $this->ldap )
238                                                        @ldap_close($this->ldap);
239                                        }
240                                }
241                        }
242                }
243               
244                return $members;
245        }
246
247        private function getMemberUid( $pGidNumber, $pMemberUid )
248        {
249                $filter         = "(&(phpgwAccountType=g)(|{$pGidNumber})(memberuid={$pMemberUid}))";
250                $justthese      = array("memberuid");
251       
252                if( $this->ldap )
253                {
254                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese );
255                        $result = ldap_get_entries($this->ldap,$search);
256                        if( $result["count"] )
257                                return true;
258                }
259
260                return false;
261        }
262
263        public final function getGroupsMemberUid( $pGroup, $pLdap )
264        {
265                if( $pLdap == $this->ldap_host || $pLdap == 'localhost' )
266                {
267                        $this->ldapRoot();
268                       
269                        if( $this->ldap )
270                        {
271                                $filter = "(&(objectclass=posixgroup)(|".$pGroup."))";
272                                if( strpos($pGroup, "gidnumber") === false )
273                                        $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))";
274                                       
275                                $justthese = array("dn","memberuid","gidnumber");
276                                $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
277                                $result = ldap_get_entries($this->ldap,$search);
278                        }
279                }
280                else
281                {
282                        $confHosts      = $this->hostsJabber;
283
284            $confHosts_count = count($confHosts);
285                        for($i = 0; $i < $confHosts_count; ++$i )
286                        {
287                                if( $this->ldap )
288                                        @ldap_close($this->ldap);
289                                       
290                                if( trim($pLdap) === trim($confHosts[$i]['serverLdap']) )
291                                {
292                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
293                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
294                                        $this->ldap_user        = $confHosts[$i]['user'];
295                                        $this->ldap_org         = $confHosts[$i]['org'];
296                                        $this->ldap_pass        = $confHosts[$i]['password'];
297                                        $this->ldap = $this->ldapCatalog();
298                                       
299                                        if( $this->ldap )
300                                        {
301                                                $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))";
302                                                $justthese = array("dn","memberuid","gidnumber");
303                                                $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese);
304                                                $result = ldap_get_entries($this->ldap,$search);
305                                        }
306                                       
307                                }
308                        }
309                }
310
311                if( $result['count'] > 0 )
312                        return $result;
313
314                return false;
315        }
316
317        public final function getOrganizationsLdap($pLdap_host)
318        {
319
320                if( $pLdap_host == $this->ldap_host || $pLdap_host == 'localhost' )
321                {
322                        $this->ldapRoot();
323                }
324                else
325                {
326                        $confHosts      = $this->hostsJabber;
327
328            $confHosts_count = count($confHosts);
329                        for($i = 0; $i < $confHosts_count; ++$i )
330                        {
331                                if( $pLdap_host == $confHosts[$i]['serverLdap'] )
332                                {
333                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
334                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
335                                        $this->ldap_user        = $confHosts[$i]['user'];
336                                        $this->ldap_org         = $confHosts[$i]['org'];
337                                        $this->ldap_pass        = $confHosts[$i]['password'];
338                               
339                                        $this->ldap = $this->ldapCatalog();
340                                }
341                        }
342                }
343               
344                if( $this->ldap )
345                {
346                        $filter = "(objectClass=organizationalUnit)";
347                        $justthese = array("dn");
348                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
349                        $info = ldap_get_entries($this->ldap, $search);
350               
351                        for ($i=0; $i<$info["count"]; ++$i)
352                        {
353                                $a_sectors[] = $info[$i]['dn'];
354                        }       
355                }
356
357                // Retiro o count do array info e inverto o array para ordenação.
358                foreach ($a_sectors as $context)
359                {
360                        $array_dn = ldap_explode_dn ( $context, 1 );
361                        $array_dn_reverse  = array_reverse ( $array_dn, true );
362                        array_pop ( $array_dn_reverse );
363                        $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
364                }
365               
366                // Ordenação
367                natcasesort($inverted_dn);
368
369                foreach ( $inverted_dn as $dn=>$invert_ufn )
370                {
371            $display = '';
372
373            $array_dn_reverse = explode ( "#", $invert_ufn );
374            $array_dn  = array_reverse ( $array_dn_reverse, true );
375
376            $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1);
377
378            if ($level == 0)
379                    $display .= '+';
380            else
381            {
382                                for( $i = 0; $i < $level; ++$i)
383                                        $display .= '---';
384            }
385
386            reset ( $array_dn );
387            $display .= ' ' . (current ( $array_dn ) );
388                       
389                        $dn = trim(strtolower($dn));
390                        $options[$dn] = $display;
391        }
392
393            return $options;
394
395        }
396
397        public final function getUsersLdapCatalog( $search, $pLdap = false, $uid = false )
398        {
399                $confHosts      = $this->hostsJabber;
400                $result = array();
401                $return = array();
402                $conn   = "";
403
404        $confHosts_count = count($confHosts);
405                for( $i = 0; $i < $confHosts_count; ++$i )
406                {
407                        if( $pLdap && $pLdap == $confHosts[$i]['serverLdap'] )
408                        {
409                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
410                                $this->ldap_context = $confHosts[$i]['contextLdap'];
411                                $this->ldap_user        = $confHosts[$i]['user'];
412                                $this->ldap_org         = $confHosts[$i]['org'];
413                                $this->ldap_pass        = $confHosts[$i]['password'];
414                                $this->ldap             = $this->ldapCatalog();
415
416                                if( $this->ldap )
417                                {
418                                        $filter         = ( $uid ) ? "(&(phpgwaccounttype=u)(|".$uid.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
419                                        $justthese      = array( $this->attribute ,"uidNumber" ,"cn" ,"mail" ,"phpgwAccountVisible" ,"dn" ,"jpegPhoto" );                                                               
420                                        $searchRoot     = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
421                                        $search1        = @ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
422                                        $entry1         = @ldap_get_entries( $this->ldap, $search1 );
423                                        $result         = $this->resultArray( $entry1, $this->ldap, $this->ldap_org );
424       
425                                        if( count($return) > 0 )
426                                        $return = array_merge($return, $result);
427                                        else
428                                                $return = $result;
429                                }
430                               
431                                if( $this->ldap )
432                                        ldap_close($this->ldap);
433                        }
434                }
435               
436                return $return;
437        }
438
439        public final function getUsersLdapRoot( $search, $uidnumber = false, $ous = false )
440        {
441                $result = array();
442                $this->ldapRoot();
443
444                if( $this->ldap )
445                {
446                        $searchRoot     = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
447                        $filter         = ($uidnumber) ? "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
448                        $justthese      = array( $this->attribute, "uidNumber", "cn", "mail", "phpgwAccountVisible", "dn", "jpegPhoto" );                                                               
449                        $search         = @ldap_search( $this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
450                        $entry          = @ldap_get_entries( $this->ldap, $search );
451                        $result         = $this->resultArray( $entry, $this->ldap );
452                }               
453
454                return $result;
455        }
456       
457        private final function resultArray($pArray, $pConn, $pOrg = false)
458        {
459                $entry  = $pArray;
460                $result = array();
461                $j              = 0;
462               
463                for( $i = 0 ; $i < $entry['count']; ++$i )
464                {
465                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
466                        {
467                                $result[$j]['uidnumber']        = $entry[$i]['uidnumber'][0];                   
468                                $result[$j]['mail']                     = $entry[$i]['mail'][0];
469                                $result[$j]['uid']                      = $entry[$i][$this->attribute][0];
470                                $result[$j]['jid']                      = $entry[$i][$this->attribute][0];
471
472                                if(!strpos($entry[$i]['dn'],"ou=") === FALSE){
473                                    $ou = explode("dc=", $entry[$i]['dn']);
474                                    $ou = explode("ou=",$ou[0]);
475                                    $ou = array_pop($ou);
476                                    $result[$j]['ou']   = strtoupper(substr($ou,0,strlen($ou)-1));
477                                }else{
478                                    $ou = explode("dc=", $entry[$i]['dn']);
479                                    $result[$j]['ou'] = strtoupper(substr($ou[1],0,strlen($ou[1])-1));
480                                }
481                               
482                                if( $pOrg === "*" )
483                                        $result[$j]['ouAll'] = "*";
484                                                                               
485                                if( $entry[$i]['jpegphoto'][0] )
486                                {
487                                        $result[$j]['photo'] = "1";
488                                        $filterPhoto = "(objectclass=*)";
489                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
490                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
491                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
492                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
493                                }
494                                else
495                                        $result[$j]['photo'] = "0";
496
497                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
498                        }
499               
500                        $organization = $this->attr_org;
501                }
502               
503                return $result;
504        }
505}
506
507?>
Note: See TracBrowser for help on using the repository browser.