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

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