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 *
RevLine 
[382]1<?php
[417]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  \***************************************************************************/
[382]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{
[946]19        private $attr_org;
20        private $common;
21        private $hostsJabber;
[382]22        private $ldap;
23        private $ldap_context;
24        private $ldap_dn;
[946]25        private $ldap_host;
[382]26        private $ldap_pass;
27        private $max_result;
28       
29        public final function __construct()
30        {
[946]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;
[382]39        }
40
41        public final function __destruct()
42        {
43                if( $this->ldap )
44                        ldap_close($this->ldap);
45        }       
46
[946]47        private final function ldapConn()
[382]48        {
[946]49                $this->common = new common();           
[382]50               
[946]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 );
[382]54        }
55       
[946]56        private final function ldapRoot()
[551]57        {
[946]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'];
[551]62
[946]63                $this->ldapConn();
64        }
[551]65
[946]66        private final function ldapCatalog()
67        {
68                $version3 = true;
69                $refer  = true;
70
71                if(!function_exists('ldap_connect'))
72                        return false;
[551]73               
[946]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 )       
[551]99                {
[946]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                        }
[551]115                }
[946]116
117                return $result_groups;
[551]118        }
119
[946]120        public final function getGroupsMemberUid($pGroup)
[697]121        {
[946]122                $this->ldapRoot();
[697]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
[946]138        public final function getOrganizationsLdap()
[551]139        {
[946]140                $this->ldapRoot();
141       
[551]142                if( $this->ldap )
143                {
[946]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                }
[551]149
[946]150                foreach($entry as $tmp)
151                        if($tmp['ou'][0] != "")
152                                $result_org[] = $tmp['ou'][0]; 
[551]153
[946]154                return $result_org;
155        }
[551]156
[946]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 );             
[551]167                }
168               
[946]169                return $ldapRoot;
[551]170        }
171
[946]172        private final function getUsersLdapCatalog( $search )
[382]173        {
[946]174                $confHosts      = $this->hostsJabber;
175                $result = array();
176                $return = array();
177                $conn   = "";           
178
179                for($i = 0; $i < count($confHosts); $i++ )
[382]180                {
[946]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'];
[697]185
[946]186                        $conn = $this->ldapCatalog();
[382]187
[946]188                        if( $conn )
[382]189                        {
[946]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 );
[382]194
[946]195                                $result = $this->resultArray($entry1, $conn );
[382]196
[946]197                                if( count($return) > 0 )
198                                $return = array_merge($return, $result);
199                                else
200                                        $return = $result;                             
[382]201
[946]202                        unset($result);
[382]203
[946]204                        ldap_close($conn);
[382]205                        }
206                }
[946]207               
208                return $return;
[382]209        }
[946]210
211        private final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
[417]212        {
[946]213                $this->ldapRoot();
214                $result = array();
[519]215
216                if( $this->ldap )
217                {
[946]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 );
[519]223
[946]224                        $result = $this->resultArray($entry, $this->ldap );
225                }               
[519]226
[946]227                return $result;
[417]228        }
[563]229       
[946]230        private final function resultArray($pArray, $pConn)
[563]231        {
[946]232                $entry = $pArray;
233                $result = array();
234
235                $j = 0;
236                for($i = 0 ; $i < $entry['count']; $i++)
[563]237                {
[946]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";
[563]259
[946]260                                $result[$j++]['cn']     = $entry[$i]['cn'][0];
[563]261                        }
[946]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                        }
[563]289                }
[946]290                return $result;
[563]291        }
[382]292}
293
[519]294?>
Note: See TracBrowser for help on using the repository browser.