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

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