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

Revision 1991, 89.4 KB checked in by valmir.sena, 14 years ago (diff)

Ticket #902 - Permitir que seja definido um dominio diferente por organizacao

  • 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");
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] . "\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                {
767                        $lists_search = ldap_search($ldap_conn_following_ref, $context, $lists_filter, $justthese);
768                        $users_search = ldap_search($ldap_conn_following_ref, $context, $users_filter, $justthese);
769                }
770                else
771                {
772                        $lists_search = ldap_list($ldap_conn_following_ref, $context, $lists_filter, $justthese);
773                        $users_search = ldap_list($ldap_conn_following_ref, $context, $users_filter, $justthese);
774                }
775               
776                /* email lists */
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                /* users */
801                $users_entries = ldap_get_entries($ldap_conn_following_ref, $users_search);
802                for ($i=0; $i<$users_entries["count"]; $i++)
803                {
804                        $u_tmp[$users_entries[$i]["mail"][0]] = $users_entries[$i]["cn"][0];
805                }
806                       
807                if (count($u_tmp))
808                        natcasesort($u_tmp);
809                       
810                $i = 0;
811                $users = array();
812               
813                $options .= '<option  value="-1" disabled>-----------------------------&nbsp;&nbsp;&nbsp;&nbsp;'.$this->functions->lang('users').'&nbsp;&nbsp;&nbsp;&nbsp;---------------------------- </option>'."\n";
814                       
815                if (count($u_tmp))
816                {
817                        foreach ($u_tmp as $mail => $cn)
818                        {
819                                $options .= "<option value=$mail class='line-above'>$cn</option>";
820                        }
821                        unset($u_tmp);
822                }
823               
824                ldap_close($ldap_conn_following_ref);
825                return $options;
826        }
827
828        function get_available_groups($params)
829        {
830                $context = $params['context'];
831                $justthese = array("cn", "gidNumber");
832        $groups_list=ldap_list($this->ldap, $context, ("(phpgwAccountType=g)"), $justthese);
833        ldap_sort($this->ldap, $groups_list, "cn");
834       
835        $entries = ldap_get_entries($this->ldap, $groups_list);
836               
837                $options = '';
838                for ($i=0; $i<$entries['count']; $i++)
839                {
840                        $options .= "<option value=" . $entries[$i]['gidnumber'][0] . ">" . $entries[$i]['cn'][0] . "</option>";
841                }
842       
843        return $options;               
844        }
845       
846        function get_available_maillists($params)
847        {
848                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
849                        return false;
850               
851                $context = $params['context'];
852                $justthese = array("uid","mail","uidNumber");
853        $maillists=ldap_list($ldapMasterConnect, $context, ("(phpgwAccountType=l)"), $justthese);
854        ldap_sort($ldapMasterConnect, $maillists, "uid");
855       
856        $entries = ldap_get_entries($ldapMasterConnect, $maillists);
857       
858                $options = '';                 
859                for ($i=0; $i<$entries['count']; $i++)
860                {
861                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['uid'][0] . " (" . $entries[$i]['mail'][0] . ")" . "</option>";
862                }
863       
864        ldap_close($ldapMasterConnect);
865        return $options;
866        }
867       
868        function ldap_add_entry($dn, $entry)
869        {
870                $result = array();
871                if (!@ldap_add ( $this->ldap, $dn, $entry ))
872                {
873                        $result['status']               = false;
874                        $result['error_number'] = ldap_errno($this->ldap);
875                        $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);
876                }
877                else
878                        $result['status'] = true;
879               
880                return $result;
881        }
882       
883        function ldap_save_photo($dn, $pathphoto, $photo_exist=false)
884        {
885                $fd = fopen($pathphoto, "r");
886                $fsize = filesize($pathphoto);
887                $jpegStr = fread($fd, $fsize);
888                fclose ($fd);
889                $attrs['jpegPhoto'] = $jpegStr;
890                       
891                if ($photo_exist)
892                        $res = @ldap_mod_replace($this->ldap, $dn, $attrs);
893                else
894                        $res = @ldap_mod_add($this->ldap, $dn, $attrs);
895                       
896                if ($res)
897                {
898                        $result['status'] = true;
899                }
900                else
901                {
902                        $result['status'] = false;
903                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_save_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
904                }
905               
906                return $result;
907        }
908       
909        function ldap_remove_photo($dn)
910        {
911                $attrs['jpegPhoto'] = array();
912                $res = ldap_mod_del($this->ldap, $dn, $attrs);
913               
914                if ($res)
915                {
916                        $result['status'] = true;
917                }
918                else
919                {
920                        $result['status'] = false;
921                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_remove_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
922                }
923               
924                return $result;
925        }       
926       
927        // Pode receber tanto um único memberUid quanto um array de memberUid's
928        function add_user2group($gidNumber, $memberUid)
929        {
930                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
931                $justthese = array("dn");
932                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
933                $entry = ldap_get_entries($this->ldap, $search);
934                $group_dn = $entry[0]['dn'];
935                $attrs['memberUid'] = $memberUid;
936               
937                $res = @ldap_mod_add($this->ldap, $group_dn, $attrs);
938               
939                if ($res)
940                {
941                        $result['status'] = true;
942                }
943                else
944                {
945                        $result['status'] = false;
946                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
947                }
948                return $result;
949        }
950       
951        function remove_user2group($gidNumber, $memberUid)
952        {
953                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
954                $justthese = array("dn");
955                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
956                $entry = ldap_get_entries($this->ldap, $search);
957                $group_dn = $entry[0]['dn'];
958                $attrs['memberUid'] = $memberUid;
959                $res = @ldap_mod_del($this->ldap, $group_dn, $attrs);
960               
961                if ($res)
962                {
963                        $result['status'] = true;
964                }
965                else
966                {
967                        $result['status'] = false;
968                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
969                }
970                return $result;
971        }
972       
973        function add_user2maillist($uid, $mail)
974        {
975                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
976                {
977                        $result['status'] = false;
978                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
979                        return $result;
980                }
981                       
982                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
983                $justthese = array("dn");
984                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
985                $entry = ldap_get_entries($ldapMasterConnect, $search);
986                $group_dn = $entry[0]['dn'];
987                $attrs['mailForwardingAddress'] = $mail;
988                $res = @ldap_mod_add($ldapMasterConnect, $group_dn, $attrs);
989               
990                if ($res)
991                {
992                        $result['status'] = true;
993                }
994                else
995                {
996                        $result['status'] = false;
997                        if (ldap_errno($ldapMasterConnect) == '50')
998                        {
999                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
1000                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
1001                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
1002                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
1003                        }                                       
1004                        else
1005                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1006                }
1007               
1008                ldap_close($ldapMasterConnect);
1009                return $result;
1010        }
1011       
1012        function add_user2maillist_scl($dn, $array_emails)
1013        {
1014                $attrs['mailSenderAddress'] = $array_emails;
1015               
1016                $res = @ldap_mod_add($this->ldap, $dn, $attrs);
1017               
1018                if ($res)
1019                {
1020                        $result['status'] = true;
1021                }
1022                else
1023                {
1024                        $result['status'] = false;
1025                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1026                }
1027                return $result;
1028        }
1029
1030        function remove_user2maillist($uid, $mail)
1031        {
1032                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1033                {
1034                        $result['status'] = false;
1035                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1036                        return $result;
1037                }
1038               
1039                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
1040                $justthese = array("dn");
1041                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1042                $entry = ldap_get_entries($ldapMasterConnect, $search);
1043                $group_dn = $entry[0]['dn'];
1044                $attrs['mailForwardingAddress'] = $mail;
1045                $res = @ldap_mod_del($ldapMasterConnect, $group_dn, $attrs);
1046               
1047                if ($res)
1048                {
1049                        $result['status'] = true;
1050                }
1051                else
1052                {
1053                        $result['status'] = false;
1054                        if (ldap_errno($ldapMasterConnect) == '50')
1055                        {
1056                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->remove_user2maillist' . ".\n" .
1057                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
1058                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
1059                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
1060                        }                                       
1061                        else
1062                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1063                }
1064                ldap_close($ldapMasterConnect);
1065                return $result;
1066        }
1067
1068        function remove_user2maillist_scl($dn, $array_emails)
1069        {
1070                $attrs['mailSenderAddress'] = $array_emails;
1071                $res = @ldap_mod_del($this->ldap, $dn, $attrs);
1072               
1073                if ($res)
1074                {
1075                        $result['status'] = true;
1076                }
1077                else
1078                {
1079                        $result['status'] = false;
1080                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1081                }
1082                return $result;
1083        }
1084
1085        function replace_user2maillists($new_mail, $old_mail)
1086        {
1087                $filter = "(&(phpgwAccountType=l)(mailforwardingaddress=$old_mail))";
1088                $justthese = array("dn");
1089                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1090                $entries = ldap_get_entries($this->ldap, $search);
1091                $result['status'] = true;
1092                for ($i=0; $i<$entries['count']; $i++)
1093                {
1094                        $attrs['mailforwardingaddress'] = $old_mail;
1095                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
1096                        $attrs['mailforwardingaddress'] = $new_mail;
1097                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
1098               
1099                        if ((!$res1) || (!$res2))
1100                        {
1101                                $result['status'] = false;
1102                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user2maillists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1103                        }
1104                }
1105               
1106                return $result;
1107        }
1108       
1109        function get_user_info($uidnumber)
1110        {
1111                foreach ($this->manager_contexts as $index=>$context)
1112                {
1113                        $filter="(&(phpgwAccountType=u)(uidNumber=".$uidnumber."))";
1114                        $search = ldap_search($this->ldap, $context, $filter);
1115                        $entry = ldap_get_entries($this->ldap, $search);
1116                       
1117                        if ($entry['count'])
1118                        {
1119                                //Pega o dn do setor do usuario.
1120                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1121                                $sector_dn_array = explode(",", $entry[0]['dn']);
1122                                for($i=1; $i<count($sector_dn_array); $i++)
1123                                        $sector_dn .= $sector_dn_array[$i] . ',';
1124                                //Retira ultimo pipe.
1125                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1126               
1127                                $result['context']                              = $sector_dn;
1128                                $result['uid']                                  = $entry[0]['uid'][0];
1129                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
1130                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
1131                                $result['departmentnumber']             = $entry[0]['departmentnumber'][0];
1132                                $result['givenname']                    = $entry[0]['givenname'][0];
1133                                $result['sn']                                   = $entry[0]['sn'][0];
1134                                $result['telephonenumber']              = $entry[0]['telephonenumber'][0];
1135                                $result['passwd_expired']               = $entry[0]['phpgwlastpasswdchange'][0];
1136                                $result['phpgwaccountstatus']   = $entry[0]['phpgwaccountstatus'][0];
1137                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
1138                                $result['accountstatus']                = $entry[0]['accountstatus'][0];
1139                                $result['mail']                                 = $entry[0]['mail'][0];
1140                                $result['mailalternateaddress'] = $entry[0]['mailalternateaddress'];
1141                                $result['mailforwardingaddress']= $entry[0]['mailforwardingaddress'];
1142                                $result['deliverymode']                 = $entry[0]['deliverymode'][0];
1143                                $result['userPasswordRFC2617']  = $entry[0]['userpasswordrfc2617'][0];
1144
1145                                //Photo
1146                                if ($entry[0]['jpegphoto']['count'] == 1)
1147                                        $result['photo_exist'] = 'true';
1148               
1149                                // Samba
1150                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1151                                {
1152                                        if ($entry[0]['objectclass'][$i] == 'sambaSamAccount')
1153                                                $result['sambaUser'] = true;
1154                                }
1155                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($result['sambaUser']))
1156                                {
1157                                        $result['sambaaccflags'] = $entry[0]['sambaacctflags'][0];
1158                                        $result['sambalogonscript'] = $entry[0]['sambalogonscript'][0];
1159                                        $result['homedirectory'] = $entry[0]['homedirectory'][0];
1160                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1161                                        array_pop($a_tmp);
1162                                        $result['sambasid'] = implode("-", $a_tmp);
1163                                }
1164
1165                                // Verifica o acesso do gerente aos atributos corporativos
1166                                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
1167                                {
1168                                        $result['corporative_information_employeenumber']= $entry[0]['employeenumber'][0];
1169                                        $result['corporative_information_cpf']                  = $entry[0]['cpf'][0];
1170                                        $result['corporative_information_rg']                   = $entry[0]['rg'][0];
1171                                        $result['corporative_information_rguf']                 = $entry[0]['rguf'][0];
1172                                        $result['corporative_information_description']  = utf8_decode($entry[0]['description'][0]);
1173                                }
1174                               
1175                                // MailLists
1176                                $result['maillists_info'] = $this->get_user_maillists($result['mail']);
1177                                if($result['maillists_info'])
1178                                {
1179                                        foreach ($result['maillists_info'] as $maillist)
1180                                        {
1181                                                $result['maillists'][] = $maillist['uid'];
1182                                        }
1183                                }
1184                               
1185                                // Groups
1186                                $justthese = array("gidnumber","cn");
1187                                $filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
1188                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1189                        ldap_sort($this->ldap, $search, "cn");
1190                        $entries = ldap_get_entries($this->ldap, $search);
1191                        for ($i=0; $i<$entries['count']; $i++)
1192                        {
1193                                $result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
1194                        }
1195                        }
1196                }
1197                if (is_array($result))
1198                        return $result;
1199                else
1200                        return false;
1201        }
1202               
1203        function get_user_maillists($mail)
1204        {
1205                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1206                        return false;
1207               
1208                $result = array();
1209               
1210                //Mostra somente os mailists dos contextos do gerente
1211                $justthese = array("uid","mail","uidnumber");
1212                $filter="(&(phpgwAccountType=l)(mailforwardingaddress=$mail))";
1213               
1214                foreach ($this->manager_contexts as $index=>$context)
1215                {
1216                        $search = ldap_search($ldapMasterConnect, $context, $filter, $justthese);
1217                $entries = ldap_get_entries($ldapMasterConnect, $search);
1218               
1219                for ($i=0; $i<$entries['count']; $i++)
1220                {
1221                                $result[ $entries[$i]['uid'][0] ]['uid']                = $entries[$i]['uid'][0];
1222                                $result[ $entries[$i]['uid'][0] ]['mail']               = $entries[$i]['mail'][0];
1223                               
1224                                $a_tmp[] = $entries[$i]['uid'][0];
1225                }
1226                }
1227       
1228        if($a_tmp) {
1229                natcasesort($a_tmp);
1230       
1231                foreach ($a_tmp as $uid)
1232                {
1233                                $return[$uid]['uid']            = $result[$uid]['uid'];
1234                                $return[$uid]['mail']           = $result[$uid]['mail'];
1235                }
1236        }
1237        ldap_close($ldapMasterConnect);
1238                return $return;
1239        }
1240       
1241        function get_group_info($gidnumber)
1242        {
1243                foreach ($this->manager_contexts as $index=>$context)
1244                {
1245                        $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1246                        $search = ldap_search($this->ldap, $context, $filter);
1247                        $entry = ldap_get_entries($this->ldap, $search);
1248                       
1249                        if ($entry['count'])
1250                        {
1251                                //Pega o dn do setor do grupo.
1252                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1253                                $sector_dn_array = explode(",", $entry[0]['dn']);
1254                                for($i=1; $i<count($sector_dn_array); $i++)
1255                                        $sector_dn .= $sector_dn_array[$i] . ',';
1256                                //Retira ultimo pipe.
1257                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1258               
1259                                $result['context']                              = $sector_dn;
1260                                $result['cn']                                   = $entry[0]['cn'][0];
1261                                $result['description']                  = $entry[0]['description'][0];
1262                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
1263                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
1264                                $result['email']                                = $entry[0]['mail'][0];
1265               
1266                                //MemberUid
1267                                for ($i=0; $i<$entry[0]['memberuid']['count']; $i++)
1268                                {
1269                                        $justthese = array("cn","uid","uidnumber");
1270                       
1271                                        // Montagem dinamica do filtro
1272                                        $filter="(&(phpgwAccountType=u)(|";
1273                                        for ($k=0; (($k<10) && ($i<$entry[0]['memberuid']['count'])); $k++)
1274                                        {
1275                                                $filter .= "(uid=".$entry[0]['memberuid'][$i].")";
1276                                                $i++;
1277                                        }
1278                                        $i--;
1279                                        $filter .= "))";
1280                       
1281                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1282                                        $user_entry = ldap_get_entries($this->ldap, $search);
1283
1284                                        for ($j=0; $j<$user_entry['count']; $j++)
1285                                        {
1286                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['cn'] = $user_entry[$j]['cn'][0];
1287                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['uidnumber'] = $user_entry[$j]['uidnumber'][0];
1288                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['type'] = 'u';
1289                                        }
1290                                }
1291               
1292                                // Checamos e-mails que não fazem parte do expresso.
1293                                // Criamos um array temporario
1294                                $tmp_array = array();
1295                                if($result['memberuid_info'])
1296                                        foreach ($result['memberuid_info'] as $uid => $user_data)
1297                                        {
1298                                                $tmp_array[] = $uid;
1299                                        }
1300               
1301                                if($entry[0]['memberuid']) {
1302                                        // Retira o count do array
1303                                        array_shift($entry[0]['memberuid']);
1304                                        // Vemos a diferença
1305                                        $array_diff = array_diff($entry[0]['memberuid'], $tmp_array);
1306                                        // Incluimos no resultado                       
1307                                        foreach ($array_diff as $index=>$uid)
1308                                        {
1309                                                $result['memberuid_info'][$uid]['cn'] = $uid;
1310                                        }
1311                                }
1312               
1313                                // Samba
1314                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1315                                {
1316                                        if ($entry[0]['objectclass'][$i] == 'sambaGroupMapping')
1317                                                $result['sambaGroup'] = true;
1318
1319                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1320                                        array_pop($a_tmp);
1321                                        $result['sambasid'] = implode("-", $a_tmp);
1322                                }
1323                                return $result;
1324                        }
1325                }
1326        }       
1327       
1328        function get_maillist_info($uidnumber)
1329        {
1330                /* folling referral connection */
1331                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
1332                if ($ldap_conn_following_ref)
1333                {
1334                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
1335                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
1336
1337                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
1338                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
1339                }
1340               
1341                foreach ($this->manager_contexts as $index=>$context)
1342                {
1343                        $filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
1344                        $search = ldap_search($this->ldap, $context, $filter);
1345                        $entry = ldap_get_entries($this->ldap, $search);
1346                       
1347                        if ($entry['count'])
1348                        {
1349                                //Pega o dn do setor do usuario.
1350                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1351                                $sector_dn_array = explode(",", $entry[0]['dn']);
1352                                for($i=1; $i<count($sector_dn_array); $i++)
1353                                        $sector_dn .= $sector_dn_array[$i] . ',';
1354                                //Retira ultimo pipe.
1355                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1356                       
1357                                $result['context']                              = $sector_dn;
1358                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
1359                                $result['uid']                                  = strtolower($entry[0]['uid'][0]);
1360                                $result['cn']                                   = $entry[0]['cn'][0];
1361                                $result['mail']                                 = $entry[0]['mail'][0];
1362                                $result['description']                  = utf8_decode($entry[0]['description'][0]);
1363                                $result['accountStatus']                = $entry[0]['accountstatus'][0];
1364                                $result['phpgwAccountVisible']  = $entry[0]['phpgwaccountvisible'][0];
1365                       
1366                                //Members
1367                                for ($i=0; $i<$entry[0]['mailforwardingaddress']['count']; $i++)
1368                                {
1369                                        $justthese = array("cn", "uidnumber", "uid", "phpgwaccounttype", "mail");
1370                               
1371                                        // Montagem dinamica do filtro, para nao ter muitas conexoes com o ldap
1372                                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|";
1373                                        for ($k=0; (($k<10) && ($i<$entry[0]['mailforwardingaddress']['count'])); $k++)
1374                                        {
1375                                                $filter .= "(mail=".$entry[0]['mailforwardingaddress'][$i].")";
1376                                                $i++;
1377                                        }
1378                                        $i--;
1379                                        $filter .= "))";
1380                               
1381                                        $search = ldap_search($ldap_conn_following_ref, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1382                                        $user_entry = ldap_get_entries($ldap_conn_following_ref, $search);
1383                                                                       
1384                                        for ($j=0; $j<$user_entry['count']; $j++)
1385                                        {
1386                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['uid'] = $user_entry[$j]['uid'][0];
1387                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['cn'] = $user_entry[$j]['cn'][0];
1388                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['type'] = $user_entry[$j]['phpgwaccounttype'][0];
1389                                                $result['mailForwardingAddress'][] = $user_entry[$j]['mail'][0];
1390                                        }
1391                                }
1392
1393                                // Emails não encontrados no ldap
1394                                array_shift($entry[0]['mailforwardingaddress']); //Retira o count do array
1395                                $missing_emails = array_diff($entry[0]['mailforwardingaddress'], $result['mailForwardingAddress']);
1396                               
1397                                // Incluimos estes no resultado
1398                                foreach ($missing_emails as $index=>$mailforwardingaddress)
1399                                {
1400                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['uid'] = $mailforwardingaddress;
1401                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['cn'] = 'E-Mail nao encontrado';
1402                                        $result['mailForwardingAddress'][] = $mailforwardingaddress;
1403                                }
1404                               
1405                                ldap_close($ldap_conn_following_ref);
1406                                return $result;
1407                        }
1408                }
1409        }       
1410
1411        function get_maillist_scl_info($uidnumber)
1412        {
1413                foreach ($this->manager_contexts as $index=>$context)
1414                {
1415                        $filter="(&(phpgwAccountType=l)(uidNumber=$uidnumber))";
1416                        $search = ldap_search($this->ldap, $context, $filter);
1417                        $entry = ldap_get_entries($this->ldap, $search);
1418
1419                        if ($entry['count'])
1420                        {
1421                                //Pega o dn do setor do usuario.
1422                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1423                                $sector_dn_array = explode(",", $entry[0]['dn']);
1424                                for($i=1; $i<count($sector_dn_array); $i++)
1425                                        $sector_dn .= $sector_dn_array[$i] . ',';
1426                                //Retira ultimo pipe.
1427                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1428               
1429                                $result['dn']                                           = $entry[0]['dn'];
1430                                $result['context']                                      = $sector_dn;
1431                                $result['uidnumber']                            = $entry[0]['uidnumber'][0];
1432                                $result['uid']                                          = $entry[0]['uid'][0];
1433                                $result['cn']                                           = $entry[0]['cn'][0];
1434                                $result['mail']                                         = $entry[0]['mail'][0];
1435                                $result['accountStatus']                        = $entry[0]['accountstatus'][0];
1436                                $result['phpgwAccountVisible']          = $entry[0]['phpgwaccountvisible'][0];
1437                                $result['accountRestrictive']           = $entry[0]['accountrestrictive'][0];
1438                                $result['participantCanSendMail']       = $entry[0]['participantcansendmail'][0];
1439               
1440                                //Senders
1441                                for ($i=0; $i<$entry[0]['mailsenderaddress']['count']; $i++)
1442                                {
1443                                        $justthese = array("cn", "uidnumber", "uid", "mail");
1444                                        $filter="(&(phpgwAccountType=u)(mail=".$entry[0]['mailsenderaddress'][$i]."))";
1445                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1446                                        $user_entry = ldap_get_entries($this->ldap, $search);
1447                       
1448                                        $result['senders_info'][$user_entry[0]['mail'][0]]['uid'] = $user_entry[0]['uid'][0];
1449                                        $result['senders_info'][$user_entry[0]['mail'][0]]['cn'] = $user_entry[0]['cn'][0];
1450                                        $result['members'][] = $user_entry[0]['mail'][0];
1451                                }
1452                                return $result;
1453                        }
1454                }
1455        }       
1456
1457        function group_exist($gidnumber)
1458        {
1459                $justthese = array("cn");
1460                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1461                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1462                               
1463                $entry = ldap_get_entries($this->ldap, $search);
1464                if ($entry['count'] == 0)
1465                        return false;
1466                else
1467                        return true;
1468        }
1469
1470        function gidnumbers2cn($gidnumbers)
1471        {
1472                $result = array();
1473                if (count($gidnumbers))
1474                {
1475                        $justthese = array("cn");
1476                        $i = 0;
1477                        foreach ($gidnumbers as $gidnumber)
1478                        {
1479                                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1480                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1481                               
1482                                $entry = ldap_get_entries($this->ldap, $search);
1483                                if ($entry['count'] == 0)
1484                                        $result['groups_info'][$i]['cn'] = '_' . $this->functions->lang('group only exist on DB, but does not exist on ldap');
1485                                       
1486                                else
1487                                        $result['groups_info'][$i]['cn'] = $entry[0]['cn'][0];
1488                                $result['groups_info'][$i]['gidnumber'] = $gidnumber;
1489                       
1490                                /* o gerente pode excluir um grupo de um usuario onde este grupo esta em outra OU ? */
1491                                /* é o mesmo que o manager editar um grupo de outra OU */
1492                                $result['groups_info'][$i]['group_disabled'] = 'true';
1493                                foreach ($this->manager_contexts as $index=>$context)
1494                                {
1495                                        if (strpos(strtolower($entry[0]['dn']), strtolower($context)))
1496                                        {
1497                                                $result['groups_info'][$i]['group_disabled'] = 'false';
1498                                        }
1499                                }
1500
1501                                $i++;
1502                        }
1503                }
1504                return $result;
1505        }
1506
1507        function uidnumber2uid($uidnumber)
1508        {
1509                $justthese = array("uid");
1510                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1511                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1512                $entry = ldap_get_entries($this->ldap, $search);
1513                return $entry[0]['uid'][0];
1514        }
1515       
1516        function uid2cn($uid)
1517        {
1518                $justthese = array("cn");
1519                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=".$uid."))";
1520                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1521                $entry = ldap_get_entries($this->ldap, $search);
1522                return $entry[0]['cn'][0];
1523        }
1524
1525        function uidnumber2mail($uidnumber)
1526        {
1527                $justthese = array("mail");
1528                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1529                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1530                $entry = ldap_get_entries($this->ldap, $search);
1531                return $entry[0]['mail'][0];
1532        }
1533
1534        function get_associated_domain($params)
1535        {
1536                        $justthese = array("associatedDomain");
1537                        $filter="(objectClass=domainRelatedObject)";;
1538                        $context = $params['context'];
1539                        $search = ldap_search($this->ldap,$context, $filter, $justthese);
1540                        $entry = ldap_get_entries($this->ldap, $search);
1541                        return $entry[0]['associateddomain'][0];
1542        }
1543       
1544        function change_user_context($dn, $newrdn, $newparent)
1545        {
1546                if (!ldap_rename ( $this->ldap, $dn, $newrdn, $newparent, true ))
1547                {
1548                        $return['status'] = false;
1549                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->change_user_context ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1550                }
1551                else
1552                        $return['status'] = true;
1553               
1554                return $return;
1555        }
1556       
1557        function replace_user_attributes($dn, $ldap_mod_replace)
1558        {
1559                if (!@ldap_mod_replace ( $this->ldap, $dn, $ldap_mod_replace ))
1560                {
1561                        $return['status'] = false;
1562                        $return['error_number'] = ldap_errno($this->ldap);
1563                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1564                }
1565                else
1566                        $return['status'] = true;
1567               
1568                return $return;
1569        }
1570       
1571        function add_user_attributes($dn, $ldap_add)
1572        {
1573                if (!@ldap_mod_add ( $this->ldap, $dn, $ldap_add ))
1574                {
1575                        $return['status'] = false;
1576                        $return['error_number'] = ldap_errno($this->ldap);
1577                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1578                }
1579                else
1580                        $return['status'] = true;
1581               
1582                return $return;
1583        }
1584       
1585        function remove_user_attributes($dn, $ldap_remove)
1586        {
1587                if (!@ldap_mod_del ( $this->ldap, $dn, $ldap_remove ))
1588                {
1589                        $return['status'] = false;
1590                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1591                }
1592                else
1593                        $return['status'] = true;
1594               
1595                return $return;
1596        }
1597       
1598        function set_user_password($uid, $password)
1599        {
1600                $justthese = array("userPassword");
1601                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1602                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1603            $entry = ldap_get_entries($this->ldap, $search);
1604                $dn = $entry[0]['dn'];
1605                $userPassword = $entry[0]['userpassword'][0];
1606                $ldap_mod_replace['userPassword'] = $password;
1607                $this->replace_user_attributes($dn, $ldap_mod_replace);
1608                return $userPassword;
1609        }
1610       
1611        function delete_user($user_info)
1612        {
1613                // Verifica acesso do gerente (OU) ao tentar deletar um usuário.
1614                $manager_access = false;
1615                foreach ($this->manager_contexts as $index=>$context)
1616                {
1617                        if ( (strpos(strtolower($user_info['context']), strtolower($context))) || (strtolower($user_info['context']) == strtolower($context)) )
1618                        {
1619                                $manager_access = true;
1620                                break;
1621                        }
1622                }
1623                if (!$manager_access)
1624                {
1625                        $return['status'] = false;
1626                        $result['msg'] = $this->functions->lang('You do not have access to delete this user') . ".";
1627                        return $return;
1628                }
1629               
1630                $return['status'] = true;
1631                $return['msg'] = "";
1632                               
1633                // GROUPS
1634                $attrs = array();
1635                $attrs['memberuid'] = $user_info['uid'];
1636               
1637                if (count($user_info['groups_info']))
1638                {
1639                        foreach ($user_info['groups_info'] as $group_info)
1640                        {
1641                                $gidnumber = $group_info['gidnumber'];
1642                                $justthese = array("dn");
1643                                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1644                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1645                        $entry = ldap_get_entries($this->ldap, $search);
1646                                $dn = $entry[0]['dn'];
1647
1648                                if (!@ldap_mod_del($this->ldap, $dn, $attrs))
1649                                {
1650                                        $return['status'] = false;
1651                                        $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);
1652                                }
1653                        }
1654                }
1655               
1656                //INSTITUTIONAL ACCOUNTS
1657                $attrs = array();
1658                $attrs['mailForwardingAddress'] = $user_info['mail'];
1659               
1660                $justthese = array("dn");
1661                $filter="(&(phpgwAccountType=i)(mailforwardingaddress=".$user_info['mail']."))";
1662                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1663            $entries = ldap_get_entries($this->ldap, $search);
1664               
1665                for ($i=0; $i<$entries['count']; $i++)
1666                {
1667                        if ( !@ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs) )
1668                        {
1669                                $result['status'] = false;
1670                                $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);
1671                        }
1672                }
1673               
1674                // MAILLISTS
1675                $attrs = array();
1676                $attrs['mailForwardingAddress'] = $user_info['mail'];
1677               
1678                if (count($user_info['maillists_info']))
1679                {
1680                       
1681                        if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1682                        {
1683                                $return['status'] = false;
1684                                $result['msg'] = $this->functions->lang('Connection with ldap_master fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1685                                return $return;
1686                        }
1687                       
1688                        foreach ($user_info['maillists_info'] as $maillists_info)
1689                        {
1690                                $uid = $maillists_info['uid'];
1691                                $justthese = array("dn");
1692                                $filter="(&(phpgwAccountType=l)(uid=".$uid."))";
1693                                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1694                        $entry = ldap_get_entries($ldapMasterConnect, $search);
1695                                $dn = $entry[0]['dn'];
1696                       
1697                                if (!@ldap_mod_del($ldapMasterConnect, $dn, $attrs))
1698                                {
1699                                        $return['status'] = false;
1700                                        if (ldap_errno($ldapMasterConnect) == '50')
1701                                        {
1702                                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
1703                                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
1704                                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
1705                                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
1706                                        }
1707                                        else
1708                                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1709                                }
1710                        }
1711                        ldap_close($ldapMasterConnect);
1712                }
1713                       
1714                // UID
1715                $dn = "uid=" . $user_info['uid'] . "," . $user_info['context'];
1716                if (!@ldap_delete($this->ldap, $dn))
1717                {
1718                        $return['status'] = false;
1719                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1720                }
1721                /* jakjr */
1722                return $return;
1723        }
1724       
1725        function delete_maillist($uidnumber, $mail)
1726        {
1727                $return['status'] = true;
1728               
1729                $justthese = array("dn");
1730               
1731                // remove listas dentro de listas
1732                $filter="(&(phpgwAccountType=l)(mailForwardingAddress=".$mail."))";
1733                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1734                $entry = ldap_get_entries($this->ldap, $search);
1735                $attrs['mailForwardingAddress'] = $mail;
1736                for ($i=0; $i<=$entry['count']; $i++)
1737            {
1738                        $dn = $entry[$i]['dn'];
1739                @ldap_mod_del ( $this->ldap, $dn,  $attrs);
1740            }
1741               
1742                $filter="(&(phpgwAccountType=l)(uidnumber=".$uidnumber."))";
1743                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1744                $entry = ldap_get_entries($this->ldap, $search);
1745                $dn = $entry[0]['dn'];
1746               
1747                if (!@ldap_delete($this->ldap, $dn))
1748                {
1749                        $return['status'] = false;
1750                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1751                }
1752               
1753                return $return;
1754        }
1755
1756        function delete_group($gidnumber)
1757        {
1758                $return['status'] = true;
1759               
1760                $justthese = array("dn");
1761                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1762                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1763                $entry = ldap_get_entries($this->ldap, $search);
1764                $dn = $entry[0]['dn'];
1765               
1766                if (!@ldap_delete($this->ldap, $dn))
1767                {
1768                        $return['status'] = false;
1769                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1770                }
1771               
1772                return $return;
1773        }
1774
1775        function check_access_to_renamed($uid)
1776        {
1777                $justthese = array("dn");
1778                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1779               
1780                foreach ($this->manager_contexts as $index=>$context)
1781                {
1782                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1783                        $entry = ldap_get_entries($this->ldap, $search);
1784                        if ($entry['count'])
1785                                return true;
1786                }
1787            return false;
1788        }
1789
1790        function check_rename_new_uid($uid)
1791        {
1792                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1793                        return false;
1794               
1795                $justthese = array("dn");
1796                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1797               
1798                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1799                $count_entries = @ldap_count_entries($ldapMasterConnect, $search);
1800               
1801                if ($count_entries)
1802                        return false;
1803                       
1804                return true;
1805        }
1806       
1807        function rename_uid($uid, $new_uid)
1808        {
1809                $return['status'] = true;
1810               
1811                $justthese = array("dn");
1812                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1813                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1814            $entry = ldap_get_entries($this->ldap, $search);
1815                $dn = $entry[0]['dn'];
1816               
1817                $explode_dn = ldap_explode_dn($dn, 0);
1818                $rdn = "uid=" . $new_uid;
1819
1820                $parent = array();
1821                for ($j=1; $j<(count($explode_dn)-1); $j++)
1822                        $parent[] = $explode_dn[$j];
1823                $parent = implode(",", $parent);
1824               
1825                $return['new_dn'] = $rdn . ',' . $parent;
1826                       
1827                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, true))
1828                {
1829                        $return['status'] = false;
1830                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->rename_uid ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1831                }
1832               
1833                //Grupos
1834                $justthese = array("dn");
1835                $filter="(&(phpgwAccountType=g)(memberuid=".$uid."))";
1836                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1837            $entry = ldap_get_entries($this->ldap, $search);
1838        $array_mod_add['memberUid'] = $new_uid;
1839        $array_mod_del['memberUid'] = $uid;
1840
1841            for ($i=0; $i<=$entry['count']; $i++)
1842            {
1843                $dn = $entry[$i]['dn'];
1844                @ldap_mod_add ( $this->ldap, $dn,  $array_mod_add);
1845                @ldap_mod_del ( $this->ldap, $dn,  $array_mod_del);
1846            }
1847                return $return;
1848        }
1849
1850        function rename_cn($cn, $new_cn)
1851        {
1852                $return['status'] = true;
1853               
1854                $justthese = array("dn");
1855                $filter="(&(phpgwAccountType=g)(uid=".$cn."))";
1856                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1857            $entry = ldap_get_entries($this->ldap, $search);
1858                $dn = $entry[0]['dn'];
1859               
1860                $explode_dn = ldap_explode_dn($dn, 0);
1861                $rdn = "cn=" . $new_cn;
1862
1863                $parent = array();
1864                for ($j=1; $j<(count($explode_dn)-1); $j++)
1865                        $parent[] = $explode_dn[$j];
1866                $parent = implode(",", $parent);
1867               
1868                $return['new_dn'] = $rdn . ',' . $parent;
1869                       
1870                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, false))
1871                {
1872                        $return['status'] = false;
1873                }
1874               
1875                return $return;
1876        }
1877       
1878        function exist_sambadomains($contexts, $sambaDomainName)
1879        {
1880                $justthese = array("dn");
1881                $filter="(&(objectClass=sambaDomain)(sambaDomainName=$sambaDomainName))";
1882               
1883                foreach ($contexts as $index=>$context)
1884                {
1885                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1886                    $entry = ldap_get_entries($this->ldap, $search);
1887           
1888                        if ($entry['count'])
1889                                return true;
1890                }
1891                return false;
1892        }
1893       
1894        // Primeiro nilvel de organização.
1895        function exist_sambadomains_in_context($params)
1896        {
1897                $dn = $GLOBALS['phpgw_info']['server']['ldap_context'];
1898                $array_dn = ldap_explode_dn ( $dn, 0 );
1899               
1900                $context = $params['context'];
1901                $array_context = ldap_explode_dn ( $context, 0 );
1902               
1903                // Pego o setor no caso do contexto ser um sub-setor.
1904                if (($array_dn['count']+1) < ($array_context['count']))
1905                {
1906                        // inverto o array_dn para poder retirar o count
1907                        $array_dn_reverse  = array_reverse ( $array_dn, false );
1908                       
1909                        //retiro o count
1910                        array_pop($array_dn_reverse);
1911                       
1912                        //incluo o setor no dn
1913                        array_push ( $array_dn_reverse,  $array_context[ $array_context['count'] - 1 - $array_dn['count']]);
1914                       
1915                        // Volto a ordem natural
1916                        $array_dn  = array_reverse ( $array_dn_reverse, false );
1917                       
1918                        // Implodo
1919                        $context = implode ( ",", $array_dn );
1920                }
1921               
1922                $justthese = array("dn","sambaDomainName");
1923                $filter="(objectClass=sambaDomain)";
1924                $search = ldap_list($this->ldap, $context, $filter, $justthese);
1925            $entry = ldap_get_entries($this->ldap, $search);
1926           
1927            for ($i=0; $i<$entry['count']; $i++)
1928            {
1929                        $return['sambaDomains'][$i] = $entry[$i]['sambadomainname'][0];
1930            }
1931           
1932                if ($entry['count'])
1933                        $return['status'] = true;
1934                else
1935                        $return['status'] = false;
1936                       
1937                return $return;
1938        }
1939        function exist_domain_name_sid($sambadomainname, $sambasid)
1940        {
1941                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
1942
1943                $justthese = array("dn","sambaDomainName");
1944                $filter="(&(objectClass=sambaDomain)(sambaSID=$sambasid)(sambaDomainName=$sambadomainname))";
1945                $search = ldap_search($this->ldap, $context, $filter, $justthese);
1946            $count_entries = ldap_count_entries($this->ldap, $search);
1947           
1948            if ($count_entries > 0)
1949                return true;
1950            else
1951                return false;
1952        }
1953       
1954        function add_sambadomain($sambadomainname, $sambasid, $context)
1955        {
1956                $result = array();
1957               
1958                $dn                                                             = "sambaDomainName=$sambadomainname,$context";
1959                $entry['sambaSID']                                      = $sambasid;
1960                $entry['objectClass']                           = 'sambaDomain';
1961                $entry['sambaAlgorithmicRidBase']       = '1000';
1962                $entry['sambaDomainName']                       = $sambadomainname;
1963               
1964                if (!@ldap_add ( $this->ldap, $dn, $entry ))
1965                {
1966                        $return['status'] = false;
1967                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_sambadomain ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1968                }
1969                else
1970                        $return['status'] = true;
1971               
1972                return $return;
1973        }
1974       
1975        function delete_sambadomain($sambadomainname)
1976        {
1977                $return['status'] = true;
1978                $filter="(sambaDomainName=$sambadomainname)";
1979                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
1980            $entry = ldap_get_entries($this->ldap, $search);
1981         
1982                if ($entry['count'] != 0)
1983            {
1984                        $dn = $entry[0]['dn'];
1985                       
1986                        if (!@ldap_delete($this->ldap, $dn))
1987                        {
1988                                $return['status'] = false;
1989                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_sambadomain ($sambadomainname)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1990                        }
1991            }
1992           
1993                return $return;
1994        }
1995       
1996        function search_user($params)
1997        {
1998                $search = $params['search'];
1999                $justthese = array("cn","uid", "mail");
2000        $users_list=ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(&(phpgwAccountType=u) (|(cn=*$search*)(mail=$search*)) )", $justthese);
2001       
2002        if (ldap_count_entries($this->ldap, $users_list) == 0)
2003        {
2004                $return['status'] = 'false';
2005                $result['msg'] = $this->functions->lang('Any result was found') . '.';
2006                return $return;
2007        }
2008       
2009        ldap_sort($this->ldap, $users_list, "cn");
2010       
2011        $entries = ldap_get_entries($this->ldap, $users_list);
2012               
2013                $options = '';
2014                for ($i=0; $i<$entries['count']; $i++)
2015                {
2016                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['cn'][0] . " (".$entries[$i]['mail'][0].")" . "</option>";
2017                }
2018       
2019        return $options;               
2020        }
2021       
2022        function create_institutional_accounts($params)
2023        {
2024                /* Begin: Access verification */
2025                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'add_institutional_accounts'))
2026                {
2027                        $return['status'] = false;
2028                        $return['msg'] = $this->functions->lang('You do not have right to create institutional accounts') . ".";
2029                        return $return;
2030                }
2031               
2032                $access_granted = false;
2033                foreach ($this->manager_contexts as $idx=>$manager_context)
2034                {
2035                        if (stristr($params['context'], $manager_context))
2036                        {
2037                                $access_granted = true;
2038                                break;
2039                        }
2040                }
2041                if (!$access_granted)
2042                {
2043                        $return['status'] = false;
2044                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
2045                        return $return;
2046                }
2047                /* End: Access verification */
2048
2049                /* Begin: Validation */
2050                if ( (empty($params['cn'])) || (empty($params['mail'])) )
2051                {
2052                        $result['status'] = false;
2053                        $result['msg']  = $this->functions->lang('Field mail or name is empty');
2054                        return $result;
2055                }
2056
2057                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
2058                {
2059                        $result['status'] = false;
2060                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
2061                        return $result;
2062                }
2063
2064                $uid = 'institutional_account_' . $params['mail'];
2065                $dn = "uid=$uid," . $params['context'];
2066
2067                $filter = "(mail=".$params['mail'].")";
2068                $justthese = array("cn");
2069                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2070                $entries = @ldap_get_entries($this->ldap,$search);
2071                if ($entries['count'] != 0)
2072                {
2073                        $result['status'] = false;
2074                        $result['msg'] = $this->functions->lang('Field mail already in use');
2075                        return $result;
2076                }
2077                /* End: Validation */
2078                                               
2079                $info = array();
2080                $info['cn']                                     = $params['cn'];
2081                $info['sn']                                     = $params['cn'];
2082                $info['uid']                            = $uid;
2083                $info['mail']                           = $params['mail'];
2084                $info['description']            = $params['desc'];
2085                $info['phpgwAccountType']       = 'i';
2086                $info['objectClass'][]          = 'inetOrgPerson';
2087                $info['objectClass'][]          = 'phpgwAccount';
2088                $info['objectClass'][]          = 'top';
2089                $info['objectClass'][]          = 'person';
2090                $info['objectClass'][]          = 'qmailUser';
2091                $info['objectClass'][]          = 'organizationalPerson';
2092               
2093                if ($params['accountStatus'] == 'on')
2094                {
2095                        $info['accountStatus'] = 'active';
2096                }
2097                if ($params['phpgwAccountVisible'] == 'on')
2098                {
2099                        $info['phpgwAccountVisible'] = '-1';
2100                }
2101               
2102                if (!empty($params['owners']))
2103                {
2104                        foreach($params['owners'] as $index=>$uidnumber)
2105                        {
2106                                $info['mailForwardingAddress'][] = $this->uidnumber2mail($uidnumber);
2107                        }
2108                }               
2109               
2110                $result = array();
2111                if (!@ldap_add ( $this->ldap, $dn, $info ))
2112                {
2113                        $result['status'] = false;
2114                        $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->create_institutional_accounts';
2115                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2116                }
2117                else
2118                        $result['status'] = true;
2119               
2120                return $result;
2121        }
2122       
2123        function save_institutional_accounts($params)
2124        {
2125                /* Begin: Access verification */
2126                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
2127                {
2128                        $return['status'] = false;
2129                        $return['msg'] = $this->functions->lang('You do not have right to edit institutional accounts') . ".";
2130                        return $return;
2131                }
2132                $access_granted = false;
2133                foreach ($this->manager_contexts as $idx=>$manager_context)
2134                {
2135                        if (stristr($params['context'], $manager_context))
2136                        {
2137                                $access_granted = true;
2138                                break;
2139                        }
2140                }
2141                if (!$access_granted)
2142                {
2143                        $return['status'] = false;
2144                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
2145                        return $return;
2146                }
2147                /* End: Access verification */
2148               
2149                /* Begin: Validation */
2150                if ( (empty($params['cn'])) || (empty($params['mail'])) )
2151                {
2152                        $result['status'] = false;
2153                        $result['msg']  = $this->functions->lang('Field mail or name is empty') . '.';
2154                        return $result;
2155                }
2156
2157                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
2158                {
2159                        $result['status'] = false;
2160                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
2161                        return $result;
2162                }
2163
2164                $uid = 'institutional_account_' . $params['mail'];
2165                $dn = strtolower("uid=$uid," . $params['context']);
2166                $anchor = strtolower($params['anchor']);
2167
2168                $filter = "(mail=".$params['mail'].")";
2169                $justthese = array("cn");
2170                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2171                $entries = @ldap_get_entries($this->ldap,$search);
2172               
2173                if ( ($entries['count'] > 1) || (($entries['count'] == 1) && ($entries[0]['dn'] != $anchor)) )
2174                {
2175                        $result['status'] = false;
2176                        $result['msg'] = $this->functions->lang('Field mail already in use.');
2177                        return $result;
2178                }
2179                /* End: Validation */
2180               
2181                $result = array();
2182                $result['status'] = true;
2183               
2184                if ($anchor != $dn)
2185                {
2186                        if (!@ldap_rename($this->ldap, $anchor, "uid=$uid", $params['context'], true))
2187                        {
2188                                $result['status'] = false;
2189                                $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_rename';
2190                                $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2191                        }
2192                }
2193               
2194                $info = array();
2195                $info['cn']                                     = $params['cn'];
2196                $info['sn']                                     = $params['cn'];
2197                $info['uid']                            = $uid;
2198                $info['mail']                           = $params['mail'];
2199               
2200                if ($params['accountStatus'] == 'on')
2201                        $info['accountStatus'] = 'active';
2202                else
2203                        $info['accountStatus'] = array();
2204               
2205                if ($params['phpgwAccountVisible'] == 'on')
2206                        $info['phpgwAccountVisible'] = '-1';
2207                else
2208                        $info['phpgwAccountVisible'] = array();
2209               
2210                if ($params['desc'] != '')
2211                        $info['description'] = utf8_encode($params['desc']);
2212                else
2213                        $info['description'] = array();
2214               
2215                if (!empty($params['owners']))
2216                {
2217                        foreach($params['owners'] as $index=>$uidnumber)
2218                        {
2219                                $mailForwardingAddress = $this->uidnumber2mail($uidnumber);
2220                                if ($mailForwardingAddress != '')
2221                                        $info['mailForwardingAddress'][] = $mailForwardingAddress;
2222                        }
2223                }
2224                else
2225                        $info['mailForwardingAddress'] = array();
2226               
2227                if (!@ldap_modify ( $this->ldap, $dn, $info ))
2228                {
2229                        $result['status'] = false;
2230                        $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_modify';
2231                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2232                }
2233
2234                return $result;
2235        }
2236       
2237        function get_institutional_accounts($params)
2238        {
2239                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'list_institutional_accounts'))
2240                {
2241                        $return['status'] = false;
2242                        $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
2243                        return $return;
2244                }
2245
2246                $input = $params['input'];
2247                $justthese = array("cn", "mail", "uid");
2248                $trs = array();
2249                               
2250                foreach ($this->manager_contexts as $idx=>$context)
2251                {
2252                $institutional_accounts = ldap_search($this->ldap, $context, ("(&(phpgwAccountType=i)(|(mail=$input*)(cn=*$input*)))"), $justthese);
2253                $entries = ldap_get_entries($this->ldap, $institutional_accounts);
2254               
2255                        for ($i=0; $i<$entries['count']; $i++)
2256                        {
2257                                $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>";
2258                                $trs[$tr] = $entries[$i]['cn'][0];
2259                        }
2260                }
2261       
2262        $trs_string = '';
2263        if (count($trs))
2264        {
2265                natcasesort($trs);
2266                foreach ($trs as $tr=>$cn)
2267                {
2268                        $trs_string .= $tr;
2269                }
2270        }
2271       
2272        $return['status'] = 'true';
2273        $return['trs'] = $trs_string;
2274        return $return;
2275}       
2276       
2277        function get_institutional_account_data($params)
2278        {
2279                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
2280                {
2281                        $return['status'] = false;
2282                        $return['msg'] = $this->functions->lang('You do not have right to edit institutional accounts') . ".";
2283                        return $return;
2284                }
2285               
2286                $uid = $params['uid'];
2287                //$justthese = array("accountStatus", "phpgwAccountVisible", "cn", "mail", "mailForwardingAddress", "description");
2288                               
2289        $institutional_accounts = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"));
2290        $entrie = ldap_get_entries($this->ldap, $institutional_accounts);
2291               
2292                if ($entrie['count'] != 1)
2293                {
2294                        $return['status'] = 'false';
2295                        $result['msg'] = $this->functions->lang('Problems loading datas') . '.';
2296                }
2297                else
2298                {
2299                        $tmp_user_context = split(",", $entrie[0]['dn']);
2300                        $tmp_reverse_user_context = array_reverse($tmp_user_context);
2301                        array_pop($tmp_reverse_user_context);
2302                        $return['user_context'] = implode(",", array_reverse($tmp_reverse_user_context));
2303                       
2304                        $return['status'] = 'true';
2305                        $return['accountStatus']                = $entrie[0]['accountstatus'][0];
2306                        $return['phpgwAccountVisible']  = $entrie[0]['phpgwaccountvisible'][0];
2307                        $return['cn']                                   = $entrie[0]['cn'][0];
2308                        $return['mail']                                 = $entrie[0]['mail'][0];
2309                        $return['description']                  = utf8_decode($entrie[0]['description'][0]);
2310
2311                        if ($entrie[0]['mailforwardingaddress']['count'] > 0)
2312                        {
2313                                $a_cn = array();
2314                                for ($i=0; $i<$entrie[0]['mailforwardingaddress']['count']; $i++)
2315                                {
2316                                        $tmp = $this->mailforwardingaddress2uidnumber($entrie[0]['mailforwardingaddress'][$i]);
2317                                        if (!$tmp) {}
2318                                        else
2319                                                $a_cn[$tmp['uidnumber']] = $tmp['cn'];
2320                                }
2321                                natcasesort($a_cn);
2322                                foreach($a_cn as $uidnumber => $cn)
2323                                {
2324                                        $return['owners'] .= '<option value='. $uidnumber .'>' . $cn . '</option>';
2325                                }
2326                        }
2327                }
2328               
2329                return $return;
2330        }
2331        function get_shared_accounts($params)
2332                {
2333                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'list_shared_accounts'))
2334                {
2335                        $return['status'] = false;
2336                        $return['msg'] = $this->functions->lang('You do not have right to list shared accounts') . ".";
2337                        return $return;
2338                }
2339
2340                $input = $params['input'];
2341                $justthese = array("cn", "mail", "uid");
2342                $trs = array();
2343                               
2344                foreach ($this->manager_contexts as $idx=>$context)
2345                {
2346                $institutional_accounts = ldap_search($this->ldap, $context, ("(&(phpgwAccountType=s)(|(mail=$input*)(cn=*$input*)))"), $justthese);
2347                $entries = ldap_get_entries($this->ldap, $institutional_accounts);
2348               
2349                        for ($i=0; $i<$entries['count']; $i++)
2350                        {
2351                                $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>";
2352                                $trs[$tr] = $entries[$i]['cn'][0];
2353                        }
2354                }
2355       
2356        $trs_string = '';
2357        if (count($trs))
2358        {
2359                natcasesort($trs);
2360                foreach ($trs as $tr=>$cn)
2361                {
2362                        $trs_string .= $tr;
2363                }
2364        }
2365       
2366        $return['status'] = 'true';
2367        $return['trs'] = $trs_string;
2368        return $return;
2369        }
2370       
2371        function save_shared_accounts($params)
2372        {
2373                        /* Begin: Access verification */
2374                        if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_shared_accounts'))
2375                        {
2376                                $return['status'] = false;
2377                                $return['msg'] = $this->functions->lang('You do not have right to edit shared accounts') . ".";
2378                                return $return;
2379                        }
2380                        $access_granted = false;
2381                        foreach ($this->manager_contexts as $idx=>$manager_context)
2382                        {
2383                                if (stristr($params['context'], $manager_context))
2384                                {
2385                                        $access_granted = true;
2386                                        break;
2387                                }
2388                        }
2389                        if (!$access_granted)
2390                        {
2391                                $return['status'] = false;
2392                                $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
2393                                return $return;
2394                        }
2395                        /* End: Access verification */
2396                       
2397                        /* Begin: Validation */
2398                        if ( (empty($params['cn'])) || (empty($params['mail'])) )
2399                        {
2400                                $result['status'] = false;
2401                                $result['msg']  = $this->functions->lang('Field mail or name is empty') . '.';
2402                                return $result;
2403                        }
2404       
2405                        if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
2406                        {
2407                                $result['status'] = false;
2408                                $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
2409                                return $result;
2410                        }                       
2411                       
2412                        $dn = strtolower("uid=$params[uid]," . $params['context']);
2413                        $anchor = strtolower($params['anchor']);
2414                       
2415                        $filter = "(mail=".$params['mail'].")";
2416                        $justthese = array("cn");
2417                        $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2418                        $entries = @ldap_get_entries($this->ldap,$search);
2419                       
2420                        if ( ($entries['count'] > 1) || (($entries['count'] == 1) && ($entries[0]['dn'] != $anchor)) )
2421                        {
2422                                $result['status'] = false;
2423                                $result['msg'] = $this->functions->lang('Field mail already in use.');
2424                                return $result;
2425                        }
2426                        /* End: Validation */
2427
2428                        $result = array();
2429                        $result['status'] = true;
2430                       
2431                        if ($anchor != $dn)
2432                        {
2433                                if (!@ldap_rename($this->ldap, $anchor, "uid=$params[uid]", $params['context'], true))
2434                                {
2435                                        $result['status'] = false;
2436                                        $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_shared_accounts: ldap_rename';
2437                                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2438                                }
2439                        }
2440                       
2441                        $info = array();
2442                        $info['cn']                                     = $params['cn'];
2443                        $info['sn']                                     = $params['cn'];
2444                        $info['uid']                            = $params['uid'];
2445                        $info['mail']                           = $params['mail'];
2446                       
2447                        if ($params['accountStatus'] == 'on')
2448                                $info['accountStatus'] = 'active';
2449                        else
2450                                $info['accountStatus'] = array();
2451                       
2452                        if ($params['phpgwAccountVisible'] == 'on')
2453                                $info['phpgwAccountVisible'] = '-1';
2454                        else
2455                                $info['phpgwAccountVisible'] = array();
2456                       
2457                        if ($params['desc'] != '')
2458                                $info['description'] = utf8_encode($params['desc']);
2459                        else
2460                                $info['description'] = array();
2461                       
2462                        if (!@ldap_modify ( $this->ldap, $dn, $info ))
2463                        {
2464                                $result['status'] = false;
2465                                $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_shared_accounts: ldap_modify';
2466                                $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2467                        }
2468                        //print_r($info);echo "Teste $dn".$result['msg'];exit();
2469                        return $result;
2470        }       
2471       
2472        function get_shared_account_data($params)
2473        {
2474                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_shared_accounts'))
2475                {
2476                        $return['status'] = false;
2477                        $return['msg'] = $this->functions->lang('You do not have right to edit an shared accounts') . ".";
2478                        return $return;
2479                }
2480               
2481                $uid = $params['uid'];
2482                //$justthese = array("accountStatus", "phpgwAccountVisible", "cn", "mail", "mailForwardingAddress", "description");
2483                               
2484        $shared_accounts = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=s)(uid=$uid))"));
2485        $entrie = ldap_get_entries($this->ldap, $shared_accounts);
2486               
2487                if ($entrie['count'] != 1)
2488                {
2489                        $return['status'] = 'false';
2490                        $result['msg'] = $this->functions->lang('Problems loading datas') . '.';
2491                }
2492                else
2493                {
2494                        $tmp_user_context = split(",", $entrie[0]['dn']);
2495                        $tmp_reverse_user_context = array_reverse($tmp_user_context);
2496                        array_pop($tmp_reverse_user_context);
2497                        $return['user_context'] = implode(",", array_reverse($tmp_reverse_user_context));
2498                       
2499                        $return['status'] = 'true';
2500                        $return['accountStatus']                = $entrie[0]['accountstatus'][0];
2501                        $return['phpgwAccountVisible']  = $entrie[0]['phpgwaccountvisible'][0];
2502                        $return['cn']                                   = $entrie[0]['cn'][0];
2503                        $return['mail']                                 = $entrie[0]['mail'][0];
2504                        $return['description']                  = utf8_decode($entrie[0]['description'][0]);
2505                       
2506                }
2507               
2508                return $return;
2509        }               
2510        function mailforwardingaddress2uidnumber($mail)
2511        {
2512                $justthese = array("uidnumber","cn");
2513        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=u)(mail=$mail))"), $justthese);
2514        $entrie = ldap_get_entries($this->ldap, $search);
2515                if ($entrie['count'] != 1)
2516                        return false;
2517                else
2518                {
2519                        $return['uidnumber'] = $entrie[0]['uidnumber'][0];
2520                        $return['cn'] = $entrie[0]['cn'][0];
2521                        return $return;
2522                }
2523        }
2524       
2525        function delete_institutional_account_data($params)
2526        {
2527                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'remove_institutional_accounts'))
2528                {
2529                        $return['status'] = false;
2530                        $return['msg'] = $this->functions->lang('You do not have right to delete institutional accounts') . ".";
2531                        return $return;
2532                }
2533
2534                $uid = $params['uid'];
2535                $return['status'] = true;
2536                               
2537                $justthese = array("cn");
2538        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"), $justthese);
2539        $entrie = ldap_get_entries($this->ldap, $search);
2540                if ($entrie['count'] > 1)
2541                {
2542                        $return['status'] = false;
2543                        $return['msg']  = $this->functions->lang('More then one uid was found');
2544                        return $return;
2545                }               
2546                if ($entrie['count'] == 0)
2547                {
2548                        $return['status'] = false;
2549                        $return['msg']  = $this->functions->lang('No uid was found');
2550                        return $return;
2551                }               
2552               
2553                $dn = $entrie[0]['dn'];
2554                if (!@ldap_delete($this->ldap, $dn))
2555                {
2556                        $return['status'] = false;
2557                        $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->delete_institutional_accounts: ldap_delete";
2558                        $return['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2559                        return $return;
2560                }
2561               
2562                return $return;
2563        }
2564       
2565        function replace_mail_from_institutional_account($newMail, $oldMail)
2566        {
2567                $filter = "(&(phpgwAccountType=i)(mailforwardingaddress=$oldMail))";
2568                $justthese = array("dn");
2569                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2570                $entries = ldap_get_entries($this->ldap, $search);
2571                $result['status'] = true;
2572                for ($i=0; $i<$entries['count']; $i++)
2573                {
2574                        $attrs['mailforwardingaddress'] = $oldMail;
2575                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
2576                        $attrs['mailforwardingaddress'] = $newMail;
2577                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
2578               
2579                        if ((!$res1) || (!$res2))
2580                        {
2581                                $result['status'] = false;
2582                                $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->replace_mail_from_institutional_account.";
2583                        }
2584                }
2585               
2586                return $result;
2587        }
2588        function delete_shared_account_data($params)
2589        {
2590                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'delete_shared_accounts'))
2591                {
2592                        $return['status'] = false;
2593                        $return['msg'] = $this->functions->lang('You do not have right to delete shared accounts') . ".";
2594                        return $return;
2595                }               
2596                $uid = $params['uid'];
2597                $return['status'] = true;
2598
2599                $justthese = array("cn");
2600        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(&(phpgwAccountType=s)(uid=$uid))", $justthese);
2601
2602        $entrie = ldap_get_entries($this->ldap, $search);
2603       
2604                if ($entrie['count'] > 1)
2605                {
2606                        $return['status'] = false;
2607                        $return['msg']  = $this->functions->lang('More then one uid was found');
2608                        return $return;
2609                }
2610                if ($entrie['count'] == 0)
2611                {
2612                        $return['status'] = false;
2613                        $return['msg']  = $this->functions->lang('No uid was found');
2614                        return $return;
2615                }
2616
2617                $dn = $entrie[0]['dn'];
2618                if (!@ldap_delete($this->ldap, $dn))
2619                {
2620                        $return['status'] = false;
2621                        $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->delete_shared_accounts: ldap_delete";
2622                        $return['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2623                        return $return;
2624                }
2625
2626                return $return;
2627        }
2628}
2629?>
Note: See TracBrowser for help on using the repository browser.