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

Revision 1695, 9.0 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #777 - Correcao do problema descrito no ticket em questao

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