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

Revision 2421, 11.0 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Adicionado a funcionalidade de procurar contatos no ldap, com templates XSL.

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