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

Revision 585, 76.9 KB checked in by niltonneto, 15 years ago (diff)

Correção de algumas traduções.

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