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

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

Ticket #808 - Busca implementada utilizando somente os grupos cadastrados.

  • 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                                                $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", false, $this->hostsJabberLdap[$i]['serverLdap'] );
243                                }
244                        }
245        }
246
247        if ( is_array($result) )
248        {
249                for( $i = 0; $i < count($result); $i++ )
250                        if( is_array($result[$i]) )
251                                $members = array_merge($members,$result[$i]);
252        }
253             
254        if( count($members) > 150)
255        {
256                $ou = substr( $this->ou_User, strpos($this->ou_User, "=") + 1 );
257               
258                        for($i = 0 ; $i < count($members); $i++ )
259                        {
260                                if( $ou == $members[$i]['ou'] )
261                                {
262                                        $uids_org[] = $members[$i];
263                                }
264                                else
265                                {
266                                        if(isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']))
267                                                unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$members[$i]['ou']]);
268                                }
269                        }
270                       
271                        return $uids_org;
272        }           
273
274                return $members;       
275        }
276       
277        private final function strallpos($haystack, $needle, $offset = 0)
278        {
279            $result = array();
280            for($i = $offset; $i< strlen($haystack); $i++ )
281            {
282                $pos = strpos($haystack,$needle,$i);
283                if($pos !== FALSE)
284                {
285                    $offset =  $pos;
286                    if($offset >= $i)
287                        $result[] = $i = $offset;
288                }
289            }
290       
291        return $result;
292        }
293
294        public final function verifyAddNewContact($pUid)
295        {
296                $groupsLocked   = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
297                $gidNumbers             = array();
298                $uid                    = $pUid['uid'];
299                $uid_User               = substr($this->dn_User, 0, strpos($this->dn_User, ","));
300                $uid_User               = substr($uid_User, 4);
301               
302                foreach($groupsLocked as $tmp)
303                {
304                        $groups = explode(":", $tmp);
305                        $gidNumbers[] = $groups[1];
306                }
307
308                $filter_gid = implode(")(gidnumber=",$gidNumbers);
309            $filter_gid = "(gidnumber=". $filter_gid. ")";
310       
311                $result = $this->ldap->getGroupsMemberUid( $filter_gid, "localhost" );
312
313                if( $result && is_array($result) )
314                {
315                        array_shift($result);
316                        $i = 0;
317                       
318                        foreach($result as $value)
319                        {
320                                $Groups[$i]['dn'] = $value['dn'];
321                                $Groups[$i]['gidnumber'] = $value['gidnumber'][0];
322                                if(array_key_exists('memberuid',$value))
323                                {
324                                        array_shift($value['memberuid']);
325                                        $Groups[$i++]['memberuid'] = $value['memberuid'];
326                                }
327                        }
328
329                        $search = array();
330                        $search_Gid = array();
331
332                        // Verifica Uid em Grupo Bloqueado
333                        foreach($Groups as $value)
334                        {                       
335                                if( array_search( $uid , $value['memberuid'] ) !== false )
336                                {
337                                        $ou = substr($value['dn'],strpos($value['dn'], "ou="));
338                                        if( array_search($uid_User, $value['memberuid']) === false )
339                                        {
340                                                $search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
341                                                $search_Gid[] = $value['gidnumber'];
342                                        }
343                                }
344                        }
345                }
346               
347       
348                if( $this->groupsLocked() )
349                {
350                        if( count($search) > 0 )
351                        {
352                                // Verifica permissões do grupo
353                                foreach($groupsLocked as $value)
354                                {                                                       
355                                        $tpGroups = explode(":",$value);
356                                        if( $tpGroups[1] == $search_Gid[0] )
357                                        {
358                                                $ousTp = explode(",",$tpGroups[2]);
359                                                $ou_User = strtoupper(trim($this->dn_User));
360                                               
361                                                $posAll = $this->strallpos($ou_User, "OU=" );
362                                                $orgs = array();
363                               
364                                                for( $i = 0 ; $i < count($posAll); $i++ )
365                                                {
366                                                        $pos = strpos($ou_User, ",");
367                                                        $tmpString = substr($ou_User, $posAll[$i] + 3);
368                                                        $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
369                                                }
370                               
371                                                $ou_User = implode("/", array_reverse($orgs));
372
373                                                if( array_search( $ou_User, $ousTp) !== false )
374                                                        return "true";
375                                        }
376                                }
377                                return "false";
378                        }
379                        else
380                                return "true";
381                }
382                else
383                {               
384                        // Se Bloqueado verifica o Grupo       
385                        if( count($search) > 0 )
386                        {
387                                if( array_search($this->ou_User, $search) === false )
388                                {
389                                        // Verifica permissões do grupo
390                                        foreach($groupsLocked as $value)
391                                        {                                                       
392                                                $tpGroups = explode(":",$value);
393                                               
394                                                if( $tpGroups[1] == $search_Gid[0] )
395                                                {
396                                                        $ousTp = explode(",",$tpGroups[2]);
397                                                        $ou_User = strtoupper(trim($this->dn_User));
398                                       
399                                                        $posAll = $this->strallpos($ou_User, "OU=" );
400                                                        $orgs = array();
401                                       
402                                                        for( $i = 0 ; $i < count($posAll); $i++ )
403                                                        {
404                                                                $pos = strpos($ou_User, ",");
405                                                                $tmpString = substr($ou_User, $posAll[$i] + 3);
406                                                                $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
407                                                        }
408                                       
409                                                        $ou_User = implode("/", array_reverse($orgs));
410                                                       
411                                                        if( array_search( $ou_User, $ousTp) !== false )
412                                                                return "true";
413                                                }
414                                        }
415                                        return "false";
416                                }
417                                return "true";
418                        }                                       
419                        return "true";
420                }
421        }
422}
423
424?>
Note: See TracBrowser for help on using the repository browser.