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

Revision 1864, 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                        }
187                        else
188                                $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid);
189        }
190       
191        if( !$this->groupsLocked() )
192        {
193                        $array_uids_external = array();
194
195                        if( $serversLdap )
196                        {
197                                foreach( $serversLdap as $key => $tmp )
198                                {
199                                        if( $key != $this->serverLdap )
200                                        {
201                                                $groupsExternal = unserialize($tmp);
202
203                                                if(count($groupsExternal))
204                                                {
205                                                        foreach( $groupsExternal as $tmpExt )
206                                                        {
207                                                                $group = explode(":",$tmpExt);
208                                                                $array_result = $this->ldap->getGroupsMemberUid( $group[0], $key );
209                                                                @array_shift($array_result[0]['memberuid']);
210                                                                $array_uids_external = @array_merge($array_uids_external, $array_result[0]['memberuid']);
211                                                                unset($array_result);
212                                                        }
213                                                       
214                                                        for( $i = 0; $i < count( $array_uids_external ) ; $i+=50 )
215                                                {   
216                                                $partial_uids_external  = array_slice($array_uids_external,$i,50);
217                                                $filter_uid_external    = implode(")(uid=",$partial_uids_external);
218                                                $filter_uid_external    = "(uid=". $filter_uid_external. ")";
219                                                $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $filter_uid_external, $key );
220                                                }
221                                                }
222                                        }
223                                }                                       
224                        }
225
226                        if( count( $array_uids_external ) == 0 )
227                        $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*");
228        }
229
230        if ( is_array($result) )
231        {
232                for( $i = 0; $i < count($result); $i++ )
233                        if( is_array($result[$i]) )
234                                $members = array_merge($members,$result[$i]);
235        }
236             
237        if( count($members) > 150)
238        {
239                $ou = substr( $this->ou_User, strpos($this->ou_User, "=") + 1 );
240                        for($i = 0 ; $i < count($members); $i++ )
241                        {
242                                if( $ou == $members[$i]['ou'] )
243                                        $uids_org[] = $members[$i];
244                                else
245                                        if(isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']))
246                                                unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$members[$i]['ou']]);
247                        }
248                        return $uids_org;
249        }           
250
251                return $members;       
252        }
253       
254        private final function strallpos($haystack, $needle, $offset = 0)
255        {
256            $result = array();
257            for($i = $offset; $i< strlen($haystack); $i++ )
258            {
259                $pos = strpos($haystack,$needle,$i);
260                if($pos !== FALSE)
261                {
262                    $offset =  $pos;
263                    if($offset >= $i)
264                        $result[] = $i = $offset;
265                }
266            }
267       
268        return $result;
269        }
270
271        public final function verifyAddNewContact($pUid)
272        {
273                $groupsLocked   = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
274                $gidNumbers             = array();
275                $uid                    = $pUid['uid'];
276                $uid_User               = substr($this->dn_User, 0, strpos($this->dn_User, ","));
277                $uid_User               = substr($uid_User, 4);
278               
279                foreach($groupsLocked as $tmp)
280                {
281                        $groups = explode(":", $tmp);
282                        $gidNumbers[] = $groups[1];
283                }
284
285                $filter_gid = implode(")(gidnumber=",$gidNumbers);
286            $filter_gid = "(gidnumber=". $filter_gid. ")";
287       
288                $result = $this->ldap->getGroupsMemberUid( $filter_gid, "localhost" );
289
290                if( $result && is_array($result) )
291                {
292                        array_shift($result);
293                        $i = 0;
294                       
295                        foreach($result as $value)
296                        {
297                                $Groups[$i]['dn'] = $value['dn'];
298                                $Groups[$i]['gidnumber'] = $value['gidnumber'][0];
299                                if(array_key_exists('memberuid',$value))
300                                {
301                                        array_shift($value['memberuid']);
302                                        $Groups[$i++]['memberuid'] = $value['memberuid'];
303                                }
304                        }
305
306                        $search = array();
307                        $search_Gid = array();
308
309                        // Verifica Uid em Grupo Bloqueado
310                        foreach($Groups as $value)
311                        {                       
312                                if( array_search( $uid , $value['memberuid'] ) !== false )
313                                {
314                                        $ou = substr($value['dn'],strpos($value['dn'], "ou="));
315                                        if( array_search($uid_User, $value['memberuid']) === false )
316                                        {
317                                                $search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
318                                                $search_Gid[] = $value['gidnumber'];
319                                        }
320                                }
321                        }
322                }
323               
324       
325                if( $this->groupsLocked() )
326                {
327                        if( count($search) > 0 )
328                        {
329                                // Verifica permissões do grupo
330                                foreach($groupsLocked as $value)
331                                {                                                       
332                                        $tpGroups = explode(":",$value);
333                                        if( $tpGroups[1] == $search_Gid[0] )
334                                        {
335                                                $ousTp = explode(",",$tpGroups[2]);
336                                                $ou_User = strtoupper(trim($this->dn_User));
337                                               
338                                                $posAll = $this->strallpos($ou_User, "OU=" );
339                                                $orgs = array();
340                               
341                                                for( $i = 0 ; $i < count($posAll); $i++ )
342                                                {
343                                                        $pos = strpos($ou_User, ",");
344                                                        $tmpString = substr($ou_User, $posAll[$i] + 3);
345                                                        $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
346                                                }
347                               
348                                                $ou_User = implode("/", array_reverse($orgs));
349
350                                                if( array_search( $ou_User, $ousTp) !== false )
351                                                        return "true";
352                                        }
353                                }
354                                return "false";
355                        }
356                        else
357                                return "true";
358                }
359                else
360                {               
361                        // Se Bloqueado verifica o Grupo       
362                        if( count($search) > 0 )
363                        {
364                                if( array_search($this->ou_User, $search) === false )
365                                {
366                                        // Verifica permissões do grupo
367                                        foreach($groupsLocked as $value)
368                                        {                                                       
369                                                $tpGroups = explode(":",$value);
370                                               
371                                                if( $tpGroups[1] == $search_Gid[0] )
372                                                {
373                                                        $ousTp = explode(",",$tpGroups[2]);
374                                                        $ou_User = strtoupper(trim($this->dn_User));
375                                       
376                                                        $posAll = $this->strallpos($ou_User, "OU=" );
377                                                        $orgs = array();
378                                       
379                                                        for( $i = 0 ; $i < count($posAll); $i++ )
380                                                        {
381                                                                $pos = strpos($ou_User, ",");
382                                                                $tmpString = substr($ou_User, $posAll[$i] + 3);
383                                                                $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
384                                                        }
385                                       
386                                                        $ou_User = implode("/", array_reverse($orgs));
387                                                       
388                                                        if( array_search( $ou_User, $ousTp) !== false )
389                                                                return "true";
390                                                }
391                                        }
392                                        return "false";
393                                }
394                                return "true";
395                        }                                       
396                        return "true";
397                }
398        }
399}
400
401?>
Note: See TracBrowser for help on using the repository browser.