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

Revision 946, 8.5 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #505 - Arquivos modificados para a administração de hosts virtuais no servidor Jabber.

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