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

Revision 551, 8.2 KB checked in by niltonneto, 16 years ago (diff)

resolve #358, #363, #365, #366

  • 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 $ldap;
20        private $ldap_host;
21        private $ldap_context;
22        private $ldap_dn;
23        private $ldap_pass;
24        private $common;
25        private $jid;
26        private $max_result;
27       
28        public final function __construct()
29        {
30                $this->ldap_host        = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
31                $this->ldap_context     = $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'];
32                $this->ldap_dn          = $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'];
33                $this->ldap_pass        = $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'];
34                $this->jid                      = $_SESSION['phpgw_info']['jabberit_messenger']['user'];
35                $this->max_result       = 50;
36        }
37
38        public final function __destruct()
39        {
40                if( $this->ldap )
41                        ldap_close($this->ldap);
42        }       
43
44        private final function _connect_ldap()
45        {
46                $this->common = new common();
47               
48                if( !$this->ldap )
49                {
50                        $GLOBALS['phpgw_info']['server']['ldap_version3'] = true;
51                        $this->ldap = $this->common->ldapConnect( $this->ldap_host,$this->ldap_dn,$this->ldap_pass, true );
52                }
53        }
54       
55        private final function _ldapConnect($refer = false)
56        {
57                function ldapRebindJabberit($ldap_connection, $ldap_url)
58                {
59                        $acc = $_SESSION['phpgw_info']['jabberit_messenger']['ldapManager']['acc'];
60                        $pw = $_SESSION['phpgw_info']['jabberit_messenger']['ldapManager']['pw'];
61                       
62                        @ldap_bind($ldap_connection, $acc,$pw);
63                }
64
65                // Using ContactCenter configuration.
66                $this->ldap_host        =       $_SESSION['phpgw_info']['jabberit_messenger']['ldapManager']['host'];
67                $this->ldap_context     =       $_SESSION['phpgw_info']['jabberit_messenger']['ldapManager']['dn'];
68                $this->ldap_dn          =       $_SESSION['phpgw_info']['jabberit_messenger']['ldapManager']['acc'];
69                $this->ldap_pass        =       $_SESSION['phpgw_info']['jabberit_messenger']['ldapManager']['pw'];
70
71                $this->ldap = ldap_connect($this->ldap_host);
72                ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
73                ldap_set_option($this->ldap, LDAP_OPT_REFERRALS, $refer);
74               
75                if ($refer)
76                {
77                        ldap_set_rebind_proc($this->ldap, ldapRebindJabberit);
78                }
79               
80                @ldap_bind($this->ldap,$this->ldap_dn,$this->ldap_pass);
81        }
82
83        public final function list_participants_externals($pSearch)
84        {
85                $this->_ldapConnect(true);
86               
87                $mail = $pSearch['mail'];
88               
89                if( $this->ldap )
90                {
91                        $filter = "(&(phpgwaccounttype=u)(mail=".$mail."))";
92                        $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");
93                        $search = ldap_search($this->ldap,$this->ldap_context,$filter,$justthese, 0, $this->max_result + 1);
94                        $entry1 = ldap_get_entries($this->ldap,$search);
95                        $entry = ldap_first_entry( $this->ldap, $search );
96                }                       
97               
98                if( $entry1['count'] > 0 )
99                {
100                        $i = 0;
101                        $result_user = array();
102                        $result = array();
103
104                        while($entry)
105                        {
106                       
107                                if ( $entry1[$i]['phpgwaccountvisible'][0] != '-1' )
108                                {
109                                        $result['uidnumber'] = @ldap_get_values($this->ldap, $entry, 'uidnumber');                     
110                                        $result['mail'] = @ldap_get_values($this->ldap, $entry, 'mail');
111                                        $result['uid'] = @ldap_get_values($this->ldap, $entry, 'uid');
112                                        $result['cn'] = @ldap_get_values($this->ldap, $entry, 'cn');
113
114                                        foreach ( $result as $key => $value )
115                                                $result_user[$i][$key] = $value[0];
116
117                                        $ou = explode('dc=', $entry1[$i]['dn']);
118                                        $ou = explode("ou=",$ou[0]);
119                                        $ou = array_pop($ou);
120                                        $result_user[$i]['dn'] = strtoupper(substr($ou,0,strlen($ou)-1));
121
122                                        $result_user[$i]['photo'] = 0;
123
124                                        $photo = @ldap_get_values_len($this->ldap, $entry, 'jpegphoto');
125
126                                        if ( $photo )
127                                        {
128                                                $result_user[$i]['photo'] = 1;
129                                                $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result_user[$i]['uid'])] = $photo[0];
130                                        }
131
132                                        $i++;
133                                }                               
134                                $entry = ldap_next_entry($this->ldap,$entry);   
135                        }
136                       
137                        $return  = "<uids>";
138                        $return .= "<".$result_user[0]['dn']."><data>";
139                        $return .= "<cn>".$result_user[0]['cn']."</cn>";
140                        $return .= "<mail>".$result_user[0]['mail']."</mail>";
141                        $return .= "<uid>".$result_user[0]['uid']."</uid>";
142                        $return .= "<photo>".$result_user[0]['photo']."</photo>";                                                                       
143                        $return .= "</data></".$result_user[0]['dn'].">";                       
144                        $return .= "</uids>";
145
146                        return $return;         
147                }
148               
149                return '<empty/>';
150        }
151
152        public final function list_users_ldap($search, $uidnumber)
153        {
154                $this->_connect_ldap();
155       
156                if( $this->ldap )
157                {
158                        $filter = "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))";
159                        $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto");
160                        // SERPRO
161                        //$justthese = array("uid","uidNumber","cn","mail","description","phpgwAccountVisible","dn","jpegPhoto");                       
162                        $search = ldap_search($this->ldap,$this->ldap_context,$filter,$justthese, 0, $this->max_result + 1);
163                        $entry1 = ldap_get_entries($this->ldap,$search);
164                        $entry = ldap_first_entry( $this->ldap, $search );
165                }
166
167                if( $entry1['count'] > 0 )
168                {
169                        if( count($entry1) < $this->max_result )
170                        {
171                                $i = 0;
172                                $result_user = array();
173                                $result = array();
174
175                                while($entry)
176                                {
177                               
178                                        if ( $entry1[$i]['phpgwaccountvisible'][0] != '-1' )
179                                        {
180                                                $result['uidnumber'] = @ldap_get_values($this->ldap, $entry, 'uidnumber');                     
181                                                $result['mail'] = @ldap_get_values($this->ldap, $entry, 'mail');
182                                                $result['uid'] = @ldap_get_values($this->ldap, $entry, 'uid');
183                                                $result['cn'] = @ldap_get_values($this->ldap, $entry, 'cn');
184                                                // SERPRO
185                                                //$result['description'] = @ldap_get_values($this->ldap, $entry, 'description');
186
187                                                foreach ( $result as $key => $value )
188                                                        $result_user[$i][$key] = $value[0];
189
190                                                $ou = explode('dc=', $entry1[$i]['dn']);
191                                                $ou = explode("ou=",$ou[0]);
192                                                $ou = array_pop($ou);
193                                                $result_user[$i]['dn'] = strtoupper(substr($ou,0,strlen($ou)-1));
194
195                                                $result_user[$i]['photo'] = 0;
196                                                $photo = @ldap_get_values_len($this->ldap, $entry, 'jpegphoto');
197                                                if ( $photo )
198                                                {
199                                                        $result_user[$i]['photo'] = 1;
200                                                        $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result_user[$i]['uid'])] = $photo[0];
201                                                }
202                                               
203                                                $organization = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['attributes_org_ldap_jabberit']);
204
205                                                if(is_array($organization))
206                                                {
207                                                        foreach($organization as $attr)
208                                                        {
209                                                                $tmp = explode(";",$attr);
210                                                                if( strtolower(trim($tmp[0])) == strtolower(trim($result_user[$i]['dn'])) )
211                                                                {
212                                                                        switch(strtolower(trim($tmp[1])))
213                                                                        {
214                                                                                case "mail" :
215                                                                                                $uid = $result_user[$i]['mail'];
216                                                                                                $uid = substr($uid,0,strpos($uid,"@"));
217                                                                                                $result_user[$i]['uid'] = $uid;
218                                                                                                break;
219       
220                                                                                case "description" :
221                                                                                                // SERPRO
222                                                                                                // parte antes do arroba;
223                                                                                                $result_user[$i]['uid'] = $result_user[$i]['description'];                                                                             
224                                                                                                break;
225                                                                        }
226                                                                }
227                                                        }
228                                                }
229                                               
230                                                $i++;
231                                        }                               
232                                        $entry = ldap_next_entry($this->ldap,$entry);   
233                                }
234                               
235                                return $result_user;
236                        }
237                        else
238                        {
239                                return "Many Results";
240                        }
241                }
242                return 0;
243        }
244       
245        public final function list_organizations_ldap()
246        {
247                $result_org = "<organizations>";
248
249                $this->_connect_ldap();
250       
251                if( $this->ldap )
252                {
253                        $filter="ou=*";         
254                        $justthese = array("ou");
255                        $search = ldap_search($this->ldap,$this->ldap_context,$filter,$justthese);                     
256                        $entry = ldap_get_entries($this->ldap, $search);
257                }
258
259                foreach($entry as $tmp)
260                        if($tmp['ou'][0] != "")
261                                $result_org .= "<ou>" . $tmp['ou'][0] . "</ou>"; 
262
263                $result_org .= "</organizations>";
264               
265                return $result_org;
266        }
267}
268
269?>
Note: See TracBrowser for help on using the repository browser.