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

Revision 2411, 11.1 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Adicionado a funcionalidade de procurar contatos no ldap.

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