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

Revision 414, 75.0 KB checked in by niltonneto, 16 years ago (diff)

Alterações feitas por João Alfredo.
Email: jakjr@…

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