source: branches/2.2/jabberit_messenger/inc/class.contacts_im.inc.php @ 3266

Revision 3266, 9.9 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #1316 - Correcoes e melhorias na busca dos contatos para o Modulo IM.

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