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

Revision 540, 76.9 KB checked in by eduardoalex, 16 years ago (diff)

Adição da funcionalidade Geração automática de login.

Ao criar um usuário, existe a opção de o login ser criado automaticamente
pelo expresso, a partir de um algoritmo existente. Esses algoritmos deverão
estar descritos em classes que implementem a interface login do módulo
expressoAdmin. O nome das classes deverão também possuir a seguinte
estrutura: “class.”+”login_”+nome do algoritmo + “.inc.php”.

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