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

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