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

Revision 1853, 12.4 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #808 - Busca implementada utilizando somente os grupos cadastrados.

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