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

Revision 493, 75.4 KB checked in by niltonneto, 16 years ago (diff)

Correção de tradução

  • 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;'.$this->functions->lang('email lists').'&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;'.$this->functions->lang('users').'&nbsp;&nbsp;&nbsp;&nbsp;---------------------------- </option>'."\n";
622                       
623                if (count($u_tmp))
624                {
625                        foreach ($u_tmp as $mail => $cn)
626                        {
627                                $options .= "<option value=$mail class='line-above'>$cn</option>";
628                        }
629                        unset($u_tmp);
630                }
631               
632                ldap_close($ldap_conn_following_ref);
633                return $options;
634        }
635
636        function get_available_groups($params)
637        {
638                $context = $params['context'];
639                $justthese = array("cn", "gidNumber");
640        $groups_list=ldap_list($this->ldap, $context, ("(phpgwAccountType=g)"), $justthese);
641        ldap_sort($this->ldap, $groups_list, "cn");
642       
643        $entries = ldap_get_entries($this->ldap, $groups_list);
644               
645                $options = '';
646                for ($i=0; $i<$entries['count']; $i++)
647                {
648                        $options .= "<option value=" . $entries[$i]['gidnumber'][0] . ">" . $entries[$i]['cn'][0] . "</option>";
649                }
650       
651        return $options;               
652        }
653       
654        function get_available_maillists($params)
655        {
656                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
657                        return false;
658               
659                $context = $params['context'];
660                $justthese = array("uid","mail","uidNumber");
661        $maillists=ldap_list($ldapMasterConnect, $context, ("(phpgwAccountType=l)"), $justthese);
662        ldap_sort($ldapMasterConnect, $maillists, "uid");
663       
664        $entries = ldap_get_entries($ldapMasterConnect, $maillists);
665       
666                $options = '';                 
667                for ($i=0; $i<$entries['count']; $i++)
668                {
669                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['uid'][0] . " (" . $entries[$i]['mail'][0] . ")" . "</option>";
670                }
671       
672        ldap_close($ldapMasterConnect);
673        return $options;
674        }
675       
676        function ldap_add_entry($dn, $entry)
677        {
678                $result = array();
679                if (!@ldap_add ( $this->ldap, $dn, $entry ))
680                {
681                        $result['status']               = false;
682                        $result['error_number'] = ldap_errno($this->ldap);
683                        $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);
684                }
685                else
686                        $result['status'] = true;
687               
688                return $result;
689        }
690       
691        function ldap_save_photo($dn, $pathphoto, $photo_exist=false)
692        {
693                $fd = fopen($pathphoto, "r");
694                $fsize = filesize($pathphoto);
695                $jpegStr = fread($fd, $fsize);
696                fclose ($fd);
697                $attrs['jpegPhoto'] = $jpegStr;
698                       
699                if ($photo_exist)
700                        $res = @ldap_mod_replace($this->ldap, $dn, $attrs);
701                else
702                        $res = @ldap_mod_add($this->ldap, $dn, $attrs);
703                       
704                if ($res)
705                {
706                        $result['status'] = true;
707                }
708                else
709                {
710                        $result['status'] = false;
711                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_save_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
712                }
713               
714                return $result;
715        }
716       
717        function ldap_remove_photo($dn)
718        {
719                $attrs['jpegPhoto'] = array();
720                $res = ldap_mod_del($this->ldap, $dn, $attrs);
721               
722                if ($res)
723                {
724                        $result['status'] = true;
725                }
726                else
727                {
728                        $result['status'] = false;
729                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_remove_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
730                }
731               
732                return $result;
733        }       
734       
735        // Pode receber tanto um único memberUid quanto um array de memberUid's
736        function add_user2group($gidNumber, $memberUid)
737        {
738                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
739                $justthese = array("dn");
740                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
741                $entry = ldap_get_entries($this->ldap, $search);
742                $group_dn = $entry[0]['dn'];
743                $attrs['memberUid'] = $memberUid;
744               
745                $res = @ldap_mod_add($this->ldap, $group_dn, $attrs);
746               
747                if ($res)
748                {
749                        $result['status'] = true;
750                }
751                else
752                {
753                        $result['status'] = false;
754                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
755                }
756                return $result;
757        }
758       
759        function remove_user2group($gidNumber, $memberUid)
760        {
761                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
762                $justthese = array("dn");
763                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
764                $entry = ldap_get_entries($this->ldap, $search);
765                $group_dn = $entry[0]['dn'];
766                $attrs['memberUid'] = $memberUid;
767                $res = @ldap_mod_del($this->ldap, $group_dn, $attrs);
768               
769                if ($res)
770                {
771                        $result['status'] = true;
772                }
773                else
774                {
775                        $result['status'] = false;
776                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
777                }
778                return $result;
779        }
780       
781        function add_user2maillist($uid, $mail)
782        {
783                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
784                {
785                        $result['status'] = false;
786                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
787                        return $result;
788                }
789                       
790                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
791                $justthese = array("dn");
792                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
793                $entry = ldap_get_entries($ldapMasterConnect, $search);
794                $group_dn = $entry[0]['dn'];
795                $attrs['mailForwardingAddress'] = $mail;
796                $res = @ldap_mod_add($ldapMasterConnect, $group_dn, $attrs);
797               
798                if ($res)
799                {
800                        $result['status'] = true;
801                }
802                else
803                {
804                        $result['status'] = false;
805                        if (ldap_errno($ldapMasterConnect) == '50')
806                        {
807                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
808                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
809                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
810                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
811                        }                                       
812                        else
813                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
814                }
815               
816                ldap_close($ldapMasterConnect);
817                return $result;
818        }
819       
820        function add_user2maillist_scl($dn, $array_emails)
821        {
822                $attrs['mailSenderAddress'] = $array_emails;
823               
824                $res = @ldap_mod_add($this->ldap, $dn, $attrs);
825               
826                if ($res)
827                {
828                        $result['status'] = true;
829                }
830                else
831                {
832                        $result['status'] = false;
833                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
834                }
835                return $result;
836        }
837
838        function remove_user2maillist($uid, $mail)
839        {
840                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
841                {
842                        $result['status'] = false;
843                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
844                        return $result;
845                }
846               
847                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
848                $justthese = array("dn");
849                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
850                $entry = ldap_get_entries($ldapMasterConnect, $search);
851                $group_dn = $entry[0]['dn'];
852                $attrs['mailForwardingAddress'] = $mail;
853                $res = @ldap_mod_del($ldapMasterConnect, $group_dn, $attrs);
854               
855                if ($res)
856                {
857                        $result['status'] = true;
858                }
859                else
860                {
861                        $result['status'] = false;
862                        if (ldap_errno($ldapMasterConnect) == '50')
863                        {
864                               
865                                $return['msg'] = $this->functions->lang('You do not have right to create institutional accounts') . ".";
866
867                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
868                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
869                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
870                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
871                        }                                       
872                        else
873                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
874                }
875                ldap_close($ldapMasterConnect);
876                return $result;
877        }
878
879        function remove_user2maillist_scl($dn, $array_emails)
880        {
881                $attrs['mailSenderAddress'] = $array_emails;
882                $res = @ldap_mod_del($this->ldap, $dn, $attrs);
883               
884                if ($res)
885                {
886                        $result['status'] = true;
887                }
888                else
889                {
890                        $result['status'] = false;
891                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
892                }
893                return $result;
894        }
895
896        function replace_user2maillists($new_mail, $old_mail)
897        {
898                $filter = "(&(phpgwAccountType=l)(mailforwardingaddress=$old_mail))";
899                $justthese = array("dn");
900                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
901                $entries = ldap_get_entries($this->ldap, $search);
902                $result['status'] = true;
903                for ($i=0; $i<$entries['count']; $i++)
904                {
905                        $attrs['mailforwardingaddress'] = $old_mail;
906                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
907                        $attrs['mailforwardingaddress'] = $new_mail;
908                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
909               
910                        if ((!$res1) || (!$res2))
911                        {
912                                $result['status'] = false;
913                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user2maillists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
914                        }
915                }
916               
917                return $result;
918        }
919       
920        function get_user_info($uidnumber)
921        {
922                foreach ($this->manager_contexts as $index=>$context)
923                {
924                        $filter="(&(phpgwAccountType=u)(uidNumber=".$uidnumber."))";
925                        $search = ldap_search($this->ldap, $context, $filter);
926                        $entry = ldap_get_entries($this->ldap, $search);
927                       
928                        if ($entry['count'])
929                        {
930                                //Pega o dn do setor do usuario.
931                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
932                                $sector_dn_array = explode(",", $entry[0]['dn']);
933                                for($i=1; $i<count($sector_dn_array); $i++)
934                                        $sector_dn .= $sector_dn_array[$i] . ',';
935                                //Retira ultimo pipe.
936                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
937               
938                                $result['context']                              = $sector_dn;
939                                $result['uid']                                  = $entry[0]['uid'][0];
940                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
941                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
942                                $result['departmentnumber']             = $entry[0]['departmentnumber'][0];
943                                $result['givenname']                    = $entry[0]['givenname'][0];
944                                $result['sn']                                   = $entry[0]['sn'][0];
945                                $result['telephonenumber']              = $entry[0]['telephonenumber'][0];
946                                $result['passwd_expired']               = $entry[0]['phpgwlastpasswdchange'][0];
947                                $result['phpgwaccountstatus']   = $entry[0]['phpgwaccountstatus'][0];
948                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
949                                $result['accountstatus']                = $entry[0]['accountstatus'][0];
950                                $result['mail']                                 = $entry[0]['mail'][0];
951                                $result['mailalternateaddress'] = $entry[0]['mailalternateaddress'];
952                                $result['mailforwardingaddress']= $entry[0]['mailforwardingaddress'];
953                                $result['deliverymode']                 = $entry[0]['deliverymode'][0];
954                                $result['userPasswordRFC2617']  = $entry[0]['userpasswordrfc2617'][0];
955
956                                //Photo
957                                if ($entry[0]['jpegphoto']['count'] == 1)
958                                        $result['photo_exist'] = 'true';
959               
960                                // Samba
961                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
962                                {
963                                        if ($entry[0]['objectclass'][$i] == 'sambaSamAccount')
964                                                $result['sambaUser'] = true;
965                                }
966                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($result['sambaUser']))
967                                {
968                                        $result['sambaaccflags'] = $entry[0]['sambaacctflags'][0];
969                                        $result['sambalogonscript'] = $entry[0]['sambalogonscript'][0];
970                                        $result['homedirectory'] = $entry[0]['homedirectory'][0];
971                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
972                                        array_pop($a_tmp);
973                                        $result['sambasid'] = implode("-", $a_tmp);
974                                }
975
976                                // Verifica o acesso do gerente aos atributos corporativos
977                                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
978                                {
979                                        $result['corporative_information_employeenumber']= $entry[0]['employeenumber'][0];
980                                        $result['corporative_information_cpf']                  = $entry[0]['cpf'][0];
981                                        $result['corporative_information_rg']                   = $entry[0]['rg'][0];
982                                        $result['corporative_information_rguf']                 = $entry[0]['rguf'][0];
983                                        $result['corporative_information_description']  = utf8_decode($entry[0]['description'][0]);
984                                }
985                               
986                                // MailLists
987                                $result['maillists_info'] = $this->get_user_maillists($result['mail']);
988                                if($result['maillists_info'])
989                                {
990                                        foreach ($result['maillists_info'] as $maillist)
991                                        {
992                                                $result['maillists'][] = $maillist['uid'];
993                                        }
994                                }
995                               
996                                // Groups
997                                $justthese = array("gidnumber","cn");
998                                $filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
999                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1000                        ldap_sort($this->ldap, $search, "cn");
1001                        $entries = ldap_get_entries($this->ldap, $search);
1002                        for ($i=0; $i<$entries['count']; $i++)
1003                        {
1004                                $result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
1005                        }
1006                        }
1007                }
1008                return $result;
1009        }
1010               
1011        function get_user_maillists($mail)
1012        {
1013                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1014                        return false;
1015               
1016                $result = array();
1017               
1018                //Mostra somente os mailists dos contextos do gerente
1019                $justthese = array("uid","mail","uidnumber");
1020                $filter="(&(phpgwAccountType=l)(mailforwardingaddress=$mail))";
1021               
1022                foreach ($this->manager_contexts as $index=>$context)
1023                {
1024                        $search = ldap_search($ldapMasterConnect, $context, $filter, $justthese);
1025                $entries = ldap_get_entries($ldapMasterConnect, $search);
1026               
1027                for ($i=0; $i<$entries['count']; $i++)
1028                {
1029                                $result[ $entries[$i]['uid'][0] ]['uid']                = $entries[$i]['uid'][0];
1030                                $result[ $entries[$i]['uid'][0] ]['mail']               = $entries[$i]['mail'][0];
1031                               
1032                                $a_tmp[] = $entries[$i]['uid'][0];
1033                }
1034                }
1035       
1036        if($a_tmp) {
1037                natcasesort($a_tmp);
1038       
1039                foreach ($a_tmp as $uid)
1040                {
1041                                $return[$uid]['uid']            = $result[$uid]['uid'];
1042                                $return[$uid]['mail']           = $result[$uid]['mail'];
1043                }
1044        }
1045        ldap_close($ldapMasterConnect);
1046                return $return;
1047        }
1048       
1049        function get_group_info($gidnumber)
1050        {
1051                foreach ($this->manager_contexts as $index=>$context)
1052                {
1053                        $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1054                        $search = ldap_search($this->ldap, $context, $filter);
1055                        $entry = ldap_get_entries($this->ldap, $search);
1056                       
1057                        if ($entry['count'])
1058                        {
1059                                //Pega o dn do setor do grupo.
1060                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1061                                $sector_dn_array = explode(",", $entry[0]['dn']);
1062                                for($i=1; $i<count($sector_dn_array); $i++)
1063                                        $sector_dn .= $sector_dn_array[$i] . ',';
1064                                //Retira ultimo pipe.
1065                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1066               
1067                                $result['context']                              = $sector_dn;
1068                                $result['cn']                                   = $entry[0]['cn'][0];
1069                                $result['description']                  = $entry[0]['description'][0];
1070                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
1071                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
1072                                $result['email']                                = $entry[0]['mail'][0];
1073               
1074                                //MemberUid
1075                                for ($i=0; $i<$entry[0]['memberuid']['count']; $i++)
1076                                {
1077                                        $justthese = array("cn","uid","uidnumber");
1078                       
1079                                        // Montagem dinamica do filtro
1080                                        $filter="(&(phpgwAccountType=u)(|";
1081                                        for ($k=0; (($k<10) && ($i<$entry[0]['memberuid']['count'])); $k++)
1082                                        {
1083                                                $filter .= "(uid=".$entry[0]['memberuid'][$i].")";
1084                                                $i++;
1085                                        }
1086                                        $i--;
1087                                        $filter .= "))";
1088                       
1089                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1090                                        $user_entry = ldap_get_entries($this->ldap, $search);
1091
1092                                        for ($j=0; $j<$user_entry['count']; $j++)
1093                                        {
1094                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['cn'] = $user_entry[$j]['cn'][0];
1095                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['uidnumber'] = $user_entry[$j]['uidnumber'][0];
1096                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['type'] = 'u';
1097                                        }
1098                                }
1099               
1100                                // Checamos e-mails que não fazem parte do expresso.
1101                                // Criamos um array temporario
1102                                $tmp_array = array();
1103                                if($result['memberuid_info'])
1104                                        foreach ($result['memberuid_info'] as $uid => $user_data)
1105                                        {
1106                                                $tmp_array[] = $uid;
1107                                        }
1108               
1109                                if($entry[0]['memberuid']) {
1110                                        // Retira o count do array
1111                                        array_shift($entry[0]['memberuid']);
1112                                        // Vemos a diferença
1113                                        $array_diff = array_diff($entry[0]['memberuid'], $tmp_array);
1114                                        // Incluimos no resultado                       
1115                                        foreach ($array_diff as $index=>$uid)
1116                                        {
1117                                                $result['memberuid_info'][$uid]['cn'] = $uid;
1118                                        }
1119                                }
1120               
1121                                // Samba
1122                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1123                                {
1124                                        if ($entry[0]['objectclass'][$i] == 'sambaGroupMapping')
1125                                                $result['sambaGroup'] = true;
1126
1127                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1128                                        array_pop($a_tmp);
1129                                        $result['sambasid'] = implode("-", $a_tmp);
1130                                }
1131                                return $result;
1132                        }
1133                }
1134        }       
1135       
1136        function get_maillist_info($uidnumber)
1137        {
1138                /* folling referral connection */
1139                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
1140                if ($ldap_conn_following_ref)
1141                {
1142                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
1143                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
1144
1145                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
1146                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
1147                }
1148               
1149                foreach ($this->manager_contexts as $index=>$context)
1150                {
1151                        $filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
1152                        $search = ldap_search($this->ldap, $context, $filter);
1153                        $entry = ldap_get_entries($this->ldap, $search);
1154                       
1155                        if ($entry['count'])
1156                        {
1157                                //Pega o dn do setor do usuario.
1158                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1159                                $sector_dn_array = explode(",", $entry[0]['dn']);
1160                                for($i=1; $i<count($sector_dn_array); $i++)
1161                                        $sector_dn .= $sector_dn_array[$i] . ',';
1162                                //Retira ultimo pipe.
1163                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1164                       
1165                                $result['context']                              = $sector_dn;
1166                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
1167                                $result['uid']                                  = strtolower($entry[0]['uid'][0]);
1168                                $result['cn']                                   = $entry[0]['cn'][0];
1169                                $result['mail']                                 = $entry[0]['mail'][0];
1170                                $result['description']                  = utf8_decode($entry[0]['description'][0]);
1171                                $result['accountStatus']                = $entry[0]['accountstatus'][0];
1172                                $result['phpgwAccountVisible']  = $entry[0]['phpgwaccountvisible'][0];
1173                       
1174                                //Members
1175                                for ($i=0; $i<$entry[0]['mailforwardingaddress']['count']; $i++)
1176                                {
1177                                        $justthese = array("cn", "uidnumber", "uid", "phpgwaccounttype", "mail");
1178                               
1179                                        // Montagem dinamica do filtro, para nao ter muitas conexoes com o ldap
1180                                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|";
1181                                        for ($k=0; (($k<10) && ($i<$entry[0]['mailforwardingaddress']['count'])); $k++)
1182                                        {
1183                                                $filter .= "(mail=".$entry[0]['mailforwardingaddress'][$i].")";
1184                                                $i++;
1185                                        }
1186                                        $i--;
1187                                        $filter .= "))";
1188                               
1189                                        $search = ldap_search($ldap_conn_following_ref, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1190                                        $user_entry = ldap_get_entries($ldap_conn_following_ref, $search);
1191                                                                       
1192                                        for ($j=0; $j<$user_entry['count']; $j++)
1193                                        {
1194                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['uid'] = $user_entry[$j]['uid'][0];
1195                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['cn'] = $user_entry[$j]['cn'][0];
1196                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['type'] = $user_entry[$j]['phpgwaccounttype'][0];
1197                                                $result['mailForwardingAddress'][] = $user_entry[$j]['mail'][0];
1198                                        }
1199                                }
1200
1201                                // Emails não encontrados no ldap
1202                                array_shift($entry[0]['mailforwardingaddress']); //Retira o count do array
1203                                $missing_emails = array_diff($entry[0]['mailforwardingaddress'], $result['mailForwardingAddress']);
1204                               
1205                                // Incluimos estes no resultado
1206                                foreach ($missing_emails as $index=>$mailforwardingaddress)
1207                                {
1208                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['uid'] = $mailforwardingaddress;
1209                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['cn'] = 'E-Mail nao encontrado';
1210                                        $result['mailForwardingAddress'][] = $mailforwardingaddress;
1211                                }
1212                               
1213                                ldap_close($ldap_conn_following_ref);
1214                                return $result;
1215                        }
1216                }
1217        }       
1218
1219        function get_maillist_scl_info($uidnumber)
1220        {
1221                foreach ($this->manager_contexts as $index=>$context)
1222                {
1223                        $filter="(&(phpgwAccountType=l)(uidNumber=$uidnumber))";
1224                        $search = ldap_search($this->ldap, $context, $filter);
1225                        $entry = ldap_get_entries($this->ldap, $search);
1226
1227                        if ($entry['count'])
1228                        {
1229                                //Pega o dn do setor do usuario.
1230                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1231                                $sector_dn_array = explode(",", $entry[0]['dn']);
1232                                for($i=1; $i<count($sector_dn_array); $i++)
1233                                        $sector_dn .= $sector_dn_array[$i] . ',';
1234                                //Retira ultimo pipe.
1235                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1236               
1237                                $result['dn']                                           = $entry[0]['dn'];
1238                                $result['context']                                      = $sector_dn;
1239                                $result['uidnumber']                            = $entry[0]['uidnumber'][0];
1240                                $result['uid']                                          = $entry[0]['uid'][0];
1241                                $result['cn']                                           = $entry[0]['cn'][0];
1242                                $result['mail']                                         = $entry[0]['mail'][0];
1243                                $result['accountStatus']                        = $entry[0]['accountstatus'][0];
1244                                $result['phpgwAccountVisible']          = $entry[0]['phpgwaccountvisible'][0];
1245                                $result['accountRestrictive']           = $entry[0]['accountrestrictive'][0];
1246                                $result['participantCanSendMail']       = $entry[0]['participantcansendmail'][0];
1247               
1248                                //Senders
1249                                for ($i=0; $i<$entry[0]['mailsenderaddress']['count']; $i++)
1250                                {
1251                                        $justthese = array("cn", "uidnumber", "uid", "mail");
1252                                        $filter="(&(phpgwAccountType=u)(mail=".$entry[0]['mailsenderaddress'][$i]."))";
1253                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1254                                        $user_entry = ldap_get_entries($this->ldap, $search);
1255                       
1256                                        $result['senders_info'][$user_entry[0]['mail'][0]]['uid'] = $user_entry[0]['uid'][0];
1257                                        $result['senders_info'][$user_entry[0]['mail'][0]]['cn'] = $user_entry[0]['cn'][0];
1258                                        $result['members'][] = $user_entry[0]['mail'][0];
1259                                }
1260                                return $result;
1261                        }
1262                }
1263        }       
1264
1265        function group_exist($gidnumber)
1266        {
1267                $justthese = array("cn");
1268                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1269                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1270                               
1271                $entry = ldap_get_entries($this->ldap, $search);
1272                if ($entry['count'] == 0)
1273                        return false;
1274                else
1275                        return true;
1276        }
1277
1278        function gidnumbers2cn($gidnumbers)
1279        {
1280                $result = array();
1281                if (count($gidnumbers))
1282                {
1283                        $justthese = array("cn");
1284                        $i = 0;
1285                        foreach ($gidnumbers as $gidnumber)
1286                        {
1287                                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1288                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1289                               
1290                                $entry = ldap_get_entries($this->ldap, $search);
1291                                if ($entry['count'] == 0)
1292                                        $result['groups_info'][$i]['cn'] = '_' . $this->functions->lang('group only exist on DB, but does not exist on ldap');
1293                                       
1294                                else
1295                                        $result['groups_info'][$i]['cn'] = $entry[0]['cn'][0];
1296                                $result['groups_info'][$i]['gidnumber'] = $gidnumber;
1297                       
1298                                /* o gerente pode excluir um grupo de um usuario onde este grupo esta em outra OU ? */
1299                                /* é o mesmo que o manager editar um grupo de outra OU */
1300                                $result['groups_info'][$i]['group_disabled'] = 'true';
1301                                foreach ($this->manager_contexts as $index=>$context)
1302                                {
1303                                        if (strpos(strtolower($entry[0]['dn']), strtolower($context)))
1304                                        {
1305                                                $result['groups_info'][$i]['group_disabled'] = 'false';
1306                                        }
1307                                }
1308
1309                                $i++;
1310                        }
1311                }
1312                return $result;
1313        }
1314
1315        function uidnumber2uid($uidnumber)
1316        {
1317                $justthese = array("uid");
1318                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1319                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1320                $entry = ldap_get_entries($this->ldap, $search);
1321                return $entry[0]['uid'][0];
1322        }
1323
1324        function uidnumber2mail($uidnumber)
1325        {
1326                $justthese = array("mail");
1327                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1328                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1329                $entry = ldap_get_entries($this->ldap, $search);
1330                return $entry[0]['mail'][0];
1331        }
1332
1333       
1334        function change_user_context($dn, $newrdn, $newparent)
1335        {
1336                if (!ldap_rename ( $this->ldap, $dn, $newrdn, $newparent, true ))
1337                {
1338                        $return['status'] = false;
1339                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->change_user_context ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1340                }
1341                else
1342                        $return['status'] = true;
1343               
1344                return $return;
1345        }
1346       
1347        function replace_user_attributes($dn, $ldap_mod_replace)
1348        {
1349                if (!@ldap_mod_replace ( $this->ldap, $dn, $ldap_mod_replace ))
1350                {
1351                        $return['status'] = false;
1352                        $return['error_number'] = ldap_errno($this->ldap);
1353                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1354                }
1355                else
1356                        $return['status'] = true;
1357               
1358                return $return;
1359        }
1360       
1361        function add_user_attributes($dn, $ldap_add)
1362        {
1363                if (!@ldap_mod_add ( $this->ldap, $dn, $ldap_add ))
1364                {
1365                        $return['status'] = false;
1366                        $return['error_number'] = ldap_errno($this->ldap);
1367                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1368                }
1369                else
1370                        $return['status'] = true;
1371               
1372                return $return;
1373        }
1374       
1375        function remove_user_attributes($dn, $ldap_remove)
1376        {
1377                if (!@ldap_mod_del ( $this->ldap, $dn, $ldap_remove ))
1378                {
1379                        $return['status'] = false;
1380                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1381                }
1382                else
1383                        $return['status'] = true;
1384               
1385                return $return;
1386        }
1387       
1388        function set_user_password($uid, $password)
1389        {
1390                $justthese = array("userPassword");
1391                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1392                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1393            $entry = ldap_get_entries($this->ldap, $search);
1394                $dn = $entry[0]['dn'];
1395                $userPassword = $entry[0]['userpassword'][0];
1396                $ldap_mod_replace['userPassword'] = $password;
1397                $this->replace_user_attributes($dn, $ldap_mod_replace);
1398                return $userPassword;
1399        }
1400       
1401        function delete_user($user_info)
1402        {
1403                // Verifica acesso do gerente (OU) ao tentar deletar um usuário.
1404                $manager_access = false;
1405                foreach ($this->manager_contexts as $index=>$context)
1406                {
1407                        if ( (strpos(strtolower($user_info['context']), strtolower($context))) || (strtolower($user_info['context']) == strtolower($context)) )
1408                        {
1409                                $manager_access = true;
1410                                break;
1411                        }
1412                }
1413                if (!$manager_access)
1414                {
1415                        $return['status'] = false;
1416                        $result['msg'] = $this->functions->lang('You do not have access to delete this user') . ".";
1417                        return $return;
1418                }
1419               
1420                $return['status'] = true;
1421                $return['msg'] = "";
1422                               
1423                // GROUPS
1424                $attrs = array();
1425                $attrs['memberuid'] = $user_info['uid'];
1426               
1427                if (count($user_info['groups_info']))
1428                {
1429                        foreach ($user_info['groups_info'] as $group_info)
1430                        {
1431                                $gidnumber = $group_info['gidnumber'];
1432                                $justthese = array("dn");
1433                                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1434                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1435                        $entry = ldap_get_entries($this->ldap, $search);
1436                                $dn = $entry[0]['dn'];
1437
1438                                if (!@ldap_mod_del($this->ldap, $dn, $attrs))
1439                                {
1440                                        $return['status'] = false;
1441                                        $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);
1442                                }
1443                        }
1444                }
1445               
1446                //INSTITUTIONAL ACCOUNTS
1447                $attrs = array();
1448                $attrs['mailForwardingAddress'] = $user_info['mail'];
1449               
1450                $justthese = array("dn");
1451                $filter="(&(phpgwAccountType=i)(mailforwardingaddress=".$user_info['mail']."))";
1452                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1453            $entries = ldap_get_entries($this->ldap, $search);
1454               
1455                for ($i=0; $i<$entries['count']; $i++)
1456                {
1457                        if ( !@ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs) )
1458                        {
1459                                $result['status'] = false;
1460                                $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);
1461                        }
1462                }
1463               
1464                // MAILLISTS
1465                $attrs = array();
1466                $attrs['mailForwardingAddress'] = $user_info['mail'];
1467               
1468                if (count($user_info['maillists_info']))
1469                {
1470                       
1471                        if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1472                        {
1473                                $return['status'] = false;
1474                                $result['msg'] = $this->functions->lang('Connection with ldap_master fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1475                                return $return;
1476                        }
1477                       
1478                        foreach ($user_info['maillists_info'] as $maillists_info)
1479                        {
1480                                $uid = $maillists_info['uid'];
1481                                $justthese = array("dn");
1482                                $filter="(&(phpgwAccountType=l)(uid=".$uid."))";
1483                                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1484                        $entry = ldap_get_entries($ldapMasterConnect, $search);
1485                                $dn = $entry[0]['dn'];
1486                       
1487                                if (!@ldap_mod_del($ldapMasterConnect, $dn, $attrs))
1488                                {
1489                                        $return['status'] = false;
1490                                        if (ldap_errno($ldapMasterConnect) == '50')
1491                                        {
1492                                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
1493                                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
1494                                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
1495                                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
1496                                        }
1497                                        else
1498                                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1499                                }
1500                        }
1501                        ldap_close($ldapMasterConnect);
1502                }
1503                       
1504                // UID
1505                $dn = "uid=" . $user_info['uid'] . "," . $user_info['context'];
1506                if (!@ldap_delete($this->ldap, $dn))
1507                {
1508                        $return['status'] = false;
1509                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1510                }
1511               
1512                return $return;
1513        }
1514       
1515        function delete_maillist($uidnumber, $mail)
1516        {
1517                $return['status'] = true;
1518               
1519                $justthese = array("dn");
1520               
1521                // remove listas dentro de listas
1522                $filter="(&(phpgwAccountType=l)(mailForwardingAddress=".$mail."))";
1523                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1524                $entry = ldap_get_entries($this->ldap, $search);
1525                $attrs['mailForwardingAddress'] = $mail;
1526                for ($i=0; $i<=$entry['count']; $i++)
1527            {
1528                        $dn = $entry[$i]['dn'];
1529                @ldap_mod_del ( $this->ldap, $dn,  $attrs);
1530            }
1531               
1532                $filter="(&(phpgwAccountType=l)(uidnumber=".$uidnumber."))";
1533                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1534                $entry = ldap_get_entries($this->ldap, $search);
1535                $dn = $entry[0]['dn'];
1536               
1537                if (!@ldap_delete($this->ldap, $dn))
1538                {
1539                        $return['status'] = false;
1540                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1541                }
1542               
1543                return $return;
1544        }
1545
1546        function delete_group($gidnumber)
1547        {
1548                $return['status'] = true;
1549               
1550                $justthese = array("dn");
1551                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1552                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1553                $entry = ldap_get_entries($this->ldap, $search);
1554                $dn = $entry[0]['dn'];
1555               
1556                if (!@ldap_delete($this->ldap, $dn))
1557                {
1558                        $return['status'] = false;
1559                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1560                }
1561               
1562                return $return;
1563        }
1564
1565        function check_access_to_renamed($uid)
1566        {
1567                $justthese = array("dn");
1568                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1569               
1570                foreach ($this->manager_contexts as $index=>$context)
1571                {
1572                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1573                        $entry = ldap_get_entries($this->ldap, $search);
1574                        if ($entry['count'])
1575                                return true;
1576                }
1577            return false;
1578        }
1579
1580        function check_rename_new_uid($uid)
1581        {
1582                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1583                        return false;
1584               
1585                $justthese = array("dn");
1586                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1587               
1588                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1589                $count_entries = @ldap_count_entries($ldapMasterConnect, $search);
1590               
1591                if ($count_entries)
1592                        return false;
1593                       
1594                return true;
1595        }
1596       
1597        function rename_uid($uid, $new_uid)
1598        {
1599                $return['status'] = true;
1600               
1601                $justthese = array("dn");
1602                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1603                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1604            $entry = ldap_get_entries($this->ldap, $search);
1605                $dn = $entry[0]['dn'];
1606               
1607                $explode_dn = ldap_explode_dn($dn, 0);
1608                $rdn = "uid=" . $new_uid;
1609
1610                $parent = array();
1611                for ($j=1; $j<(count($explode_dn)-1); $j++)
1612                        $parent[] = $explode_dn[$j];
1613                $parent = implode(",", $parent);
1614               
1615                $return['new_dn'] = $rdn . ',' . $parent;
1616                       
1617                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, true))
1618                {
1619                        $return['status'] = false;
1620                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->rename_uid ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1621                }
1622               
1623                //Grupos
1624                $justthese = array("dn");
1625                $filter="(&(phpgwAccountType=g)(memberuid=".$uid."))";
1626                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1627            $entry = ldap_get_entries($this->ldap, $search);
1628        $array_mod_add['memberUid'] = $new_uid;
1629        $array_mod_del['memberUid'] = $uid;
1630
1631            for ($i=0; $i<=$entry['count']; $i++)
1632            {
1633                $dn = $entry[$i]['dn'];
1634                @ldap_mod_add ( $this->ldap, $dn,  $array_mod_add);
1635                @ldap_mod_del ( $this->ldap, $dn,  $array_mod_del);
1636            }
1637                return $return;
1638        }
1639
1640        function rename_cn($cn, $new_cn)
1641        {
1642                $return['status'] = true;
1643               
1644                $justthese = array("dn");
1645                $filter="(&(phpgwAccountType=g)(uid=".$cn."))";
1646                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1647            $entry = ldap_get_entries($this->ldap, $search);
1648                $dn = $entry[0]['dn'];
1649               
1650                $explode_dn = ldap_explode_dn($dn, 0);
1651                $rdn = "cn=" . $new_cn;
1652
1653                $parent = array();
1654                for ($j=1; $j<(count($explode_dn)-1); $j++)
1655                        $parent[] = $explode_dn[$j];
1656                $parent = implode(",", $parent);
1657               
1658                $return['new_dn'] = $rdn . ',' . $parent;
1659                       
1660                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, false))
1661                {
1662                        $return['status'] = false;
1663                }
1664               
1665                return $return;
1666        }
1667       
1668        function exist_sambadomains($contexts, $sambaDomainName)
1669        {
1670                $justthese = array("dn");
1671                $filter="(&(objectClass=sambaDomain)(sambaDomainName=$sambaDomainName))";
1672               
1673                foreach ($contexts as $index=>$context)
1674                {
1675                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1676                    $entry = ldap_get_entries($this->ldap, $search);
1677           
1678                        if ($entry['count'])
1679                                return true;
1680                }
1681                return false;
1682        }
1683       
1684        // Primeiro nilvel de organização.
1685        function exist_sambadomains_in_context($params)
1686        {
1687                $dn = $GLOBALS['phpgw_info']['server']['ldap_context'];
1688                $array_dn = ldap_explode_dn ( $dn, 0 );
1689               
1690                $context = $params['context'];
1691                $array_context = ldap_explode_dn ( $context, 0 );
1692               
1693                // Pego o setor no caso do contexto ser um sub-setor.
1694                if (($array_dn['count']+1) < ($array_context['count']))
1695                {
1696                        // inverto o array_dn para poder retirar o count
1697                        $array_dn_reverse  = array_reverse ( $array_dn, false );
1698                       
1699                        //retiro o count
1700                        array_pop($array_dn_reverse);
1701                       
1702                        //incluo o setor no dn
1703                        array_push ( $array_dn_reverse,  $array_context[ $array_context['count'] - 1 - $array_dn['count']]);
1704                       
1705                        // Volto a ordem natural
1706                        $array_dn  = array_reverse ( $array_dn_reverse, false );
1707                       
1708                        // Implodo
1709                        $context = implode ( ",", $array_dn );
1710                }
1711               
1712                $justthese = array("dn","sambaDomainName");
1713                $filter="(objectClass=sambaDomain)";
1714                $search = ldap_list($this->ldap, $context, $filter, $justthese);
1715            $entry = ldap_get_entries($this->ldap, $search);
1716           
1717            for ($i=0; $i<$entry['count']; $i++)
1718            {
1719                        $return['sambaDomains'][$i] = $entry[$i]['sambadomainname'][0];
1720            }
1721           
1722                if ($entry['count'])
1723                        $return['status'] = true;
1724                else
1725                        $return['status'] = false;
1726                       
1727                return $return;
1728        }
1729        function exist_domain_name_sid($sambadomainname, $sambasid)
1730        {
1731                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
1732
1733                $justthese = array("dn","sambaDomainName");
1734                $filter="(&(objectClass=sambaDomain)(sambaSID=$sambasid)(sambaDomainName=$sambadomainname))";
1735                $search = ldap_search($this->ldap, $context, $filter, $justthese);
1736            $count_entries = ldap_count_entries($this->ldap, $search);
1737           
1738            if ($count_entries > 0)
1739                return true;
1740            else
1741                return false;
1742        }
1743       
1744        function add_sambadomain($sambadomainname, $sambasid, $context)
1745        {
1746                $result = array();
1747               
1748                $dn                                                             = "sambaDomainName=$sambadomainname,$context";
1749                $entry['sambaSID']                                      = $sambasid;
1750                $entry['objectClass']                           = 'sambaDomain';
1751                $entry['sambaAlgorithmicRidBase']       = '1000';
1752                $entry['sambaDomainName']                       = $sambadomainname;
1753               
1754                if (!@ldap_add ( $this->ldap, $dn, $entry ))
1755                {
1756                        $return['status'] = false;
1757                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_sambadomain ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1758                }
1759                else
1760                        $return['status'] = true;
1761               
1762                return $return;
1763        }
1764       
1765        function delete_sambadomain($sambadomainname)
1766        {
1767                $return['status'] = true;
1768                $filter="(sambaDomainName=$sambadomainname)";
1769                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
1770            $entry = ldap_get_entries($this->ldap, $search);
1771         
1772                if ($entry['count'] != 0)
1773            {
1774                        $dn = $entry[0]['dn'];
1775                       
1776                        if (!@ldap_delete($this->ldap, $dn))
1777                        {
1778                                $return['status'] = false;
1779                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_sambadomain ($sambadomainname)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1780                        }
1781            }
1782           
1783                return $return;
1784        }
1785       
1786        function search_user($params)
1787        {
1788                $search = $params['search'];
1789                $justthese = array("cn","uid", "mail");
1790        $users_list=ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(&(phpgwAccountType=u) (|(cn=*$search*)(mail=$search*)) )", $justthese);
1791       
1792        if (ldap_count_entries($this->ldap, $users_list) == 0)
1793        {
1794                $return['status'] = 'false';
1795                $result['msg'] = $this->functions->lang('Any result was found') . '.';
1796                return $return;
1797        }
1798       
1799        ldap_sort($this->ldap, $users_list, "cn");
1800       
1801        $entries = ldap_get_entries($this->ldap, $users_list);
1802               
1803                $options = '';
1804                for ($i=0; $i<$entries['count']; $i++)
1805                {
1806                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['cn'][0] . " (".$entries[$i]['mail'][0].")" . "</option>";
1807                }
1808       
1809        return $options;               
1810        }
1811       
1812        function create_institutional_accounts($params)
1813        {
1814                /* Begin: Access verification */
1815                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'add_institutional_accounts'))
1816                {
1817                        $return['status'] = false;
1818                        $return['msg'] = $this->functions->lang('You do not have right to create institutional accounts') . ".";
1819                        return $return;
1820                }
1821               
1822                $access_granted = false;
1823                foreach ($this->manager_contexts as $idx=>$manager_context)
1824                {
1825                        if (stristr($params['context'], $manager_context))
1826                        {
1827                                $access_granted = true;
1828                                break;
1829                        }
1830                }
1831                if (!$access_granted)
1832                {
1833                        $return['status'] = false;
1834                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
1835                        return $return;
1836                }
1837                /* End: Access verification */
1838
1839                /* Begin: Validation */
1840                if ( (empty($params['cn'])) || (empty($params['mail'])) )
1841                {
1842                        $result['status'] = false;
1843                        $result['msg']  = $this->functions->lang('Field mail or name is empty');
1844                        return $result;
1845                }
1846
1847                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
1848                {
1849                        $result['status'] = false;
1850                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
1851                        return $result;
1852                }
1853
1854                $uid = 'institutional_account_' . $params['mail'];
1855                $dn = "uid=$uid," . $params['context'];
1856
1857                $filter = "(mail=".$params['mail'].")";
1858                $justthese = array("cn");
1859                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1860                $entries = @ldap_get_entries($this->ldap,$search);
1861                if ($entries['count'] != 0)
1862                {
1863                        $result['status'] = false;
1864                        $result['msg'] = $this->functions->lang('Field mail already in use');
1865                        return $result;
1866                }
1867                /* End: Validation */
1868                                               
1869                $info = array();
1870                $info['cn']                                     = $params['cn'];
1871                $info['sn']                                     = $params['cn'];
1872                $info['uid']                            = $uid;
1873                $info['mail']                           = $params['mail'];
1874                $info['phpgwAccountType']       = 'i';
1875                $info['objectClass'][]          = 'inetOrgPerson';
1876                $info['objectClass'][]          = 'phpgwAccount';
1877                $info['objectClass'][]          = 'top';
1878                $info['objectClass'][]          = 'person';
1879                $info['objectClass'][]          = 'qmailUser';
1880                $info['objectClass'][]          = 'organizationalPerson';
1881               
1882                if ($params['accountStatus'] == 'on')
1883                {
1884                        $info['accountStatus'] = 'active';
1885                }
1886                if ($params['phpgwAccountVisible'] == 'on')
1887                {
1888                        $info['phpgwAccountVisible'] = '-1';
1889                }
1890               
1891                if (!empty($params['owners']))
1892                {
1893                        foreach($params['owners'] as $index=>$uidnumber)
1894                        {
1895                                $info['mailForwardingAddress'][] = $this->uidnumber2mail($uidnumber);
1896                        }
1897                }               
1898               
1899                $result = array();
1900                if (!@ldap_add ( $this->ldap, $dn, $info ))
1901                {
1902                        $result['status'] = false;
1903                        $result['msg']  = $this->functions->lang('Error in function') . ' ldap_functions->create_institutional_accounts';
1904                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
1905                }
1906                else
1907                        $result['status'] = true;
1908               
1909                return $result;
1910        }
1911       
1912        function save_institutional_accounts($params)
1913        {
1914                /* Begin: Access verification */
1915                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
1916                {
1917                        $return['status'] = false;
1918                        $return['msg'] = $this->functions->lang('You do not have right to edit institutional accounts') . ".";
1919                        return $return;
1920                }
1921                $access_granted = false;
1922                foreach ($this->manager_contexts as $idx=>$manager_context)
1923                {
1924                        if (stristr($params['context'], $manager_context))
1925                        {
1926                                $access_granted = true;
1927                                break;
1928                        }
1929                }
1930                if (!$access_granted)
1931                {
1932                        $return['status'] = false;
1933                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
1934                        return $return;
1935                }
1936                /* End: Access verification */
1937               
1938                /* Begin: Validation */
1939                if ( (empty($params['cn'])) || (empty($params['mail'])) )
1940                {
1941                        $result['status'] = false;
1942                        $result['msg']  = $this->functions->lang('Field mail or name is empty') . '.';
1943                        return $result;
1944                }
1945
1946                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
1947                {
1948                        $result['status'] = false;
1949                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
1950                        return $result;
1951                }
1952
1953                $uid = 'institutional_account_' . $params['mail'];
1954                $dn = strtolower("uid=$uid," . $params['context']);
1955                $anchor = strtolower($params['anchor']);
1956
1957                $filter = "(mail=".$params['mail'].")";
1958                $justthese = array("cn");
1959                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1960                $entries = @ldap_get_entries($this->ldap,$search);
1961               
1962                if ( ($entries['count'] > 1) || (($entries['count'] == 1) && ($entries[0]['dn'] != $anchor)) )
1963                {
1964                        $result['status'] = false;
1965                        $result['msg'] = $this->functions->lang('Field mail already in use.');
1966                        return $result;
1967                }
1968                /* End: Validation */
1969               
1970                $result = array();
1971                $result['status'] = true;
1972               
1973                if ($anchor != $dn)
1974                {
1975                        if (!@ldap_rename($this->ldap, $anchor, "uid=$uid", $params['context'], true))
1976                        {
1977                                $result['status'] = false;
1978                                $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_rename';
1979                                $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
1980                        }
1981                }
1982               
1983                $info = array();
1984                $info['cn']                                     = $params['cn'];
1985                $info['sn']                                     = $params['cn'];
1986                $info['uid']                            = $uid;
1987                $info['mail']                           = $params['mail'];
1988               
1989                if ($params['accountStatus'] == 'on')
1990                        $info['accountStatus'] = 'active';
1991                else
1992                        $info['accountStatus'] = array();
1993               
1994                if ($params['phpgwAccountVisible'] == 'on')
1995                        $info['phpgwAccountVisible'] = '-1';
1996                else
1997                        $info['phpgwAccountVisible'] = array();
1998               
1999                if ($params['description'] != '')
2000                        $info['description'] = utf8_encode($params['description']);
2001                else
2002                        $info['description'] = array();
2003               
2004                if (!empty($params['owners']))
2005                {
2006                        foreach($params['owners'] as $index=>$uidnumber)
2007                        {
2008                                $mailForwardingAddress = $this->uidnumber2mail($uidnumber);
2009                                if ($mailForwardingAddress != '')
2010                                        $info['mailForwardingAddress'][] = $mailForwardingAddress;
2011                        }
2012                }
2013                else
2014                        $info['mailForwardingAddress'] = array();
2015               
2016                if (!@ldap_modify ( $this->ldap, $dn, $info ))
2017                {
2018                        $result['status'] = false;
2019                        $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_modify';
2020                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2021                }
2022
2023                return $result;
2024        }
2025       
2026        function get_institutional_accounts($params)
2027        {
2028                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'list_institutional_accounts'))
2029                {
2030                        $return['status'] = false;
2031                        $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
2032                        return $return;
2033                }
2034
2035                $input = $params['input'];
2036                $justthese = array("cn", "mail", "uid");
2037                $trs = array();
2038                               
2039                foreach ($this->manager_contexts as $idx=>$context)
2040                {
2041                $institutional_accounts = ldap_search($this->ldap, $context, ("(&(phpgwAccountType=i)(|(mail=$input*)(cn=*$input*)))"), $justthese);
2042                $entries = ldap_get_entries($this->ldap, $institutional_accounts);
2043               
2044                        for ($i=0; $i<$entries['count']; $i++)
2045                        {
2046                                $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>";
2047                                $trs[$tr] = $entries[$i]['cn'][0];
2048                        }
2049                }
2050       
2051        $trs_string = '';
2052        if (count($trs))
2053        {
2054                natcasesort($trs);
2055                foreach ($trs as $tr=>$cn)
2056                {
2057                        $trs_string .= $tr;
2058                }
2059        }
2060       
2061        $return['status'] = 'true';
2062        $return['trs'] = $trs_string;
2063        return $return;
2064        }
2065       
2066        function get_institutional_account_data($params)
2067        {
2068                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
2069                {
2070                        $return['status'] = false;
2071                        $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
2072                        return $return;
2073                }
2074               
2075                $uid = $params['uid'];
2076                //$justthese = array("accountStatus", "phpgwAccountVisible", "cn", "mail", "mailForwardingAddress", "description");
2077                               
2078        $institutional_accounts = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"));
2079        $entrie = ldap_get_entries($this->ldap, $institutional_accounts);
2080               
2081                if ($entrie['count'] != 1)
2082                {
2083                        $return['status'] = 'false';
2084                        $result['msg'] = $this->functions->lang('Problems loading datas') . '.';
2085                }
2086                else
2087                {
2088                        $tmp_user_context = split(",", $entrie[0]['dn']);
2089                        $tmp_reverse_user_context = array_reverse($tmp_user_context);
2090                        array_pop($tmp_reverse_user_context);
2091                        $return['user_context'] = implode(",", array_reverse($tmp_reverse_user_context));
2092                       
2093                        $return['status'] = 'true';
2094                        $return['accountStatus']                = $entrie[0]['accountstatus'][0];
2095                        $return['phpgwAccountVisible']  = $entrie[0]['phpgwaccountvisible'][0];
2096                        $return['cn']                                   = $entrie[0]['cn'][0];
2097                        $return['mail']                                 = $entrie[0]['mail'][0];
2098                        $return['description']                  = utf8_decode($entrie[0]['description'][0]);
2099
2100                        if ($entrie[0]['mailforwardingaddress']['count'] > 0)
2101                        {
2102                                $a_cn = array();
2103                                for ($i=0; $i<$entrie[0]['mailforwardingaddress']['count']; $i++)
2104                                {
2105                                        $tmp = $this->mailforwardingaddress2uidnumber($entrie[0]['mailforwardingaddress'][$i]);
2106                                        if (!$tmp) {}
2107                                        else
2108                                                $a_cn[$tmp['uidnumber']] = $tmp['cn'];
2109                                }
2110                                natcasesort($a_cn);
2111                                foreach($a_cn as $uidnumber => $cn)
2112                                {
2113                                        $return['owners'] .= '<option value='. $uidnumber .'>' . $cn . '</option>';
2114                                }
2115                        }
2116                }
2117               
2118                return $return;
2119        }
2120       
2121        function mailforwardingaddress2uidnumber($mail)
2122        {
2123                $justthese = array("uidnumber","cn");
2124        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=u)(mail=$mail))"), $justthese);
2125        $entrie = ldap_get_entries($this->ldap, $search);
2126                if ($entrie['count'] != 1)
2127                        return false;
2128                else
2129                {
2130                        $return['uidnumber'] = $entrie[0]['uidnumber'][0];
2131                        $return['cn'] = $entrie[0]['cn'][0];
2132                        return $return;
2133                }
2134        }
2135       
2136        function delete_institutional_account_data($params)
2137        {
2138                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'remove_institutional_accounts'))
2139                {
2140                        $return['status'] = false;
2141                        $return['msg'] = $this->functions->lang('You do not have right to delete institutional accounts') . ".";
2142                        return $return;
2143                }
2144
2145                $uid = $params['uid'];
2146                $return['status'] = true;
2147                               
2148                $justthese = array("cn");
2149        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"), $justthese);
2150        $entrie = ldap_get_entries($this->ldap, $search);
2151                if ($entrie['count'] > 1)
2152                {
2153                        $return['status'] = false;
2154                        $return['msg']  = $this->functions->lang('More then one uid was found');
2155                        return $return;
2156                }               
2157                if ($entrie['count'] == 0)
2158                {
2159                        $return['status'] = false;
2160                        $return['msg']  = $this->functions->lang('No uid was found');
2161                        return $return;
2162                }               
2163               
2164                $dn = $entrie[0]['dn'];
2165                if (!@ldap_delete($this->ldap, $dn))
2166                {
2167                        $return['status'] = false;
2168                        $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->delete_institutional_accounts: ldap_delete";
2169                        $return['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2170                        return $return;
2171                }
2172               
2173                return $return;
2174        }
2175       
2176        function replace_mail_from_institutional_account($newMail, $oldMail)
2177        {
2178                $filter = "(&(phpgwAccountType=i)(mailforwardingaddress=$oldMail))";
2179                $justthese = array("dn");
2180                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2181                $entries = ldap_get_entries($this->ldap, $search);
2182                $result['status'] = true;
2183                for ($i=0; $i<$entries['count']; $i++)
2184                {
2185                        $attrs['mailforwardingaddress'] = $oldMail;
2186                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
2187                        $attrs['mailforwardingaddress'] = $newMail;
2188                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
2189               
2190                        if ((!$res1) || (!$res2))
2191                        {
2192                                $result['status'] = false;
2193                                $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->replace_mail_from_institutional_account.";
2194                        }
2195                }
2196               
2197                return $result;
2198        }
2199}
2200?>
Note: See TracBrowser for help on using the repository browser.