source: branches/2.2/jabberit_messenger/inc/class.ldap_im.inc.php @ 3102

Revision 3102, 11.5 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

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