source: branches/2.0/jabberit_messenger/inc/class.ldap_im.inc.php @ 1160

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

Ticket #571 - Somente criando os *.jars( applet.jar, filetransfer.jar e xhtml.jar )

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