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

Revision 2707, 90.6 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1058 - Corrigindo problemas no expressoAdmin1_2.

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