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

Revision 1504, 9.1 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #680 - Revertida as modificacoes para autenticacao do Modulo IM.

  • 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
13define('PHPGW_INCLUDE_ROOT', '../');
14define('PHPGW_API_INC','../phpgwapi/inc');
15require_once( PHPGW_API_INC . '/class.common.inc.php');
16
17class ldap_im
18{
19        private $attr_org;
20        private $common;
21        private $hostsJabber;
22        private $ldap;
23        private $ldap_context;
24        private $ldap_user;
25        private $ldap_host;
26        private $ldap_org;
27        private $ldap_pass;
28        private $max_result;
29       
30        public final function __construct()
31        {
32                // Attributes org ldap;
33                $this->attr_org = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['attributes_org_ldap_jabberit']);
34               
35                // Hosts Jabber
36                $this->hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
37               
38                // Result Ldap
39                $this->max_result = 30;
40        }
41
42        public final function __destruct()
43        {
44                if( $this->ldap )
45                        ldap_close($this->ldap);
46        }       
47
48        private final function ldapConn()
49        {
50                $this->common = new common();           
51               
52                $GLOBALS['phpgw_info']['server']['ldap_version3'] = true;
53               
54                if( $this->ldap_user && $this->ldap_pass )
55                        $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_user . "," . $this->ldap_context , $this->ldap_pass, false );
56                else
57                        $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_context , "", false );
58        }
59       
60        private final function ldapRoot()
61        {
62                $this->ldap_host        = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
63                $this->ldap_context     = $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'];
64                $this->ldap_user        = $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'];
65                $this->ldap_pass        = $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'];
66
67                $this->ldapConn();
68        }
69
70        private final function ldapCatalog()
71        {
72                $version3 = true;
73                $refer  = true;
74
75                if(!function_exists('ldap_connect'))
76                        return false;
77               
78                if(!$conn = ldap_connect($this->ldap_host))
79                        return false;
80
81                if( $version3 )
82                        if( !ldap_set_option($conn,LDAP_OPT_PROTOCOL_VERSION,3) )
83                                $version3 = false;
84
85                ldap_set_option($conn, LDAP_OPT_REFERRALS, $refer);
86
87                // Bind as Admin
88                if($this->ldap_user && $this->ldap_pass && !ldap_bind($conn, $this->ldap_user . "," .$this->ldap_context, $this->ldap_pass))
89                        return false;
90               
91                // Bind as Anonymous
92                if(!$this->ldap_user && !$this->ldap_pass && !@ldap_bind($conn))
93                        return false;
94
95                return $conn;
96        }
97
98        public final function getGroupsLdap($pOrg)
99        {
100                $this->ldapRoot();
101                $result_groups = "";
102
103                if( $this->ldap && $pOrg['ou'] != "-1")
104                {
105                        $organization = 'ou=' . $pOrg['ou'] .",". $this->ldap_context;
106                        $filter = "(&(phpgwAccountType=g)(objectClass=posixGroup))";
107                        $justthese = array("cn","gidNumber");
108                        $search = ldap_list($this->ldap, $organization, $filter, $justthese);
109                        $entry = ldap_get_entries( $this->ldap, $search );
110
111                        if( $entry )
112                        {                                       
113                                $result_groups = "<ldap>";
114                                foreach($entry as $tmp)
115                                        if( $tmp['gidnumber'][0] != "" )
116                                                $result_groups .= "<org><cn>".$tmp['cn'][0]."</cn><gid>".$tmp['gidnumber'][0]."</gid></org>";
117                               
118                                $result_groups .= "</ldap>";                                           
119                        }
120                       
121                        return $result_groups;
122                }
123               
124                return $result_groups;
125        }
126
127        public final function getGroupsMemberUid($pGroup)
128        {
129                $this->ldapRoot();
130               
131                if( $this->ldap )
132                {
133                        $filter = "(&(objectclass=posixgroup)(|".$pGroup."))";
134                        $justthese = array("dn","memberuid","gidnumber");
135                        $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese);
136                        $result = ldap_get_entries($this->ldap,$search);
137                       
138                        if( $result['count'] > 0 )
139                                return $result;
140                }               
141               
142                return false;
143        }
144
145        public final function getOrganizationsLdap()
146        {
147                $this->ldapRoot();
148               
149                if( $this->ldap )
150                {
151                        $filter = "(ou=*)";
152                        $justthese = array("dn");
153                        $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
154                        $info = ldap_get_entries($this->ldap, $search);
155               
156                        for ($i=0; $i<$info["count"]; $i++)
157                                $a_sectors[] = $info[$i]['dn'];
158                }
159
160                // Retiro o count do array info e inverto o array para ordenação.
161                foreach ($a_sectors as $context)
162                {
163                        $array_dn = ldap_explode_dn ( $context, 1 );
164                        $array_dn_reverse  = array_reverse ( $array_dn, true );
165                        array_pop ( $array_dn_reverse );
166                        $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
167                }
168               
169                // Ordenação
170                natcasesort($inverted_dn);
171
172                foreach ( $inverted_dn as $dn=>$invert_ufn )
173                {
174            $display = '';
175
176            $array_dn_reverse = explode ( "#", $invert_ufn );
177            $array_dn  = array_reverse ( $array_dn_reverse, true );
178
179            $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1);
180
181            if ($level == 0)
182                    $display .= '+';
183            else
184            {
185                                for( $i = 0; $i < $level; $i++)
186                                        $display .= '---';
187            }
188
189            reset ( $array_dn );
190            $display .= ' ' . (current ( $array_dn ) );
191                       
192                        $dn = trim(strtolower($dn));
193                        $options[$dn] = $display;
194        }
195
196            return $options;
197
198        }
199
200        public final function getUsersLdapCatalog( $search )
201        {
202                $confHosts      = $this->hostsJabber;
203                $result = array();
204                $return = array();
205                $conn   = "";           
206
207                for($i = 0; $i < count($confHosts); $i++ )
208                {
209                        $this->ldap_host        = $confHosts[$i]['serverLdap'];
210                        $this->ldap_context = $confHosts[$i]['contextLdap'];
211                        $this->ldap_user        = $confHosts[$i]['user'];
212                        $this->ldap_org         = $confHosts[$i]['org'];
213                        $this->ldap_pass        = $confHosts[$i]['password'];
214                       
215                        $conn = $this->ldapCatalog();
216
217                        if( $conn )
218                        {
219                                $filter = "(&(phpgwaccounttype=u)(".$search ."))";
220                                $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                               
221                                $searchRoot = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context;
222                               
223                                $search1 = @ldap_search($conn, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
224                                $entry1 = @ldap_get_entries( $conn, $search1 );
225                                $result = $this->resultArray($entry1, $conn );
226                               
227                                if( count($return) > 0 )
228                                $return = array_merge($return, $result);
229                                else
230                                        $return = $result;                             
231
232                        unset($result);
233
234                        ldap_close($conn);
235                        }
236                }
237               
238                return $return;
239        }
240
241        public final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
242        {
243                $this->ldapRoot();
244                $result = array();
245
246                if( $this->ldap )
247                {
248                        $searchRoot = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
249                        $filter = "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))";
250                        $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");                                                               
251                        $search = ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
252                        $entry = ldap_get_entries( $this->ldap, $search );
253
254                        $result = $this->resultArray($entry, $this->ldap );
255                }               
256
257                return $result;
258        }
259       
260        private final function resultArray($pArray, $pConn)
261        {
262                $entry = $pArray;
263                $result = array();
264
265                $j = 0;
266                for($i = 0 ; $i < $entry['count']; $i++)
267                {
268                        if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' )
269                        {
270                                $result[$j]['uidnumber'] = $entry[$i]['uidnumber'][0];                 
271                                $result[$j]['mail']     =  $entry[$i]['mail'][0];
272                                $result[$j]['uid']      =  $entry[$i]['uid'][0];
273                                $result[$j]['jid']      = $entry[$i]['uid'][0];
274                                $ou = explode("dc=", $entry[$i]['dn']);
275                                $ou = explode("ou=",$ou[0]);
276                                $ou = array_pop($ou);
277                                $result[$j]['ou']       = strtoupper(substr($ou,0,strlen($ou)-1));                                     
278                                if( $entry[$i]['jpegphoto'][0] )
279                                {
280                                        $result[$j]['photo'] = "1";
281                                        $filterPhoto = "(objectclass=*)";
282                                        $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto"));
283                                        $firstEntry = ldap_first_entry($pConn, $photoLdap);
284                                        $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto");
285                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0];
286                                }
287                                else
288                                        $result[$j]['photo'] = "0";
289
290                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
291                        }
292               
293                        $organization = $this->attr_org;
294       
295                        if(is_array($organization))
296                        {
297                                foreach($organization as $attr)
298                                {
299                                        $tmp = explode(";",$attr);
300                                        if( strtolower(trim($tmp[0])) == strtolower(trim($result[$i]['dn'])) )
301                                        {
302                                                switch(strtolower(trim($tmp[1])))
303                                                {
304                                                        case "mail" :
305                                                                        $uid = $result[$i]['mail'];
306                                                                        $uid = substr($uid,0,strpos($uid,"@"));
307                                                                        $result[$i]['uid'] = $uid;
308                                                                        break;
309       
310                                                        case "description" :
311                                                                        // SERPRO
312                                                                        // parte antes do arroba;
313                                                                        $result[$i]['uid'] = $result[$i]['description'];                                                                               
314                                                                        break;
315                                                }
316                                        }
317                                }
318                        }
319                }
320                return $result;
321        }
322}
323
324?>
Note: See TracBrowser for help on using the repository browser.