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

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