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

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