source: branches/2.2/expressoAdmin1_2/inc/class.ldap_functions.inc.php @ 3370

Revision 3370, 92.9 KB checked in by eduardoalex, 13 years ago (diff)

Ticket #1195 - Adicionado o tratamento para os campos com acentuacao

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