source: trunk/jabberit_messenger/jmessenger/inc/class.ContactsIm.inc.php @ 7673

Revision 7673, 9.9 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
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( count($users) == 0  )
57                        return "<empty></empty>";
58               
59                if( $users === "manyresults" )
60                {
61                        if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']) )
62                                unset($_SESSION['phpgw_info']['jabberit_messenger']['photo']);
63                       
64                        return "<manyresults></manyresults>";
65                }
66                               
67                // Hosts Jabber
68                $hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
69               
70                if( is_array($users) )
71                {
72            $users_count = count($users);
73                        for($i = 0; $i < $users_count; ++$i)
74                        {
75                                if( is_array($hostsJabber) )
76                                {
77                                        foreach($hostsJabber as $itens)
78                                        {
79                                                if( trim($users[$i]['ou']) === trim($itens['org']) && strpos($users[$i]['jid'],"@") === false )
80                                                {
81                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
82                                                }
83                                               
84                                                if( array_key_exists('ouAll', $users[$i]) && trim($itens['org']) === "*" )
85                                                {
86                                                        $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
87                                                }
88                                        }
89                                }
90
91                                if( strpos($users[$i]['jid'],"@") === false )
92                                {
93                                        $users[$i]['jid'] = $users[$i]['jid']."@".$this->serverJabber;
94                                }
95                        }
96
97                        foreach($users as $tmp)
98                        {
99                                if ( !array_key_exists($tmp['ou'], $order) )
100                                        $order[$tmp['ou']] = array();
101
102                                $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>';
103                        }
104                       
105                        ksort($order);
106                               
107                        $return = '<uids>';
108                        foreach ( $order as $key => $val )
109                                $return .= '<'.$key.'>'.implode('',$val).'</'.$key.'>';
110                        $return .= '</uids>';
111                }
112               
113                return $return;
114        }
115
116        private final function getUsersIm($pName)
117        {   
118                $array_uids             = array();
119                $countUids              = 0;
120        $members                = array();
121        $result                 = array();
122        $uidType                = $this->attribute;
123                $serversLdap    = unserialize( trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']) );
124
125                if( $serversLdap )
126                {
127                        // Usa Grupos Ldap
128                        $filters = array( );
129                       
130                        foreach( $serversLdap as $servers => $groups )
131                        {
132                                $filter = '';
133                                foreach( unserialize($groups) as $group )
134                                        $filter .= '(gidnumber' . strstr( $group, ':' ) . ')';
135
136                                $filters[ $servers ] = str_replace( ':', '=', $filter );
137                        }
138                       
139                        foreach( $serversLdap as $key => $tmp )
140                        {
141                                if( $key === $this->serverLdap )
142                                {
143                                        $result[$key] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*");
144                                        $countUids += count($result[$key]);
145                                }
146                                else
147                                {
148                                        if( !$this->groupsLocked() )
149                                        {
150                                                $result[$key] =  $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $key );
151                                                $countUids += count($result[$key]);
152                                        }
153                                }
154                        }
155                       
156                        if( $countUids >  $this->ldap->getMaxResults() )
157                        {
158                                return "manyresults";
159                        }
160                       
161                        $_RESULT = $this->ldap->getMembers($result, $filters);
162                       
163                        foreach( $_RESULT as $key => $value )
164                                $array_uids = array_merge($array_uids, $_RESULT[$key]);
165 
166                }
167                else
168                {
169                // Ldap Root
170                $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*");
171               
172                        // Ldap Catalog                 
173                        if( count($this->hostsJabberLdap) )
174                        {
175                                foreach( $this->hostsJabberLdap as $conf )
176                                {
177                                        $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $conf['serverLdap'] );
178                                }
179                        }
180
181                foreach( $result as $value )
182                {
183                                $array_uids = array_merge($array_uids, $value );
184                }
185                       
186                        if( count($array_uids) >  $this->ldap->getMaxResults() )
187                        {
188                                return "manyresults";
189                        }
190                }
191
192                if( $this->groupsLocked() )
193                {
194            $orgs[]                     = substr($this->ou_User, ( strpos($this->ou_User, "ou=") + 3 ) );
195            $orgsGroupsLocked   = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']);
196           
197                        foreach( $orgsGroupsLocked as $tmp )
198                        {
199                                if( $tmp != "")
200                                        $orgs[] = $tmp;
201                        }
202
203            $orgs = array_unique($orgs);
204
205                        $_restrict = array();
206
207            $orgs_count = count($orgs);
208                        for( $i = 0 ; $i < $orgs_count; ++$i )
209                        {
210                $array_uids_count = count($array_uids);
211                                for( $j = 0 ; $j < $array_uids_count; ++$j )
212                                {
213                                        if( trim($array_uids[$j]['ou']) === trim($orgs[$i]) )
214                                        {
215                                                $_restrict[] = $array_uids[$j];
216                                        }       
217                                }       
218                        }
219                       
220                        return $_restrict;
221                }
222                else
223                {       
224                        return $array_uids;
225                }       
226        }
227       
228        private final function groupsLocked()
229        {
230                $memberShip = array();
231                $groupsLocked =  explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
232               
233                foreach($_SESSION['phpgw_info']['jabberit_messenger']['membership'] as $tmp)
234                        $memberShip[] = $tmp['account_name'];
235               
236                foreach($groupsLocked as $tmp)
237                {
238                        $groups = explode(":", $tmp);
239                       
240                        if( array_search($groups[1], $memberShip) !== False )
241                        {       
242                                $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked'] = $groups[2];
243                                return true;
244                        }
245                }
246               
247                return false;
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
400?>
Note: See TracBrowser for help on using the repository browser.