source: trunk/jabberit_messenger/inc/class.contacts_im.inc.php @ 7673

Revision 7673, 10.4 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • Property svn:executable set to *
Line 
1<?php
2  /***************************************************************************\
3  *  Expresso - Expresso Messenger                                            *
4  *     - Alexandre Correia / Rodrigo Souza                                                               *
5  *     - JETI - http://jeti-im.org/                                                                              *
6  * ------------------------------------------------------------------------- *
7  *  This program is free software; you can redistribute it and/or modify it  *
8  *  under the terms of the GNU General Public License as published by the    *
9  *  Free Software Foundation; either version 2 of the License, or (at your   *
10  *  option) any later version.                                               *
11  \***************************************************************************/
12
13
14require_once "class.ldap_im.inc.php";
15require_once "class.db_im.inc.php";
16
17class contacts_im
18{
19        private $db;
20        private $dn_User;
21        private $hostsJabberLdap;       
22        private $ldap;
23        private $ou_User;
24        private $serverJabber;
25        private $serverLdap;
26               
27        function __construct()
28        {
29                $this->ldap = new ldap_im();
30                $this->db = new db_im();
31
32                // (DN) User
33                $this->dn_User = $_SESSION['phpgw_info']['jabberit_messenger']['account_dn'];
34
35                // (OU) User
36                $this->ou_User = $this->dn_User;
37                $this->ou_User = substr($this->ou_User,strpos($this->ou_User, "ou="));
38                $this->ou_User = strtoupper(substr($this->ou_User, 0, strpos($this->ou_User, ",dc=")));
39               
40                // Server Name Jabber
41                $this->serverJabber = $_SESSION['phpgw_info']['jabberit_messenger']['name_jabberit'];
42       
43                // Server Name Ldap
44                $this->serverLdap       = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
45               
46                // Hosts Jabber / Ldap
47                $this->hostsJabberLdap = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']); 
48        }
49
50        private final function groupsLocked()
51        {
52                $memberShip = array();
53                $groupsLocked =  explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
54               
55                foreach($_SESSION['phpgw_info']['jabberit_messenger']['membership'] as $tmp)
56                        $memberShip[] = $tmp['account_name'];
57               
58                foreach($groupsLocked as $tmp)
59                {
60                        $groups = explode(":", $tmp);
61                       
62                        if( array_search($groups[1], $memberShip) !== False )
63                        {       
64                                $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked'] = $groups[2];
65                                return true;
66                        }
67                }
68               
69                return false;
70        }
71
72        public final function list_contacts($param)
73        {
74                $order          = array();
75                $ou_User        = substr($this->ou_User, (strpos($this->ou_User,"=")+1));
76                $return         = "";
77                $users          = $this->getUsersIm($param['name']);
78
79                if( count($users) == 0  )
80                        return "<empty></empty>";
81               
82                if( $users === "manyresults" )
83                {
84                        if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']) )
85                                unset($_SESSION['phpgw_info']['jabberit_messenger']['photo']);
86                       
87                        return "<manyresults></manyresults>";
88                }
89                               
90                // Hosts Jabber
91                $hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
92
93                if( is_array($users) )
94                {
95            $users_count = count($users);
96                        for($i = 0; $i < $users_count; ++$i)
97                        {
98                                if( is_array($hostsJabber) )
99                                {
100                                        foreach($hostsJabber as $itens)
101                                        {
102                                                if( trim($users[$i]['ou']) === trim($itens['org']) && strpos($users[$i]['jid'],"@") === false )
103                                                {
104                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
105                                                }
106                                               
107                                                if( array_key_exists('ouAll', $users[$i]) && trim($itens['org']) === "*" )
108                                                {
109                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
110                                                }
111                                        }
112                                }
113
114                                if( strpos($users[$i]['jid'],"@") === false )
115                                {
116                                        $users[$i]['jid'] = $users[$i]['jid']."@".$this->serverJabber;
117                                }
118                        }
119
120                        foreach($users as $tmp)
121                        {
122                            if ( !array_key_exists($tmp['ou'], $order) )
123                                        $order[$tmp['ou']] = array();
124
125                                $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>';
126                        }
127                       
128                        ksort($order);
129                               
130                        $return = '<uids>';
131                        foreach ( $order as $key => $val )
132                                $return .= '<'.$key.'>'.implode('',$val).'</'.$key.'>';
133                        $return .= '</uids>';
134                }
135               
136                return $return;
137        }
138
139        private final function getUsersIm($pName)
140        {
141                $array_uids             = array();
142                $countUids              = 0;
143        $members                = array();
144        $result                 = array();
145        $uidType                = "uid";
146                $serversLdap    = unserialize( trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']) );
147
148                if( $serversLdap )
149                {
150                        // Usa Grupos Ldap
151                        $filters = array( );
152                       
153                        foreach( $serversLdap as $servers => $groups )
154                        {
155                                $filter = '';
156                                foreach( unserialize($groups) as $group )
157                                        $filter .= '(gidnumber' . strstr( $group, ':' ) . ')';
158
159                                $filters[ $servers ] = str_replace( ':', '=', $filter );
160                        }
161
162                        foreach( $serversLdap as $key => $tmp )
163                        {
164                                if( $key === $this->serverLdap )
165                                {
166                                        $result[$key] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*");
167                                        $countUids += count($result[$key]);
168                                }
169                                else
170                                {
171                                        if( !$this->groupsLocked() )
172                                        {
173                                                $result[$key] =  $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $key );
174                                                $countUids += count($result[$key]);
175                                        }
176                                }
177                        }
178
179                        if( $countUids >  $this->ldap->getMaxResults() )
180                        {
181                                return "manyresults";
182                        }
183
184                        $_RESULT = $this->ldap->getMembers($result, $filters);
185                       
186                        foreach( $_RESULT as $key => $value )
187                                $array_uids = array_merge($array_uids, $_RESULT[$key]);
188                }
189                else
190                {
191                // Ldap Root
192                $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*");
193               
194                // Ldap Catalog                 
195                        if( count($this->hostsJabberLdap) )
196                        {
197                                foreach( $this->hostsJabberLdap as $conf )
198                                {
199                                        $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $conf['serverLdap'] );
200                                }
201                        }
202               
203                foreach( $result as $key => $value )
204                {
205                                $array_uids = array_merge($array_uids, $value);
206                }
207               
208                if( count($array_uids) >  $this->ldap->getMaxResults() )
209                        {
210                                return "manyresults";
211                        }
212                }
213
214                if( $this->groupsLocked() )
215                {
216            $orgs[]                     = substr($this->ou_User, ( strpos($this->ou_User, "ou=") + 3 ) );
217            $orgsGroupsLocked   = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']);
218           
219                        foreach( $orgsGroupsLocked as $tmp )
220                        {
221                                if( $tmp != "")
222                                        $orgs[] = $tmp;
223                        }
224
225            $orgs = array_unique($orgs);
226
227                        $_restrict = array();
228
229            $orgs_count = count($orgs);
230                        for( $i = 0 ; $i < $orgs_count; ++$i )
231                        {
232                $array_uids_count = count($array_uids);
233                                for( $j = 0 ; $j < $array_uids_count; ++$j )
234                                {
235                                        if( trim($array_uids[$j]['ou']) === trim($orgs[$i]) )
236                                        {
237                                                $_restrict[] = $array_uids[$j];
238                                        }       
239                                }       
240                        }
241                       
242                        return $_restrict;
243                }
244                else
245                {       
246                        return $array_uids;
247                }       
248        }
249       
250        private final function strallpos($haystack, $needle, $offset = 0)
251        {
252            $result = array();
253            for($i = $offset; $i< strlen($haystack); ++$i )
254            {
255                $pos = strpos($haystack,$needle,$i);
256                if($pos !== FALSE)
257                {
258                    $offset =  $pos;
259                    if($offset >= $i)
260                        $result[] = $i = $offset;
261                }
262            }
263       
264        return $result;
265        }
266
267        public final function verifyAddNewContact($pUid)
268        {
269                $groupsLocked   = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
270                $gidNumbers             = array();
271                $uid                    = $pUid['uid'];
272                $uid_User               = substr($this->dn_User, 0, strpos($this->dn_User, ","));
273                $uid_User               = substr($uid_User, 4);
274               
275                foreach($groupsLocked as $tmp)
276                {
277                        $groups = explode(":", $tmp);
278                        $gidNumbers[] = $groups[1];
279                }
280
281                $filter_gid = implode(")(gidnumber=",$gidNumbers);
282            $filter_gid = "(gidnumber=". $filter_gid. ")";
283       
284                $result = $this->ldap->getGroupsMemberUid( $filter_gid, "localhost" );
285
286                if( $result && is_array($result) )
287                {
288                        array_shift($result);
289                        $i = 0;
290                       
291                        foreach($result as $value)
292                        {
293                                $Groups[$i]['dn'] = $value['dn'];
294                                $Groups[$i]['gidnumber'] = $value['gidnumber'][0];
295                                if(array_key_exists('memberuid',$value))
296                                {
297                                        array_shift($value['memberuid']);
298                                        $Groups[$i++]['memberuid'] = $value['memberuid'];
299                                }
300                        }
301
302                        $search = array();
303                        $search_Gid = array();
304
305                        // Verifica Uid em Grupo Bloqueado
306                        foreach($Groups as $value)
307                        {                       
308                                if( array_search( $uid , $value['memberuid'] ) !== false )
309                                {
310                                        $ou = substr($value['dn'],strpos($value['dn'], "ou="));
311                                        if( array_search($uid_User, $value['memberuid']) === false )
312                                        {
313                                                $search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
314                                                $search_Gid[] = $value['gidnumber'];
315                                        }
316                                }
317                        }
318                }
319               
320       
321                if( $this->groupsLocked() )
322                {
323                        if( count($search) > 0 )
324                        {
325                                // Verifica permissões do grupo
326                                foreach($groupsLocked as $value)
327                                {                                                       
328                                        $tpGroups = explode(":",$value);
329                                        if( $tpGroups[1] == $search_Gid[0] )
330                                        {
331                                                $ousTp = explode(",",$tpGroups[2]);
332                                                $ou_User = strtoupper(trim($this->dn_User));
333                                               
334                                                $posAll = $this->strallpos($ou_User, "OU=" );
335                                                $orgs = array();
336
337                        $posAll_count = count($posAll);
338                                                for( $i = 0 ; $i < $posAll_count; ++$i )
339                                                {
340                                                        $pos = strpos($ou_User, ",");
341                                                        $tmpString = substr($ou_User, $posAll[$i] + 3);
342                                                        $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
343                                                }
344                               
345                                                $ou_User = implode("/", array_reverse($orgs));
346
347                                                if( array_search( $ou_User, $ousTp) !== false )
348                                                        return "true";
349                                        }
350                                }
351                                return "false";
352                        }
353                        else
354                                return "true";
355                }
356                else
357                {               
358                        // Se Bloqueado verifica o Grupo       
359                        if( count($search) > 0 )
360                        {
361                                if( array_search($this->ou_User, $search) === false )
362                                {
363                                        // Verifica permissões do grupo
364                                        foreach($groupsLocked as $value)
365                                        {                                                       
366                                                $tpGroups = explode(":",$value);
367                                               
368                                                if( $tpGroups[1] == $search_Gid[0] )
369                                                {
370                                                        $ousTp = explode(",",$tpGroups[2]);
371                                                        $ou_User = strtoupper(trim($this->dn_User));
372                                       
373                                                        $posAll = $this->strallpos($ou_User, "OU=" );
374                                                        $orgs = array();
375
376                            $posAll_count = count($posAll);
377                                                        for( $i = 0 ; $i < $posAll_count; ++$i )
378                                                        {
379                                                                $pos = strpos($ou_User, ",");
380                                                                $tmpString = substr($ou_User, $posAll[$i] + 3);
381                                                                $orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
382                                                        }
383                                       
384                                                        $ou_User = implode("/", array_reverse($orgs));
385                                                       
386                                                        if( array_search( $ou_User, $ousTp) !== false )
387                                                                return "true";
388                                                }
389                                        }
390                                        return "false";
391                                }
392                                return "true";
393                        }                                       
394                        return "true";
395                }
396        }
397}
398
399?>
Note: See TracBrowser for help on using the repository browser.