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

Revision 2364, 90.2 KB checked in by amuller, 14 years ago (diff)

Ticket #1008 - Adicionando licença aos arquivos php

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