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

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