source: trunk/jabberit_messenger/inc/class.contacts_im.inc.php @ 1900

Revision 1900, 11.7 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #860 - Corrigir quando a autenticacao do jabber nao e pelo campo uid do ldap.

  • 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
13
14require_once "class.ldap_im.inc.php";
15require_once "class.db_im.inc.php";
16
17class contacts_im
18{
19        private $db;
20        private $dn_User;
21        private $hostsJabberLdap;       
22        private $ldap;
23        private $ou_User;
24        private $serverJabber;
25        private $serverLdap;
26               
27        function __construct()
28        {
29                $this->ldap = new ldap_im();
30                $this->db = new db_im();
31
32                // (DN) User
33                $this->dn_User = $_SESSION['phpgw_info']['jabberit_messenger']['account_dn'];
34
35                // (OU) User
36                $this->ou_User = $this->dn_User;
37                $this->ou_User = substr($this->ou_User,strpos($this->ou_User, "ou="));
38                $this->ou_User = strtoupper(substr($this->ou_User, 0, strpos($this->ou_User, ",dc=")));
39               
40                // Server Name Jabber
41                $this->serverJabber = $_SESSION['phpgw_info']['jabberit_messenger']['name_jabberit'];
42       
43                // Server Name Ldap
44                $this->serverLdap       = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
45               
46                // Hosts Jabber / Ldap
47                $this->hostsJabberLdap = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']); 
48        }
49
50        private final function groupsLocked()
51        {
52                $memberShip = array();
53                $groupsLocked =  explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
54               
55                foreach($_SESSION['phpgw_info']['jabberit_messenger']['membership'] as $tmp)
56                        $memberShip[] = $tmp['account_name'];
57               
58                foreach($groupsLocked as $tmp)
59                {
60                        $groups = explode(":", $tmp);
61                       
62                        if( array_search($groups[1], $memberShip) !== False )
63                        {       
64                                $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked'] = $groups[2];
65                                return true;
66                        }
67                }
68               
69                return false;
70        }
71
72        public final function list_contacts($param)
73        {
74                $order          = array();
75                $ou_User        = substr($this->ou_User, (strpos($this->ou_User,"=")+1));
76                $return         = '<empty/>';
77                $users          = $this->getUsersIm($param['name']);
78
79               
80                if (!is_array($users) && trim($users) === 'Many Results')
81                        return "<error>Many Results</error>";
82
83                // Hosts Jabber
84                $hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
85
86                if( is_array($users) )
87                {       
88                        for($i = 0; $i < count($users); $i++)                           
89                        {
90                                if( is_array($hostsJabber) )
91                                {
92                                        foreach($hostsJabber as $itens)
93                                        {
94                                                if( trim($users[$i]['ou']) === trim($itens['org']) && strpos($users[$i]['jid'],"@") === false )
95                                                {
96                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
97                                                }
98                                               
99                                                if( array_key_exists('ouAll', $users[$i]) && trim($itens['org']) === "*" )
100                                                {
101                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
102                                                }
103                                        }
104                                }
105
106                                if( strpos($users[$i]['jid'],"@") === false )
107                                {
108                                        $users[$i]['jid'] = $users[$i]['jid']."@".$this->serverJabber;
109                                }
110                        }
111
112                        foreach($users as $tmp)
113                        {
114                                if ( !array_key_exists($tmp['ou'], $order) )
115                                        $order[$tmp['ou']] = array();
116
117                                $order[$tmp['ou']][] = '<data><ou>'.$tmp['ou'].'</ou><cn>'.$tmp['cn'].'</cn><mail>'.$tmp['mail'].'</mail><uid>'.$tmp['uid'].'</uid><jid>'.$tmp['jid'].'</jid><photo>'.$tmp['photo'].'</photo></data>';
118                        }
119                       
120                        ksort($order);
121                               
122                        $return = '<uids>';
123                        foreach ( $order as $key => $val )
124                                $return .= '<'.$key.'>'.implode('',$val).'</'.$key.'>';
125                        $return .= '</uids>';
126                }
127               
128                return $return;
129        }
130
131        private final function getUsersIm($pName)
132        {   
133                $array_uids             = array();
134        $members                = array();
135        $result                 = array();
136        $uidType                = "uid";
137                $serversLdap    = unserialize(trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']));
138               
139        if( $serversLdap )
140        {
141                if( array_key_exists($this->serverLdap, $serversLdap) )
142                {
143                        $groups = unserialize($serversLdap[$this->serverLdap]);
144                }
145
146                if( count($groups) > 0 )
147                {
148                                foreach($groups as $tmp)
149                                {
150                                        $group = explode(":",$tmp);
151                                        $array_result = $this->ldap->getGroupsMemberUid($group[0], $this->serverLdap );
152                                        @array_shift($array_result[0]['memberuid']);
153                                        $array_uids = @array_merge($array_uids, $array_result[0]['memberuid']);
154                                        unset($array_result);
155                                }
156                }
157        }                               
158               
159        if( count($array_uids) == 0 )
160        {
161                $array_uids = $this->db->get_accounts_acl();
162                $uidType = "uidnumber";
163        }       
164
165        for( $i = 0; $i < count($array_uids) ; $i+=50 )
166        {   
167            $partial_uids = array_slice($array_uids,$i,50);
168            $filter_uid = implode(")(".$uidType."=",$partial_uids);
169            $filter_uid = "(".$uidType."=". $filter_uid. ")";
170
171                        if( $this->groupsLocked() )
172                        {
173                    $orgs[] = $this->ou_User;
174                    $orgsGroupsLocked = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']);
175                   
176                                foreach( $orgsGroupsLocked as $tmp )
177                                {
178                                        if( $tmp != "" )
179                                        {
180                                                if( strpos($tmp, "/") !== false )
181                                                {
182                                                        $tt = explode("/", $tmp);
183                                                        $newOU = implode(",OU=",array_reverse($tt));
184                                                        $orgs[] = "OU=". $newOU ;
185                                                }
186                                                else
187                                                        $orgs[] = "OU=". $tmp;
188                                        }           
189                                }
190
191                    $orgs = array_unique($orgs);
192
193                                foreach( $orgs as $orgB )
194                                        $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid, $orgB );
195
196                        }
197                        else
198                                $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid);
199        }
200       
201        if( !$this->groupsLocked() )
202        {
203                        $array_uids_external = array();
204
205                        unset($serversLdap[$this->serverLdap]);
206
207                        if( $serversLdap  && count($serversLdap))
208                        {
209                                foreach( $serversLdap as $key => $tmp )
210                                {
211                                        $groupsExternal = unserialize($tmp);
212
213                                        if( count($groupsExternal) )
214                                        {
215                                                foreach( $groupsExternal as $tmpExt )
216                                                {
217                                                        $group = explode(":",$tmpExt);
218                                                        $array_result = $this->ldap->getGroupsMemberUid( $group[0], $key );
219                                                        @array_shift($array_result[0]['memberuid']);
220                                                        $array_uids_external = @array_merge($array_uids_external, $array_result[0]['memberuid']);
221                                                        unset($array_result);
222                                                }
223                                               
224                                                for( $i = 0; $i < count( $array_uids_external ) ; $i+=50 )
225                                        {   
226                                        $partial_uids_external  = array_slice($array_uids_external,$i,50);
227                                        $filter_uid_external    = implode(")(uid=",$partial_uids_external);
228                                        $filter_uid_external    = "(uid=". $filter_uid_external. ")";
229                                        $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $filter_uid_external, $key );
230                                        }
231                                        unset($array_uids_external);
232                                        }
233                                       
234                                }
235                        }
236                       
237                        if( count($this->hostsJabberLdap) )
238                        {
239                                for($i = 0 ; $i < count($this->hostsJabberLdap); $i++)
240                                {
241                                        if( !array_key_exists($this->hostsJabberLdap[$i]['serverLdap'], $serversLdap) )
242                                        {
243                                                $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", false, $this->hostsJabberLdap[$i]['serverLdap'] );
244                                        }
245                                }
246                        }
247        }
248       
249        if ( is_array($result) )
250        {
251                for( $i = 0; $i < count($result); $i++ )
252                        if( is_array($result[$i]) )
253                                $members = array_merge($members,$result[$i]);
254        }
255             
256        if( count($members) > 150)
257        {
258                $ou = substr( $this->ou_User, strpos($this->ou_User, "=") + 1 );
259               
260                        for($i = 0 ; $i < count($members); $i++ )
261                        {
262                                if( $ou == $members[$i]['ou'] )
263                                {
264                                        $uids_org[] = $members[$i];
265                                }
266                                else
267                                {
268                                        if(isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']))
269                                                unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$members[$i]['ou']]);
270                                }
271                        }
272                       
273                        return $uids_org;
274        }           
275
276                return $members;       
277        }
278       
279        private final function strallpos($haystack, $needle, $offset = 0)
280        {
281            $result = array();
282            for($i = $offset; $i< strlen($haystack); $i++ )
283            {
284                $pos = strpos($haystack,$needle,$i);
285                if($pos !== FALSE)
286                {
287                    $offset =  $pos;
288                    if($offset >= $i)
289                        $result[] = $i = $offset;
290                }
291            }
292       
293        return $result;
294        }
295
296        public final function verifyAddNewContact($pUid)
297        {
298                $groupsLocked   = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
299                $gidNumbers             = array();
300                $uid                    = $pUid['uid'];
301                $uid_User               = substr($this->dn_User, 0, strpos($this->dn_User, ","));
302                $uid_User               = substr($uid_User, 4);
303               
304                foreach($groupsLocked as $tmp)
305                {
306                        $groups = explode(":", $tmp);
307                        $gidNumbers[] = $groups[1];
308                }
309
310                $filter_gid = implode(")(gidnumber=",$gidNumbers);
311            $filter_gid = "(gidnumber=". $filter_gid. ")";
312       
313                $result = $this->ldap->getGroupsMemberUid( $filter_gid, "localhost" );
314
315                if( $result && is_array($result) )
316                {
317                        array_shift($result);
318                        $i = 0;
319                       
320                        foreach($result as $value)
321                        {
322                                $Groups[$i]['dn'] = $value['dn'];
323                                $Groups[$i]['gidnumber'] = $value['gidnumber'][0];
324                                if(array_key_exists('memberuid',$value))
325                                {
326                                        array_shift($value['memberuid']);
327                                        $Groups[$i++]['memberuid'] = $value['memberuid'];
328                                }
329                        }
330
331                        $search = array();
332                        $search_Gid = array();
333
334                        // Verifica Uid em Grupo Bloqueado
335                        foreach($Groups as $value)
336                        {                       
337                                if( array_search( $uid , $value['memberuid'] ) !== false )
338                                {
339                                        $ou = substr($value['dn'],strpos($value['dn'], "ou="));
340                                        if( array_search($uid_User, $value['memberuid']) === false )
341                                        {
342                                                $search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
343                                                $search_Gid[] = $value['gidnumber'];
344                                        }
345                                }
346                        }
347                }
348               
349       
350                if( $this->groupsLocked() )
351                {
352                        if( count($search) > 0 )
353                        {
354                                // Verifica permissões do grupo
355                                foreach($groupsLocked as $value)
356                                {                                                       
357                                        $tpGroups = explode(":",$value);
358                                        if( $tpGroups[1] == $search_Gid[0] )
359                                        {
360                                                $ousTp = explode(",",$tpGroups[2]);
361                                                $ou_User = strtoupper(trim($this->dn_User));
362                                               
363                                                $posAll = $this->strallpos($ou_User, "OU=" );
364                                                $orgs = array();
365                               
366                                                for( $i = 0 ; $i < count($posAll); $i++ )
367                                                {
368                                                        $pos = strpos($ou_User, ",");
369                                                        $tmpString = substr($ou_User, $posAll[$i] + 3);
370                                                        $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
371                                                }
372                               
373                                                $ou_User = implode("/", array_reverse($orgs));
374
375                                                if( array_search( $ou_User, $ousTp) !== false )
376                                                        return "true";
377                                        }
378                                }
379                                return "false";
380                        }
381                        else
382                                return "true";
383                }
384                else
385                {               
386                        // Se Bloqueado verifica o Grupo       
387                        if( count($search) > 0 )
388                        {
389                                if( array_search($this->ou_User, $search) === false )
390                                {
391                                        // Verifica permissões do grupo
392                                        foreach($groupsLocked as $value)
393                                        {                                                       
394                                                $tpGroups = explode(":",$value);
395                                               
396                                                if( $tpGroups[1] == $search_Gid[0] )
397                                                {
398                                                        $ousTp = explode(",",$tpGroups[2]);
399                                                        $ou_User = strtoupper(trim($this->dn_User));
400                                       
401                                                        $posAll = $this->strallpos($ou_User, "OU=" );
402                                                        $orgs = array();
403                                       
404                                                        for( $i = 0 ; $i < count($posAll); $i++ )
405                                                        {
406                                                                $pos = strpos($ou_User, ",");
407                                                                $tmpString = substr($ou_User, $posAll[$i] + 3);
408                                                                $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
409                                                        }
410                                       
411                                                        $ou_User = implode("/", array_reverse($orgs));
412                                                       
413                                                        if( array_search( $ou_User, $ousTp) !== false )
414                                                                return "true";
415                                                }
416                                        }
417                                        return "false";
418                                }
419                                return "true";
420                        }                                       
421                        return "true";
422                }
423        }
424}
425
426?>
Note: See TracBrowser for help on using the repository browser.