source: sandbox/jabberit_messenger/trophy_expresso/inc/class.LdapIM.inc.php @ 2912

Revision 2912, 13.2 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Correcao para o caminho do arquivo attributeLdap, verificando assim a existencia desse arquivo.

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