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

Revision 3530, 93.1 KB checked in by brunocosta, 13 years ago (diff)

Ticket #1325 - Adapta buscas LDAP para casos que não existem OUs.

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