source: sandbox/jabberit_messenger/trophy_expresso/inc/class.ContactsIm.inc.php @ 2833

Revision 2833, 11.3 KB checked in by emmanuel.ferro, 14 years ago (diff)

Ticket #986 - [SERPRO] -Substitui "uid" hardcoded p/ parametro em attributeLdap

  • 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               
52                $order          = array();
53                $ou_User        = substr($this->ou_User, (strpos($this->ou_User,"=")+1));
54                $return         = '<empty/>';
55                $users          = $this->getUsersIm($param['name']);
56
57               
58                if (!is_array($users) && trim($users) === 'Many Results')
59                        return "<error>Many Results</error>";
60
61                // Hosts Jabber
62                $hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
63               
64                if( is_array($users) )
65                {       
66                        for($i = 0; $i < count($users); $i++)                           
67                        {
68                                if( is_array($hostsJabber) )
69                                {
70                                        foreach($hostsJabber as $itens)
71                                        {
72                                                if( trim($users[$i]['ou']) === trim($itens['org']) && strpos($users[$i]['jid'],"@") === false )
73                                                {
74                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
75                                                }
76                                               
77                                                if( array_key_exists('ouAll', $users[$i]) && trim($itens['org']) === "*" )
78                                                {
79                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
80                                                }
81                                        }
82                                }
83
84                                if( strpos($users[$i]['jid'],"@") === false )
85                                {
86                                        $users[$i]['jid'] = $users[$i]['jid']."@".$this->serverJabber;
87                                }
88                        }
89
90                        foreach($users as $tmp)
91                        {
92                                if ( !array_key_exists($tmp['ou'], $order) )
93                                        $order[$tmp['ou']] = array();
94
95                                $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>';
96                        }
97                       
98                        ksort($order);
99                               
100                        $return = '<uids>';
101                        foreach ( $order as $key => $val )
102                                $return .= '<'.$key.'>'.implode('',$val).'</'.$key.'>';
103                        $return .= '</uids>';
104                }
105               
106                return $return;
107        }
108
109        private final function getUsersIm($pName)
110        {   
111                $array_uids             = array();
112        $members                = array();
113        $result                 = array();
114        $uidType                = "uid";
115                $serversLdap    = unserialize(trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']));
116
117        if( $serversLdap )
118        {
119                if( array_key_exists($this->serverLdap, $serversLdap) )
120                {
121                        $groups = unserialize($serversLdap[$this->serverLdap]);
122                }
123
124                if( count($groups) > 0 )
125                {
126                                foreach($groups as $tmp)
127                                {
128                                        $group = explode(":",$tmp);
129                                        $array_result = $this->ldap->getGroupsMemberUid($group[0], $this->serverLdap );
130                                        @array_shift($array_result[0]['memberuid']);
131                                        $array_uids = @array_merge($array_uids, $array_result[0]['memberuid']);
132                                        unset($array_result);
133                                }
134                }
135        }                               
136               
137        if( count($array_uids) == 0 )
138        {
139                $array_uids = $this->db->get_accounts_acl();
140                $uidType = "uidnumber";
141        }       
142
143        for( $i = 0; $i < count($array_uids) ; $i+=50 )
144        {   
145            $partial_uids = array_slice($array_uids,$i,50);
146            $filter_uid = implode(")(".$uidType."=",$partial_uids);
147            $filter_uid = "(".$uidType."=". $filter_uid. ")";
148
149                        if( $this->groupsLocked() )
150                        {
151                    $orgs[] = $this->ou_User;
152                    $orgsGroupsLocked = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']);
153                   
154                                foreach( $orgsGroupsLocked as $tmp )
155                                {
156                                        if( $tmp != "" )
157                                        {
158                                                if( strpos($tmp, "/") !== false )
159                                                {
160                                                        $tt = explode("/", $tmp);
161                                                        $newOU = implode(",OU=",array_reverse($tt));
162                                                        $orgs[] = "OU=". $newOU ;
163                                                }
164                                                else
165                                                        $orgs[] = "OU=". $tmp;
166                                        }           
167                                }
168
169                    $orgs = array_unique($orgs);
170
171                                foreach( $orgs as $orgB )
172                                        $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid, $orgB );
173
174                        }
175                        else
176                                $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid);
177        }
178       
179        if( !$this->groupsLocked() )
180        {
181                        unset($serversLdap[$this->serverLdap]);
182
183                        if( $serversLdap  && count($serversLdap))
184                        {
185                                foreach( $serversLdap as $key => $tmp )
186                                {
187                                        $array_uids_external = array( );
188                                        $groupsExternal = unserialize($tmp);
189
190                                        if( count($groupsExternal) )
191                                        {
192                                                foreach( $groupsExternal as $tmpExt )
193                                                {
194                                                        $group = explode(":",$tmpExt);
195                                                        $array_result = $this->ldap->getGroupsMemberUid( $group[0], $key );
196                                                       
197                                                        if( $array_result[0]['memberuid']['count'] )
198                                                                unset($array_result[0]['memberuid']['count']);
199
200                                                        $array_uids_external = @array_merge($array_uids_external, $array_result[0]['memberuid']);
201                                                }
202                                               
203                                               
204                                                if( is_array($array_uids_external) )
205                                                {
206                                                        for( $i = 0; $i < count( $array_uids_external ) ; $i+=50 )
207                                                {   
208                                                $partial_uids_external  = array_slice($array_uids_external,$i,50);
209                                                $filter_uid_external    = implode(")(".$this->attribute."=",$partial_uids_external);
210                                                $filter_uid_external    = "(".$this->attribute."=". $filter_uid_external. ")";
211                                                $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $filter_uid_external, $key );
212                                                }
213                                        }
214                                        }
215                                }
216                        }
217
218                        if( count($this->hostsJabberLdap) )
219                        {
220                                for($i = 0 ; $i < count($this->hostsJabberLdap); $i++)
221                                {
222                                        if( !@array_key_exists($this->hostsJabberLdap[$i]['serverLdap'], $serversLdap) )
223                                        {
224                                                $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", false, $this->hostsJabberLdap[$i]['serverLdap'] );
225                                        }
226                                }
227                        }
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               
241                        for($i = 0 ; $i < count($members); $i++ )
242                        {
243                                if( $ou == $members[$i]['ou'] )
244                                {
245                                        $uids_org[] = $members[$i];
246                                }
247                                else
248                                {
249                                        if(isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']))
250                                                unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$members[$i]['ou']]);
251                                }
252                        }
253                       
254                        return $uids_org;
255        }           
256
257                return $members;       
258        }
259       
260        private final function groupsLocked()
261        {
262                $memberShip = array();
263                $groupsLocked =  explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
264               
265                foreach($_SESSION['phpgw_info']['jabberit_messenger']['membership'] as $tmp)
266                        $memberShip[] = $tmp['account_name'];
267               
268                foreach($groupsLocked as $tmp)
269                {
270                        $groups = explode(":", $tmp);
271                       
272                        if( array_search($groups[1], $memberShip) !== False )
273                        {       
274                                $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked'] = $groups[2];
275                                return true;
276                        }
277                }
278               
279                return false;
280        }
281       
282        private final function strallpos($haystack, $needle, $offset = 0)
283        {
284            $result = array();
285            for($i = $offset; $i< strlen($haystack); $i++ )
286            {
287                $pos = strpos($haystack,$needle,$i);
288                if($pos !== FALSE)
289                {
290                    $offset =  $pos;
291                    if($offset >= $i)
292                        $result[] = $i = $offset;
293                }
294            }
295       
296        return $result;
297        }
298
299        public final function verifyAddNewContact($pUid)
300        {
301                $groupsLocked   = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
302                $gidNumbers             = array();
303                $uid                    = $pUid['uid'];
304                $uid_User               = substr($this->dn_User, 0, strpos($this->dn_User, ","));
305                $uid_User               = substr($uid_User, 4);
306               
307                foreach($groupsLocked as $tmp)
308                {
309                        $groups = explode(":", $tmp);
310                        $gidNumbers[] = $groups[1];
311                }
312
313                $filter_gid = implode(")(gidnumber=",$gidNumbers);
314            $filter_gid = "(gidnumber=". $filter_gid. ")";
315       
316                $result = $this->ldap->getGroupsMemberUid( $filter_gid, "localhost" );
317
318                if( $result && is_array($result) )
319                {
320                        array_shift($result);
321                        $i = 0;
322                       
323                        foreach($result as $value)
324                        {
325                                $Groups[$i]['dn'] = $value['dn'];
326                                $Groups[$i]['gidnumber'] = $value['gidnumber'][0];
327                                if(array_key_exists('memberuid',$value))
328                                {
329                                        array_shift($value['memberuid']);
330                                        $Groups[$i++]['memberuid'] = $value['memberuid'];
331                                }
332                        }
333
334                        $search = array();
335                        $search_Gid = array();
336
337                        // Verifica Uid em Grupo Bloqueado
338                        foreach($Groups as $value)
339                        {                       
340                                if( array_search( $uid , $value['memberuid'] ) !== false )
341                                {
342                                        $ou = substr($value['dn'],strpos($value['dn'], "ou="));
343                                        if( array_search($uid_User, $value['memberuid']) === false )
344                                        {
345                                                $search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
346                                                $search_Gid[] = $value['gidnumber'];
347                                        }
348                                }
349                        }
350                }
351               
352       
353                if( $this->groupsLocked() )
354                {
355                        if( count($search) > 0 )
356                        {
357                                // Verifica permissões do grupo
358                                foreach($groupsLocked as $value)
359                                {                                                       
360                                        $tpGroups = explode(":",$value);
361                                        if( $tpGroups[1] == $search_Gid[0] )
362                                        {
363                                                $ousTp = explode(",",$tpGroups[2]);
364                                                $ou_User = strtoupper(trim($this->dn_User));
365                                               
366                                                $posAll = $this->strallpos($ou_User, "OU=" );
367                                                $orgs = array();
368                               
369                                                for( $i = 0 ; $i < count($posAll); $i++ )
370                                                {
371                                                        $pos = strpos($ou_User, ",");
372                                                        $tmpString = substr($ou_User, $posAll[$i] + 3);
373                                                        $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
374                                                }
375                               
376                                                $ou_User = implode("/", array_reverse($orgs));
377
378                                                if( array_search( $ou_User, $ousTp) !== false )
379                                                        return "true";
380                                        }
381                                }
382                                return "false";
383                        }
384                        else
385                                return "true";
386                }
387                else
388                {               
389                        // Se Bloqueado verifica o Grupo       
390                        if( count($search) > 0 )
391                        {
392                                if( array_search($this->ou_User, $search) === false )
393                                {
394                                        // Verifica permissões do grupo
395                                        foreach($groupsLocked as $value)
396                                        {                                                       
397                                                $tpGroups = explode(":",$value);
398                                               
399                                                if( $tpGroups[1] == $search_Gid[0] )
400                                                {
401                                                        $ousTp = explode(",",$tpGroups[2]);
402                                                        $ou_User = strtoupper(trim($this->dn_User));
403                                       
404                                                        $posAll = $this->strallpos($ou_User, "OU=" );
405                                                        $orgs = array();
406                                       
407                                                        for( $i = 0 ; $i < count($posAll); $i++ )
408                                                        {
409                                                                $pos = strpos($ou_User, ",");
410                                                                $tmpString = substr($ou_User, $posAll[$i] + 3);
411                                                                $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
412                                                        }
413                                       
414                                                        $ou_User = implode("/", array_reverse($orgs));
415                                                       
416                                                        if( array_search( $ou_User, $ousTp) !== false )
417                                                                return "true";
418                                                }
419                                        }
420                                        return "false";
421                                }
422                                return "true";
423                        }                                       
424                        return "true";
425                }
426        }
427
428}
429
430?>
Note: See TracBrowser for help on using the repository browser.