source: trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php @ 471

Revision 471, 75.3 KB checked in by niltonneto, 16 years ago (diff)

Alterações feitas por Joao Alfredo:

  • Corrigido bug que gerava tela branca após criação de setor.
  • Corrigido tradução.
  • Escondido Exibir registro de acesso na criação de contas.
  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2define('PHPGW_INCLUDE_ROOT','../');
3define('PHPGW_API_INC','../phpgwapi/inc');     
4include_once(PHPGW_API_INC.'/class.common.inc.php');
5include_once('class.functions.inc.php');
6
7function ldapRebind($ldap_connection, $ldap_url)
8{
9        // Enquanto estivermos utilizando referral na arvore ldap, teremos que continuar a utilizar o usuário sistemas:expresso.
10        // Depois, quando não existir mais referral, não existirá a necessidade de ldapRebind.
11        //ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
12        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
13        {
14                @ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
15        }
16}
17
18class ldap_functions
19{
20        var $ldap;
21        var $current_config;
22        var $functions;
23        var $manager_contexts;
24       
25        function ldap_functions(){             
26                $GLOBALS['phpgw_info']['server'] = $_SESSION['phpgw_info']['expresso']['server'];
27                $this->current_config = $_SESSION['phpgw_info']['expresso']['expressoAdmin'];
28                $common = new common();
29               
30                if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
31                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
32                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
33                {
34                        $this->ldap = $common->ldapConnect($GLOBALS['phpgw_info']['server']['ldap_master_host'],
35                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'],
36                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
37                }
38                else
39                {
40                        $this->ldap = $common->ldapConnect();
41                }
42               
43                $this->functions = new functions;
44                $manager_acl = $this->functions->read_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid']);
45                $this->manager_contexts = $manager_acl['contexts'];
46        }
47       
48        /* expressoAdmin: email lists : deve utilizar o ldap Host Master com o usuario e senha do CC*/
49        /* ldap connection following referrals and using Master config, from setup */
50        function ldapMasterConnect()
51        {
52                /*
53                $common = new common();
54                if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
55                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
56                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
57                {
58                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_master_host']);
59                        ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
60                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
61                        ldap_set_rebind_proc($ldap_connection, ldapRebind);
62                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
63                }
64                else
65                {
66                        $ldap_connection = $common->ldapConnect($GLOBALS['phpgw_info']['server']['ldap_host'],
67                                                                                           $GLOBALS['phpgw_info']['server']['ldap_root_dn'],
68                                                                                           $GLOBALS['phpgw_info']['server']['ldap_root_pw'], true);
69                }
70               
71                // If success, return follow_referral connection. Else, return normal connection.
72                if ($ldap_connection)
73                        return $ldap_connection;
74                else
75                        return $this->ldap;
76                */
77               
78                if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) && ($ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_master_host'])) )
79                {
80                        ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
81                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
82                        ldap_set_rebind_proc($ldap_connection, ldapRebind);
83                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
84                        {
85                                if ( ! ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']) )
86                                {
87                                        return false;
88                                }
89                        }
90                        return $ldap_connection;
91                }
92                else
93                {
94                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
95                        if ($ldap_connection)
96                        {
97                                ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
98                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
99                                if ( ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']) )
100                                        return $ldap_connection;
101                        }
102                }
103               
104                return false;
105        }
106               
107        function validate_fields($params)
108        {
109                /* ldap connection following referals and using Contac Center config*/
110                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
111                {
112                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
113                        if ($ldap_connection)
114                        {
115                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
116                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
117                               
118                                if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
119                                        ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
120                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
121                        }
122                        else
123                        {
124                                $result['status'] = false;
125                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
126                                return $result;
127                        }
128                }
129                else
130                {
131                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
132                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
133                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
134                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
135                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
136                }
137               
138                $result['status'] = true;
139               
140                $params = unserialize($params['attributes']);
141                $type = $params['type'];
142                $uid = $params['uid'];
143                $mail = $params['mail'];
144                $mailalternateaddress = $params['mailalternateaddress'];
145                $cpf = $params['cpf'];
146                               
147                if ($_SESSION['phpgw_info']['expresso']['global_denied_users'][$uid])
148                {
149                        $result['status'] = false;
150                        $result['msg'] = $this->functions->lang('this login can not be used because is a system account') . ".";
151                        return $result;
152                }
153               
154                if (($type == 'create_user') || ($type == 'rename_user'))
155                {
156                        if ($this->current_config['expressoAdmin_prefix_org'] == 'true')
157                        {
158                                //Obtenho UID sem a organização. Na criação o uid já vem sem a organização
159                                $tmp_uid_without_org = split("-", $params['uid']);
160                                $tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
161                                array_pop($tmp_reverse_uid_without_org);
162                                $uid_without_org = implode("-", $tmp_reverse_uid_without_org);
163                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(uid=$uid)(uid=$uid_without_org)))";
164                        }
165                        else
166                        {
167                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
168                        }
169                        /*
170                        //UID
171                        if (($type == 'rename_user') && ($this->current_config['expressoAdmin_prefix_org'] == 'true'))
172                        {
173                                //Obtenho UID sem a organização. Na criação o uid já vem sem a organização
174                                $tmp_uid_without_org = split("-", $params['uid']);
175                                $tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
176                                array_pop($tmp_reverse_uid_without_org);
177                                $uid_without_org = implode("-", $tmp_reverse_uid_without_org);
178                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(uid=$uid)(uid=$uid_without_org)))";
179                        }
180                        else
181                        {
182                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
183                        }
184                        */
185                       
186                        $justthese = array("uid", "mail", "cn");
187                        $search = ldap_search($ldap_connection, $context, $filter, $justthese);
188                        $count_entries = ldap_count_entries($ldap_connection,$search);
189                        if ($count_entries > 0)
190                        {
191                                $entries = ldap_get_entries($ldap_connection, $search);
192                               
193                                for ($i=0; $i<$entries['count']; $i++)
194                                {
195                                        $users .= $entries[$i]['cn'][0] . ' - ' . $entries[$i]['mail'][0] . "\n";
196                                }
197                               
198                                $result['status'] = false;
199                                $result['msg'] = $this->functions->lang('this login is already used by') . ":\n" . $users;
200                                return $result;
201                        }
202
203                        // GRUPOS
204                        $filter = "(&(phpgwAccountType=g)(cn=$uid))";
205                        $justthese = array("cn");
206                        $search = ldap_search($ldap_connection, $context, $filter, $justthese);
207                        $count_entries = ldap_count_entries($ldap_connection,$search);
208                        if ($count_entries > 0)
209                        {
210                                $result['status'] = false;
211                                $result['msg'] = $this->functions->lang('This login is being used by a group') . ".";
212                                return $result;
213                        }
214                       
215                       
216                        // UID em outras organizações, pesquiso apenas na maquina local e se utilizar prefix_org
217                        if ($this->current_config['expressoAdmin_prefix_org'] == 'true')
218                        {
219                                $ldap_connection2 = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
220                                ldap_set_option($ldap_connection2,LDAP_OPT_PROTOCOL_VERSION,3);
221                                ldap_set_option($ldap_connection2, LDAP_OPT_REFERRALS, false);
222                                ldap_bind($ldap_connection2, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
223                                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
224                               
225                                //Obtenho UID sem a organização
226                                /*
227                                $tmp_uid_without_org = split("-", $params['uid']);
228                                if (count($tmp_uid_without_org) < 2)
229                                {
230                                        $result['status'] = false;
231                                        $result['msg'] = 'Novo login sem organização.';
232                                        return $result;
233                                }
234                                $tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
235                                array_pop($tmp_reverse_uid_without_org);
236                                $uid_without_org = implode("-", $tmp_reverse_uid_without_org);
237                                */
238                               
239                                $filter = "(ou=*)";
240                                $justthese = array("ou");
241                                $search = ldap_list($ldap_connection2, $context, $filter, $justthese);
242                                $entries = ldap_get_entries($ldap_connection2   ,$search);
243                               
244                                foreach ($entries as $index=>$org)
245                                {
246                                        $organization = $org['ou'][0];
247                                        $organization = strtolower($organization);
248                               
249                                        $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$organization-$uid))";
250                                       
251                                        $justthese = array("uid");
252                                        $search = ldap_search($ldap_connection2, $context, $filter, $justthese);
253                                        $count_entries = ldap_count_entries($ldap_connection2,$search);
254                                        if ($count_entries > 0)
255                                        {
256                                                $result['status'] = false;
257                                                $result['msg'] = $this->functions->lang('this login is already used by a user in another organization') . ".";
258                                                ldap_close($ldap_connection2);
259                                                return $result;
260                                        }
261                                }
262                                ldap_close($ldap_connection2);
263                        }
264                }
265               
266                if ($type == 'rename_user')
267                {
268                        return $result;
269                }
270               
271                // MAIL
272                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
273                $justthese = array("mail", "uid");
274                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
275                $entries = ldap_get_entries($ldap_connection,$search);
276                if ($entries['count'] == 1){
277                        if ($entries[0]['uid'][0] != $uid){
278                                $result['status'] = false;
279                                $result['msg'] = $this->functions->lang('this email address is being used by 1 user') . ": " . $entries[0]['uid'][0];
280                                return $result;
281                        }
282                }
283                else if ($entries['count'] > 1){
284                        $result['status'] = false;
285                        $result['msg'] = $this->functions->lang('this email address is being used by 2 or more users') . ".";
286                        return $result;
287                }
288               
289                // MAILAlternateAddress
290                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mailalternateaddress)(mailalternateaddress=$mailalternateaddress)))";
291                $justthese = array("mail", "uid");
292                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
293                $entries = ldap_get_entries($ldap_connection,$search);
294                if ($entries['count'] == 1){
295                        if ($entries[0]['uid'][0] != $uid){
296                                $result['status'] = false;
297                                $result['msg'] = $this->functions->lang('alternative email is being used by 1 user') . ": " . $entries[0]['uid'][0];
298                                return $result;
299                        }
300                }
301                else if ($entries['count'] > 1){
302                        $result['status'] = false;
303                        $result['msg'] = $this->functions->lang('alternative email is being used by 2 or more users') . ".";
304                        return $result;
305                }
306
307                //Begin: Check CPF, only if the manager has access to this field.
308                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
309                {
310                        if (!empty($cpf))
311                        {
312                                if (!$this->functions->checkCPF($cpf))
313                                {
314                                        $result['status'] = false;
315                                        $result['msg'] = $this->functions->lang('Field CPF is invalid') . '.';
316                                        return $result;
317                                }
318                                else
319                                {
320                                        //retira caracteres que não são números.
321                                        $cpf = ereg_replace("[^0-9]", "", $cpf);
322                               
323                                        $local_ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
324                                        if ($ldap_connection)
325                                        {
326                                                ldap_set_option($local_ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
327                                                ldap_set_option($local_ldap_connection, LDAP_OPT_REFERRALS, false);
328                                                ldap_bind($local_ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
329                                        }
330                                        else
331                                        {
332                                                $result['status'] = false;
333                                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
334                                                return $result;
335                                        }
336                               
337                                        $filter = "(&(phpgwAccountType=u)(cpf=$cpf))";
338                                        $justthese = array("cn","uid");
339                                        $search = ldap_search($local_ldap_connection, $context, $filter, $justthese);
340                                        $entries = ldap_get_entries($local_ldap_connection,$search);
341                               
342                                        if ( ($entries['count'] != 1) && (strcasecmp($uid, $entries[0]['uid'][0]) != 0) )
343                                        {
344                                                if ($entries['count'] > 0)
345                                                {
346                                                        $result['question'] = $this->functions->lang('Field CPF used by') . ":\n";
347                                                        for ($i=0; $i<$entries['count']; $i++)
348                                                        {
349                                                                if (strcasecmp($uid, $entries[$i]['uid'][0]) != 0)
350                                                                        $result['question'] .= "- " . $entries[$i]['cn'][0] . "\n";
351                                                        }
352                                                        $result['question'] .= $this->functions->lang("Do you want to continue anyway") . "?";
353                                                        return $result;
354                                                }
355                                        }
356                                        ldap_close($local_ldap_connection);
357                                }
358                        }
359                        else if ($this->current_config['expressoAdmin_cpf_obligation'])
360                        {
361                                $result['status'] = false;
362                                $result['msg'] = $this->functions->lang('Field CPF must be completed') . '.';
363                                return $result;
364                        }
365                }
366                //End: Check CPF
367
368                return $result;
369        }
370       
371        function validate_fields_group($params)
372        {
373                /* ldap connection following referals and using Contac Center config*/
374                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
375                {
376                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
377                        if ($ldap_connection)
378                        {
379                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
380                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
381                                if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
382                                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
383                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
384                        }
385                        else
386                        {
387                                $result['status'] = false;
388                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
389                                return $result;
390                        }
391                }
392                else
393                {
394                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
395                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
396                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
397                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
398                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
399                }
400
401                $cn = $params['cn'];
402                $result['status'] = true;
403               
404                if ($_SESSION['phpgw_info']['expresso']['global_denied_groups'][$cn])
405                {
406                        $result['status'] = false;
407                        $result['msg'] = $this->functions->lang('This group name can not be used because is a System Account') . ".";
408                        return $result;
409                }
410               
411                // CN
412                $filter = "(&(phpgwAccountType=g)(cn=$cn))";
413                $justthese = array("cn");
414                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
415                $count_entries = ldap_count_entries($ldap_connection,$search);
416                if ($count_entries > 0)
417                {
418                        $result['status'] = false;
419                        $result['msg'] = $this->functions->lang('This name is already used') . ".";
420                        return $result;
421                }
422               
423                // UID
424                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$cn))";
425                $justthese = array("uid");
426                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
427                $count_entries = ldap_count_entries($ldap_connection,$search);
428                if ($count_entries > 0)
429                {
430                        $result['status'] = false;
431                        $result['msg'] = $this->functions->lang('This grupo name is already used by an user') . ".";
432                        return $result;
433                }
434               
435                return $result;
436        }
437       
438        function validate_fields_maillist($params)
439        {
440                /* ldap connection following referals and using Contac Center config*/
441                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
442                {
443                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
444                        if ($ldap_connection)
445                        {
446                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
447                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
448                                if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
449                                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
450                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
451                        }
452                        else
453                        {
454                                $result['status'] = false;
455                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
456                                return $result;
457                        }
458                }
459                else
460                {
461                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
462                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
463                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
464                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
465                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
466                }
467               
468                $uid = $params['uid'];
469                $mail = $params['mail'];
470                $result['status'] = true;
471               
472                if ($_SESSION['phpgw_info']['expresso']['global_denied_users'][$uid])
473                {
474                        $result['status'] = false;
475                        $result['msg'] = $this->functions->lang('This LOGIN can not be used because is a System Account') . ".";
476                        return $result;
477                }
478               
479                // UID
480                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
481                $justthese = array("uid");
482                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
483                $count_entries = ldap_count_entries($ldap_connection,$search);
484                if ($count_entries > 0)
485                {
486                        $result['status'] = false;
487                        $result['msg'] = $this->functions->lang('this email list login is already used') . ".";
488                        return $result;
489                }
490               
491                // MAIL
492                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
493                $justthese = array("mail");
494                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
495                $count_entries = ldap_count_entries($ldap_connection,$search);
496                if ($count_entries > 0)
497                {
498                        $result['status'] = false;
499                        $result['msg'] = $this->functions->lang('this email address is already used') . ".";
500                        return $result;
501                }
502               
503                return $result;
504        }
505
506        //Busca usuários de um contexto e já retorna as options do select;
507        function get_available_users($params)
508        {
509                $context = $params['context'];
510                $recursive = $params['recursive'];
511                $justthese = array("cn", "uidNumber");
512                $filter="(phpgwAccountType=u)";
513               
514                if ($recursive == 'true')
515                        $groups_list=ldap_search($this->ldap, $context, $filter, $justthese);
516                else
517                $groups_list=ldap_list($this->ldap, $context, $filter, $justthese);
518       
519        $entries = ldap_get_entries($this->ldap, $groups_list);
520       
521                for ($i=0; $i<$entries["count"]; $i++){
522                        $u_tmp[$entries[$i]["uidnumber"][0]] = $entries[$i]["cn"][0];
523                }
524                       
525                if (count($u_tmp))
526                        natcasesort($u_tmp);
527
528                $i = 0;
529                $users = array();
530                       
531                if (count($u_tmp))
532                {
533                        foreach ($u_tmp as $uidnumber => $cn)
534                        {
535                                $options .= "<option value=$uidnumber>$cn</option>";
536                        }
537                        unset($u_tmp);
538                }
539
540        return $options;
541        }
542
543        //Busca usuários e listas de um contexto e já retorna as options do select;
544        function get_available_users_and_maillist($params)
545        {
546                $context = $params['context'];
547                $recursive = $params['recursive'];
548               
549                //Usado para retirar a própria lista das possibilidades de inclusão.
550                $denied_uidnumber = $params['denied_uidnumber'];
551               
552                $justthese = array("cn", "uidNumber", "mail");
553               
554                $users_filter="(phpgwAccountType=u)";
555                $lists_filter = $denied_uidnumber == '' ? "(phpgwAccountType=l)" : "(&(phpgwAccountType=l)(!(uidnumber=$denied_uidnumber)))";
556               
557                $users = Array();
558                $lists = Array();               
559
560                /* folling referral connection */
561                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
562                if ($ldap_conn_following_ref)
563                {
564                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
565                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
566
567                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
568                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
569                }
570                else
571                        return false;
572
573                if ($recursive == 'true')
574                {
575                        $lists_search = ldap_search($ldap_conn_following_ref, $context, $lists_filter, $justthese);
576                        $users_search = ldap_search($ldap_conn_following_ref, $context, $users_filter, $justthese);
577                }
578                else
579                {
580                        $lists_search = ldap_list($ldap_conn_following_ref, $context, $lists_filter, $justthese);
581                        $users_search = ldap_list($ldap_conn_following_ref, $context, $users_filter, $justthese);
582                }
583               
584                /* email lists */
585                $lists_entries = ldap_get_entries($ldap_conn_following_ref, $lists_search);
586               
587                for ($i=0; $i<$lists_entries["count"]; $i++)
588                {
589                        $l_tmp[$lists_entries[$i]["mail"][0]] = $lists_entries[$i]["cn"][0];
590                }
591                       
592                if (count($l_tmp))
593                        natcasesort($l_tmp);
594                       
595                $i = 0;
596                $lists = array();
597               
598                $options .= '<option  value="-1" disabled>------------------------------&nbsp;&nbsp;&nbsp;&nbsp;Listas&nbsp;&nbsp;&nbsp;&nbsp;------------------------------ </option>'."\n";   
599                if (count($l_tmp))
600                {
601                        foreach ($l_tmp as $mail => $cn)
602                        {
603                                $options .= "<option value=$mail>$cn</option>";
604                        }
605                        unset($l_tmp);
606                }
607               
608                /* users */
609                $users_entries = ldap_get_entries($ldap_conn_following_ref, $users_search);
610                for ($i=0; $i<$users_entries["count"]; $i++)
611                {
612                        $u_tmp[$users_entries[$i]["mail"][0]] = $users_entries[$i]["cn"][0];
613                }
614                       
615                if (count($u_tmp))
616                        natcasesort($u_tmp);
617                       
618                $i = 0;
619                $users = array();
620               
621                $options .= '<option  value="-1" disabled>-----------------------------&nbsp;&nbsp;&nbsp;&nbsp;Usuários&nbsp;&nbsp;&nbsp;&nbsp;---------------------------- </option>'."\n";   
622                if (count($u_tmp))
623                {
624                        foreach ($u_tmp as $mail => $cn)
625                        {
626                                $options .= "<option value=$mail class='line-above'>$cn</option>";
627                        }
628                        unset($u_tmp);
629                }
630               
631                ldap_close($ldap_conn_following_ref);
632                return $options;
633        }
634
635        function get_available_groups($params)
636        {
637                $context = $params['context'];
638                $justthese = array("cn", "gidNumber");
639        $groups_list=ldap_list($this->ldap, $context, ("(phpgwAccountType=g)"), $justthese);
640        ldap_sort($this->ldap, $groups_list, "cn");
641       
642        $entries = ldap_get_entries($this->ldap, $groups_list);
643               
644                $options = '';
645                for ($i=0; $i<$entries['count']; $i++)
646                {
647                        $options .= "<option value=" . $entries[$i]['gidnumber'][0] . ">" . $entries[$i]['cn'][0] . "</option>";
648                }
649       
650        return $options;               
651        }
652       
653        function get_available_maillists($params)
654        {
655                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
656                        return false;
657               
658                $context = $params['context'];
659                $justthese = array("uid","mail","uidNumber");
660        $maillists=ldap_list($ldapMasterConnect, $context, ("(phpgwAccountType=l)"), $justthese);
661        ldap_sort($ldapMasterConnect, $maillists, "uid");
662       
663        $entries = ldap_get_entries($ldapMasterConnect, $maillists);
664       
665                $options = '';                 
666                for ($i=0; $i<$entries['count']; $i++)
667                {
668                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['uid'][0] . " (" . $entries[$i]['mail'][0] . ")" . "</option>";
669                }
670       
671        ldap_close($ldapMasterConnect);
672        return $options;
673        }
674       
675        function ldap_add_entry($dn, $entry)
676        {
677                $result = array();
678                if (!@ldap_add ( $this->ldap, $dn, $entry ))
679                {
680                        $result['status']               = false;
681                        $result['error_number'] = ldap_errno($this->ldap);
682                        $result['msg']                  = $this->functions->lang('Error on function') . " ldap_functions->ldap_add_entry ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_errno($this->ldap) . ldap_error($this->ldap);
683                }
684                else
685                        $result['status'] = true;
686               
687                return $result;
688        }
689       
690        function ldap_save_photo($dn, $pathphoto, $photo_exist=false)
691        {
692                $fd = fopen($pathphoto, "r");
693                $fsize = filesize($pathphoto);
694                $jpegStr = fread($fd, $fsize);
695                fclose ($fd);
696                $attrs['jpegPhoto'] = $jpegStr;
697                       
698                if ($photo_exist)
699                        $res = @ldap_mod_replace($this->ldap, $dn, $attrs);
700                else
701                        $res = @ldap_mod_add($this->ldap, $dn, $attrs);
702                       
703                if ($res)
704                {
705                        $result['status'] = true;
706                }
707                else
708                {
709                        $result['status'] = false;
710                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_save_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
711                }
712               
713                return $result;
714        }
715       
716        function ldap_remove_photo($dn)
717        {
718                $attrs['jpegPhoto'] = array();
719                $res = ldap_mod_del($this->ldap, $dn, $attrs);
720               
721                if ($res)
722                {
723                        $result['status'] = true;
724                }
725                else
726                {
727                        $result['status'] = false;
728                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_remove_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
729                }
730               
731                return $result;
732        }       
733       
734        // Pode receber tanto um único memberUid quanto um array de memberUid's
735        function add_user2group($gidNumber, $memberUid)
736        {
737                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
738                $justthese = array("dn");
739                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
740                $entry = ldap_get_entries($this->ldap, $search);
741                $group_dn = $entry[0]['dn'];
742                $attrs['memberUid'] = $memberUid;
743               
744                $res = @ldap_mod_add($this->ldap, $group_dn, $attrs);
745               
746                if ($res)
747                {
748                        $result['status'] = true;
749                }
750                else
751                {
752                        $result['status'] = false;
753                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
754                }
755                return $result;
756        }
757       
758        function remove_user2group($gidNumber, $memberUid)
759        {
760                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
761                $justthese = array("dn");
762                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
763                $entry = ldap_get_entries($this->ldap, $search);
764                $group_dn = $entry[0]['dn'];
765                $attrs['memberUid'] = $memberUid;
766                $res = @ldap_mod_del($this->ldap, $group_dn, $attrs);
767               
768                if ($res)
769                {
770                        $result['status'] = true;
771                }
772                else
773                {
774                        $result['status'] = false;
775                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
776                }
777                return $result;
778        }
779       
780        function add_user2maillist($uid, $mail)
781        {
782                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
783                {
784                        $result['status'] = false;
785                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
786                        return $result;
787                }
788                       
789                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
790                $justthese = array("dn");
791                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
792                $entry = ldap_get_entries($ldapMasterConnect, $search);
793                $group_dn = $entry[0]['dn'];
794                $attrs['mailForwardingAddress'] = $mail;
795                $res = @ldap_mod_add($ldapMasterConnect, $group_dn, $attrs);
796               
797                if ($res)
798                {
799                        $result['status'] = true;
800                }
801                else
802                {
803                        $result['status'] = false;
804                        if (ldap_errno($ldapMasterConnect) == '50')
805                        {
806                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
807                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
808                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
809                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
810                        }                                       
811                        else
812                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
813                }
814               
815                ldap_close($ldapMasterConnect);
816                return $result;
817        }
818       
819        function add_user2maillist_scl($dn, $array_emails)
820        {
821                $attrs['mailSenderAddress'] = $array_emails;
822               
823                $res = @ldap_mod_add($this->ldap, $dn, $attrs);
824               
825                if ($res)
826                {
827                        $result['status'] = true;
828                }
829                else
830                {
831                        $result['status'] = false;
832                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
833                }
834                return $result;
835        }
836
837        function remove_user2maillist($uid, $mail)
838        {
839                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
840                {
841                        $result['status'] = false;
842                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
843                        return $result;
844                }
845               
846                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
847                $justthese = array("dn");
848                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
849                $entry = ldap_get_entries($ldapMasterConnect, $search);
850                $group_dn = $entry[0]['dn'];
851                $attrs['mailForwardingAddress'] = $mail;
852                $res = @ldap_mod_del($ldapMasterConnect, $group_dn, $attrs);
853               
854                if ($res)
855                {
856                        $result['status'] = true;
857                }
858                else
859                {
860                        $result['status'] = false;
861                        if (ldap_errno($ldapMasterConnect) == '50')
862                        {
863                               
864                                $return['msg'] = $this->functions->lang('You do not have right to create institutional accounts') . ".";
865
866                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
867                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
868                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
869                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
870                        }                                       
871                        else
872                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
873                }
874                ldap_close($ldapMasterConnect);
875                return $result;
876        }
877
878        function remove_user2maillist_scl($dn, $array_emails)
879        {
880                $attrs['mailSenderAddress'] = $array_emails;
881                $res = @ldap_mod_del($this->ldap, $dn, $attrs);
882               
883                if ($res)
884                {
885                        $result['status'] = true;
886                }
887                else
888                {
889                        $result['status'] = false;
890                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
891                }
892                return $result;
893        }
894
895        function replace_user2maillists($new_mail, $old_mail)
896        {
897                $filter = "(&(phpgwAccountType=l)(mailforwardingaddress=$old_mail))";
898                $justthese = array("dn");
899                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
900                $entries = ldap_get_entries($this->ldap, $search);
901                $result['status'] = true;
902                for ($i=0; $i<$entries['count']; $i++)
903                {
904                        $attrs['mailforwardingaddress'] = $old_mail;
905                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
906                        $attrs['mailforwardingaddress'] = $new_mail;
907                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
908               
909                        if ((!$res1) || (!$res2))
910                        {
911                                $result['status'] = false;
912                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user2maillists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
913                        }
914                }
915               
916                return $result;
917        }
918       
919        function get_user_info($uidnumber)
920        {
921                foreach ($this->manager_contexts as $index=>$context)
922                {
923                        $filter="(&(phpgwAccountType=u)(uidNumber=".$uidnumber."))";
924                        $search = ldap_search($this->ldap, $context, $filter);
925                        $entry = ldap_get_entries($this->ldap, $search);
926                       
927                        if ($entry['count'])
928                        {
929                                //Pega o dn do setor do usuario.
930                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
931                                $sector_dn_array = explode(",", $entry[0]['dn']);
932                                for($i=1; $i<count($sector_dn_array); $i++)
933                                        $sector_dn .= $sector_dn_array[$i] . ',';
934                                //Retira ultimo pipe.
935                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
936               
937                                $result['context']                              = $sector_dn;
938                                $result['uid']                                  = $entry[0]['uid'][0];
939                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
940                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
941                                $result['departmentnumber']             = $entry[0]['departmentnumber'][0];
942                                $result['givenname']                    = $entry[0]['givenname'][0];
943                                $result['sn']                                   = $entry[0]['sn'][0];
944                                $result['telephonenumber']              = $entry[0]['telephonenumber'][0];
945                                $result['passwd_expired']               = $entry[0]['phpgwlastpasswdchange'][0];
946                                $result['phpgwaccountstatus']   = $entry[0]['phpgwaccountstatus'][0];
947                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
948                                $result['accountstatus']                = $entry[0]['accountstatus'][0];
949                                $result['mail']                                 = $entry[0]['mail'][0];
950                                $result['mailalternateaddress'] = $entry[0]['mailalternateaddress'];
951                                $result['mailforwardingaddress']= $entry[0]['mailforwardingaddress'];
952                                $result['deliverymode']                 = $entry[0]['deliverymode'][0];
953                                $result['userPasswordRFC2617']  = $entry[0]['userpasswordrfc2617'][0];
954
955                                //Photo
956                                if ($entry[0]['jpegphoto']['count'] == 1)
957                                        $result['photo_exist'] = 'true';
958               
959                                // Samba
960                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
961                                {
962                                        if ($entry[0]['objectclass'][$i] == 'sambaSamAccount')
963                                                $result['sambaUser'] = true;
964                                }
965                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($result['sambaUser']))
966                                {
967                                        $result['sambaaccflags'] = $entry[0]['sambaacctflags'][0];
968                                        $result['sambalogonscript'] = $entry[0]['sambalogonscript'][0];
969                                        $result['homedirectory'] = $entry[0]['homedirectory'][0];
970                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
971                                        array_pop($a_tmp);
972                                        $result['sambasid'] = implode("-", $a_tmp);
973                                }
974
975                                // Verifica o acesso do gerente aos atributos corporativos
976                                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
977                                {
978                                        $result['corporative_information_employeenumber']= $entry[0]['employeenumber'][0];
979                                        $result['corporative_information_cpf']                  = $entry[0]['cpf'][0];
980                                        $result['corporative_information_rg']                   = $entry[0]['rg'][0];
981                                        $result['corporative_information_rguf']                 = $entry[0]['rguf'][0];
982                                        $result['corporative_information_description']  = utf8_decode($entry[0]['description'][0]);
983                                }
984                               
985                                // MailLists
986                                $result['maillists_info'] = $this->get_user_maillists($result['mail']);
987                                if($result['maillists_info'])
988                                {
989                                        foreach ($result['maillists_info'] as $maillist)
990                                        {
991                                                $result['maillists'][] = $maillist['uid'];
992                                        }
993                                }
994                               
995                                // Groups
996                                $justthese = array("gidnumber","cn");
997                                $filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
998                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
999                        ldap_sort($this->ldap, $search, "cn");
1000                        $entries = ldap_get_entries($this->ldap, $search);
1001                        for ($i=0; $i<$entries['count']; $i++)
1002                        {
1003                                $result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
1004                        }
1005                        }
1006                }
1007                return $result;
1008        }
1009               
1010        function get_user_maillists($mail)
1011        {
1012                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1013                        return false;
1014               
1015                $result = array();
1016               
1017                //Mostra somente os mailists dos contextos do gerente
1018                $justthese = array("uid","mail","uidnumber");
1019                $filter="(&(phpgwAccountType=l)(mailforwardingaddress=$mail))";
1020               
1021                foreach ($this->manager_contexts as $index=>$context)
1022                {
1023                        $search = ldap_search($ldapMasterConnect, $context, $filter, $justthese);
1024                $entries = ldap_get_entries($ldapMasterConnect, $search);
1025               
1026                for ($i=0; $i<$entries['count']; $i++)
1027                {
1028                                $result[ $entries[$i]['uid'][0] ]['uid']                = $entries[$i]['uid'][0];
1029                                $result[ $entries[$i]['uid'][0] ]['mail']               = $entries[$i]['mail'][0];
1030                               
1031                                $a_tmp[] = $entries[$i]['uid'][0];
1032                }
1033                }
1034       
1035        if($a_tmp) {
1036                natcasesort($a_tmp);
1037       
1038                foreach ($a_tmp as $uid)
1039                {
1040                                $return[$uid]['uid']            = $result[$uid]['uid'];
1041                                $return[$uid]['mail']           = $result[$uid]['mail'];
1042                }
1043        }
1044        ldap_close($ldapMasterConnect);
1045                return $return;
1046        }
1047       
1048        function get_group_info($gidnumber)
1049        {
1050                foreach ($this->manager_contexts as $index=>$context)
1051                {
1052                        $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1053                        $search = ldap_search($this->ldap, $context, $filter);
1054                        $entry = ldap_get_entries($this->ldap, $search);
1055                       
1056                        if ($entry['count'])
1057                        {
1058                                //Pega o dn do setor do grupo.
1059                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1060                                $sector_dn_array = explode(",", $entry[0]['dn']);
1061                                for($i=1; $i<count($sector_dn_array); $i++)
1062                                        $sector_dn .= $sector_dn_array[$i] . ',';
1063                                //Retira ultimo pipe.
1064                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1065               
1066                                $result['context']                              = $sector_dn;
1067                                $result['cn']                                   = $entry[0]['cn'][0];
1068                                $result['description']                  = $entry[0]['description'][0];
1069                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
1070                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
1071                                $result['email']                                = $entry[0]['mail'][0];
1072               
1073                                //MemberUid
1074                                for ($i=0; $i<$entry[0]['memberuid']['count']; $i++)
1075                                {
1076                                        $justthese = array("cn","uid","uidnumber");
1077                       
1078                                        // Montagem dinamica do filtro
1079                                        $filter="(&(phpgwAccountType=u)(|";
1080                                        for ($k=0; (($k<10) && ($i<$entry[0]['memberuid']['count'])); $k++)
1081                                        {
1082                                                $filter .= "(uid=".$entry[0]['memberuid'][$i].")";
1083                                                $i++;
1084                                        }
1085                                        $i--;
1086                                        $filter .= "))";
1087                       
1088                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1089                                        $user_entry = ldap_get_entries($this->ldap, $search);
1090
1091                                        for ($j=0; $j<$user_entry['count']; $j++)
1092                                        {
1093                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['cn'] = $user_entry[$j]['cn'][0];
1094                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['uidnumber'] = $user_entry[$j]['uidnumber'][0];
1095                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['type'] = 'u';
1096                                        }
1097                                }
1098               
1099                                // Checamos e-mails que não fazem parte do expresso.
1100                                // Criamos um array temporario
1101                                $tmp_array = array();
1102                                if($result['memberuid_info'])
1103                                        foreach ($result['memberuid_info'] as $uid => $user_data)
1104                                        {
1105                                                $tmp_array[] = $uid;
1106                                        }
1107               
1108                                if($entry[0]['memberuid']) {
1109                                        // Retira o count do array
1110                                        array_shift($entry[0]['memberuid']);
1111                                        // Vemos a diferença
1112                                        $array_diff = array_diff($entry[0]['memberuid'], $tmp_array);
1113                                        // Incluimos no resultado                       
1114                                        foreach ($array_diff as $index=>$uid)
1115                                        {
1116                                                $result['memberuid_info'][$uid]['cn'] = $uid;
1117                                        }
1118                                }
1119               
1120                                // Samba
1121                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1122                                {
1123                                        if ($entry[0]['objectclass'][$i] == 'sambaGroupMapping')
1124                                                $result['sambaGroup'] = true;
1125
1126                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1127                                        array_pop($a_tmp);
1128                                        $result['sambasid'] = implode("-", $a_tmp);
1129                                }
1130                                return $result;
1131                        }
1132                }
1133        }       
1134       
1135        function get_maillist_info($uidnumber)
1136        {
1137                /* folling referral connection */
1138                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
1139                if ($ldap_conn_following_ref)
1140                {
1141                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
1142                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
1143
1144                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
1145                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
1146                }
1147               
1148                foreach ($this->manager_contexts as $index=>$context)
1149                {
1150                        $filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
1151                        $search = ldap_search($this->ldap, $context, $filter);
1152                        $entry = ldap_get_entries($this->ldap, $search);
1153                       
1154                        if ($entry['count'])
1155                        {
1156                                //Pega o dn do setor do usuario.
1157                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1158                                $sector_dn_array = explode(",", $entry[0]['dn']);
1159                                for($i=1; $i<count($sector_dn_array); $i++)
1160                                        $sector_dn .= $sector_dn_array[$i] . ',';
1161                                //Retira ultimo pipe.
1162                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1163                       
1164                                $result['context']                              = $sector_dn;
1165                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
1166                                $result['uid']                                  = strtolower($entry[0]['uid'][0]);
1167                                $result['cn']                                   = $entry[0]['cn'][0];
1168                                $result['mail']                                 = $entry[0]['mail'][0];
1169                                $result['description']                  = utf8_decode($entry[0]['description'][0]);
1170                                $result['accountStatus']                = $entry[0]['accountstatus'][0];
1171                                $result['phpgwAccountVisible']  = $entry[0]['phpgwaccountvisible'][0];
1172                       
1173                                //Members
1174                                for ($i=0; $i<$entry[0]['mailforwardingaddress']['count']; $i++)
1175                                {
1176                                        $justthese = array("cn", "uidnumber", "uid", "phpgwaccounttype", "mail");
1177                               
1178                                        // Montagem dinamica do filtro, para nao ter muitas conexoes com o ldap
1179                                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|";
1180                                        for ($k=0; (($k<10) && ($i<$entry[0]['mailforwardingaddress']['count'])); $k++)
1181                                        {
1182                                                $filter .= "(mail=".$entry[0]['mailforwardingaddress'][$i].")";
1183                                                $i++;
1184                                        }
1185                                        $i--;
1186                                        $filter .= "))";
1187                               
1188                                        $search = ldap_search($ldap_conn_following_ref, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1189                                        $user_entry = ldap_get_entries($ldap_conn_following_ref, $search);
1190                                                                       
1191                                        for ($j=0; $j<$user_entry['count']; $j++)
1192                                        {
1193                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['uid'] = $user_entry[$j]['uid'][0];
1194                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['cn'] = $user_entry[$j]['cn'][0];
1195                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['type'] = $user_entry[$j]['phpgwaccounttype'][0];
1196                                                $result['mailForwardingAddress'][] = $user_entry[$j]['mail'][0];
1197                                        }
1198                                }
1199
1200                                // Emails não encontrados no ldap
1201                                array_shift($entry[0]['mailforwardingaddress']); //Retira o count do array
1202                                $missing_emails = array_diff($entry[0]['mailforwardingaddress'], $result['mailForwardingAddress']);
1203                               
1204                                // Incluimos estes no resultado
1205                                foreach ($missing_emails as $index=>$mailforwardingaddress)
1206                                {
1207                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['uid'] = $mailforwardingaddress;
1208                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['cn'] = 'E-Mail nao encontrado';
1209                                        $result['mailForwardingAddress'][] = $mailforwardingaddress;
1210                                }
1211                               
1212                                ldap_close($ldap_conn_following_ref);
1213                                return $result;
1214                        }
1215                }
1216        }       
1217
1218        function get_maillist_scl_info($uidnumber)
1219        {
1220                foreach ($this->manager_contexts as $index=>$context)
1221                {
1222                        $filter="(&(phpgwAccountType=l)(uidNumber=$uidnumber))";
1223                        $search = ldap_search($this->ldap, $context, $filter);
1224                        $entry = ldap_get_entries($this->ldap, $search);
1225
1226                        if ($entry['count'])
1227                        {
1228                                //Pega o dn do setor do usuario.
1229                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1230                                $sector_dn_array = explode(",", $entry[0]['dn']);
1231                                for($i=1; $i<count($sector_dn_array); $i++)
1232                                        $sector_dn .= $sector_dn_array[$i] . ',';
1233                                //Retira ultimo pipe.
1234                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1235               
1236                                $result['dn']                                           = $entry[0]['dn'];
1237                                $result['context']                                      = $sector_dn;
1238                                $result['uidnumber']                            = $entry[0]['uidnumber'][0];
1239                                $result['uid']                                          = $entry[0]['uid'][0];
1240                                $result['cn']                                           = $entry[0]['cn'][0];
1241                                $result['mail']                                         = $entry[0]['mail'][0];
1242                                $result['accountStatus']                        = $entry[0]['accountstatus'][0];
1243                                $result['phpgwAccountVisible']          = $entry[0]['phpgwaccountvisible'][0];
1244                                $result['accountRestrictive']           = $entry[0]['accountrestrictive'][0];
1245                                $result['participantCanSendMail']       = $entry[0]['participantcansendmail'][0];
1246               
1247                                //Senders
1248                                for ($i=0; $i<$entry[0]['mailsenderaddress']['count']; $i++)
1249                                {
1250                                        $justthese = array("cn", "uidnumber", "uid", "mail");
1251                                        $filter="(&(phpgwAccountType=u)(mail=".$entry[0]['mailsenderaddress'][$i]."))";
1252                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1253                                        $user_entry = ldap_get_entries($this->ldap, $search);
1254                       
1255                                        $result['senders_info'][$user_entry[0]['mail'][0]]['uid'] = $user_entry[0]['uid'][0];
1256                                        $result['senders_info'][$user_entry[0]['mail'][0]]['cn'] = $user_entry[0]['cn'][0];
1257                                        $result['members'][] = $user_entry[0]['mail'][0];
1258                                }
1259                                return $result;
1260                        }
1261                }
1262        }       
1263
1264        function group_exist($gidnumber)
1265        {
1266                $justthese = array("cn");
1267                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1268                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1269                               
1270                $entry = ldap_get_entries($this->ldap, $search);
1271                if ($entry['count'] == 0)
1272                        return false;
1273                else
1274                        return true;
1275        }
1276
1277        function gidnumbers2cn($gidnumbers)
1278        {
1279                $result = array();
1280                if (count($gidnumbers))
1281                {
1282                        $justthese = array("cn");
1283                        $i = 0;
1284                        foreach ($gidnumbers as $gidnumber)
1285                        {
1286                                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1287                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1288                               
1289                                $entry = ldap_get_entries($this->ldap, $search);
1290                                if ($entry['count'] == 0)
1291                                        $result['groups_info'][$i]['cn'] = '_Grupo existe no BD mas não no LDAP';
1292                                else
1293                                        $result['groups_info'][$i]['cn'] = $entry[0]['cn'][0];
1294                                $result['groups_info'][$i]['gidnumber'] = $gidnumber;
1295                       
1296                                /* o gerente pode excluir um grupo de um usuario onde este grupo esta em outra OU ? */
1297                                /* é o mesmo que o manager editar um grupo de outra OU */
1298                                $result['groups_info'][$i]['group_disabled'] = 'true';
1299                                foreach ($this->manager_contexts as $index=>$context)
1300                                {
1301                                        if (strpos(strtolower($entry[0]['dn']), strtolower($context)))
1302                                        {
1303                                                $result['groups_info'][$i]['group_disabled'] = 'false';
1304                                        }
1305                                }
1306
1307                                $i++;
1308                        }
1309                }
1310                return $result;
1311        }
1312
1313        function uidnumber2uid($uidnumber)
1314        {
1315                $justthese = array("uid");
1316                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1317                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1318                $entry = ldap_get_entries($this->ldap, $search);
1319                return $entry[0]['uid'][0];
1320        }
1321
1322        function uidnumber2mail($uidnumber)
1323        {
1324                $justthese = array("mail");
1325                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1326                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1327                $entry = ldap_get_entries($this->ldap, $search);
1328                return $entry[0]['mail'][0];
1329        }
1330
1331       
1332        function change_user_context($dn, $newrdn, $newparent)
1333        {
1334                if (!ldap_rename ( $this->ldap, $dn, $newrdn, $newparent, true ))
1335                {
1336                        $return['status'] = false;
1337                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->change_user_context ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1338                }
1339                else
1340                        $return['status'] = true;
1341               
1342                return $return;
1343        }
1344       
1345        function replace_user_attributes($dn, $ldap_mod_replace)
1346        {
1347                if (!@ldap_mod_replace ( $this->ldap, $dn, $ldap_mod_replace ))
1348                {
1349                        $return['status'] = false;
1350                        $return['error_number'] = ldap_errno($this->ldap);
1351                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1352                }
1353                else
1354                        $return['status'] = true;
1355               
1356                return $return;
1357        }
1358       
1359        function add_user_attributes($dn, $ldap_add)
1360        {
1361                if (!@ldap_mod_add ( $this->ldap, $dn, $ldap_add ))
1362                {
1363                        $return['status'] = false;
1364                        $return['error_number'] = ldap_errno($this->ldap);
1365                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1366                }
1367                else
1368                        $return['status'] = true;
1369               
1370                return $return;
1371        }
1372       
1373        function remove_user_attributes($dn, $ldap_remove)
1374        {
1375                if (!@ldap_mod_del ( $this->ldap, $dn, $ldap_remove ))
1376                {
1377                        $return['status'] = false;
1378                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1379                }
1380                else
1381                        $return['status'] = true;
1382               
1383                return $return;
1384        }
1385       
1386        function set_user_password($uid, $password)
1387        {
1388                $justthese = array("userPassword");
1389                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1390                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1391            $entry = ldap_get_entries($this->ldap, $search);
1392                $dn = $entry[0]['dn'];
1393                $userPassword = $entry[0]['userpassword'][0];
1394                $ldap_mod_replace['userPassword'] = $password;
1395                $this->replace_user_attributes($dn, $ldap_mod_replace);
1396                return $userPassword;
1397        }
1398       
1399        function delete_user($user_info)
1400        {
1401                // Verifica acesso do gerente (OU) ao tentar deletar um usuário.
1402                $manager_access = false;
1403                foreach ($this->manager_contexts as $index=>$context)
1404                {
1405                        if ( (strpos(strtolower($user_info['context']), strtolower($context))) || (strtolower($user_info['context']) == strtolower($context)) )
1406                        {
1407                                $manager_access = true;
1408                                break;
1409                        }
1410                }
1411                if (!$manager_access)
1412                {
1413                        $return['status'] = false;
1414                        $result['msg'] = $this->functions->lang('You do not have access to delete this user') . ".";
1415                        return $return;
1416                }
1417               
1418                $return['status'] = true;
1419                $return['msg'] = "";
1420                               
1421                // GROUPS
1422                $attrs = array();
1423                $attrs['memberuid'] = $user_info['uid'];
1424               
1425                if (count($user_info['groups_info']))
1426                {
1427                        foreach ($user_info['groups_info'] as $group_info)
1428                        {
1429                                $gidnumber = $group_info['gidnumber'];
1430                                $justthese = array("dn");
1431                                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1432                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1433                        $entry = ldap_get_entries($this->ldap, $search);
1434                                $dn = $entry[0]['dn'];
1435
1436                                if (!@ldap_mod_del($this->ldap, $dn, $attrs))
1437                                {
1438                                        $return['status'] = false;
1439                                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user from group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1440                                }
1441                        }
1442                }
1443               
1444                //INSTITUTIONAL ACCOUNTS
1445                $attrs = array();
1446                $attrs['mailForwardingAddress'] = $user_info['mail'];
1447               
1448                $justthese = array("dn");
1449                $filter="(&(phpgwAccountType=i)(mailforwardingaddress=".$user_info['mail']."))";
1450                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1451            $entries = ldap_get_entries($this->ldap, $search);
1452               
1453                for ($i=0; $i<$entries['count']; $i++)
1454                {
1455                        if ( !@ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs) )
1456                        {
1457                                $result['status'] = false;
1458                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, institutional accounts ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1459                        }
1460                }
1461               
1462                // MAILLISTS
1463                $attrs = array();
1464                $attrs['mailForwardingAddress'] = $user_info['mail'];
1465               
1466                if (count($user_info['maillists_info']))
1467                {
1468                       
1469                        if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1470                        {
1471                                $return['status'] = false;
1472                                $result['msg'] = $this->functions->lang('Connection with ldap_master fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1473                                return $return;
1474                        }
1475                       
1476                        foreach ($user_info['maillists_info'] as $maillists_info)
1477                        {
1478                                $uid = $maillists_info['uid'];
1479                                $justthese = array("dn");
1480                                $filter="(&(phpgwAccountType=l)(uid=".$uid."))";
1481                                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1482                        $entry = ldap_get_entries($ldapMasterConnect, $search);
1483                                $dn = $entry[0]['dn'];
1484                       
1485                                if (!@ldap_mod_del($ldapMasterConnect, $dn, $attrs))
1486                                {
1487                                        $return['status'] = false;
1488                                        if (ldap_errno($ldapMasterConnect) == '50')
1489                                        {
1490                                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
1491                                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
1492                                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
1493                                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
1494                                        }
1495                                        else
1496                                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1497                                }
1498                        }
1499                        ldap_close($ldapMasterConnect);
1500                }
1501                       
1502                // UID
1503                $dn = "uid=" . $user_info['uid'] . "," . $user_info['context'];
1504                if (!@ldap_delete($this->ldap, $dn))
1505                {
1506                        $return['status'] = false;
1507                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1508                }
1509               
1510                return $return;
1511        }
1512       
1513        function delete_maillist($uidnumber, $mail)
1514        {
1515                $return['status'] = true;
1516               
1517                $justthese = array("dn");
1518               
1519                // remove listas dentro de listas
1520                $filter="(&(phpgwAccountType=l)(mailForwardingAddress=".$mail."))";
1521                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1522                $entry = ldap_get_entries($this->ldap, $search);
1523                $attrs['mailForwardingAddress'] = $mail;
1524                for ($i=0; $i<=$entry['count']; $i++)
1525            {
1526                        $dn = $entry[$i]['dn'];
1527                @ldap_mod_del ( $this->ldap, $dn,  $attrs);
1528            }
1529               
1530                $filter="(&(phpgwAccountType=l)(uidnumber=".$uidnumber."))";
1531                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1532                $entry = ldap_get_entries($this->ldap, $search);
1533                $dn = $entry[0]['dn'];
1534               
1535                if (!@ldap_delete($this->ldap, $dn))
1536                {
1537                        $return['status'] = false;
1538                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1539                }
1540               
1541                return $return;
1542        }
1543
1544        function delete_group($gidnumber)
1545        {
1546                $return['status'] = true;
1547               
1548                $justthese = array("dn");
1549                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1550                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1551                $entry = ldap_get_entries($this->ldap, $search);
1552                $dn = $entry[0]['dn'];
1553               
1554                if (!@ldap_delete($this->ldap, $dn))
1555                {
1556                        $return['status'] = false;
1557                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1558                }
1559               
1560                return $return;
1561        }
1562
1563        function check_access_to_renamed($uid)
1564        {
1565                $justthese = array("dn");
1566                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1567               
1568                foreach ($this->manager_contexts as $index=>$context)
1569                {
1570                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1571                        $entry = ldap_get_entries($this->ldap, $search);
1572                        if ($entry['count'])
1573                                return true;
1574                }
1575            return false;
1576        }
1577
1578        function check_rename_new_uid($uid)
1579        {
1580                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1581                        return false;
1582               
1583                $justthese = array("dn");
1584                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1585               
1586                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1587                $count_entries = @ldap_count_entries($ldapMasterConnect, $search);
1588               
1589                if ($count_entries)
1590                        return false;
1591                       
1592                return true;
1593        }
1594       
1595        function rename_uid($uid, $new_uid)
1596        {
1597                $return['status'] = true;
1598               
1599                $justthese = array("dn");
1600                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1601                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1602            $entry = ldap_get_entries($this->ldap, $search);
1603                $dn = $entry[0]['dn'];
1604               
1605                $explode_dn = ldap_explode_dn($dn, 0);
1606                $rdn = "uid=" . $new_uid;
1607
1608                $parent = array();
1609                for ($j=1; $j<(count($explode_dn)-1); $j++)
1610                        $parent[] = $explode_dn[$j];
1611                $parent = implode(",", $parent);
1612               
1613                $return['new_dn'] = $rdn . ',' . $parent;
1614                       
1615                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, true))
1616                {
1617                        $return['status'] = false;
1618                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->rename_uid ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1619                }
1620               
1621                //Grupos
1622                $justthese = array("dn");
1623                $filter="(&(phpgwAccountType=g)(memberuid=".$uid."))";
1624                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1625            $entry = ldap_get_entries($this->ldap, $search);
1626        $array_mod_add['memberUid'] = $new_uid;
1627        $array_mod_del['memberUid'] = $uid;
1628
1629            for ($i=0; $i<=$entry['count']; $i++)
1630            {
1631                $dn = $entry[$i]['dn'];
1632                @ldap_mod_add ( $this->ldap, $dn,  $array_mod_add);
1633                @ldap_mod_del ( $this->ldap, $dn,  $array_mod_del);
1634            }
1635                return $return;
1636        }
1637
1638        function rename_cn($cn, $new_cn)
1639        {
1640                $return['status'] = true;
1641               
1642                $justthese = array("dn");
1643                $filter="(&(phpgwAccountType=g)(uid=".$cn."))";
1644                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1645            $entry = ldap_get_entries($this->ldap, $search);
1646                $dn = $entry[0]['dn'];
1647               
1648                $explode_dn = ldap_explode_dn($dn, 0);
1649                $rdn = "cn=" . $new_cn;
1650
1651                $parent = array();
1652                for ($j=1; $j<(count($explode_dn)-1); $j++)
1653                        $parent[] = $explode_dn[$j];
1654                $parent = implode(",", $parent);
1655               
1656                $return['new_dn'] = $rdn . ',' . $parent;
1657                       
1658                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, false))
1659                {
1660                        $return['status'] = false;
1661                }
1662               
1663                return $return;
1664        }
1665       
1666        function exist_sambadomains($contexts, $sambaDomainName)
1667        {
1668                $justthese = array("dn");
1669                $filter="(&(objectClass=sambaDomain)(sambaDomainName=$sambaDomainName))";
1670               
1671                foreach ($contexts as $index=>$context)
1672                {
1673                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1674                    $entry = ldap_get_entries($this->ldap, $search);
1675           
1676                        if ($entry['count'])
1677                                return true;
1678                }
1679                return false;
1680        }
1681       
1682        // Primeiro nilvel de organização.
1683        function exist_sambadomains_in_context($params)
1684        {
1685                $dn = $GLOBALS['phpgw_info']['server']['ldap_context'];
1686                $array_dn = ldap_explode_dn ( $dn, 0 );
1687               
1688                $context = $params['context'];
1689                $array_context = ldap_explode_dn ( $context, 0 );
1690               
1691                // Pego o setor no caso do contexto ser um sub-setor.
1692                if (($array_dn['count']+1) < ($array_context['count']))
1693                {
1694                        // inverto o array_dn para poder retirar o count
1695                        $array_dn_reverse  = array_reverse ( $array_dn, false );
1696                       
1697                        //retiro o count
1698                        array_pop($array_dn_reverse);
1699                       
1700                        //incluo o setor no dn
1701                        array_push ( $array_dn_reverse,  $array_context[ $array_context['count'] - 1 - $array_dn['count']]);
1702                       
1703                        // Volto a ordem natural
1704                        $array_dn  = array_reverse ( $array_dn_reverse, false );
1705                       
1706                        // Implodo
1707                        $context = implode ( ",", $array_dn );
1708                }
1709               
1710                $justthese = array("dn","sambaDomainName");
1711                $filter="(objectClass=sambaDomain)";
1712                $search = ldap_list($this->ldap, $context, $filter, $justthese);
1713            $entry = ldap_get_entries($this->ldap, $search);
1714           
1715            for ($i=0; $i<$entry['count']; $i++)
1716            {
1717                        $return['sambaDomains'][$i] = $entry[$i]['sambadomainname'][0];
1718            }
1719           
1720                if ($entry['count'])
1721                        $return['status'] = true;
1722                else
1723                        $return['status'] = false;
1724                       
1725                return $return;
1726        }
1727        function exist_domain_name_sid($sambadomainname, $sambasid)
1728        {
1729                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
1730
1731                $justthese = array("dn","sambaDomainName");
1732                $filter="(&(objectClass=sambaDomain)(sambaSID=$sambasid)(sambaDomainName=$sambadomainname))";
1733                $search = ldap_search($this->ldap, $context, $filter, $justthese);
1734            $count_entries = ldap_count_entries($this->ldap, $search);
1735           
1736            if ($count_entries > 0)
1737                return true;
1738            else
1739                return false;
1740        }
1741       
1742        function add_sambadomain($sambadomainname, $sambasid, $context)
1743        {
1744                $result = array();
1745               
1746                $dn                                                             = "sambaDomainName=$sambadomainname,$context";
1747                $entry['sambaSID']                                      = $sambasid;
1748                $entry['objectClass']                           = 'sambaDomain';
1749                $entry['sambaAlgorithmicRidBase']       = '1000';
1750                $entry['sambaDomainName']                       = $sambadomainname;
1751               
1752                if (!@ldap_add ( $this->ldap, $dn, $entry ))
1753                {
1754                        $return['status'] = false;
1755                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_sambadomain ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1756                }
1757                else
1758                        $return['status'] = true;
1759               
1760                return $return;
1761        }
1762       
1763        function delete_sambadomain($sambadomainname)
1764        {
1765                $return['status'] = true;
1766                $filter="(sambaDomainName=$sambadomainname)";
1767                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
1768            $entry = ldap_get_entries($this->ldap, $search);
1769         
1770                if ($entry['count'] != 0)
1771            {
1772                        $dn = $entry[0]['dn'];
1773                       
1774                        if (!@ldap_delete($this->ldap, $dn))
1775                        {
1776                                $return['status'] = false;
1777                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_sambadomain ($sambadomainname)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1778                        }
1779            }
1780           
1781                return $return;
1782        }
1783       
1784        function search_user($params)
1785        {
1786                $search = $params['search'];
1787                $justthese = array("cn","uid", "mail");
1788        $users_list=ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(&(phpgwAccountType=u) (|(cn=*$search*)(mail=$search*)) )", $justthese);
1789       
1790        if (ldap_count_entries($this->ldap, $users_list) == 0)
1791        {
1792                $return['status'] = 'false';
1793                $result['msg'] = $this->functions->lang('Any result was found') . '.';
1794                return $return;
1795        }
1796       
1797        ldap_sort($this->ldap, $users_list, "cn");
1798       
1799        $entries = ldap_get_entries($this->ldap, $users_list);
1800               
1801                $options = '';
1802                for ($i=0; $i<$entries['count']; $i++)
1803                {
1804                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['cn'][0] . " (".$entries[$i]['mail'][0].")" . "</option>";
1805                }
1806       
1807        return $options;               
1808        }
1809       
1810        function create_institutional_accounts($params)
1811        {
1812                /* Begin: Access verification */
1813                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'add_institutional_accounts'))
1814                {
1815                        $return['status'] = false;
1816                        $return['msg'] = $this->functions->lang('You do not have right to create institutional accounts') . ".";
1817                        return $return;
1818                }
1819               
1820                $access_granted = false;
1821                foreach ($this->manager_contexts as $idx=>$manager_context)
1822                {
1823                        if (stristr($params['context'], $manager_context))
1824                        {
1825                                $access_granted = true;
1826                                break;
1827                        }
1828                }
1829                if (!$access_granted)
1830                {
1831                        $return['status'] = false;
1832                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
1833                        return $return;
1834                }
1835                /* End: Access verification */
1836
1837                /* Begin: Validation */
1838                if ( (empty($params['cn'])) || (empty($params['mail'])) )
1839                {
1840                        $result['status'] = false;
1841                        $result['msg']  = $this->functions->lang('Field mail or name is empty');
1842                        return $result;
1843                }
1844
1845                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
1846                {
1847                        $result['status'] = false;
1848                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
1849                        return $result;
1850                }
1851
1852                $uid = 'institutional_account_' . $params['mail'];
1853                $dn = "uid=$uid," . $params['context'];
1854
1855                $filter = "(mail=".$params['mail'].")";
1856                $justthese = array("cn");
1857                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1858                $entries = @ldap_get_entries($this->ldap,$search);
1859                if ($entries['count'] != 0)
1860                {
1861                        $result['status'] = false;
1862                        $result['msg'] = $this->functions->lang('Field mail already in use');
1863                        return $result;
1864                }
1865                /* End: Validation */
1866                                               
1867                $info = array();
1868                $info['cn']                                     = $params['cn'];
1869                $info['sn']                                     = $params['cn'];
1870                $info['uid']                            = $uid;
1871                $info['mail']                           = $params['mail'];
1872                $info['phpgwAccountType']       = 'i';
1873                $info['objectClass'][]          = 'inetOrgPerson';
1874                $info['objectClass'][]          = 'phpgwAccount';
1875                $info['objectClass'][]          = 'top';
1876                $info['objectClass'][]          = 'person';
1877                $info['objectClass'][]          = 'qmailUser';
1878                $info['objectClass'][]          = 'organizationalPerson';
1879               
1880                if ($params['accountStatus'] == 'on')
1881                {
1882                        $info['accountStatus'] = 'active';
1883                }
1884                if ($params['phpgwAccountVisible'] == 'on')
1885                {
1886                        $info['phpgwAccountVisible'] = '-1';
1887                }
1888               
1889                if (!empty($params['owners']))
1890                {
1891                        foreach($params['owners'] as $index=>$uidnumber)
1892                        {
1893                                $info['mailForwardingAddress'][] = $this->uidnumber2mail($uidnumber);
1894                        }
1895                }               
1896               
1897                $result = array();
1898                if (!@ldap_add ( $this->ldap, $dn, $info ))
1899                {
1900                        $result['status'] = false;
1901                        $result['msg']  = $this->functions->lang('Error in function') . ' ldap_functions->create_institutional_accounts';
1902                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
1903                }
1904                else
1905                        $result['status'] = true;
1906               
1907                return $result;
1908        }
1909       
1910        function save_institutional_accounts($params)
1911        {
1912                /* Begin: Access verification */
1913                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
1914                {
1915                        $return['status'] = false;
1916                        $return['msg'] = $this->functions->lang('You do not have right to edit institutional accounts') . ".";
1917                        return $return;
1918                }
1919                $access_granted = false;
1920                foreach ($this->manager_contexts as $idx=>$manager_context)
1921                {
1922                        if (stristr($params['context'], $manager_context))
1923                        {
1924                                $access_granted = true;
1925                                break;
1926                        }
1927                }
1928                if (!$access_granted)
1929                {
1930                        $return['status'] = false;
1931                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
1932                        return $return;
1933                }
1934                /* End: Access verification */
1935               
1936                /* Begin: Validation */
1937                if ( (empty($params['cn'])) || (empty($params['mail'])) )
1938                {
1939                        $result['status'] = false;
1940                        $result['msg']  = $this->functions->lang('Field mail or name is empty') . '.';
1941                        return $result;
1942                }
1943
1944                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
1945                {
1946                        $result['status'] = false;
1947                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
1948                        return $result;
1949                }
1950
1951                $uid = 'institutional_account_' . $params['mail'];
1952                $dn = strtolower("uid=$uid," . $params['context']);
1953                $anchor = strtolower($params['anchor']);
1954
1955                $filter = "(mail=".$params['mail'].")";
1956                $justthese = array("cn");
1957                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1958                $entries = @ldap_get_entries($this->ldap,$search);
1959               
1960                if ( ($entries['count'] > 1) || (($entries['count'] == 1) && ($entries[0]['dn'] != $anchor)) )
1961                {
1962                        $result['status'] = false;
1963                        $result['msg'] = $this->functions->lang('Field mail already in use.');
1964                        return $result;
1965                }
1966                /* End: Validation */
1967               
1968                $result = array();
1969                $result['status'] = true;
1970               
1971                if ($anchor != $dn)
1972                {
1973                        if (!@ldap_rename($this->ldap, $anchor, "uid=$uid", $params['context'], true))
1974                        {
1975                                $result['status'] = false;
1976                                $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_rename';
1977                                $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
1978                        }
1979                }
1980               
1981                $info = array();
1982                $info['cn']                                     = $params['cn'];
1983                $info['sn']                                     = $params['cn'];
1984                $info['uid']                            = $uid;
1985                $info['mail']                           = $params['mail'];
1986               
1987                if ($params['accountStatus'] == 'on')
1988                        $info['accountStatus'] = 'active';
1989                else
1990                        $info['accountStatus'] = array();
1991               
1992                if ($params['phpgwAccountVisible'] == 'on')
1993                        $info['phpgwAccountVisible'] = '-1';
1994                else
1995                        $info['phpgwAccountVisible'] = array();
1996               
1997                if ($params['description'] != '')
1998                        $info['description'] = utf8_encode($params['description']);
1999                else
2000                        $info['description'] = array();
2001               
2002                if (!empty($params['owners']))
2003                {
2004                        foreach($params['owners'] as $index=>$uidnumber)
2005                        {
2006                                $mailForwardingAddress = $this->uidnumber2mail($uidnumber);
2007                                if ($mailForwardingAddress != '')
2008                                        $info['mailForwardingAddress'][] = $mailForwardingAddress;
2009                        }
2010                }
2011                else
2012                        $info['mailForwardingAddress'] = array();
2013               
2014                if (!@ldap_modify ( $this->ldap, $dn, $info ))
2015                {
2016                        $result['status'] = false;
2017                        $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_modify';
2018                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2019                }
2020
2021                return $result;
2022        }
2023       
2024        function get_institutional_accounts($params)
2025        {
2026                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'list_institutional_accounts'))
2027                {
2028                        $return['status'] = false;
2029                        $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
2030                        return $return;
2031                }
2032
2033                $input = $params['input'];
2034                $justthese = array("cn", "mail", "uid");
2035                $trs = array();
2036                               
2037                foreach ($this->manager_contexts as $idx=>$context)
2038                {
2039                $institutional_accounts = ldap_search($this->ldap, $context, ("(&(phpgwAccountType=i)(|(mail=$input*)(cn=*$input*)))"), $justthese);
2040                $entries = ldap_get_entries($this->ldap, $institutional_accounts);
2041               
2042                        for ($i=0; $i<$entries['count']; $i++)
2043                        {
2044                                $tr = "<tr class='normal' onMouseOver=this.className='selected' onMouseOut=this.className='normal'><td onClick=edit_institutional_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['cn'][0] . "</td><td onClick=edit_institutional_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['mail'][0] . "</td><td align='center' onClick=delete_institutional_accounts('".$entries[$i]['uid'][0]."')><img HEIGHT='16' WIDTH='16' src=./expressoAdmin1_2/templates/default/images/delete.png></td></tr>";
2045                                $trs[$tr] = $entries[$i]['cn'][0];
2046                        }
2047                }
2048       
2049        $trs_string = '';
2050        if (count($trs))
2051        {
2052                natcasesort($trs);
2053                foreach ($trs as $tr=>$cn)
2054                {
2055                        $trs_string .= $tr;
2056                }
2057        }
2058       
2059        $return['status'] = 'true';
2060        $return['trs'] = $trs_string;
2061        return $return;
2062        }
2063       
2064        function get_institutional_account_data($params)
2065        {
2066                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
2067                {
2068                        $return['status'] = false;
2069                        $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
2070                        return $return;
2071                }
2072               
2073                $uid = $params['uid'];
2074                //$justthese = array("accountStatus", "phpgwAccountVisible", "cn", "mail", "mailForwardingAddress", "description");
2075                               
2076        $institutional_accounts = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"));
2077        $entrie = ldap_get_entries($this->ldap, $institutional_accounts);
2078               
2079                if ($entrie['count'] != 1)
2080                {
2081                        $return['status'] = 'false';
2082                        $result['msg'] = $this->functions->lang('Problems loading datas') . '.';
2083                }
2084                else
2085                {
2086                        $tmp_user_context = split(",", $entrie[0]['dn']);
2087                        $tmp_reverse_user_context = array_reverse($tmp_user_context);
2088                        array_pop($tmp_reverse_user_context);
2089                        $return['user_context'] = implode(",", array_reverse($tmp_reverse_user_context));
2090                       
2091                        $return['status'] = 'true';
2092                        $return['accountStatus']                = $entrie[0]['accountstatus'][0];
2093                        $return['phpgwAccountVisible']  = $entrie[0]['phpgwaccountvisible'][0];
2094                        $return['cn']                                   = $entrie[0]['cn'][0];
2095                        $return['mail']                                 = $entrie[0]['mail'][0];
2096                        $return['description']                  = utf8_decode($entrie[0]['description'][0]);
2097
2098                        if ($entrie[0]['mailforwardingaddress']['count'] > 0)
2099                        {
2100                                $a_cn = array();
2101                                for ($i=0; $i<$entrie[0]['mailforwardingaddress']['count']; $i++)
2102                                {
2103                                        $tmp = $this->mailforwardingaddress2uidnumber($entrie[0]['mailforwardingaddress'][$i]);
2104                                        if (!$tmp) {}
2105                                        else
2106                                                $a_cn[$tmp['uidnumber']] = $tmp['cn'];
2107                                }
2108                                natcasesort($a_cn);
2109                                foreach($a_cn as $uidnumber => $cn)
2110                                {
2111                                        $return['owners'] .= '<option value='. $uidnumber .'>' . $cn . '</option>';
2112                                }
2113                        }
2114                }
2115               
2116                return $return;
2117        }
2118       
2119        function mailforwardingaddress2uidnumber($mail)
2120        {
2121                $justthese = array("uidnumber","cn");
2122        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=u)(mail=$mail))"), $justthese);
2123        $entrie = ldap_get_entries($this->ldap, $search);
2124                if ($entrie['count'] != 1)
2125                        return false;
2126                else
2127                {
2128                        $return['uidnumber'] = $entrie[0]['uidnumber'][0];
2129                        $return['cn'] = $entrie[0]['cn'][0];
2130                        return $return;
2131                }
2132        }
2133       
2134        function delete_institutional_account_data($params)
2135        {
2136                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'remove_institutional_accounts'))
2137                {
2138                        $return['status'] = false;
2139                        $return['msg'] = $this->functions->lang('You do not have right to delete institutional accounts') . ".";
2140                        return $return;
2141                }
2142
2143                $uid = $params['uid'];
2144                $return['status'] = true;
2145                               
2146                $justthese = array("cn");
2147        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"), $justthese);
2148        $entrie = ldap_get_entries($this->ldap, $search);
2149                if ($entrie['count'] > 1)
2150                {
2151                        $return['status'] = false;
2152                        $return['msg']  = $this->functions->lang('More then one uid was found');
2153                        return $return;
2154                }               
2155                if ($entrie['count'] == 0)
2156                {
2157                        $return['status'] = false;
2158                        $return['msg']  = $this->functions->lang('No uid was found');
2159                        return $return;
2160                }               
2161               
2162                $dn = $entrie[0]['dn'];
2163                if (!@ldap_delete($this->ldap, $dn))
2164                {
2165                        $return['status'] = false;
2166                        $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->delete_institutional_accounts: ldap_delete";
2167                        $return['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2168                        return $return;
2169                }
2170               
2171                return $return;
2172        }
2173       
2174        function replace_mail_from_institutional_account($newMail, $oldMail)
2175        {
2176                $filter = "(&(phpgwAccountType=i)(mailforwardingaddress=$oldMail))";
2177                $justthese = array("dn");
2178                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2179                $entries = ldap_get_entries($this->ldap, $search);
2180                $result['status'] = true;
2181                for ($i=0; $i<$entries['count']; $i++)
2182                {
2183                        $attrs['mailforwardingaddress'] = $oldMail;
2184                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
2185                        $attrs['mailforwardingaddress'] = $newMail;
2186                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
2187               
2188                        if ((!$res1) || (!$res2))
2189                        {
2190                                $result['status'] = false;
2191                                $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->replace_mail_from_institutional_account.";
2192                        }
2193                }
2194               
2195                return $result;
2196        }
2197}
2198?>
Note: See TracBrowser for help on using the repository browser.