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

Revision 1861, 12.2 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                return false;
220        }
221
222        public final function getOrganizationsLdap($pLdap_host)
223        {
224
225                if( $pLdap_host == $this->ldap_host || $pLdap_host == 'localhost' )
226                {
227                        $this->ldapRoot();
228                }
229                else
230                {
231                        $confHosts      = $this->hostsJabber;
232                       
233                        for($i = 0; $i < count($confHosts); $i++ )
234                        {
235                                if( $pLdap_host == $confHosts[$i]['serverLdap'] )
236                                {
237                                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
238                                        $this->ldap_context = $confHosts[$i]['contextLdap'];
239                                        $this->ldap_user        = $confHosts[$i]['user'];
240                                        $this->ldap_org         = $confHosts[$i]['org'];
241                                        $this->ldap_pass        = $confHosts[$i]['password'];
242                               
243                                        $this->ldap = $this->ldapCatalog();
244                                }
245                        }
246                }
247               
248                if( $this->ldap )
249                {
250                        $filter = "(ou=*)";
251                        $justthese = array("dn");
252                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
253                        $info = ldap_get_entries($this->ldap, $search);
254               
255                        for ($i=0; $i<$info["count"]; $i++)
256                        {
257                                $a_sectors[] = $info[$i]['dn'];
258                        }       
259                }
260
261                // Retiro o count do array info e inverto o array para ordenação.
262                foreach ($a_sectors as $context)
263                {
264                        $array_dn = ldap_explode_dn ( $context, 1 );
265                        $array_dn_reverse  = array_reverse ( $array_dn, true );
266                        array_pop ( $array_dn_reverse );
267                        $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
268                }
269               
270                // Ordenação
271                natcasesort($inverted_dn);
272
273                foreach ( $inverted_dn as $dn=>$invert_ufn )
274                {
275            $display = '';
276
277            $array_dn_reverse = explode ( "#", $invert_ufn );
278            $array_dn  = array_reverse ( $array_dn_reverse, true );
279
280            $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1);
281
282            if ($level == 0)
283                    $display .= '+';
284            else
285            {
286                                for( $i = 0; $i < $level; $i++)
287                                        $display .= '---';
288            }
289
290            reset ( $array_dn );
291            $display .= ' ' . (current ( $array_dn ) );
292                       
293                        $dn = trim(strtolower($dn));
294                        $options[$dn] = $display;
295        }
296
297            return $options;
298
299        }
300
301        public final function getUsersLdapCatalog( $search, $uid = false, $pLdap = false )
302        {
303                $confHosts      = $this->hostsJabber;
304                $result = array();
305                $return = array();
306                $conn   = "";           
307
308                for( $i = 0; $i < count($confHosts); $i++ )
309                {
310                        if( $this->ldap )
311                                @ldap_close($this->ldap);
312                       
313                        if( $pLdap && $pLdap == $confHosts[$i]['serverLdap'] )
314                        {
315                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
316                                $this->ldap_context = $confHosts[$i]['contextLdap'];
317                                $this->ldap_user        = $confHosts[$i]['user'];
318                                $this->ldap_org         = $confHosts[$i]['org'];
319                                $this->ldap_pass        = $confHosts[$i]['password'];
320                                $this->ldap             = $this->ldapCatalog();
321                        }
322                        else
323                        {
324                                $this->ldap_host        = $confHosts[$i]['serverLdap'];
325                                $this->ldap_context = $confHosts[$i]['contextLdap'];
326                                $this->ldap_user        = $confHosts[$i]['user'];
327                                $this->ldap_org         = $confHosts[$i]['org'];
328                                $this->ldap_pass        = $confHosts[$i]['password'];
329                                $this->ldap             = $this->ldapCatalog();
330                        }       
331
332                        if( $this->ldap )
333                        {
334                                $filter         = ( $uid ) ? "(&(phpgwaccounttype=u)(|".$uid.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
335                                $justthese      = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                         
336                                $searchRoot     = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
337                                $search1        = @ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
338                                $entry1         = @ldap_get_entries( $this->ldap, $search1 );
339                                $result         = $this->resultArray( $entry1, $this->ldap );
340
341                                if( count($return) > 0 )
342                                $return = array_merge($return, $result);
343                                else
344                                        $return = $result;                             
345                        }
346                }
347               
348                return $return;
349        }
350
351        public final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
352        {
353                $result = array();
354                $this->ldapRoot();
355
356                if( $this->ldap )
357                {
358                        $searchRoot     = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
359                        $filter         = "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))";
360                        $justthese      = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                         
361                        $search         = ldap_search( $this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
362                        $entry          = ldap_get_entries( $this->ldap, $search );
363                        $result         = $this->resultArray( $entry, $this->ldap );
364                }               
365
366                return $result;
367        }
368       
369        private final function resultArray($pArray, $pConn)
370        {
371                $entry = $pArray;
372                $result = array();
373
374                $j = 0;
375                for($i = 0 ; $i < $entry['count']; $i++)
376                {
377                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
378                        {
379                                $result[$j]['uidnumber'] = $entry[$i]['uidnumber'][0];                 
380                                $result[$j]['mail']     =  $entry[$i]['mail'][0];
381                                $result[$j]['uid']      =  $entry[$i]['uid'][0];
382                                $result[$j]['jid']      = $entry[$i]['uid'][0];
383                                $ou = explode("dc=", $entry[$i]['dn']);
384                                $ou = explode("ou=",$ou[0]);
385                                $ou = array_pop($ou);
386                                $result[$j]['ou']       = strtoupper(substr($ou,0,strlen($ou)-1));                                     
387                                if( $entry[$i]['jpegphoto'][0] )
388                                {
389                                        $result[$j]['photo'] = "1";
390                                        $filterPhoto = "(objectclass=*)";
391                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
392                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
393                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
394                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
395                                }
396                                else
397                                        $result[$j]['photo'] = "0";
398
399                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
400                        }
401               
402                        $organization = $this->attr_org;
403       
404                        if(is_array($organization))
405                        {
406                                foreach($organization as $attr)
407                                {
408                                        $tmp = explode(";",$attr);
409                                        if( strtolower(trim($tmp[0])) == strtolower(trim($result[$i]['dn'])) )
410                                        {
411                                                switch(strtolower(trim($tmp[1])))
412                                                {
413                                                        case "mail" :
414                                                                        $uid = $result[$i]['mail'];
415                                                                        $uid = substr($uid,0,strpos($uid,"@"));
416                                                                        $result[$i]['uid'] = $uid;
417                                                                        break;
418       
419                                                        case "description" :
420                                                                        // SERPRO
421                                                                        // parte antes do arroba;
422                                                                        $result[$i]['uid'] = $result[$i]['description'];                                                                               
423                                                                        break;
424                                                }
425                                        }
426                                }
427                        }
428                }
429                return $result;
430        }
431}
432
433?>
Note: See TracBrowser for help on using the repository browser.